-
Notifications
You must be signed in to change notification settings - Fork 0
props and state
flyw edited this page Jul 12, 2018
·
1 revision
控制组件有两种类型的数据:props 和 state 。
-
props由父级设置,并且在组件的整个生命周期内都是固定的。大多数组件在创建时可以使用不同的参数进行自定义。 这些创建参数称为
props。render() { <Image source={uri} /> }
在
Image内let uri = this.props.source;
-
对于即将发生变化的数据,我们必须使用
state。通常,您应该在构造函数中初始化
state,然后在想要更改它时调用setState()。constructor(props) { super(props); this.state = { text: '原', }; } onChange() { this.setState({ text: '新' }) } render() { return ( <Button onPress={this.onChange()}> <Text>{this.state.text}</Text> </Button> }; }