2016-12-26 76 views
0

我有两个组件我想将页面上的某些属性数据传递给其他页面/组件。但无法用道具做到这一点。组件之间的数据传输反应原生

<TouchableOpacity onPress={() => {this.navigateTo('component2'); setState(url: 'www.xyz.com', data: 'abc' }> 
    <View style={{ flexDirection: 'row', height: 60, justifyContent: 'center', alignItems: 'center', paddingLeft: 10 }}> 
    <Image source={deals} /> 
    <View style={{ flex: 1 }}> 
     <Text style={{ color: '#000' }}> Deal Center </Text> 
    </View> 
    </View> 
</TouchableOpacity> 

并希望从component2中读取状态。任何人都可以请帮助

+0

调用你的方法上点击

_navigate(component,data) { this.props.navigator.push({ name: component, passProps: { data:data } }) } 

在第二组件,您可以访问数据是否使用助焊剂/终极版/ RxJS? –

+0

http://stackoverflow.com/questions/38203791/communicate-between-components-in-react-native-child-parent –

+0

小姑娘特卖延伸元器件{ 静态propTypes = { popRoute:React.PropTypes.func, 导航:React.PropTypes.shape({ 键:React.PropTypes.string, }), } 构造(道具){ 超级(道具); this.state = { name:'Deals', }; } navigateTo(route){ this.props.navigateTo(route,'home'); } –

回答

0

你可以写一个导航到1 Component和passProps的函数。

例子:

,并把此功能在你的代码,如:

<TouchableOpacity onPress={this.navigateTo.bind(this,'component')}> 
     ... 
</TouchableOpacity> 

在你的第二个组件,您可以拨打喜欢的道具:

this.props.urlthis.props.data

这是一个很好的教程Rea ct本机导航器,take a look

0

首先您必须设置路线。

<Second navigator={navigator} {...route.passProps} /> 

那么只有你可以通过道具来第二组分

然后使用this.props.data

+0

''帮助我。我忘了添加路线。谢谢。 –