【问题标题】:What can i pass in as placeholder for first argument in map function Warning: Encountered two children with the same key, `[object Object]`我可以传入什么作为映射函数中第一个参数的占位符警告:遇到两个具有相同键的孩子,`[object Object]`
【发布时间】:2021-11-09 22:22:40
【问题描述】:

这是我的地图函数,它没有将 i 注册为索引,因为我认为地图函数需要元素作为第一个元素

{listArray.map((i: number) => (
    <IRServiceAlertRulesListItem addListItem={addListItem} listArray={listArray} key={i} />
  ))}

在我的控制台中出现此错误 警告:遇到两个孩子使用相同的密钥,[object Object]

【问题讨论】:

  • listArray 中的每个项目是什么样的?看起来好像你在期待一个数字,但它们实际上是对象。

标签: arrays reactjs typescript


【解决方案1】:

这是因为你的 map 函数中的 i 变量是一个对象(也许这里的 i: number 类型有问题?),你不能在 React 中使用 Object 作为键。 请改用index

{listArray.map((i, index) => (
    <IRServiceAlertRulesListItem addListItem={addListItem} listArray={listArray} key={index} />
  ))}

如果您在每个 listArray 元素中都有一个唯一属性,请使用它而不是 index

【讨论】:

    猜你喜欢
    • 2021-03-08
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2018-01-01
    • 2016-07-10
    • 2017-09-11
    • 2017-12-15
    • 2017-05-17
    相关资源
    最近更新 更多