ANTD表的控制台警告。警告:列表中的每个孩子都应具有独特的“键”。支柱
观察控制台中以下警告。在调试时,可以观察到警告来自ANTD表组件。
所有代码都附加在下面。
表组件
<Table
columns={columns}
dataSource={dataSource}
/>
dataSource
const dataSource = [
{
id: "1",
todo: "todo-1",
description: "description-1",
status: false
},
{
id: "2",
todo: "todo-2",
description: "description-2",
status: true
},
{
id: "3",
todo: "todo-3",
description: "description-3",
status: false
}
];
列
const columns = [
{
title: "Todo",
dataIndex: "todo",
key: "todo"
},
{
title: "Description",
dataIndex: "description",
key: "description"
},
{
title: "Status",
dataIndex: "status",
key: "status",
render: (tag) => (
<>
<Tag color={tag ? "green" : "red"}>{tag ? "Done" : "Pending"}</Tag>
</>
)
}
];
Observing the below warning in the console. On debugging it is observed that the warning is coming from antd table component.
All the code is attached below.
Table Component
<Table
columns={columns}
dataSource={dataSource}
/>
dataSource
const dataSource = [
{
id: "1",
todo: "todo-1",
description: "description-1",
status: false
},
{
id: "2",
todo: "todo-2",
description: "description-2",
status: true
},
{
id: "3",
todo: "todo-3",
description: "description-3",
status: false
}
];
columns
const columns = [
{
title: "Todo",
dataIndex: "todo",
key: "todo"
},
{
title: "Description",
dataIndex: "description",
key: "description"
},
{
title: "Status",
dataIndex: "status",
key: "status",
render: (tag) => (
<>
<Tag color={tag ? "green" : "red"}>{tag ? "Done" : "Pending"}</Tag>
</>
)
}
];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两种方法!
警告是因为
dataSource
中的每个行(todo)没有唯一的属性键
。您可以通过选择以下方式之一来解决此问题。
添加一个属性
键
带有数组数据源中每个对象的唯一值。中的每个对象。
在数据源中,属性
id
似乎是每一行唯一的。您需要使表组件考虑
id
属性以识别特定行。您可以通过将Rowkey Prop传递到表组件来做到这一点。
注意:此处
ID
是唯一的。因此代码如上所述。如果唯一值与另一个密钥索引
一起使用,则需要相应地更改代码。 (如下)2 ways of doing this!
The warning is because every row(todo) in
dataSource
didn't have a unique propertykey
.You can fix this by choosing one of the following ways.
Add a property
key
with a unique value for every object in the array dataSource.Here in the dataSource the property
id
seems to be unique for each row.You need to make the table component consider the
id
property for identifying a specific row uniquely.You can do this by passing a rowKey prop to the Table Component.
Note: Here
id
is unique. So the code is as above. If in case the unique value is with another keyindex
then you need to change the code accordingly. (as below)这样做的第三种方法(如果您没有ID或索引属性):
prop.data是一个数组。
3rd way of doing this (if you don't have an id or index property):
where prop.data is an array.