【问题标题】:How can I pass fetched result as params of a component in router flux?如何将获取的结果作为路由器通量中组件的参数传递?
【发布时间】:2019-08-06 11:59:26
【问题描述】:

我正在尝试使用路由器 Flux 在 React Native 中传递选项卡栏组件内的参数。我所做的是获取的结果作为选项卡栏内组件内的参数传递。以下是我的代码

Home.js

fetch(GLOBAL.COURSES)
            .then((response) => response.json())
            .then((responseData) => {
                this.setState({
                    cats: responseData.data,
                    vdo: responseData.video,
                    isLoading: false
                })
            })

..
...
                     <Tabs>
                        <Tab
                            heading="Courses"
                        >
                            <Courses cats={this.state.cats} />
                        </Tab>
                     ...
                       ...
                  </Tabs>

Courses 页面中,我试图将这个道具作为

 {this.props.cats.map(category =>

                           <View>
                           ....
                           ....
                           </View>
)}

我收到undefined is not an object evaluating this.props.cats.map。我的代码有什么错误请帮忙。

【问题讨论】:

  • 问题是获取数据需要时间。所以至少&lt;Courses&gt; 的第一次渲染有一个未定义的cats 属性。试着像{this.props.cats &amp;&amp; this.props.cats.map(category...一样控制它
  • 如果您的初始状态定义为空数组,则无需这样做。
  • @SubhenduKundu 是的,但他实际上是在尝试从道具渲染,而不是从状态
  • 所以渲染方法是在构造函数调用之后调用的。所以你有一个空数组。 this.props.cats 将有一个空数组。并使用空数组呈现组件。然后用数组的一堆数据调用setState,再次调用renders方法,此时this.props.cats将有一个充满数组的数据。
  • 伙计,我们说的是同一件事

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


【解决方案1】:

所以渲染方法是在构造函数调用之后调用的。所以你有一个空数组。 this.props.cats 将有一个空数组。并使用空数组呈现组件。然后用数组的一堆数据调用 setState,再次调用 renders 方法,此时 this.props.cats 将有一个充满数组的数据。 嗯,请确保您将初始状态定义为

state = {
 cats = []
....
}
or 
this.state = {
 cats = []
....
}

如果它返回一个数组,还要执行 console.log responseData.data

【讨论】:

  • 是的,我已经安慰了这个结果。但是什么都没有呈现
  • responseData.data的回复是什么?是数组吗?
【解决方案2】:

所以第一次渲染组件时,this.state.cats 是未定义的。

你可以用你的代码做的是:

  1. 将您的 fetch 调用包装在异步调用中,即一个承诺。例如,

//function
const fetchCats = async () => {
  const responseData = await fetch(GLOBAL.COURSES)
     .then((response) => response.json())
     .then((responseDataSuccess) => {
          return responseDataSuccess;
  });
  this.setState({
     cats: responseData.data,
     vdo: responseData.video,
     isLoading: false
  });
}

或者

  1. 到您的组件&lt;Courses /&gt;,您可以这样做,直到汽车状态具有所需值时它才会呈现,并且初始化this.state = { cars: [] } 也是一个好习惯

import { get } from 'lodash';
// a utility set for javascript

<Tabs>
  <Tab heading="Courses" >
  {get(this.state, cats) && <Courses cats={this.state.cats} />}
  </Tab>
                     ...
                       ...
</Tabs>

附:专注于您的状态管理,一切顺利。

【讨论】:

  • 赞。这有助于@Sania
  • 您实际上不需要用异步包装它,因为@anu 仅在调用 .then() 方法之后才调用 setState。但是你答案的第二部分很重要。 :-)
【解决方案3】:

您正在尝试循环遍历您的 cat 数组,而尚未收到您的服务的响应。

一种常见的模式是在等待服务响应时显示加载微调器。

<Tab heading="Courses">
  {!this.state.isLoading && (<Courses cats={this.state.cats} />)}
  {this.state.isLoading && "Loading..."}
</Tab>

【讨论】:

  • Text strings must be rendered within text component。我从你的代码中删除了这个{this.state.isLoading &amp;&amp; "Loading..."}。现在什么都没有了。
  • 错误非常明显。尝试使用{this.state.isLoading &amp;&amp; (&lt;span&gt;Loading...&lt;/span&gt;)} 或将其替换为加载数据时要显示的任何组件。
猜你喜欢
  • 2018-03-19
  • 1970-01-01
  • 2015-07-24
  • 2017-07-06
  • 2023-03-07
  • 2016-05-06
  • 2019-07-21
  • 2020-04-12
  • 2020-02-06
相关资源
最近更新 更多