将数组传递给道具,但在迭代时出错

时间:2020-07-14 05:04:42

标签: javascript reactjs react-router-dom

我正在将数据数组传递给reactjs中的功能组件,如下所示

<Route
  path="/tags"
  component={() => <Tag {...props.Tags} />}
/>

和props.Tags的值类似

[
  {
    "Tags":"Tag1"
  },
  {
    "Tags":"Tag2"
  }
]

在Tag.js中,当我执行foreachmap时遇到此错误。

const tag = (props) => {
  props.map(p => {
    console.log(p);
  }
...
}

错误消息

TypeError: props.forEach is not a function 要么 TypeError: props.map is not a function

如何迭代作为props传递的数组?

1 个答案:

答案 0 :(得分:2)

问题在于props不是数组,而是这样的对象:

mmap

您应通过以下方式传递您的strace道具(对于道具来说,使用小写字母是最佳做法):

{
  Tags: "Tags2"
}

顺便说一句,如果您真的想迭代道具,则可以使用类似的东西:

tags