react-native/README.md

220 lines
7.9 KiB
Markdown
Raw Normal View History

2015-08-15 03:42:33 +00:00
# React Native [![Build Status](https://travis-ci.org/facebook/react-native.svg?branch=master)](https://travis-ci.org/facebook/react-native) [![npm version](https://badge.fury.io/js/react-native.svg)](http://badge.fury.io/js/react-native)
2015-01-30 01:10:49 +00:00
2015-03-26 17:20:01 +00:00
React Native enables you to build world-class application experiences on native platforms using a consistent developer experience based on JavaScript and
[React](http://facebook.github.io/react). The focus of React Native is on developer efficiency across all the platforms you care about - learn once, write anywhere. Facebook uses React Native in multiple production apps and will continue investing in React Native.
## Native iOS Components
2015-03-26 18:31:16 +00:00
With React Native, you can use the standard platform components such as `UITabBar` and `UINavigationController` on iOS. This gives your app a consistent look and feel with the rest of the platform ecosystem, and keeps the quality bar high. These components are easily incorporated into your app using their React component counterparts, such as _TabBarIOS_ and _NavigatorIOS_.
2015-03-26 17:20:01 +00:00
```javascript
var React = require('react-native');
var { TabBarIOS, NavigatorIOS } = React;
2015-03-26 17:52:14 +00:00
2015-03-26 17:20:01 +00:00
var App = React.createClass({
render: function() {
return (
<TabBarIOS>
<TabBarIOS.Item title="React Native" selected={true}>
<NavigatorIOS initialRoute={{ title: 'React Native' }} />
</TabBarIOS.Item>
</TabBarIOS>
);
},
});
2015-01-30 01:10:49 +00:00
```
2015-03-26 17:20:01 +00:00
## Asynchronous Execution
All operations between the JavaScript application code and the native platform are performed asynchronously, and the native modules can also make use of additional threads as well. This means we can decode images off of the main thread, save to disk in the background, measure text and compute layouts without blocking the UI, and more. As a result, React Native apps are naturally fluid and responsive. The communication is also fully serializable, which allows us to leverage Chrome Developer Tools to debug the JavaScript while running the complete app, either in the simulator or on a physical device.
2015-01-30 01:10:49 +00:00
2015-03-26 17:22:05 +00:00
![](http://facebook.github.io/react-native/img/chrome_breakpoint.png)
2015-01-30 01:10:49 +00:00
2015-03-26 17:20:01 +00:00
## Touch Handling
2015-01-30 01:10:49 +00:00
2015-03-26 17:20:01 +00:00
iOS has a very powerful system called the Responder Chain to negotiate touches in complex view hierarchies which does not have a universal analog on the web. React Native implements a similar responder system and provides high level components such as TouchableHighlight that integrate properly with scroll views and other elements without any additional configuration.
```javascript
var React = require('react-native');
var { ScrollView, TouchableHighlight, Text } = React;
2015-03-26 17:52:14 +00:00
2015-03-26 17:20:01 +00:00
var TouchDemo = React.createClass({
render: function() {
return (
<ScrollView>
<TouchableHighlight onPress={() => console.log('pressed')}>
<Text>Proper Touch Handling</Text>
</TouchableHighlight>
</ScrollView>
);
},
});
2015-01-30 01:10:49 +00:00
```
2015-03-26 17:20:01 +00:00
## Flexbox and Styling
2015-03-26 18:13:31 +00:00
Laying out views should be easy, which is why we brought the flexbox layout model from the web to React Native. Flexbox makes it simple to build the most common UI layouts, such as stacked and nested boxes with margin and padding. React Native also supports common web styles, such as `fontWeight`, and the `StyleSheet` abstraction provides an optimized mechanism to declare all your styles and layout right along with the components that use them and apply them inline.
2015-03-26 17:20:01 +00:00
```javascript
var React = require('react-native');
var { Image, StyleSheet, Text, View } = React;
2015-03-26 17:52:14 +00:00
2015-03-26 17:20:01 +00:00
var ReactNative = React.createClass({
render: function() {
return (
<View style={styles.row}>
<Image
source={{uri: 'http://facebook.github.io/react/img/logo_og.png'}}
style={styles.image}
/>
<View style={styles.text}>
<Text style={styles.title}>
React Native
</Text>
<Text style={styles.subtitle}>
Build high quality mobile apps using React
</Text>
</View>
</View>
);
},
});
var styles = StyleSheet.create({
row: { flexDirection: 'row', margin: 40 },
image: { width: 40, height: 40, marginRight: 10 },
text: { flex: 1, justifyContent: 'center'},
title: { fontSize: 11, fontWeight: 'bold' },
subtitle: { fontSize: 10 },
});
2015-01-30 01:10:49 +00:00
```
2015-03-26 17:20:01 +00:00
## Polyfills
2015-03-26 17:51:00 +00:00
React Native is focused on changing the way view code is written. For the rest, we look to the web for universal standards and polyfill those APIs where appropriate. You can use npm to install JavaScript libraries that work on top of the functionality baked into React Native, such as `XMLHttpRequest`, `window.requestAnimationFrame`, and `navigator.geolocation`. We are working on expanding the available APIs, and are excited for the Open Source community to contribute as well.
2015-03-26 17:20:01 +00:00
```javascript
var React = require('react-native');
var { Text } = React;
2015-03-26 17:52:14 +00:00
2015-03-26 17:20:01 +00:00
var GeoInfo = React.createClass({
getInitialState: function() {
return { position: 'unknown' };
},
componentDidMount: function() {
navigator.geolocation.getCurrentPosition(
(position) => this.setState({position}),
(error) => console.error(error)
);
},
render: function() {
return (
<Text>
Position: {JSON.stringify(this.state.position)}
</Text>
);
},
});
```
2015-01-30 01:10:49 +00:00
2015-03-26 17:20:01 +00:00
## Extensibility
It is certainly possible to create a great app using React Native without writing a single line of native code, but React Native is also designed to be easily extended with custom native views and modules - that means you can reuse anything you've already built, and can import and use your favorite native libraries. To create a simple module in iOS, create a new class that implements the `RCTBridgeModule` protocol, and wrap the function that you want to make available to JavaScript in `RCT_EXPORT_METHOD`. Additionally, the class itself must be explicitly exported with `RCT_EXPORT_MODULE();`.
2015-03-26 17:20:01 +00:00
2015-03-26 17:51:00 +00:00
```objc
2015-03-26 17:20:01 +00:00
// Objective-C
2015-03-26 17:52:14 +00:00
2015-03-26 17:20:01 +00:00
#import "RCTBridgeModule.h"
2015-03-26 17:52:14 +00:00
2015-03-26 17:20:01 +00:00
@interface MyCustomModule : NSObject <RCTBridgeModule>
@end
2015-03-26 17:52:14 +00:00
2015-03-26 17:20:01 +00:00
@implementation MyCustomModule
RCT_EXPORT_MODULE();
// Available as NativeModules.MyCustomModule.processString
RCT_EXPORT_METHOD(processString:(NSString *)input callback:(RCTResponseSenderBlock)callback)
2015-03-26 17:20:01 +00:00
{
callback(@[[input stringByReplacingOccurrencesOfString:@"Goodbye" withString:@"Hello"]]);
2015-03-26 17:20:01 +00:00
}
2015-04-19 20:58:43 +00:00
2015-03-26 17:20:01 +00:00
@end
2015-01-30 01:10:49 +00:00
```
2015-03-26 17:20:01 +00:00
```javascript
// JavaScript
2015-03-26 17:52:14 +00:00
2015-03-26 17:20:01 +00:00
var React = require('react-native');
var { NativeModules, Text } = React;
2015-03-26 17:52:14 +00:00
2015-03-26 17:20:01 +00:00
var Message = React.createClass({
2015-03-30 15:04:20 +00:00
getInitialState() {
return { text: 'Goodbye World.' };
},
componentDidMount() {
NativeModules.MyCustomModule.processString(this.state.text, (text) => {
this.setState({text});
});
},
2015-03-26 17:20:01 +00:00
render: function() {
return (
<Text>{this.state.text}</Text>
);
},
});
2015-01-30 01:10:49 +00:00
```
2015-03-26 17:20:01 +00:00
Custom iOS views can be exposed by subclassing `RCTViewManager`, implementing a `-view` method, and exporting properties with the `RCT_EXPORT_VIEW_PROPERTY` macro. Then use `requireNativeComponent` in JavaScript to use the component in your app.
2015-03-26 17:20:01 +00:00
2015-03-26 17:48:01 +00:00
```objc
2015-03-26 17:20:01 +00:00
// Objective-C
2015-03-26 17:52:14 +00:00
2015-03-26 17:20:01 +00:00
#import "RCTViewManager.h"
2015-03-26 17:52:14 +00:00
2015-03-26 17:20:01 +00:00
@interface MyCustomViewManager : RCTViewManager
@end
2015-03-26 17:52:14 +00:00
2015-03-26 17:20:01 +00:00
@implementation MyCustomViewManager
2015-04-19 20:58:43 +00:00
RCT_EXPORT_MODULE()
2015-03-26 17:20:01 +00:00
- (UIView *)view
{
return [[MyCustomView alloc] init];
}
2015-03-26 17:52:14 +00:00
2015-04-19 20:58:43 +00:00
RCT_EXPORT_VIEW_PROPERTY(myCustomProperty, NSString);
2015-03-26 17:52:14 +00:00
2015-03-30 21:45:49 +00:00
@end
2015-03-26 17:48:01 +00:00
```
```javascript
2015-03-26 17:48:27 +00:00
// JavaScript
2015-03-26 17:52:14 +00:00
var React = require('react-native');
var { requireNativeComponent } = React;
class MyCustomView extends React.Component {
render() {
return <NativeMyCustomView {...this.props} />;
}
}
MyCustomView.propTypes = {
myCustomProperty: React.PropTypes.oneOf(['a', 'b']),
};
var NativeMyCustomView = requireNativeComponent('MyCustomView', MyCustomView);
module.exports = MyCustomView;
2015-03-26 17:20:01 +00:00
```
## Running the Examples
- `git clone https://github.com/facebook/react-native.git`
- `cd react-native && npm install`
- `cd Examples`
Now open any example and hit run in Xcode.
2015-03-26 18:07:15 +00:00
Further documentation, tutorials, and more on the [React Native website](http://facebook.github.io/react-native/docs/getting-started.html).