From b5c1775beb489653d06845480fd1f24d744a1c78 Mon Sep 17 00:00:00 2001 From: Spencer Ahrens Date: Wed, 25 Mar 2015 20:55:01 -0700 Subject: [PATCH] Improve landing page. --- website/src/react-native/_index.js | 88 +++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 9 deletions(-) diff --git a/website/src/react-native/_index.js b/website/src/react-native/_index.js index 6bad5e8f9..df6c33e66 100644 --- a/website/src/react-native/_index.js +++ b/website/src/react-native/_index.js @@ -52,15 +52,15 @@ module.exports = React.createClass({ });`} -

Asynchronous

+

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 JS while running the app in the full app environment, in the sim or on a real device. + 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.

-

Touch Handling

+

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 configutation. + 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.

{`var React = require('react-native'); @@ -80,7 +80,7 @@ module.exports = React.createClass({

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 easy to build the most common UI layouts, such as stacked and nested boxes with margin and padding. React Native also supports common web syles, such as fontWeight, and the StyleSheet abstraction makes it easy to declare all your styles and layout right along with the components that use them and used inline. + 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 syles, 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.

{`var React = require('react-native'); @@ -116,16 +116,14 @@ var styles = StyleSheet.create({

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, 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 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.

{`var React = require('react-native'); var { Text } = React; module.exports = React.createClass({ getInitialState: function() { - return { - position: 'unknown', - }; + return { position: 'unknown' }; }, componentDidMount: function() { navigator.geolocation.getCurrentPosition( @@ -143,6 +141,78 @@ module.exports = React.createClass({ });`} +

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 expended 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 add RCT_EXPORT to the function you want to make available in JavaScript. +

+ +{`// Objective-C + +#import "RCTBridgeModule.h" + +@interface MyCustomModule : NSObject +@end + +@implementation MyCustomModule + +- (void)processString:(NSString *)input callback:(RCTResponseSenderBlock)callback +{ + RCT_EXPORT(); // available as NativeModules.MyCustomModule.processString + callback(@[[input stringByReplacingOccurrencesOfString:@"Goodbye" withString:@"Hello"];]]); +} +@end`} + + +{`// JavaScript + +var React = require('react-native'); +var { NativeModules, Text } = React; + +var Message = React.createClass({ + render: function() { + getInitialState() { + return { text: 'Goodbye World.' }; + }, + componentDidMount() { + NativeModules.MyCustomModule.processString(this.state.text, (text) => { + this.setState({text}); + }); + }, + return ( + {this.state.text} + ); + }, +});`} + +

+ Custom iOS views can be exposed by subclassing RCTViewManager, implementing a -(UIView *)view method, and exporting properties with the RCT_EXPORT_VIEW_PROPERTY macro. Then a simple JavaScript file connects the dots. +

+ +{`// Objective-C + +#import "RCTViewManager.h" + +@interface MyCustomViewManager : RCTViewManager +@end + +@implementation MyCustomViewManager + +- (UIView *)view +{ + return [[MyCustomView alloc] init]; +} + +RCT_EXPORT_VIEW_PROPERTY(myCustomProperty); +@end`} + + +{`// JavaScript + +module.exports = createReactIOSNativeComponentClass({ + validAttributes: { myCustomProperty: true }, + uiViewClassName: 'MyCustomView', +});`} +