最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - antd table: why record.key returns undefined when I have set the rowkey - Stack Overflow

programmeradmin5浏览0评论

The table has rowkey which is equal to the column "mac", but I can not get the key by record.key, it returns undefined, but record.mac works?

The table has rowkey which is equal to the column "mac", but I can not get the key by record.key, it returns undefined, but record.mac works?

Share Improve this question edited Jul 5, 2019 at 8:27 Dennis Vash 54k12 gold badges117 silver badges132 bronze badges asked Jul 5, 2019 at 4:06 Liao PeiLiao Pei 311 silver badge3 bronze badges 2
  • there are a few properties on a react ponent that are internal to the ponent. children is one, and another is key. React defines keys for every element / ponent and uses it for rendering performance purposes – John Ruddell Commented Jul 5, 2019 at 4:23
  • Please post code not an image – Dennis Vash Commented Jul 5, 2019 at 8:15
Add a ment  | 

3 Answers 3

Reset to default 5

The rowKey property at Table ponent accepts a function or a string if it's a single row table:

rowKey - Row's unique key, could be a string or function that returns a string string|Function(record):string

You should always provide a function, for example, if dataSource has key property: record => record.key

<Table columns={columns} dataSource={dataSource} rowKey={record => record.key} />;

The reason is that the concept of a key is something that is controlled by React internals before your ponent gets created.

You should define props.

the <Table /> ponent will take in a columnMap that maps to your dataSource.

You will need to set a rowKey if the objects in your dataSource don't specify a key parameter:

const dataSource = [{
 id: 123,
 name: 'Karl',
 nested: { id: 456 },
 mac: "some-unique-identifier"
}, ...]

alternatively, you can append one to your data and you don't need a rowKey:

const dataSource = [{
 id: 123,
 name: 'Karl',
 nested: { id: 456 },
 key: 123,
 mac: "some-unique-identifier"
}, ...]

this requires an extra step in processing.

usage with no 'key' parameter:

render() {
  return <Table
          dataSource={dataSource}
          columns={unspecifiedColumnNames}
          rowKey={(row) => row.id || row.nested.id || row.mac} // either 123 || 456 || "some-unique-identifier"
         />
}

It seems at least in antd 4.16.13 the rowKey parameter is to be deprecated. I just append a key parameter to my dataSource manually instead of doing it in the rowKey attribute's function.

发布评论

评论列表(0)

  1. 暂无评论