reactjs - 如何将获取的结果作为路由器通量中组件的参数传递?
问题描述
我正在尝试使用路由器通量在标签栏的组件内传递参数以反应本机。我所做的是获取的结果作为参数在标签栏内的组件内传递。以下是我的代码
主页.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
了。我的代码有什么错误,请帮忙。
解决方案
所以第一次你的组件渲染,this.state.cats
是未定义的。
你可以用你的代码做的是:
- 将您的 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
});
}
或者
- 对于您的组件
<Courses />
,您可以这样做,使其在汽车状态具有所需值之前不会呈现,并且初始化也是一个好习惯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>
PS专注于你的状态管理,你很高兴。
推荐阅读
- c# - Is there a good way to manage running multiple arbitrary C# entrypoints in Visual Studio/Code?
- authentication - Is there a way to add more details about the current logged user in User.Identity in .NET CORE API and access it in the services
- c# - C# Adding multiple items to list
- ionic-framework - Data is not showing in page using Ionic 4
- python - Unused variable in a for loop
- string - 为什么用彩色箱子中的彩色字符串替换子字符串不起作用?
- android - How can I connect Flutter in VS Code with the BlueStacks 4 emulator?
- c# - 在共享级别从 .Android 级别访问功能
- javascript - Del key doesn't fire in WebBrowser-hosted CKEditor
- php - PHP/CI3 - 类型转换无法按预期使用布尔值