onFetchData called twice? one before rendering and the other one after rendering
See original GitHub issueIn 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!
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
onFetchData called twice? one before rendering and ... - GitHub
It is called before render because it needs something to render. You really need to consider if you need to use onFetchData because...
Read more >React-table onFetchData getting triggered twice - Stack Overflow
My solution is: declare a fetchData state: const [fetchState, setFetchState] = useState(null);. perform a deep equal comparison to avoid ...
Read more >Why is my fetch getting called twice? : r/reactjs - Reddit
It shows it getting called twice in the console, but only shows once rendered on the page. import React, { useEffect, useState }...
Read more >React Table v6 - npm
Start using react-table in your project by running `npm i react-table` ... Column Header Groups; Custom Cell and Header and Footer Rendering ......
Read more >FAQ | React Table - TanStack
This hook function is run on every render and allows you an opportunity to override or change the final table state in a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 theon*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 toonFetchData
- so you should not need any of the callback methods and your end-of-pipe call tosetData
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.I updated my comment above… 👍 it make sense what you said.