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