Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key
React renders lists using the key prop. It works so because react allows you to reduce the complexity of diffing algorithms and reduce the number of DOM mutations. You can read a bit more in react reconciliation docs: https://reactjs.org/docs/reconciliation.html
In your case, you added the keys to the columns, but not for rows. Add the key field to the data source. So your code could be the following:
import React, { Component } from 'react';import { Table} from 'antd';import { adalApiFetch } from '../../adalConfig';class ListTenants extends Component { constructor(props) { super(props); this.state = { data: [] }; } fetchData = () => { adalApiFetch(fetch, "/Tenant", {}) .then(response => response.json()) .then(responseJson => { if (!this.isCancelled) { const results= responseJson.map(row => ({ key: row.id, // I added this line ClientId: row.ClientId, ClientSecret: row.ClientSecret, Id: row.Id, SiteCollectionTestUrl: row.SiteCollectionTestUrl, TenantDomainUrl: row.TenantDomainUrl })) this.setState({ data: results }); } }) .catch(error => { console.error(error); }); }; componentDidMount(){ this.fetchData(); } render() { const columns = [ { title: 'Client Id', dataIndex: 'ClientId', key: 'ClientId' }, { title: 'Site Collection TestUrl', dataIndex: 'SiteCollectionTestUrl', key: 'SiteCollectionTestUrl', }, { title: 'Tenant DomainUrl', dataIndex: 'TenantDomainUrl', key: 'TenantDomainUrl', } ]; return ( <Table columns={columns} dataSource={this.state.data} /> ); }}export default ListTenants;
React Table unique key / rowKey
Each record in table should have a unique
key
prop,or setrowKey
to an unique primary key.
solution 1
each col has a unique
key
// each column with unique keyimport 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
only need set
rowkey
on the table with the unique value
// table with rowkeyimport 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;