![](/img/trans.png)
[英]Warning: Each child in an array or iterator should have a unique “key” prop.; key prop already added in li
[英]Persistent Warning: “Each child in an array or iterator should have a unique 'key' prop” even if key is already set
我一直试图想出反应已经显示的警告。 显然,我已经使用密钥设置了标签,但警告仍然存在。
render() {
return (
<div className="table">
<table>
<tbody>
{Object.entries(this.props.rowData).map(rowData =>
<tr key={rowData.id}>
{Object.entries(rowData).map((data, index) =>
<td key={index}> Test </td>
)}
</tr>
)}
</tbody>
</table>
</div>
);
}
您似乎误解了Object.entries
作用。
Object.entries
为它包含的每个键/值对返回一个元组数组(包含2个元素的数组):
// logs [["id", 5], ["name", "example"]] console.log( Object.entries( { id: 5, name: 'example' } ) )
你可能想要的是Object.values
,它将从一个对象返回一个值数组。
// logs [5, 'example'] console.log( Object.values( { id: 5, name: 'example' } ) )
编辑:
根据您提供的数据 , rowData
是一个数组,因此不需要使用任何Object.*
方法。 我们可以直接映射它:
render() {
return (
<div className="table">
<table>
<tbody>
{this.props.rowData.map(row =>
<tr key={row.id}>
{Object.entries(row).map(([key,value], index) =>
<td key={index}> {key},{value} </td>
)}
</tr>
)}
</tbody>
</table>
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.