State Management Guide
Mantine React Table does not try to hide any of its internal state from you. You can initialize state with custom initial values, manage individual states yourself as you discover the need to have access to them, or store your own reference to the entire table instance wherever you need to.
This is all optional, of course. If you do not need access to any of the state, you do not need to do anything and it will just automatically be managed internally.
See the State Options API Docs for more information on which states are available for you to manage.
Relevant Table Options
# | Prop Name | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 | Partial<MRT_TableState<TData>> |
| Table State Management Guide | ||
2 | Partial<MRT_TableState<TData>> |
| Table State Management Guide | ||
Populate Initial State
If all you care about is setting parts of the initial or default state when the table mounts, then you may be able to specify that state in the initialState
prop and not have to worry about managing the state yourself.
For example, let's say you do not need access to the showColumnFilters
state, but you want to set the default value to true
when the table mounts. You can do that with the initialState
prop:
Note: If you use both
initialState
andstate
, the state initializer instate
prop will take precedence and overwrite the same state values ininitialState
. So just use eitherinitialState
orstate
, not both for the same states.
Manage Individual States as Needed
It is pretty common to need to manage certain state yourself, so that you can react to changes in that state, or have easy access to it when sending it to an API.
You can pass in any state that you are managing yourself to the state
prop, and it will be used instead of the internal state. Each state property option also has a corresponding on[StateName]Change
callback that you can use set/update your managed state as it changes internally in the table.
For example, let's say you need to store the pagination, sorting, and row selection states in a place where you can easily access it in order to use it in parameters for an API call.
Add Side Effects in Set State Callbacks
In React 18 and beyond, it is becoming more discouraged to use useEffect
to react to state changes, because in React Strict Mode (and maybe future versions of React), the useEffect hook may run twice per render. Instead, more event driven functions are recommended to be used. Here is an example for how that looks here. The callback signature for the on[StateName]Change
works just like a React setState callback from the useState
hook. This means that you have to check if the updater is a function or not, and then call the setState function with the updater callback if it is a function.
Read From the Table Instance
Note: Previously, in early MRT v1 betas, you could use the
tableInstanceRef
prop to get access to the table instance. This is no longer necessary as theuseMantineReactTable
hook now just returns the table instance directly.
The useMantineReactTable
hook returns the table instance. The <MantineReactTable />
needs the table instance for all of its internal logic, but you can also use it for your own purposes.
The table instance is the same object that you will also see as a provided parameter in many of the other callback functions throughout Mantine React Table, such as all the render...
props or the Cell
or Header
render overrides in the column definition options.
Persistent State
Persistent state is not a built-in feature of Mantine React Table, but it is an easy feature to implement yourself using the above patterns with the state
prop and the on[StateName]Change
callbacks. Here is an example of how you might implement persistent state using sessionStorage
:
First Name | Last Name | City | State | Salary |
---|---|---|---|---|
Allison | Brown | Omaha | Nebraska | 10000 |
Harry | Smith | Hickman | Nebraska | 20000 |
Sally | Williamson | Alliance | Nebraska | 30000 |
Lebron | James | Indianapolis | Indiana | 40000 |
Michael | McGinnis | Harrisonburg | Virginia | 150000 |
Joseph | Williams | Valentine | Nebraska | 100000 |
Noah | Brown | Toledo | Ohio | 50000 |
Mason | Zhang | Sacramento | California | 100000 |
Violet | Doe | San Francisco | California | 100000 |