cfanbo / cfanbo.github.io

1 stars 0 forks source link

React开发中的常见问题 | 学习笔记 #247

Open cfanbo opened 7 months ago

cfanbo commented 7 months ago

https://blog.haohtml.com/archives/16121/

当你在写react的时候报了类似于这样子的错:Each child in an array or iterator should have a unique “key” prop.Check the render method of xxxx. See https://fb.me/react-warning-keys for more information. 原因是这样子的: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(

{ names.map(function (name, key) { return
Hello, {name}!
}) }
, document.getElementById('example') );