[英]Persistent Warning: “Each child in an array or iterator should have a unique 'key' prop” even if key is already set
I've been trying to figure about the warning that react had been showing. 我一直试图想出反应已经显示的警告。 Clearly I have the tags set with key yet the warning still insists.
显然,我已经使用密钥设置了标签,但警告仍然存在。
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>
);
}
It seems that you are misunderstanding what Object.entries
does. 您似乎误解了
Object.entries
作用。
Object.entries
returns an array of tuples (array of 2 elements) for each key/value pair it contains: Object.entries
为它包含的每个键/值对返回一个元组数组(包含2个元素的数组):
// logs [["id", 5], ["name", "example"]] console.log( Object.entries( { id: 5, name: 'example' } ) )
What you probably want is Object.values
which will return an array of values from an object. 你可能想要的是
Object.values
,它将从一个对象返回一个值数组。
// logs [5, 'example'] console.log( Object.values( { id: 5, name: 'example' } ) )
EDIT: 编辑:
Based on the data you provided, rowData
is an array so there's no need to use any Object.*
methods on it. 根据您提供的数据 ,
rowData
是一个数组,因此不需要使用任何Object.*
方法。 We can just map on it directly: 我们可以直接映射它:
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.