【发布时间】: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。我的代码有什么错误请帮忙。
【问题讨论】:
-
问题是获取数据需要时间。所以至少
<Courses>的第一次渲染有一个未定义的cats属性。试着像{this.props.cats && this.props.cats.map(category...一样控制它 -
如果您的初始状态定义为空数组,则无需这样做。
-
@SubhenduKundu 是的,但他实际上是在尝试从道具渲染,而不是从状态
-
所以渲染方法是在构造函数调用之后调用的。所以你有一个空数组。 this.props.cats 将有一个空数组。并使用空数组呈现组件。然后用数组的一堆数据调用setState,再次调用renders方法,此时this.props.cats将有一个充满数组的数据。
-
伙计,我们说的是同一件事
标签: reactjs react-native jsx react-native-router-flux