美文网首页
React Native简介

React Native简介

作者: windgo | 来源:发表于2015-05-23 09:14 被阅读2456次

    React Native简介

    原文地址
    这是翻译,删减的的reac native的官方简介,翻译完之后,还是挺失望的,槽点在于,你需要学习更多东西。同时舞动js和oc两种语言,脚踏web和native两套库,才能从容应对开发中的挑战。脸书此举,不过是给web开发者向移动进军提供了桥梁,对于移动开发者,可有可无,未必有很大吸引力。

    • React Native - 使用REACT开发世界一流的原生应用.
    • 使用JavaScript和React.
    • React Native专注于夸平台的开发效率. 一次学习, 夸平台应用.

    开始搞React Native

    原生 iOS 组件

    可以直接使用iOS的原生组件, 这样界面就会和平台上得应用们一致了.

    var React = require(‘react-native’);
    var { TabBarIOS, NavigatorIOS } = React;
    var App = React.createClass({
      render: function() {
        return (
          <TabBarIOS>
            <TabBarIOS.Item title=“React Native” selected={true}>
              <NavigatorIOS initialRoute={{ title: ‘React Native’ }} />
            </TabBarIOS.Item>
          </TabBarIOS>
        );
      },
    });
    

    异步执行

    JavaScript代码和原生平台之间的操作都是异步的. 这样解码图片, 往磁盘存储文件, 测量文本大小, 计算布局等等都可以再后台运行而不阻塞界面. 这样REACTIVE-NATIVE应用运行流畅, 响应迅速. The communication is also fully serializable, 可以使用Chrome的Web调试工具进行调试.

    触摸操作

    React Native自建了一套响应系统代替了iOS的响应链系统.

    var React = require(‘react-native’);
    var { ScrollView, TouchableHighlight, Text } = React;
    
    var TouchDemo = React.createClass({
      render: function() {
        return (
          <ScrollView>
            <TouchableHighlight onPress={() => console.log(‘pressed’)}>
              <Text>Proper Touch Handling</Text>
            </TouchableHighlight>
          </ScrollView>
        );
      },
    });
    

    伸缩盒与样式表

    React-Native使用伸缩盒以及样式进行界面的布局和渲染

    var React = require(‘react-native’);
    var { Image, StyleSheet, Text, View } = React;
    
    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 },
    });
    

    其他

    React Native 聚焦于iOS的界面开发,其他方面,使用js等web通用技术.

    var React = require(‘react-native’);
    var { Text } = React;
    
    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>
        );
      },
    });
    

    扩展能力

    可以将原生的代码,导出给js用。

    RCT_EXPORT_MODULE();.
    
    // Objective-C
    
    #import “RCTBridgeModule.h”
    
    @interface MyCustomModule : NSObject <RCTBridgeModule>
    @end
    
    @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”]]);
    }
    @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 (
          <Text>{this.state.text}</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
    
    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;
    

    相关文章

      网友评论

          本文标题:React Native简介

          本文链接:https://www.haomeiwen.com/subject/ewzlqttx.html