facebook/react-native · GitHub

2,074 阅读3分钟
原文链接: github.com

React Native enables you to build world-class application experiences on native platforms using a consistent developer experience based on JavaScript and React[1]. 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

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.

 React  require(react-native);
 { TabBarIOS, NavigatorIOS }  React;

 App  React.createClass({
  render function() {
    return (
      TabBarIOS
        TabBarIOS.Item titleReact Native selected{}
          NavigatorIOS initialRoute{{ title React Native }} /
        /TabBarIOS.Item
      /TabBarIOS
    );
  },
});

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.

[2]

Touch Handling

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.

 React  require(react-native);
 { ScrollView, TouchableHighlight,  }  React;

 TouchDemo  React.createClass({
  render function() {
    return (
      ScrollView
        TouchableHighlight onPress{() => console(pressed)}
          Proper Touch Handling/
        /TouchableHighlight
      /ScrollView
    );
  },
});

Flexbox and Styling

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.

 React  require(react-native);
 { Image, StyleSheet, , View }  React;

 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.}
           style{styles.title}
            React Native
          /
           style{styles.subtitle}
            Build high quality mobile apps using React
          /
        /View
      /View
    );
  },
});
 styles  StyleSheet.create({
  row { flexDirection , margin  },
  image { width , height , marginRight  },
  text { flex , justifyContent center},
  title { fontSize , fontWeight  },
  subtitle { fontSize  },
});

Polyfills

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.

 React  require(react-native);
 {  }  React;

 GeoInfo  React.createClass({
  getInitialState function() {
    return { position unknown };
  },
  componentDidMount function() {
    navigator.geolocation.getCurrentPosition(
      (position) => .setState({position}),
      (error) => console.error(error)
    );
  },
  render function() {
    return (
      
        Position {.stringify(.state.position)}
      /
    );
  },
});

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();.

// Objective-C

#import RCTBridgeModule.h

@interface MyCustomModule : NSObject <RCTBridgeModule>


@implementation MyCustomModule

RCT_EXPORT_MODULE();

// Available as NativeModules.MyCustomModule.processString
RCT_EXPORT_METHOD(processString:(NSString *)input callback:(RCTResponseSenderBlock)callback)
{
  callback(@[[input stringByReplacingOccurrencesOfString:Goodbye withString:Hello]]);
}

// JavaScript

 React  require(react-native);
 { NativeModules,  }  React;

 Message  React.createClass({
  getInitialState() {
    return { text Goodbye World. };
  },
  componentDidMount() {
    NativeModules.MyCustomModule.processString(.state., () => {
      .setState({text});
    });
  },
  render function() {
    return (
      {.state.}/
    );
  },
});

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.

// Objective-C

#import RCTViewManager.h

@interface MyCustomViewManager : RCTViewManager


@implementation MyCustomViewManager

RCT_EXPORT_MODULE()

- (UIView *)view
{
  return [[MyCustomView alloc] ];
}

RCT_EXPORT_VIEW_PROPERTY(myCustomProperty, NSString);

// JavaScript

 React  require(react-native);
 { requireNativeComponent }  React;

class MyCustomView extends React.Component {
  render() {
    return NativeMyCustomView {...this.props} /;
  }
}
MyCustomView.propTypes  {
  myCustomProperty React.PropTypes.oneOf([, ]),
};

 NativeMyCustomView  requireNativeComponent(MyCustomView, MyCustomView);
module.exports  MyCustomView;

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.

Further documentation, tutorials, and more on the React Native website[4].