2016-12-26 31 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

''幫助我。我忘了添加路線。謝謝。 –