2017-04-04 100 views
0

我需要将一些数据从一个屏幕传递到另一个屏幕,但我不知道该怎么做。我搜索了一下,我读了关于Redux,但是它有点复杂,因为我从来没有使用它,大多数教程都让一个新手感到困惑。但是如果我能在没有Redux的情况下做到这一点,那会更好。将本地传递数据反应到另一个屏幕

所以,当我点击一个按钮,它可以运行这样的:

onSearch() { 
    var listaCarros = fetch(`URL`, { 
     method: 'GET', 
     }) 
     .then((response) => { return response.json() }) 
     .then((responseJson) => { 
     console.log(responseJson) 
     }) 
    } 

,我想通过我从这个获取数据,到另一个屏幕。 即时通讯使用路由器通量,如果有关系。

回答

0

像你想将数据传递到已经命名为SecondPage一个新的组件,你可以保存在当前组件的状态就像

onSearch() { 
    var listaCarros = fetch(`URL`, { 
     method: 'GET', 
     }) 
     .then((response) => { return response.json() }) 
     .then((responseJson) => { 
     console.log(responseJson); 
     /*for react-native-router-flux you can simply do 
     Actions.secondPage({data:responseJson}); and you will get data at SecondPage in props 

     */ 
     this.setState({ 
     dataToPass :responseJson 
     }); 
     }) 
} 

然后在下面的回报响应,您可以按以下方式

render(){ 
    return(
    {this.state.dataToPass && <SecondPage data ={this.state.dataToPass}>} //you will get data as props in your second page 
); 
} 
+0

感谢您的回应!所以,我使用了Actions.secondPage。如何在第二个屏幕上显示数据? – waze

+0

看到你将在第二页获得道具console.log(this.props)在你的第二页的componentDidMount中,在你的chrome调试器中观察你的日志,在那里你可以看到你的道具对象有一个数据键 –

+0

因为我传递数组,我需要一个ListView来显示所有的数据?但为此,我需要'this.state.data.cloneWithRows(responseJson)',但是因为Im使用'Actions.secondPage({data:responseJson})'我该如何使用ListView? @Manjeet – waze

相关问题