react异常 Each child in a list should have a unique “key” prop

Posted kybs0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react异常 Each child in a list should have a unique “key” prop相关的知识,希望对你有一定的参考价值。

react异常警告:Each child in a list should have a unique “key” prop

技术图片

原因:Dom在渲染数组时,需要一个key,不然嵌套数组时会引起歧义

解决:

1   <div className="classlist-contaier">
2     {this.state.classList.map((item, index) => {
3       return <ClassItem key={index}/>;
4     })}
5   </div>

另外,如果遍历添加组件时,在组件外再加个div之类的容器,那么key需要在上层添加。比如:

1   <div className="classlist-contaier">
2     {this.state.classList.map((item, index) => {
3       return (
4         <div key={index}>
5           <ClassItem/>
6         </div>
7       );
8     })}
9   </div>

 

以上是关于react异常 Each child in a list should have a unique “key” prop的主要内容,如果未能解决你的问题,请参考以下文章

Each child in an array or iterator should have a unique "key" prop. Check the render metho

Ant Design 出现Warning: Each child in a list should have a unique “key“ prop.解决办法

对象作为 React-Child 无效 > 使用数组代替异常

116. Populating Next Right Pointers in Each Node

Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key.

使用 @each 的 nth-child 自动增量 - SASS