New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Each record in table should have a unique key
prop,or set rowKey
to an unique primary key.
#7623
Comments
Hello @johhansantana, your issue has been closed because it does not conform to our issue requirements. Please use the Issue Helper to create an issue, thank you! |
That means the key of your data, not columns. |
I have the same problem. How can we solve this? @ant-design-bot @afc163 |
With columns if not set dataIndex property, column key should be set unique key. @johhansantana key not unique ↓ title: 'Categoría(s)', |
@ahmetkuslular The error message is pretty explicit your data set must contain items with a key property on them or you must specify the rowKey property on the Table component with the name from a field in your dataset which is unique to each row. <Table
columns={columns}
dataSource={this.props.categories}
rowKey="name"
/> |
This one was confusing to me as well, because I was confusing COLUMN keys with ROW keys. This error has nothing to do with "key" in your column config. Ant is expecting you to have a unique key specifically named "key" on each row of data, like so: { key: 1, value: 'foo'} My data had "id" instead: { id: 1, value: 'foo'} In order to resolve this, I added a rowKey property to my Table like so: <Table columns={this.columns} dataSource={this.state.results} rowKey='id' /> |
|
In Ant-design table, rowKey props type is string. |
Hey guys, adding rowKey to Table solved my issue and 'id' is key of one of the column inside table as - |
solution 1
// each column with unique key
import React from 'react';
import {
Table,
} from 'antd';
const leftTableColumns = [
{
title: 'Page / Modal',
dataIndex: 'pageModal',
key: 'pageModal',
},
{
title: 'Success Rate',
dataIndex: 'successRate',
key: 'successRate',
},
];
const LeftTable = (props) => {
const {
leftTableDatas,
} = props;
return (
<>
<Table
columns={leftTableColumns}
dataSource={leftTableDatas}
/>
</>
);
};
export {
LeftTable,
};
export default LeftTable;
solution 2
// table with rowkey
import React from 'react';
import {
Table,
} from 'antd';
const leftTableColumns = [
{
title: 'Page / Modal',
dataIndex: 'pageModal',
},
{
title: 'Success Rate',
dataIndex: 'successRate',
},
];
const LeftTable = (props) => {
const {
leftTableDatas,
} = props;
return (
<>
<Table
// shorthand rowKey
rowkey="id"
// rowKey={obj => obj.id}
columns={leftTableColumns}
dataSource={leftTableDatas}
/>
</>
);
};
export {
LeftTable,
};
export default LeftTable;
ref |
@xgqfrms, in solution 2, you can shorten the |
What I am getting is this message
indicating that the problem in the I have noticed the examples on Antd page are not throwing this error. Thanks for help. |
Hey, I've just figured it out. In It is mentioned in this section: https://ant.design/components/table/#Column |
@tuanalumi, thanks for pointing that out, I have added |
fixed it for me, thanks |
What if I want to set rowKey with index in antd V4 |
Worked like a charm, thank you! |
Awesome :) Thank You <3 |
Don't work for me |
Weirdly enough I got this error because I tried to wrap So instead of doing this: <Table {...tableProps}>
<TableColumns columns={columns} /> {/* prints all column groups and columns */}
</table> I had to do this: <Table {...tableProps}>
{getTableColumns(columns)}
</table> Not working with custom components is bad, but also printing that there's something wrong with the key is terrible as well because I spent hours trying to fix my data source, column keys, etc. And they were absolutely fine :\ Not a very descriptive error. |
what if my data doesn't have unique key data? |
just put a random id for each or use index value. |
if your data doesn't have an id yet. you can try |
@cit-gif @autumnblue when using
|
@luckymore you can see my code example : https://codesandbox.io/s/selection-and-operation-antd-4-20-7-forked-evqw0o?file=/demo.js |
@luckymore I tried on tree data table and its not working: |
@luckymore if you use key in object then you don't need rowKey |
@duyhung85 Just like @cit-gif said, I make a mistake.... you should set the key in |
in Vue 3:
|
what if my api has no ids? i mean all data may have same value |
@vitalijalbu you may want to generate the ID on the fly try to figure out first:
|
I'm trying to figure out how can I get unique keys for each record.
So far, my code looks like this:
But I'm still getting the error that each record in table should have a unique
key
.What would be the best way to achieve this?
The text was updated successfully, but these errors were encountered: