diff --git a/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx index 23994a6684d0f..fdf3990d7767d 100644 --- a/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx @@ -201,6 +201,22 @@ describe(' - Row selection', () => { expect(getCell(1, 0).querySelector('input')!).to.have.attr('data-indeterminate', 'true'); }); + // Context: https://github.com/mui/mui-x/issues/14859 + it('should not throw when controlling a selection model', () => { + function TestDataGrid() { + const [rowSelectionModel, setRowSelectionModel] = React.useState([]); + return ( + + ); + } + expect(() => { + render(); + }).not.to.throw(); + }); + describe('prop: checkboxSelectionVisibleOnly = false', () => { it('should select all rows of all pages if no row is selected', () => { render( diff --git a/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts b/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts index d50883fa5c366..9d5b7f8e0aa34 100644 --- a/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts +++ b/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts @@ -442,8 +442,12 @@ export const useGridRowSelection = ( /* * EVENTS */ + const isFirstRender = React.useRef(true); const removeOutdatedSelection = React.useCallback( (sortModelUpdated = false) => { + if (isFirstRender.current) { + return; + } const currentSelection = gridRowSelectionStateSelector(apiRef.current.state); const filteredRowsLookup = gridFilteredRowsLookupSelector(apiRef); @@ -757,4 +761,10 @@ export const useGridRowSelection = ( React.useEffect(() => { runIfRowSelectionIsEnabled(removeOutdatedSelection); }, [removeOutdatedSelection, runIfRowSelectionIsEnabled]); + + React.useEffect(() => { + if (isFirstRender.current) { + isFirstRender.current = false; + } + }, []); };