React-Native之HelloWorld

2017-10-30  本文已影响0人  Coder_Answer
前言
React-Native简介
编辑器
初始化项目
react-native init HelloWorld

如果想要指定react-native的版本的话可以使用如下命令

rninit init HelloWorld --source react-native@0.44.0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURL *jsCodeLocation;
  // 获取js文件url
  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
  // 加载控件,moduleName不能乱传,必须跟js文件中注册的模块名字保持一致
  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"HelloWorld"
                                               initialProperties:nil
                                                   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];
  return YES;
}
// ES6写法
// 1.加载React,Componet组件
import React,{compoent} from 'react'

// 2.加载原生组件
import
{
    AppRegistry,
    StyleSheet,
    View,
    Text
}
from 'react-native'

// 3.自定义组件,作为程序入口组件
export default class ReactDemo extends Component {
    // 当加载组件的时候,就会调用render方法,去渲染组件
    render(){
        return (
             <View style={styles.container}>
                <Text style={styles.welcome}>
                  HelloWorld
                </Text>
             </View>
        )
    }
}

// 4.创建样式表
// 传入一个样式对象,根据样式对象中的描述,创建样式表
var styles = Stylesheet.create({
    container: {
          flex: 1,
          justifyContent: 'center',
          alignItems: 'center',
          backgroundColor: 'rgba(15, 148, 32, 0.98)',
  },
  welcome: {
          fontSize: 20,
          textAlign: 'center',
          margin: 10,
  },
})

// 5.注册组件,程序入口
// 第一个参数:注册模块名称
// 第二个参数:函数, 此函数返回组件类名, 程序启动就会自动去加载这个组件
AppRegistry.registerComponent('ReactDemo',()=>ReactDemo)
上一篇 下一篇

猜你喜欢

热点阅读