跨平台开发资源整理React Native

ReactNative之项目结构介绍(一)

2017-05-05  本文已影响3548人  袁峥

前言

眼看很多公司都开始尝试使用ReactNative,达到跨平台开发,最近也写了很多文章,希望让更多想了解的同学快速上手ReactNative.

如果喜欢我的文章,可以关注我微博:袁峥Seemygo

ReactNative之项目结构介绍

react-native init ReactDemo
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURL *jsCodeLocation;

  // 1.获取js文件url
  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

  // 2.加载控件
  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"ReactDemo"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  // 3.创建窗口
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  
  // 4.设置窗口根控制器的View
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  
  // 5.显示窗口
  [self.window makeKeyAndVisible];
  
  return YES;
}

// 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.mainStyle}>

            </View>
        )
    }
}

// 4.创建样式表
// 传入一个样式对象,根据样式对象中的描述,创建样式表
var styles = Stylesheet.create({
    mainStyle:{
        flex:1,
        backgroundColor:'red'
    }
})

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

ReactNative语法

style={styles.mainStyle}
var str = 'hello'
<Text>{str}</Text>
<View style={{flex:1}}></View>
    render(){
        return (
            <View style={styles.mainStyle}>

            </View>
        )
    }
上一篇 下一篇

猜你喜欢

热点阅读