首页 > 解决方案 > 独特的钥匙道具?

问题描述

我不知道如何解决这个警告信息??

“警告:数组或迭代器中的每个孩子都应该有一个唯一的“关键”道具”。

  let theComments = 
  this.props.state.studentCommentReducer.studentCommentReducer.map((comments) => {
  return <CommentsItem key={comments.id} comments={comments} />;
  });



     Student: {this.props.comments.comment}

标签: reactjskeyunique

解决方案


在某种程度上,您需要为每个项目指定一个键。你可以做这样的事情

...map((comments) => {
  return <CommentsItem key={comments.id} comments={comments} />;
});

或者

...map((comments, i) => {
  return <CommentsItem key={i} comments={comments} />;
});

参考:https ://reactjs.org/docs/lists-and-keys.html#keys-must-only-be-unique-among-siblings


推荐阅读