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’)

);

 

posted @ 2016-03-29 19:16  大赚佬  阅读(3922)  评论(0编辑  收藏  举报