reactjs - 独特的钥匙道具?
问题描述
我不知道如何解决这个警告信息??
“警告:数组或迭代器中的每个孩子都应该有一个唯一的“关键”道具”。
let theComments =
this.props.state.studentCommentReducer.studentCommentReducer.map((comments) => {
return <CommentsItem key={comments.id} comments={comments} />;
});
Student: {this.props.comments.comment}
解决方案
在某种程度上,您需要为每个项目指定一个键。你可以做这样的事情
...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
推荐阅读
- javascript - 角度,基本路线的保护不起作用
- reactjs - 如何检查dom节点迭代中的孩子是否具有特定的类名
- php - **警告**:在 PHP 中使用未定义的常量 HTTP_HOST]
- integromat - 如何在自定义 Integromat 应用程序中解析此日期:2017-11-28T23:00:00Z?
- go - 带有 google-cloud-datastore 的 liquibase 示例
- c# - C# PowerShell 在使用 AddCommand 时不会捕获 PowerShell.Streams,但在使用 AddScript 时会捕获
- tsql - 链接服务器查询期间超时
- unix - 在特定列的连字符后保留部分字符串
- python-3.x - TypeError:“模块”对象在 pycharm 中无法通过 Python 使用 Selenium 调用
- python - 事件中心将 JSON 转换为 JSON 字符串