【发布时间】:2017-02-09 14:30:55
【问题描述】:
我遇到以下错误:
warning.js?0260:45Warning: 数组或迭代器中的每个子元素都应该 有一个独特的“关键”道具。检查
Tabs的渲染方法。
render() {
const { tabs } = this.props;
const { active } = this.state;
return (
<div>
<ButtonToolbar>
{tabs.map(listValue =>
<Button onClick={() => this.handleClick(listValue)}>
{listValue}
</Button>
)}
</ButtonToolbar>
// Display the chosen tab
{this.showTab(active)}
</div>
);
在这种情况下,我应该在哪里添加 id 属性。我试过了:
<ButtonToolbar>
{tabs.map(listValue =>
<Button key={listValue.id} onClick={() => this.handleClick(listValue)}>
{listValue}
</Button>
)}
</ButtonToolbar>
但它并没有摆脱错误。请指教
【问题讨论】:
-
如果你 console.log(listValue.id) 它们都是唯一的吗?
-
你也可以
tabs.map((listValue, index) => <Button key={listValue.id + index} onClick={() => this.handleClick(listValue)}> {listValue} </Button> )}