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

版权声明:本文为博主原创文章,转载请说明出处 https://blog.csdn.net/u010002184/article/details/83031832

antd使用Table报错,

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

解决方法:

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' />

https://github.com/ant-design/ant-design/issues/7623

意思就是:这个报错是说需要对dataSource的行加key,不是对列加key,同时这里的rowKey需要使用行的唯一的属性名,不能使用不唯一的属性名或不存在的属性名,不然还是报那个错。列是否加key均可。

如果使用不唯一的属性名作为行key,则报错如下:

Warning: Encountered two children with the same key, `XXX`. Keys should be unique so that components maintain their identity across updates.

如果使用不存在的属性名作为行key,则报错如下:

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

猜你喜欢

转载自blog.csdn.net/u010002184/article/details/83031832