React 错误Each child in an array or iterator should have a unique “key” prop


当你在写react的时候报了类似于这样子的错:Each child in an array or iterator should have a unique “key” prop.

原因是这样子的:React can’t know that your array is static, so you get the warning. The most practical thing to do here is to write something like.

解决办法只要在循环的每个子项添加一个key就行了,代码如下:

var names = [‘Alice’, ‘Emily’, ‘Kate’];

 

ReactDOM.render(

<div>

{

names.map(function (name, key) {

return <div key={key}>Hello, {name}!</div>

})

}

</div>,

document.getElementById(‘example’)

);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM