【问题标题】:I have given key prop to the child but still showing: Each child in a list should have a unique "key" prop我给了孩子 key 道具,但仍然显示:列表中的每个孩子都应该有一个唯一的“key”道具
【发布时间】:2021-09-08 14:51:59
【问题描述】:

我正在使用moongodb并在后端表达。 这是我的组件。

 return (
     <>
         <Addnote />
         <div className="row my-3">
             {notes.map((note) => {
                 return <Noteitem note={note} key={note._id}/>;
             })}
         </div>
     </>
)

【问题讨论】:

  • 这里notes的内容是什么?似乎有 note._id 缺失或不唯一的条目。
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: node.js reactjs mongodb express


【解决方案1】:

如 cmets 所述,note._id 不是唯一的。您可以将其修复为唯一或使用项目的索引,如下所示:

{notes.map((note, i) => {
  return <Noteitem note={note} key={i} />;
})}

或缩写为:

{notes.map((note, i) => (
  <Noteitem note={note} key={i} />;
))}

【讨论】:

    【解决方案2】:

    我认为

    {notes.map((note) => { 
    

    应该替换为

     {notes.map((note) => (
    

    括号替换 { 为 (

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-06
      • 2023-02-01
      • 2021-07-03
      • 2021-07-20
      • 2017-02-23
      • 2017-02-03
      • 2021-11-17
      相关资源
      最近更新 更多