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.
⚠️ The
initialState
can only be used to set the initial value of the state, the grid will not react if you change theinitialState
value later on.If you need to fully control specific models, use the control props instead (e.g.
prop.filterModel
orprop.sortModel
). You can find more information on the corresponding feature documentation page.
<DataGrid
{...data}
loading={loading}
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 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 the state as its first argument.
const pageSize = gridPageSizeSelector(apiRef.current.state);
<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: (state: GridState) => GridFilterModel
Example
const filterModel = gridFilterModelSelector(
apiRef.current.state
);
gridVisibleRowCountSelector
Get the amount of rows accessible after the filtering process.Signature:
gridVisibleRowCountSelector: (state: GridState) => number
Example
const visibleRowCount = gridVisibleRowCountSelector(
apiRef.current.state
);
gridVisibleSortedRowEntriesSelector
Get the id and the model of the rows accessible after the filtering process.Signature:
gridVisibleSortedRowEntriesSelector: (state: GridState) => { id: GridRowId; model: { [key: string]: any } }[]
Example
const visibleSortedRowEntries = gridVisibleSortedRowEntriesSelector(
apiRef.current.state
);
gridVisibleSortedRowIdsSelector
Get the id of the rows accessible after the filtering process.Signature:
gridVisibleSortedRowIdsSelector: (state: GridState) => GridRowId[]
Example
const visibleSortedRowIds = gridVisibleSortedRowIdsSelector(
apiRef.current.state
);
gridVisibleSortedTopLevelRowEntriesSelector
Get the id and the model of the top level rows accessible after the filtering process.Signature:
gridVisibleSortedTopLevelRowEntriesSelector: (state: GridState) => { id: GridRowId; model: { [key: string]: any } }[]
Example
const visibleSortedTopLevelRowEntries = gridVisibleSortedTopLevelRowEntriesSelector(
apiRef.current.state
);
gridVisibleTopLevelRowCountSelector
Get the amount of top level rows accessible after the filtering process.Signature:
gridVisibleTopLevelRowCountSelector: (state: GridState) => number
Example
const visibleTopLevelRowCount = gridVisibleTopLevelRowCountSelector(
apiRef.current.state
);
Pagination
gridPageCountSelector
Get the amount of pages needed to display all the rows if the pagination is enabledSignature:
gridPageCountSelector: (state: GridState) => number
Example
const pageCount = gridPageCountSelector(
apiRef.current.state
);
gridPageSelector
Get the index of the page to render if the pagination is enabledSignature:
gridPageSelector: (state: GridState) => number
Example
const page = gridPageSelector(
apiRef.current.state
);
gridPageSizeSelector
Get the maximum amount of rows to display on a single page if the pagination is enabledSignature:
gridPageSizeSelector: (state: GridState) => number
Example
const pageSize = gridPageSizeSelector(
apiRef.current.state
);
gridPaginatedVisibleSortedGridRowEntriesSelector
Get the id and the model of each row to include in the current page if the pagination is enabled.Signature:
gridPaginatedVisibleSortedGridRowEntriesSelector: (state: GridState) => { id: GridRowId; model: { [key: string]: any } }[]
Example
const paginatedVisibleSortedGridRowEntries = gridPaginatedVisibleSortedGridRowEntriesSelector(
apiRef.current.state
);
gridPaginatedVisibleSortedGridRowIdsSelector
Get the id of each row to include in the current page if the pagination is enabled.Signature:
gridPaginatedVisibleSortedGridRowIdsSelector: (state: GridState) => GridRowId[]
Example
const paginatedVisibleSortedGridRowIds = gridPaginatedVisibleSortedGridRowIdsSelector(
apiRef.current.state
);
gridPaginationRowRangeSelector
Get the index of the first and the last row to include in the current page if the pagination is enabled.Signature:
gridPaginationRowRangeSelector: (state: GridState) => { firstRowIndex: number; lastRowIndex: number } | null
Example
const paginationRowRange = gridPaginationRowRangeSelector(
apiRef.current.state
);
Sorting
gridSortModelSelector
Get the current sorting model.Signature:
gridSortModelSelector: (state: GridState) => GridSortModel
Example
const sortModel = gridSortModelSelector(
apiRef.current.state
);
gridSortedRowEntriesSelector
Get the id and the model of the rows after the sorting process.Signature:
gridSortedRowEntriesSelector: (state: GridState) => { id: GridRowId; model: { [key: string]: any } }[]
Example
const sortedRowEntries = gridSortedRowEntriesSelector(
apiRef.current.state
);
gridSortedRowIdsSelector
Get the id of the rows after the sorting process.Signature:
gridSortedRowIdsSelector: (state: GridState) => GridRowId[]
Example
const sortedRowIds = gridSortedRowIdsSelector(
apiRef.current.state
);
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.