當你在寫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’) );
