chengfengjie / chengfengjie.github.io

我叫MT
1 stars 0 forks source link

RN笔记 #37

Open chengfengjie opened 6 years ago

chengfengjie commented 6 years ago

初始化项目

react-native init <projectName>  # 初始化react项目
cd <projectName>
react-native run-ios #运行项目

属性 props

所有组件可以在标签中传入属性,如下代码传入名称为name,值为'Rexxar'的属性

<Greeting name="Rexxar"></Greeting>

组件内部用this.props.propName获取属性,属性一经指定,就不能更改,如果需要更改,需要用State状态

State(状态)

在constructor中初始化state,在需要的地方调用setState方法修改值,初始化和修改代码如下:

this.state = { showText: true } // 初始化
// 修改值
this.setState(previouseState => {
    return {  showText: ! previouseState  }
})
this.setState({ showText: !this.state.showText })
chengfengjie commented 6 years ago

样式 style

所有核心组件接收名为style的属性,传入对象,所用css采用驼峰规则,实际开发中建议采用StyleSheet.create来几种定义组件样式

const styles = StyleSheet.create({
    bigblue: {
        color: 'blue',
        fontWeight: 'bold',
        fontSize: 30,
    },
    red: {
      color: 'red',
    }
})

宽度和高度

指定宽高 style中的widthheight属性用于控制组件的宽度和高度,是与设备像素无关的逻辑像素点