question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key.

See original GitHub issue

I’m trying to figure out how can I get unique keys for each record.

So far, my code looks like this:

let lastIndex = 0
const updateIndex = () => {
  lastIndex++
  return lastIndex
}

...

constructor() {
  super()

  this.state = {
    columns: [ {
      title: 'Nombre',
      dataIndex: 'name',
      key: `name${updateIndex()}`
    }, {
      title: 'Precio',
      dataIndex: 'price',
      key: `price${updateIndex()}`
    }, {
      title: 'Precio regular',
      dataIndex: 'regularPrice',
      key: `regularPrice${updateIndex()}`
    }, {
      title: 'Categoría(s)',
      key: `id${updateIndex()}`,
      render: (text, record) => (
        <span>
          {
            record.categories.map((r, index) => (
              <span key={index}>
                {
                  index > 0 &&
                  <span className="ant-divider"/>
                }
                <span>{r.name}</span>
              </span>
            ))
          }
        </span>
      )
    }, {
      title: 'Action',
      key: `id${updateIndex()}`,
      render: (text, record) => (
        <span>
          <a href="#">Editar - {record.name}</a>
          <span className="ant-divider"/>
          <a href="#" style={{color: 'red'}}>Borrar</a>
        </span>
      )
    }, ]
  }
}

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:9
  • Comments:30 (2 by maintainers)

github_iconTop GitHub Comments

502reactions
buvinghausencommented, Jan 8, 2018

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

Source Code Here

<Table 
  columns={columns} 
  dataSource={this.props.categories} 
  rowKey="name" 
/>
180reactions
jdhirocommented, Jul 9, 2018

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' />
Read more comments on GitHub >

github_iconTop Results From Across the Web

Each record in table should have a unique `key` prop,or set ...
Each record in table should have a unique key prop,or set rowKey to an unique primary key. solution 1. each col has a...
Read more >
antdv: Each record in table should have a unique `key` prop,or ...
antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key ; 3. 点赞 ; 踩...
Read more >
Table - Ant Design
According to the React documentation, every child in an array should be assigned a unique key. The values inside the Table's dataSource and...
Read more >
Each record in table should have a unique `key` prop,or set ...
Each record in table should have a unique `key` prop,or set `rowKey` ... in table should have a unique `key` prop,or set `rowKey`...
Read more >
Each record in table should have a unique `key` prop,or set ...
Coding example for the question Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key-Reactjs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found