Below are some of the most frequently asked questions on how to use the React Table API to solve various table challenges you may encounter
Occasionally, you may need to override some of the table state from a parent component or from somewhere above the usage of
useTable. In this case, you can turn to
useControlledState option. This hook function is run on every render and allows you an opportunity to override or change the final table state in a safe way.
For example, to control a table's
pageIndex from a parent component:
It's important that the state override is done within a
useMemocall to prevent the state variable from changing on every render. It's also extremely important that you always use the
statein any dependencies to ensure that you do not block normal state updates.
When managing your data externally or asynchronously (eg. server-side pagination/sorting/grouping/etc), you will need to fetch new data as the internal table state changes. With React Hooks, this is fantastically easier than it was before now that we have the
React.useEffect hook. We can use this hook to "watch" the table state for specific changes and use those effects to trigger fetches for new data (or synchronize any other state you may be managing externally from your table component):
Using this approach, you can respond and trigger any type of side-effect using the table instance!
React Table has a few built-in side-effects of it's own (most of which are meant for resetting parts of the state when
data changes). By default, these state side-effects are on and when their conditions are met, they immediately fire off actions that will manipulate the table state. Sometimes, this may result in multiple rapid rerenders (usually just 2, or one more than normal), and could cause any side-effects you have watching the table state to also fire multiple times in-a-row. To alleviate this edge-case, React Table exports a
useAsyncDebounce function that will allow you to debounce rapid side-effects and only use the latest one.
A good example of this when doing server-side pagination and sorting, a user changes the
sortBy for a table and the
pageIndex is automatically reset to
0 via an internal side effect. This would normally cause our effect below to fire 2 times, but with
useAsyncDebounce we can make sure our data fetch function only gets called once:
Most plugins use state that should normally reset when the data sources changes, but sometimes you need to suppress that from happening if you are filtering your data externally, or immutably editing your data while looking at it, or simply doing anything external with your data that you don't want to trigger a piece of table state to reset automatically.
For those situations, each plugin provides a way to disable the state from automatically resetting internally when data or other dependencies for a piece of state change. By setting any of them to
false, you can stop the automatic resets from being triggered.
Here is an example of stopping basically every piece of state from changing as they normally do while we edit the
data source for a table:
Now, when we update our data, the above table states will not automatically reset!