react第一阶段第一个界面 - LewinJun/reactNative GitHub Wiki

原有项目接入react环境搭好了怎么看到react界面

react 其实就是一个View,facebook jsbridge解析js,把js标签动态绘制成native组件显示到这个view上。

在项目根目录里面新建index.ios.js 和index.android.js

index.ios.js

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';

export default class reactLewinDemo extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.ios.js
        </Text>
        <Text style={styles.instructions}>
          Press Cmd+R to reload,{'\n'}
          Cmd+D or shake for dev menu
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('reactLewinDemo', () => reactLewinDemo);

index.android.js

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';

export default class reactLewinDemo extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.android.js
        </Text>
        <Text style={styles.instructions}>
          Double tap R on your keyboard to reload,{'\n'}
          Shake or press menu button for dev menu
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

AppRegistry.registerComponent('reactLewinDemo', () => reactLewinDemo);

这两个是react界面主入口

iOS运行react界面

iOS这块用到的是RCTRootView这个View类去呈现视图,我这边在Appdelegate启动里面做测试

-(BOOL)reactNativeTestView:(NSDictionary *)launchOptions{
    //访问js路径,debug访问的是node本地服务器的js资源文件,8081 端口号可以看自己本地端口
    NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=false&minify=false"];
    //传给reactjs的参数json
    NSDictionary *props = @{@"url":@"http://www.lianshang.com"};
    //moduleName的名称是reactjs export的名称如:AppRegistry.registerComponent('reactLewinDemo', () => reactLewinDemo);
    RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                        moduleName:@"reactLewinDemo"
                                                 initialProperties:props
                                                     launchOptions:launchOptions];
    rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIViewController *rootViewController = [UIViewController new];
    rootViewController.view = rootView;
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
}

在AppDelegate的didFinishLaunchingWithOptions方法第一行调用return [self reactNativeTestView:launchOptions];即可。 工作做好后在项目根目录执行npm start启动node

Android运行react界面

待完善

⚠️ **GitHub.com Fallback** ⚠️