reactjs - 'this.props.tasks.map is not a function' 错误-React 错误
问题描述
将数组传递给组件时出现错误-'this.props.tasks.map is not a function'
我的 Main.js 代码
let posts = [
{
id: 1,
description: "This is a task",
status: "pending"
},
{
id: 2,
description: "This is another task",
status: "pending"
},
{
id: 3,
description: "This is an easy task",
status: "pending"
}
];
class Main extends Component {
render() {
return <div>
<Title title="Photowall" />
<Photowall posts={ posts } />
</div>
}
}
和 Photowall.js 代码
render() {
return <div>
{this.props.posts.map((item) => <Photo key={item} post={item}/>)}
</div>
}
解决方案
你必须posts
像下面这样通过。
<Photowall posts={posts} />
Photowall.js
你必须通过key={item.id}
,我想这会起作用。
render() {
return <div>
{this.props.posts.map((item) => <Photo key={item.id} post={item}/>)}
</div>
}
照片
class Photo extends Component {
render() {
const data = this.props.post;
return <p>{data.description}</p>
}
}
如果您像这样通过,{{ posts }}
那么它将在另一端被视为如下。
{
posts: [
{
id: 1,
description: "This is a task",
status: "pending"
},
{
id: 2,
description: "This is another task",
status: "pending"
},
{
id: 3,
description: "This is an easy task",
status: "pending"
}
]
}
所以这就是为什么这不起作用。
希望这对你有用!
推荐阅读
- c# - 负载测试IdentityServer4给出异常:在唱歌后尝试请求signin-oidc路由时相关失败
- r - 为什么我在使用 R 包 edgar 从 10-K 报告中提取业务描述时遇到错误?
- react-native - react-native navigation v3 tabIcons 不显示
- ios - Delphi Berlin:为什么图标应用程序和启动图像在 iOS 模拟器上不起作用?
- php - 我们可以接受某个数据库的所有表中的列的唯一值吗?
- hashmap - 将 SHA 256 摘要映射到整数的最有效方法是什么?
- sql - oracle中的日期元素
- python - 从文件中查找字符串并用作过滤器
- php - 如果变量是,则为多个数字
- ssas - 如何在部署具有时间维度的多维数据集时修复元数据管理器中的错误[“日期”离散化属性不能具有相关属性]