React开发中的常见问题

当你在写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(
    <div>
        {
          names.map(function (name, key) {
            return <div key={key}>Hello, {name}!</div>
          })
        }
    </div>,
    document.getElementById('example')
);