Migration from v7 to v8
This guide describes the changes needed to migrate the Data Grid from v7 to v8.
Introduction
This is a reference guide for upgrading @mui/x-data-grid
from v7 to v8.
Start using the new release
In package.json
, change the version of the Data Grid package to next
.
-"@mui/x-data-grid": "^7.x.x",
+"@mui/x-data-grid": "next",
-"@mui/x-data-grid-pro": "^7.x.x",
+"@mui/x-data-grid-pro": "next",
-"@mui/x-data-grid-premium": "^7.x.x",
+"@mui/x-data-grid-premium": "next",
Using next
ensures that it will always use the latest v8 pre-release version, but you can also use a fixed version, like 8.0.0-alpha.0
.
Run codemods
The preset-safe
codemod will automatically adjust the bulk of your code to account for breaking changes in v8. You can run v8.0.0/data-grid/preset-safe
targeting only Data Grid or v8.0.0/preset-safe
to target the other packages as well.
You can either run it on a specific file, folder, or your entire codebase when choosing the <path>
argument.
# Data Grid specific
npx @mui/x-codemod@latest v8.0.0/data-grid/preset-safe <path>
# Target the other packages as well
npx @mui/x-codemod@latest v8.0.0/preset-safe <path>
Breaking changes that are handled by this codemod are prefixed with a ✅ emoji.
If you have already applied the v8.0.0/data-grid/preset-safe
(or v8.0.0/preset-safe
) codemod, then you should not need to take any further action on these items.
All other changes must be handled manually.
Breaking changes
Since v8 is a major release, it contains some changes that affect the public API. These changes were done for consistency, improve stability and make room for new features. Below are described the steps you need to make to migrate from v7 to v8.
Setting license key
The deprecated LicenseInfo
export was removed from the @mui/x-data-grid-pro
and @mui/x-data-grid-premium
packages.
You have to import it from @mui/x-license
instead:
-import { LicenseInfo } from '@mui/x-data-grid-pro';
-import { LicenseInfo } from '@mui/x-data-grid-premium';
+import { LicenseInfo } from '@mui/x-license';
LicenseInfo.setLicenseKey('YOUR_LICENSE_KEY');
Props
- Passing additional props (like
data-*
,aria-*
) directly on the Data Grid component is no longer supported. To pass the props, useslotProps
:- For the
.root
element, useslotProps.root
- For the
.main
element (the one withrole="grid"
), useslotProps.main
- For the
Selection
The default value of the
rowSelectionPropagation
prop has been changed to{ parents: true, descendants: true }
which means that the selection will be propagated to the parents and descendants by default. To revert to the previous behavior, passrowSelectionPropagation={{ parents: false, descendants: false }}
.✅ The prop
indeterminateCheckboxAction
has been removed. Clicking on an indeterminate checkbox "selects" the unselected descendants.The "Select all" checkbox would now be checked when all the selectable rows are selected, ignoring rows that are not selectable because of the
isRowSelectable
prop.The row selection model has been changed from
GridRowId[]
to{ type: 'include' | 'exclude'; ids: Set<GridRowId> }
. UsingSet
allows for a more efficient row selection management. Theexclude
selection type allows to select all rows except the ones in theids
set.This change impacts the following props:
rowSelectionModel
onRowSelectionModelChange
initialState.rowSelectionModel
-const [rowSelectionModel, setRowSelectionModel] = React.useState<GridRowSelectionModel>([]); +const [rowSelectionModel, setRowSelectionModel] = React.useState<GridRowSelectionModel>({ type: 'include', ids: new Set() });
This change also impacts the
gridRowSelectionStateSelector
selector. For convenience, use thegridRowSelectionManagerSelector
selector to handle both selection types:-const rowSelection = gridRowSelectionStateSelector(apiRef); -const isRowSelected = rowSelection.includes(rowId); +const rowSelectionManager = gridRowSelectionManagerSelector(apiRef); +const isRowSelected = rowSelectionManager.has(rowId);
There is also a
createRowSelectionManager
utility function that can be used to manage the row selection:const rowSelectionManager = createRowSelectionManager({ type: 'include', ids: new Set(), }); rowSelectionManager.select(rowId); rowSelectionManager.unselect(rowId); rowSelectionManager.has(rowId);
The
selectedIdsLookupSelector
selector has been removed. Use thegridRowSelectionManagerSelector
orgridRowSelectionStateSelector
selectors instead.The
selectedGridRowsSelector
has been renamed togridRowSelectionIdsSelector
.The
selectedGridRowsCountSelector
has been renamed togridRowSelectionCountSelector
.
Changes to the public API
✅ The
rowPositionsDebounceMs
prop was removed.The
resetPageOnSortFilter
prop was removed. The Data Grid now goes back to the first page after sort or filter is applied.The
apiRef.current.resize()
method was removed.The
apiRef.current.forceUpdate()
method was removed. Use selectors combined withuseGridSelector()
hook to react to changes in the state.The
<GridOverlays />
component is not exported anymore.The
<GridSaveAltIcon />
icon is not exported anymore. ImportSaveAlt
from@mui/icons-material
instead.The
sanitizeFilterItemValue()
utility is not exported anymore.gridRowsDataRowIdToIdLookupSelector
was removed. UsegridRowsLookupSelector
in combination withgetRowId()
API method instead.-const idToIdLookup = gridRowsDataRowIdToIdLookupSelector(apiRef); -const rowId = idToIdLookup[id]; +const rowsLookup = gridRowsLookupSelector(apiRef); +const rowId = apiRef.current.getRowId(rowsLookup[id]);
✅ The feature row spanning is now stable.
<DataGrid - unstable_rowSpanning + rowSpanning />
The data source feature and its related props are now stable.
<DataGridPro - unstable_dataSource={dataSource} - unstable_dataSourceCache={cache} - unstable_lazyLoading - unstable_lazyLoadingRequestThrottleMs={100} + dataSource={dataSource} + dataSourceCache={cache} + lazyLoading + lazyLoadingRequestThrottleMs={100} />
The data source API is now stable.
- apiRef.current.unstable_dataSource.getRows() + apiRef.current.dataSource.getRows()
Return type of the
useGridApiRef()
hook and the type ofapiRef
prop are updated to explicitly include the possibilty ofnull
. In addition to this,useGridApiRef()
returns a reference that is initialized withnull
instead of{}
.Only the initial value and the type are updated. Logic that initializes the API and its availability remained the same, which means that if you could access API in a particular line of your code before, you are able to access it as well after this change.
Depending on the context in which the API is being used, you can decide what is the best way to deal with
null
value. Some options are:- Use optional chaining
- Use non-null assertion operator if you are sure your code is always executed when the
apiRef
is notnull
- Return early if
apiRef
isnull
- Throw an error if
apiRef
isnull
GridSortItem
interface is not exported anymore.createUseGridApiEventHandler()
is not exported anymore.The
showToolbar
prop is now required to display the toolbar.It is no longer necessary to pass
GridToolbar
as a slot to display the default toolbar.<DataGrid + showToolbar - slots={{ - toolbar: GridToolbar, - }} />
The quick filter is now shown in the toolbar by default. Use
slotProps={{ toolbar: { showQuickFilter: false } }}
to hide it.The signature of
unstable_onDataSourceError()
has been updated to support future use-cases.<DataGrid - unstable_onDataSourceError={(error: Error, params: GridGetRowsParams) => { - if (params.filterModel) { - // do something - } - }} + unstable_onDataSourceError={(error: GridGetRowsError | GridUpdateRowError) => { + if (error instanceof GridGetRowsError && error.params.filterModel) { + // do something + } + }} />
Behavioral changes
The "Reset" button in the column visibility panel now resets to the initial column visibility model instead of the model when the panel was opened. The reset behavior follows these rules:
- If an initial
columnVisibilityModel
is provided, it resets to that model. - If a controlled
columnVisibilityModel
is provided, it resets to the first model value. - When the columns are updated (via the
columns
prop orupdateColumns()
API method), the reset reference point updates to the currentcolumnVisibilityModel
.
To revert to the previous behavior, provide a custom component to the
slots.columnsManagement
.- If an initial
The density selector has been removed from the toolbar. It is still possible to set the density programmatically via the
density
prop. A density selector can be added to a custom toolbar passed toslots.toolbar
. See Toolbar component—Settings menu for an example.
Localization
- If
estimatedRowCount
is used, the text provided to the Table Pagination component from the Material UI library is updated and requires additional translations. Check the example at the end of Index-based pagination section.
Accessibility
- The Grid is more aligned with the WAI-ARIA authoring practices and sets the
role
attribute totreegrid
if the Data Grid is used with row grouping feature.
State
The selectors signature has been updated. They are only accepting
apiRef
as a first argument. Some selectors support additional arguments.-mySelector(state, instanceId) +mySelector(apiRef)
or
-mySelector(state, instanceId) +mySelector(apiRef, arguments)
The
useGridSelector()
signature has been updated due to the introduction of arguments parameter in the selectors. Passundefined
asarguments
if the selector doesn't use any arguments.-const output = useGridSelector(apiRef, selector, equals); +const output = useGridSelector(apiRef, selector, arguments, equals);
The
filteredRowsLookup
object of the filter state does not containtrue
values anymore. If the row is filtered out, the value isfalse
. Otherwise, the row id is not present in the object. This change only impacts you if you relied onfilteredRowsLookup
to get ids of filtered rows. In this case,usegridDataRowIdsSelector
selector to get row ids and checkfilteredRowsLookup
forfalse
values:const filteredRowsLookup = gridFilteredRowsLookupSelector(apiRef); -const filteredRowIds = Object.keys(filteredRowsLookup).filter((rowId) => filteredRowsLookup[rowId] === true); +const rowIds = gridDataRowIdsSelector(apiRef); +const filteredRowIds = rowIds.filter((rowId) => filteredRowsLookup[rowId] !== false);
The
visibleRowsLookup
state does not containtrue
values anymore. If the row is not visible, the value isfalse
. Otherwise, the row id is not present in the object:const visibleRowsLookup = gridVisibleRowsLookupSelector(apiRef); -const isRowVisible = visibleRowsLookup[rowId] === true; +const isRowVisible = visibleRowsLookup[rowId] !== false;
Other exports
- ✅
ariaV8
experimental flag is removed. It's now the default behavior. - Subcomponents that are in a React Portal must now be wrapped with
GridPortalWrapper
Filtering
- The clear button in header filter cells has moved to the header filter menu. Use
slotProps={{ headerFilterCell: { showClearIcon: true } }}
to restore the clear button in the cell. - Custom filter input components typings have been modified.
CSS classes and styling
The Data Grid now has a default background color, and its customization has moved from
theme.mixins.MuiDataGrid
totheme.palette.DataGrid
with the following properties:bg
: Sets the background color of the entire grid (new property)headerBg
: Sets the background color of the header (previously namedcontainerBackground
)pinnedBg
: Sets the background color of pinned rows and columns (previously namedpinnedBackground
)
const theme = createTheme({ - mixins: { - MuiDataGrid: { - containerBackground: '#f8fafc', - pinnedBackground: '#f1f5f9', - }, - }, + palette: { + DataGrid: { + bg: '#f8fafc', + headerBg: '#e2e8f0', + pinnedBg: '#f1f5f9', + }, + }, });
The
detailPanels
,pinnedColumns
, andpinnedRowsRenderZone
classes have been removed.The
main--hasSkeletonLoadingOverlay
class has been renamed tomain--hiddenContent
and is now also applied when the "No columns" overlay is displayed.
Slots
- The
baseFormControl
slot was removed. - The
baseInputLabel
slot was removed. - The
baseInputAdornment
slot was removed. - The
paper
slot has been renamed topanelContent
.