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().
Access the state
The state is exposed on the apiRef object.
Data Grid packages expose a set of state selectors that take the apiRef 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 way to use a selector is to call it as a function with apiRef as its first argument:
const paginationModel = gridPaginationModelSelector(apiRef);
With useGridSelector
If you only need to access the state value in the render of your components, use the useGridSelector() hook.
This hook ensures there is a reactive binding such that when the state changes, the component in which this hook is used is re-rendered.
const paginationModel = useGridSelector(apiRef, gridPaginationModelSelector);
Catalog of selectors
Some selectors have not yet been documented.
Aggregation
gridAggregationLookupSelector
Get the aggregation results as a lookup.Signature:
gridAggregationLookupSelector: (apiRef: GridApiRef) => GridAggregationLookupExample
const aggregationLookup = gridAggregationLookupSelector(apiRef);gridAggregationModelSelector
Get the aggregation model, containing the aggregation function of each column. If a column is not in the model, it is not aggregated.Signature:
gridAggregationModelSelector: (apiRef: GridApiRef) => GridAggregationModelExample
const aggregationModel = gridAggregationModelSelector(apiRef);Columns
gridColumnDefinitionsSelector
Get an array of column definitions in the order rendered on screen..Signature:
gridColumnDefinitionsSelector: (apiRef: GridApiRef) => GridStateColDef[]Example
const columnDefinitions = gridColumnDefinitionsSelector(apiRef);gridColumnFieldsSelector
Get an array of column fields in the order rendered on screen.Signature:
gridColumnFieldsSelector: (apiRef: GridApiRef) => string[]Example
const columnFields = gridColumnFieldsSelector(apiRef);gridColumnLookupSelector
Get the columns as a lookup (an object containing the field for keys and the definition for values).Signature:
gridColumnLookupSelector: (apiRef: GridApiRef) => GridColumnLookupExample
const columnLookup = gridColumnLookupSelector(apiRef);gridColumnsStateSelector
Get the columns stateSignature:
gridColumnsStateSelector: (apiRef: GridApiRef) => GridColumnsStateExample
const columnsState = gridColumnsStateSelector(apiRef);gridFilterableColumnDefinitionsSelector
Get the filterable columns as an array.Signature:
gridFilterableColumnDefinitionsSelector: (apiRef: GridApiRef) => GridStateColDef[]Example
const filterableColumnDefinitions = gridFilterableColumnDefinitionsSelector(apiRef);gridFilterableColumnLookupSelector
Get the filterable columns as a lookup (an object containing the field for keys and the definition for values).Signature:
gridFilterableColumnLookupSelector: (apiRef: GridApiRef) => GridColumnLookupExample
const filterableColumnLookup = gridFilterableColumnLookupSelector(apiRef);Visible Columns
gridColumnPositionsSelector
Get the left position in pixel of each visible columns relative to the left of the first column.Signature:
gridColumnPositionsSelector: (apiRef: GridApiRef) => number[]Example
const columnPositions = gridColumnPositionsSelector(apiRef);gridColumnVisibilityModelSelector
Get the column visibility model, containing the visibility status of each column. If a column is not registered in the model, it is visible.Signature:
gridColumnVisibilityModelSelector: (apiRef: GridApiRef) => GridColumnVisibilityModelExample
const columnVisibilityModel = gridColumnVisibilityModelSelector(apiRef);gridColumnsTotalWidthSelector
Get the summed width of all the visible columns.Signature:
gridColumnsTotalWidthSelector: (apiRef: GridApiRef) => numberExample
const columnsTotalWidth = gridColumnsTotalWidthSelector(apiRef);gridPinnedColumnsSelector
Get the visible pinned columns model.Signature:
gridPinnedColumnsSelector: (apiRef: GridApiRef) => GridPinnedColumnFieldsExample
const pinnedColumns = gridPinnedColumnsSelector(apiRef);gridVisibleColumnDefinitionsSelector
Get the visible columns as a lookup (an object containing the field for keys and the definition for values).Signature:
gridVisibleColumnDefinitionsSelector: (apiRef: GridApiRef) => GridStateColDef[]Example
const visibleColumnDefinitions = gridVisibleColumnDefinitionsSelector(apiRef);gridVisibleColumnFieldsSelector
Get the field of each visible column.Signature:
gridVisibleColumnFieldsSelector: (apiRef: GridApiRef) => string[]Example
const visibleColumnFields = gridVisibleColumnFieldsSelector(apiRef);gridVisiblePinnedColumnDefinitionsSelector
Get the visible pinned columns.Signature:
gridVisiblePinnedColumnDefinitionsSelector: (apiRef: GridApiRef) => { left: GridStateColDef[]; right: GridStateColDef[] }Example
const visiblePinnedColumnDefinitions = gridVisiblePinnedColumnDefinitionsSelector(apiRef);Filtering
gridExpandedRowCountSelector
Get the amount of rows accessible after the filtering process.Signature:
gridExpandedRowCountSelector: (apiRef: GridApiRef) => numberExample
const expandedRowCount = gridExpandedRowCountSelector(apiRef);gridExpandedSortedRowEntriesSelector
Get the id and the model of the rows accessible after the filtering process. Does not contain the collapsed children.Signature:
gridExpandedSortedRowEntriesSelector: (apiRef: GridApiRef) => GridRowEntry<GridValidRowModel>[]Example
const expandedSortedRowEntries = gridExpandedSortedRowEntriesSelector(apiRef);gridExpandedSortedRowIdsSelector
Get the id of the rows accessible after the filtering process. Does not contain the collapsed children.Signature:
gridExpandedSortedRowIdsSelector: (apiRef: GridApiRef) => GridRowId[]Example
const expandedSortedRowIds = gridExpandedSortedRowIdsSelector(apiRef);gridFilterModelSelector
Get the current filter model.Signature:
gridFilterModelSelector: (apiRef: GridApiRef) => GridFilterModelExample
const filterModel = gridFilterModelSelector(apiRef);gridFilteredDescendantRowCountSelector
Get the amount of descendant rows accessible after the filtering process.Signature:
gridFilteredDescendantRowCountSelector: (apiRef: GridApiRef) => numberExample
const filteredDescendantRowCount = gridFilteredDescendantRowCountSelector(apiRef);gridFilteredRowCountSelector
Get the amount of rows accessible after the filtering process. Includes top level and descendant rows.Signature:
gridFilteredRowCountSelector: (apiRef: GridApiRef) => numberExample
const filteredRowCount = gridFilteredRowCountSelector(apiRef);gridFilteredSortedRowEntriesSelector
Get the id and the model of the rows accessible after the filtering process. Contains the collapsed children.Signature:
gridFilteredSortedRowEntriesSelector: (apiRef: GridApiRef) => GridRowEntry<GridValidRowModel>[]Example
const filteredSortedRowEntries = gridFilteredSortedRowEntriesSelector(apiRef);gridFilteredSortedRowIdsSelector
Get the id of the rows accessible after the filtering process. Contains the collapsed children.Signature:
gridFilteredSortedRowIdsSelector: (apiRef: GridApiRef) => GridRowId[]Example
const filteredSortedRowIds = gridFilteredSortedRowIdsSelector(apiRef);gridFilteredSortedTopLevelRowEntriesSelector
Get the id and the model of the top level rows accessible after the filtering process.Signature:
gridFilteredSortedTopLevelRowEntriesSelector: (apiRef: GridApiRef) => GridRowEntry<GridValidRowModel>[]Example
const filteredSortedTopLevelRowEntries = gridFilteredSortedTopLevelRowEntriesSelector(apiRef);gridFilteredTopLevelRowCountSelector
Get the amount of top level rows accessible after the filtering process.Signature:
gridFilteredTopLevelRowCountSelector: (apiRef: GridApiRef) => numberExample
const filteredTopLevelRowCount = gridFilteredTopLevelRowCountSelector(apiRef);gridQuickFilterValuesSelector
Get the current quick filter values.Signature:
gridQuickFilterValuesSelector: (apiRef: GridApiRef) => any[] | undefinedExample
const quickFilterValues = gridQuickFilterValuesSelector(apiRef);Pagination
gridPageCountSelector
Get the amount of pages needed to display all the rows if the pagination is enabledSignature:
gridPageCountSelector: (apiRef: GridApiRef) => numberExample
const pageCount = gridPageCountSelector(apiRef);gridPageSelector
Get the index of the page to render if the pagination is enabledSignature:
gridPageSelector: (apiRef: GridApiRef) => numberExample
const page = gridPageSelector(apiRef);gridPageSizeSelector
Get the maximum amount of rows to display on a single page if the pagination is enabledSignature:
gridPageSizeSelector: (apiRef: GridApiRef) => numberExample
const pageSize = gridPageSizeSelector(apiRef);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) => GridRowEntry<GridValidRowModel>[]Example
const paginatedVisibleSortedGridRowEntries = gridPaginatedVisibleSortedGridRowEntriesSelector(apiRef);gridPaginatedVisibleSortedGridRowIdsSelector
Get the id of each row to include in the current page if the pagination is enabled.Signature:
gridPaginatedVisibleSortedGridRowIdsSelector: (apiRef: GridApiRef) => GridRowId[]Example
const paginatedVisibleSortedGridRowIds = gridPaginatedVisibleSortedGridRowIdsSelector(apiRef);gridPaginationMetaSelector
Get the pagination metaSignature:
gridPaginationMetaSelector: (apiRef: GridApiRef) => GridPaginationMetaExample
const paginationMeta = gridPaginationMetaSelector(apiRef);gridPaginationModelSelector
Get the pagination modelSignature:
gridPaginationModelSelector: (apiRef: GridApiRef) => GridPaginationModelExample
const paginationModel = gridPaginationModelSelector(apiRef);gridPaginationRowCountSelector
Get the row countSignature:
gridPaginationRowCountSelector: (apiRef: GridApiRef) => numberExample
const paginationRowCount = gridPaginationRowCountSelector(apiRef);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 } | nullExample
const paginationRowRange = gridPaginationRowRangeSelector(apiRef);gridVisibleRowsSelector
Get the rows, range and rowIndex lookup map after filtering and sorting. Does not contain the collapsed children.Signature:
gridVisibleRowsSelector: (apiRef: GridApiRef) => { rows: GridRowEntry<GridValidRowModel>[]; range: { firstRowIndex: number; lastRowIndex: number } | null; rowIdToIndexMap: Map<GridRowId, number> }Example
const visibleRows = gridVisibleRowsSelector(apiRef);Sorting
gridSortModelSelector
Get the current sorting model.Signature:
gridSortModelSelector: (apiRef: GridApiRef) => GridSortModelExample
const sortModel = gridSortModelSelector(apiRef);gridSortedRowEntriesSelector
Get the id and the model of the rows after the sorting process.Signature:
gridSortedRowEntriesSelector: (apiRef: GridApiRef) => GridRowEntry<GridValidRowModel>[]Example
const sortedRowEntries = gridSortedRowEntriesSelector(apiRef);gridSortedRowIdsSelector
Get the id of the rows after the sorting process.Signature:
gridSortedRowIdsSelector: (apiRef: GridApiRef) => GridRowId[]Example
const sortedRowIds = gridSortedRowIdsSelector(apiRef);Virtualization
gridVirtualizationColumnEnabledSelector
Get the enabled state for column virtualizationSignature:
gridVirtualizationColumnEnabledSelector: (apiRef: GridApiRef) => booleanExample
const virtualizationColumnEnabled = gridVirtualizationColumnEnabledSelector(apiRef);gridVirtualizationEnabledSelector
Get the enabled state for virtualizationSignature:
gridVirtualizationEnabledSelector: (apiRef: GridApiRef) => booleanExample
const virtualizationEnabled = gridVirtualizationEnabledSelector(apiRef);gridVirtualizationRowEnabledSelector
Get the enabled state for row virtualizationSignature:
gridVirtualizationRowEnabledSelector: (apiRef: GridApiRef) => booleanExample
const virtualizationRowEnabled = gridVirtualizationRowEnabledSelector(apiRef);gridVirtualizationSelector
Get the columns stateSignature:
gridVirtualizationSelector: (apiRef: GridApiRef) => GridVirtualizationStateExample
const virtualization = gridVirtualizationSelector(apiRef);Save and restore the state
The current state of the Data Grid can be exported using apiRef.current.exportState().
It can then be restored by either passing the returned value to the initialState prop or to the apiRef.current.restoreState() method.
Watch out for controlled models and their callbacks (onFilterModelChange if you use filterModel, for instance), as the Data Grid calls those callbacks when restoring the state.
But if the callback is not defined or if calling it does not update the prop value, then the restored value will not be applied.
Restore the state with initialState
You can pass the state returned by apiRef.current.exportState() to the initialState prop.
In the demo below, clicking on Recreate the 2nd grid will re-mount the second Data Grid with the current state of the first Grid.
Save and restore the state from external storage
You can use apiRef.current.exportState() to save a snapshot of the state to an external storage (for example using local storage or redux).
This way the state can be persisted on refresh or navigating to another page.
In the following demo, the state is saved to localStorage and restored when the page is refreshed.
This is done by listening on the beforeunload event.
When the component is unmounted, the useLayoutEffect cleanup function is being used instead.
Restore the state with apiRef
You can pass the state returned by apiRef.current.exportState() to the apiRef.current.restoreState method.
In the demo below, clicking on Save current view will create a snapshot of the changes made in the state, considering the initial state.
You can apply these changes on the Data Grid later selecting a saved view in the Custom view menu.
Restore part of the state
It is possible to restore specific properties of the state using the apiRef.current.restoreState() method.
For instance, to only restore the pinned columns:
apiRef.current.restoreState({
  pinnedColumns: ['brand'],
});