Data Grid - State
Initialize and read the state of the data grid.
Initialize the state
Some state keys can be initialized with the initialState prop.
This prop has the same format as the returned value of apiRef.current.exportState().
⚠️ The
initialStatecan only be used to set the initial value of the state, the grid will not react if you change theinitialStatevalue later on.If you need to fully control specific models, use the control props instead (e.g.
prop.filterModelorprop.sortModel). You can find more information on the corresponding feature documentation page.
<DataGrid
{...data}
loading={loading}
initialState={{
...data.initialState,
filter: {
filterModel: {
items: [{ columnField: 'quantity', operatorValue: '>', value: 10000 }],
},
},
sorting: {
sortModel: [{ field: 'desk', sort: 'asc' }],
},
}}
/>Access the state
The state is exposed on the apiRef object.
It is strongly advised not to access the state values directly because the state itself is not considered a public API and its structure can change.
The @mui/x-data-grid-pro package exposes a set of state selectors that take the apiRef.current.state as an argument and return a value.
You can use them to get data from the state without worrying about its internal structure.
Direct selector access
The simplest way to use a selector is to call it as a function with apiRef as its first argument.
const pageSize = gridPageSizeSelector(apiRef);
Note: Calling with
apiRef.current.statealso works but it may cause side effects when multiple grid instances are in the same page. If you still need to call it with the state, do not forget to pass the instance ID as the example:const pageSize = gridPageSizeSelector( apiRef.current.state, apiRef.current.instanceId, );
<Button size="small" onClick={handleSelectFirstVisibleRow}>
Toggle the selection of the 1st row of the page
</Button>
<Box sx={{ width: '100%', height: 400, bgcolor: 'background.paper' }}>
<DataGridPro apiRef={apiRef} pagination pageSize={10} {...data} />
</Box>With useGridSelector
If you want to access sole state value in the render of your components, you can use the useGridSelector hook.
const pageSize = useGridSelector(apiRef, gridPageSizeSelector);
⚠️ This hook can only be used inside the context of the grid, such as custom components. Otherwise, you will have an error saying that the state is not initialized during the first render.
<DataGridPro
{...data}
loading={loading}
apiRef={apiRef}
pagination
pageSize={10}
hideFooter
components={{
Toolbar,
}}
/>Filtering
gridFilterModelSelector
Get the current filter model.Signature:
gridFilterModelSelector: (apiRef: GridApiRef) => GridFilterModel
// or
gridFilterModelSelector: (state: GridState, instanceId?: number) => GridFilterModelExample
gridFilterModelSelector(apiRef)
// or
gridFilterModelSelector(state, apiRef.current.instanceId)gridFilterStateSelector
Signature:
gridFilterStateSelector: (state: GridState) => GridFilterStateExample
const filterState = gridFilterStateSelector(apiRef.current.state);gridFilteredSortedRowEntriesSelector
Get the id and the model of the rows accessible after the filtering process. Contains the collapsed children.Signature:
gridFilteredSortedRowEntriesSelector: (apiRef: GridApiRef) => { id: GridRowId; model: { [key: string]: any } }[]
// or
gridFilteredSortedRowEntriesSelector: (state: GridState, instanceId?: number) => { id: GridRowId; model: { [key: string]: any } }[]Example
gridFilteredSortedRowEntriesSelector(apiRef)
// or
gridFilteredSortedRowEntriesSelector(state, apiRef.current.instanceId)gridFilteredSortedRowIdsSelector
Get the id of the rows accessible after the filtering process. Contains the collapsed children.Signature:
gridFilteredSortedRowIdsSelector: (apiRef: GridApiRef) => GridRowId[]
// or
gridFilteredSortedRowIdsSelector: (state: GridState, instanceId?: number) => GridRowId[]Example
gridFilteredSortedRowIdsSelector(apiRef)
// or
gridFilteredSortedRowIdsSelector(state, apiRef.current.instanceId)gridVisibleRowCountSelector
Get the amount of rows accessible after the filtering process.Signature:
gridVisibleRowCountSelector: (apiRef: GridApiRef) => number
// or
gridVisibleRowCountSelector: (state: GridState, instanceId?: number) => numberExample
gridVisibleRowCountSelector(apiRef)
// or
gridVisibleRowCountSelector(state, apiRef.current.instanceId)gridVisibleSortedRowEntriesSelector
Get the id and the model of the rows accessible after the filtering process. Does not contain the collapsed children.Signature:
gridVisibleSortedRowEntriesSelector: (apiRef: GridApiRef) => { id: GridRowId; model: { [key: string]: any } }[]
// or
gridVisibleSortedRowEntriesSelector: (state: GridState, instanceId?: number) => { id: GridRowId; model: { [key: string]: any } }[]Example
gridVisibleSortedRowEntriesSelector(apiRef)
// or
gridVisibleSortedRowEntriesSelector(state, apiRef.current.instanceId)gridVisibleSortedRowIdsSelector
Get the id of the rows accessible after the filtering process. Does not contain the collapsed children.Signature:
gridVisibleSortedRowIdsSelector: (apiRef: GridApiRef) => GridRowId[]
// or
gridVisibleSortedRowIdsSelector: (state: GridState, instanceId?: number) => GridRowId[]Example
gridVisibleSortedRowIdsSelector(apiRef)
// or
gridVisibleSortedRowIdsSelector(state, apiRef.current.instanceId)gridVisibleSortedTopLevelRowEntriesSelector
Get the id and the model of the top level rows accessible after the filtering process.Signature:
gridVisibleSortedTopLevelRowEntriesSelector: (apiRef: GridApiRef) => { id: GridRowId; model: { [key: string]: any } }[]
// or
gridVisibleSortedTopLevelRowEntriesSelector: (state: GridState, instanceId?: number) => { id: GridRowId; model: { [key: string]: any } }[]Example
gridVisibleSortedTopLevelRowEntriesSelector(apiRef)
// or
gridVisibleSortedTopLevelRowEntriesSelector(state, apiRef.current.instanceId)gridVisibleTopLevelRowCountSelector
Get the amount of top level rows accessible after the filtering process.Signature:
gridVisibleTopLevelRowCountSelector: (apiRef: GridApiRef) => number
// or
gridVisibleTopLevelRowCountSelector: (state: GridState, instanceId?: number) => numberExample
gridVisibleTopLevelRowCountSelector(apiRef)
// or
gridVisibleTopLevelRowCountSelector(state, apiRef.current.instanceId)Pagination
gridPageCountSelector
Get the amount of pages needed to display all the rows if the pagination is enabledSignature:
gridPageCountSelector: (apiRef: GridApiRef) => number
// or
gridPageCountSelector: (state: GridState, instanceId?: number) => numberExample
gridPageCountSelector(apiRef)
// or
gridPageCountSelector(state, apiRef.current.instanceId)gridPageSelector
Get the index of the page to render if the pagination is enabledSignature:
gridPageSelector: (apiRef: GridApiRef) => number
// or
gridPageSelector: (state: GridState, instanceId?: number) => numberExample
gridPageSelector(apiRef)
// or
gridPageSelector(state, apiRef.current.instanceId)gridPageSizeSelector
Get the maximum amount of rows to display on a single page if the pagination is enabledSignature:
gridPageSizeSelector: (apiRef: GridApiRef) => number
// or
gridPageSizeSelector: (state: GridState, instanceId?: number) => numberExample
gridPageSizeSelector(apiRef)
// or
gridPageSizeSelector(state, apiRef.current.instanceId)gridPaginatedVisibleSortedGridRowEntriesSelector
Get the id and the model of each row to include in the current page if the pagination is enabled.Signature:
gridPaginatedVisibleSortedGridRowEntriesSelector: (apiRef: GridApiRef) => { id: GridRowId; model: { [key: string]: any } }[]
// or
gridPaginatedVisibleSortedGridRowEntriesSelector: (state: GridState, instanceId?: number) => { id: GridRowId; model: { [key: string]: any } }[]Example
gridPaginatedVisibleSortedGridRowEntriesSelector(apiRef)
// or
gridPaginatedVisibleSortedGridRowEntriesSelector(state, apiRef.current.instanceId)gridPaginatedVisibleSortedGridRowIdsSelector
Get the id of each row to include in the current page if the pagination is enabled.Signature:
gridPaginatedVisibleSortedGridRowIdsSelector: (apiRef: GridApiRef) => GridRowId[]
// or
gridPaginatedVisibleSortedGridRowIdsSelector: (state: GridState, instanceId?: number) => GridRowId[]Example
gridPaginatedVisibleSortedGridRowIdsSelector(apiRef)
// or
gridPaginatedVisibleSortedGridRowIdsSelector(state, apiRef.current.instanceId)gridPaginationRowRangeSelector
Get the index of the first and the last row to include in the current page if the pagination is enabled.Signature:
gridPaginationRowRangeSelector: (apiRef: GridApiRef) => { firstRowIndex: number; lastRowIndex: number } | null
// or
gridPaginationRowRangeSelector: (state: GridState, instanceId?: number) => { firstRowIndex: number; lastRowIndex: number } | nullExample
gridPaginationRowRangeSelector(apiRef)
// or
gridPaginationRowRangeSelector(state, apiRef.current.instanceId)Sorting
gridSortModelSelector
Get the current sorting model.Signature:
gridSortModelSelector: (apiRef: GridApiRef) => GridSortModel
// or
gridSortModelSelector: (state: GridState, instanceId?: number) => GridSortModelExample
gridSortModelSelector(apiRef)
// or
gridSortModelSelector(state, apiRef.current.instanceId)gridSortedRowEntriesSelector
Get the id and the model of the rows after the sorting process.Signature:
gridSortedRowEntriesSelector: (apiRef: GridApiRef) => { id: GridRowId; model: { [key: string]: any } }[]
// or
gridSortedRowEntriesSelector: (state: GridState, instanceId?: number) => { id: GridRowId; model: { [key: string]: any } }[]Example
gridSortedRowEntriesSelector(apiRef)
// or
gridSortedRowEntriesSelector(state, apiRef.current.instanceId)gridSortedRowIdsSelector
Get the id of the rows after the sorting process.Signature:
gridSortedRowIdsSelector: (apiRef: GridApiRef) => GridRowId[]
// or
gridSortedRowIdsSelector: (state: GridState, instanceId?: number) => GridRowId[]Example
gridSortedRowIdsSelector(apiRef)
// or
gridSortedRowIdsSelector(state, apiRef.current.instanceId)Save and restore the state
⚠️ This feature isn't implemented yet. It's coming.
👍 Upvote issue #820 if you want to see it land faster.