Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

antd Table is not automatically rerendered when datasource data changes

Ant Design Table is not rerendered automatically when datasource data changes.

<Table
                columns={columns}
                dataSource={filteredData}
                pagination={pagination}
                loading={loading}
                onChange={this.handleChange} />

filteredData is changed in a method based on a custom filter which is placed outside the table.

Shouldn't the table rerender automatically when filteredData is changed?

Does anybody know how to refresh the table when filteredData is changed?

like image 689
uzzi Avatar asked Dec 21 '25 18:12

uzzi


2 Answers

If you want a Table to re-render automatically, filteredData should be state.

onSourceChange = (something) => {
  this.setState({filteredData: something})
}
render(){
  return (
   <div>
    <Table
      columns={columns}
      dataSource={this.state.filteredData}
      pagination={pagination}
      loading={loading}
      onChange={this.handleChange} />
    <button onClick={()=>onSourceChange(['a','b','c'])}>change datasource</button>
   </div>
  )}
like image 51
Noname Avatar answered Dec 24 '25 11:12

Noname


I can imagine that people are still looking for answers specially with the newest version of antd.

So in Antd version 5.x table API, you could find a property call rowKey.

In version 4.0 table API, the property called key thou.

And to way to tackle the problem correctly is to set it like following:

<Table
   columns={columns}
   dataSource={this.state.filteredData}
   rowKey={this.state.filteredData.key}
   pagination={pagination}
   loading={loading}
   onChange={this.handleChange} />

Note: Please consider to have a key property in your array of course. key must be unique.

like image 39
Hooman Avatar answered Dec 24 '25 10:12

Hooman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!