gpt4 book ai didi

javascript - React/JSX 错误 : Any use of a keyed object should be wrapped in React. 插件

转载 作者:行者123 更新时间:2023-11-28 11:50:55 25 4
gpt4 key购买 nike

整个错误:

"Warning: Each child in an array or iterator should have a unique \"key\" prop. Check the render method of RenderArray"

代码:

/* jshint esnext: true */

class RenderArray extends React.Component {

constructor() {
super();
this.state = {myArray : ""};
}

componentDidMount() {
console.log(this.state.myArray);
}

componentWillMount () {
this.setState({
myArray: ['one', 'two', 'three', 'four', 'five']
});
}


render () {

showEl = this.state.myArray.map(function(i) {
return <li>{i}</li>;
})


return (
<div className="jumbotron container">
<ul>
{showEl}
</ul>
</div>
);
}
};

建议?

最佳答案

对于每个重复的元素,react 需要一个唯一的键。因此,就您而言,类似于:

showEl = this.state.myArray.map(function(i) {
return <li key={i}>{i}</li>;
})

关于javascript - React/JSX 错误 : Any use of a keyed object should be wrapped in React. 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37601466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com