1.组件嵌套传值
通过状态机 state 进行传值
<TextInput onChangeText={(phone) => this._changePhone(phone)} placeholder='请输入手机号' style={[styles.textInput , {marginBottom:20}]} autoCorrect={false}> </TextInput>
_changePhone(phone) { this.setState({ phone: phone }); }<View style={[{marginLeft:27} , {marginTop:20} , {opacity:0.7}]}> <Button wd={320} hg={50} text="登录" bg="gray" click={() => this._login()}/> </View>_login() { alert(this.state.phone) }
在改变TextInput中值的时候调用 _changePhone 方法,然后更新 phone 的状态值,然后在 Button 点击时间的时候调用 this.state.phone 来接收最后修改的 phone 的状态值。
注意,react-native 中是没有提供原生的 Button 组件,这里是博主封装的一个 Button。
2.同一页面的不同组件类传值
var first = React.createClass({ render() { return( <View> <Text text="组件类之间传值"></Text> </View> ) } }); var first = React.createClass({ render() { return( <View> <Text> {this.props.text} </Text> </View> ) } });
3.定义全局通信类
这一种传值方法在官方 API 中有详细的介绍,这里就不多说了
这里提供一个更为详细的博客,
http://blog.csdn.net/limm33/article/details/50942863