【问题标题】:React Native Pass data to another screen将 Native Pass 数据反应到另一个屏幕
【发布时间】:2017-04-04 13:51:33
【问题描述】:

我需要将一些数据从一个屏幕传递到另一个屏幕,但我不知道该怎么做。我已经搜索并阅读了有关 Redux 的信息,但它有点复杂,因为我从未使用过它,而且大多数教程对于新手来说都是令人困惑的。但如果我能在没有 Redux 的情况下做到这一点,那就更好了。

所以,当我点击一个按钮时,它会运行:

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

我想将从这里获得的数据传递到另一个屏幕。 我正在使用 router-flux,如果这很重要的话。

【问题讨论】:

    标签: react-native react-native-router-flux


    【解决方案1】:

    您可以将响应保存在当前组件的状态中,例如

    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
            });
          })
    }
    

    然后在下面作为回报,就像您想将数据传递给名为 SecondPage 的新组件一样,您可以按照以下方式进行操作

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

    【讨论】:

    • 感谢您的回复!所以,我使用了 Actions.secondPage。如何在第二个屏幕上显示数据?
    • 看到你会在第二页获得道具做console.log(this.props) 在你的第二页的componentDidMount,观察你在你的chrome调试器中的日志,在那里你可以看到你的道具对象有一个数据键
    • 由于我传递了一个数组,我需要一个 ListView 来显示所有数据,对吗?但为此我需要this.state.data.cloneWithRows(responseJson),但由于我使用Actions.secondPage({data: responseJson}),我该如何使用ListView? @Manjeet
    • 查看我对stackoverflow.com/questions/43007259/add-object-to-array-js/… 和 Listview 文档的回答
    猜你喜欢
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 2018-12-30
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多