ANTD表的控制台警告。警告:列表中的每个孩子都应具有独特的“键”。支柱

发布于 2025-01-27 11:07:48 字数 1191 浏览 3 评论 0原文

观察控制台中以下警告。在调试时,可以观察到警告来自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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中楼上月下 2025-02-03 11:07:48

两种方法!

警告是因为dataSource中的每个行(todo)没有唯一的属性

您可以通过选择以下方式之一来解决此问题。

  1. 添加一个属性带有数组数据源中每个对象的唯一值。

    中的每个对象。

  2. 在数据源中,属性id似乎是每一行唯一的。
    您需要使表组件考虑id属性以识别特定行。

您可以通过将Rowkey Prop传递到表组件来做到这一点。

  <Table
    columns={columns}
    dataSource={dataSource}
    rowKey={(record) => record.id}
  />

注意:此处ID是唯一的。因此代码如上所述。如果唯一值与另一个密钥索引一起使用,则需要相应地更改代码。 (如下)

rowKey={(record) => record.index}

2 ways of doing this!

The warning is because every row(todo) in dataSource didn't have a unique property key.

You can fix this by choosing one of the following ways.

  1. Add a property key with a unique value for every object in the array dataSource.

  2. 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.

  <Table
    columns={columns}
    dataSource={dataSource}
    rowKey={(record) => record.id}
  />

Note: Here id is unique. So the code is as above. If in case the unique value is with another key index then you need to change the code accordingly. (as below)

rowKey={(record) => record.index}
风月客 2025-02-03 11:07:48

这样做的第三种方法(如果您没有ID或索引属性):

datasource={props.data}
rowKey={record => props.data.indexOf(record)}

prop.data是一个数组。

3rd way of doing this (if you don't have an id or index property):

datasource={props.data}
rowKey={record => props.data.indexOf(record)}

where prop.data is an array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文