table icon indicating copy to clipboard operation
table copied to clipboard

[Feature] Manual firing fetchData

Open MAKARD opened this issue 8 years ago • 7 comments

Manual fetching data may be useful in interactive tables. For example, data on server is updating every 1 minute and its will be good to not unmount(or change sort/page/filter etc) table for fetch this data.

MAKARD avatar Feb 15 '18 08:02 MAKARD

You should fetch that data outside of ReactTable. onFetchData is not really meant for real-time updates. It is designed to let you know when the USER has taken an action that requires you to get new data - not for when the server has added data and you want to refresh ReactTable.

State management tools like Redux or MobX are meant for that type of work.

gary-menzel avatar Feb 15 '18 08:02 gary-menzel

@gary-menzel What about refresh table button? User make action in this case.

MAKARD avatar Feb 15 '18 08:02 MAKARD

You loaded the data externally and passed it into ReactTable. You should also reload the data externally and pass it in.

React works by observing when properties change to refresh data.

There is no action that a use can take INSIDE ReactTable to request a reload of data. It is an external action. Your data came either from props you passed in or state you initialised in something like componentDidMount in the Component that is wrapping ReactTable. That is where the data should be reloaded.

gary-menzel avatar Feb 15 '18 10:02 gary-menzel

@MAKARD, try use react ref and statefull react component:

class TableWithRefresh extends React.Component {
  updateTableData = () => this.table.fireFetchData();
  render() {
    const { tableProps } = this.props;
    return (
      <div>
        <button onClick={this.updateTableData}>Refresh data</button>
        <ReactTable
          {...tableProps}
          ref={(instance) => { this.table = instance; }}
        />
      </div>
    );
  }
}

If needed you can also use ref with stateless components, see https://reactjs.org/docs/refs-and-the-dom.html#refs-and-functional-components

0777101 avatar Mar 16 '18 10:03 0777101

The instance can now be accessed from render props https://github.com/react-tools/react-table#functional-rendering

MichaelBuen avatar May 29 '18 14:05 MichaelBuen

@gary-menzel

You loaded the data externally and passed it into ReactTable. You should also reload the data externally and pass it in.

this isn't fully true in all cases. react-table owns/controls state too. that is, i passed it onFetchData={this.fetchData}, react-table owns the stateful data that gets passed in as args to onFetchData. consequently, i can't just load my own data without a bump from react-table sharing its state with me

cdaringe avatar Jan 06 '19 00:01 cdaringe

@0777101, great find. thanks for finding that!

for future viewers, https://github.com/react-tools/react-table/issues/808#issuecomment-373673915 works, even if calling into an unofficial API is a little dicey

cdaringe avatar Jan 06 '19 00:01 cdaringe