我正在将数据数组传递给reactjs中的功能组件,如下所示
<Route
path="/tags"
component={() => <Tag {...props.Tags} />}
/>
和props.Tags的值类似
[
{
"Tags":"Tag1"
},
{
"Tags":"Tag2"
}
]
在Tag.js中,当我执行foreach
或map
时遇到此错误。
const tag = (props) => {
props.map(p => {
console.log(p);
}
...
}
错误消息
TypeError: props.forEach is not a function
要么
TypeError: props.map is not a function
如何迭代作为props传递的数组?
答案 0 :(得分:2)
问题在于props不是数组,而是这样的对象:
mmap
您应通过以下方式传递您的strace
道具(对于道具来说,使用小写字母是最佳做法):
{
Tags: "Tags2"
}
顺便说一句,如果您真的想迭代道具,则可以使用类似的东西:
tags