美文网首页
ReactNative-基础与初始化(一)

ReactNative-基础与初始化(一)

作者: 攻克乃还_ | 来源:发表于2017-08-20 10:10 被阅读21次

    1.创建ReactNative工程

    cd 项目目录
    react-native init ReactDemo
    cd ReactDemo
    react-native run-ios
    

    2.ReactNative初始化代码

    // 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)
    

    3. {} () 使用场景

    3.1.{}

    • 表达式
      style={styles.mainStyle}
    • 变量
      var str = 'hello'
      <Text>{str}</Text>
    • 对象和字典
      <View style={{flex:1}}></View>

    3.2.()

    • 创建组件<View></View>用()
      render(){ return ( <View style={styles.mainStyle}> </View> ) }

    相关文章

      网友评论

          本文标题:ReactNative-基础与初始化(一)

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