onFetchData called twice? one before rendering and the other one after rendering
In order to reset filter and sort, I have used filtered and sored prop, and I do controll them by onFilterChange and onSortChange. Code like this:
render(){
console.log('-=-=-=-=render state:', this.state)
...
return <ReactTable
{...this.props.pageSetting} //默认全局配置
columns={columns}
manual // Forces table not to paginate or sort automatically, so we can handle it server-side
data={data}
pages={pages} // Display the total number of pages
loading={loading} // Display the loading overlay when we need it
onFetchData={this.fetchData.bind(this)} // Request new data when things change
sorted={this.state.sorted} // Order to reset sort, if dont need reset could remove this
onSortedChange={this.onSortChange.bind(this)} // Order to reset sort, if dont need reset could remove this
filterable // Show the filter row
filtered={this.state.filtered} // Order to reset filter, if dont need reset could remove this
onFilteredChange={this.onFilterChange.bind(this)} // Order to reset filter, if dont need reset could remove this
onPageSizeChange={(pageSize) => { this.setState({ pageSize }) }} // Handler when page size change
className="-striped -highlight"
/>
}
onSortChange(newSort, column, shiftKey) {
// console.log('-=-=-=123 newSort:', newSort)
// console.log('-=-=-=123 column:', column)
// console.log('-=-=-=123 shiftKey:', shiftKey)
this.setState({ sorted: newSort })
}
onFilterChange(filter, value) {
console.log('-=-=-=123 filter:', filter)
console.log('-=-=-=123 value:', value)
this.setState({ filtered: filter })
}
As the code, I set state in onFilteredChange, but why onFetchData called before render? And log like this, it call twice api do confuse me!

If you are using onFetchData you can't really use the other on* callbacks with a loop-back prop. It is called before render because it needs something to render. You really need to consider if you need to use onFetchData because it is a specialised tool and you need to know what you are doing with it (that's why it is towards the end of the doco).
You see the manual in there - that means that onFetchData is being passed ALL of the information about sorting, pagination, etc. - just like the comment says. Which means you can't have onFilteredChange and onSortedChange - it doesn't make sense.
I think @jyjin's usecase does make sense. I also need to control the state of the filter and sort from outside (to reset it). So on the state of my TableComponent, I keep the sorted and filtered arrays, and pass them down as props to ReactTable.
Seeing as the table is a remote table, I do need manual and onFetchData, as that is the only way to deal with server-side data as far as I understood.
Right now my problem is with the filtering, when I change the filter the onFetchData is called two times, first with the old filter value and then a second time with the new filter value.
If setting onFilteredChange and onSortedChange does not make sense, what would be a suggestion to achieve the functionality of being able to reset the sorted and filtered?
What could also work is if the ReactTable component would expose a method resetTable which would reset the sorting/filtering/etc.
EDIT: I think I might have found a workaround. Now I'm also keeping page on my own state. Instead of having a onFetchData function, I have a refreshData function that fetches the data again based on the state (page, sorted, filtered). Then, I changed my onSortedChange and onFilteredChange to call that refreshData function in the setState callback. I also added a onPageChange to set the new page on my state. Also I'm passing down page as a prop (this.state.page).
Sorry for the incoherent post, but now I see what you mean that it doesn't make sense @gary-menzel. Indeed having both the 'one-function-for-everything approach' (onFetchData) and still being able to control part of the state yourself (via onSortedChange etc) doesn't really work nicely together and you should either choose to keep everything on your own state, or let ReactTable keep its own state and then deal with it via onFetchData yourself.
@gary-menzel I agree with @mrijke you need callback methods to let react-table know that page, filter or sort has changed. I am also facing a issue where on page change server is called twice.
EDIT - now I agree with @gary-menzel now I removed the callbacks and still onFetch data gets triggered with updated filters, sorts and page just fine.
Just because you CAN do something with ReactTable does not mean that it was meant to work that way or is reliable. That is my point. If it works for you and you don't care about possible breaking changes then go right ahead.
It seems as though @mrijke understands what I mean by using either onFetchData or the on*Change callbacks - but not both. Using both WILL eventually cause double calls.
You don't need the callback method to push props in to ReactTable. You only need the callback methods to know that the user changed something. In the case of onFetchData anything that may have caused a change is passed to onFetchData - so you should not need any of the callback methods and your end-of-pipe call to setData should then cause the update.
Obviously, if you call setData outside of that pipeline then it will cause another update - that is the nature of React.
Also - the reasons for the double call have been explained and it will take a full re-write of ReactTable to address them so I am locking this conversation.
I updated my comment above... 👍 it make sense what you said.