Skip to content

Commit 372991b

Browse files
authored
[DataGrid] Fix error caused by trying to render rows that are not in the state anymore (#17057)
1 parent 381ba8b commit 372991b

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

Diff for: packages/x-data-grid/src/components/GridRow.tsx

+7-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import { GRID_CHECKBOX_SELECTION_COL_DEF } from '../colDef/gridCheckboxSelection
2424
import { GRID_ACTIONS_COLUMN_TYPE } from '../colDef/gridActionsColDef';
2525
import { GRID_DETAIL_PANEL_TOGGLE_FIELD, PinnedColumnPosition } from '../internals/constants';
2626
import { gridSortModelSelector } from '../hooks/features/sorting/gridSortingSelector';
27-
import { gridRowMaximumTreeDepthSelector } from '../hooks/features/rows/gridRowsSelector';
27+
import {
28+
gridRowMaximumTreeDepthSelector,
29+
gridRowNodeSelector,
30+
} from '../hooks/features/rows/gridRowsSelector';
2831
import {
2932
gridEditRowsStateSelector,
3033
gridRowIsEditingSelector,
@@ -125,7 +128,7 @@ const GridRow = forwardRef<HTMLDivElement, GridRowProps>(function GridRow(props,
125128
rowReordering,
126129
);
127130
const handleRef = useForkRef(ref, refProp);
128-
const rowNode = apiRef.current.getRowNode(rowId);
131+
const rowNode = gridRowNodeSelector(apiRef, rowId);
129132
const editing = useGridSelector(apiRef, gridRowIsEditingSelector, {
130133
rowId,
131134
editMode: rootProps.editMode,
@@ -281,7 +284,7 @@ const GridRow = forwardRef<HTMLDivElement, GridRowProps>(function GridRow(props,
281284
}, [isNotVisible, rowHeight, styleProp, heightEntry, rootProps.rowSpacingType]);
282285

283286
const rowClassNames = apiRef.current.unstable_applyPipeProcessors('rowClassName', [], rowId);
284-
const ariaAttributes = rowNode ? getRowAriaAttributes(rowNode, index) : undefined;
287+
const ariaAttributes = getRowAriaAttributes(rowNode, index);
285288

286289
if (typeof rootProps.getRowClassName === 'function') {
287290
const indexRelativeToCurrentPage = index - (currentPage.range?.firstRowIndex || 0);
@@ -295,11 +298,6 @@ const GridRow = forwardRef<HTMLDivElement, GridRowProps>(function GridRow(props,
295298
rowClassNames.push(rootProps.getRowClassName(rowParams));
296299
}
297300

298-
/* Start of rendering */
299-
if (!rowNode) {
300-
return null;
301-
}
302-
303301
const getCell = (
304302
column: GridStateColDef,
305303
indexInSection: number,
@@ -328,7 +326,7 @@ const GridRow = forwardRef<HTMLDivElement, GridRowProps>(function GridRow(props,
328326
scrollbarWidth,
329327
);
330328

331-
if (rowNode?.type === 'skeletonRow') {
329+
if (rowNode.type === 'skeletonRow') {
332330
return (
333331
<slots.skeletonCell
334332
key={column.field}

Diff for: packages/x-data-grid/src/hooks/features/columns/useGridColumns.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ export function useGridColumns(
9898

9999
apiRef.current.setState(mergeColumnsState(columnsState));
100100
apiRef.current.publishEvent('columnsChange', columnsState.orderedFields);
101-
apiRef.current.updateRenderContext?.();
102101
},
103102
[logger, apiRef],
104103
);

Diff for: packages/x-data-grid/src/hooks/features/virtualization/useGridVirtualScroller.tsx

+13-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
gridColumnPositionsSelector,
2727
gridHasColSpanSelector,
2828
} from '../columns/gridColumnsSelector';
29-
import { gridPinnedRowsSelector } from '../rows/gridRowsSelector';
29+
import { gridPinnedRowsSelector, gridRowTreeSelector } from '../rows/gridRowsSelector';
3030
import { GridPinnedRowsPosition } from '../rows/gridRowsInterfaces';
3131
import { useGridVisibleRows, getVisibleRows } from '../../utils/useGridVisibleRows';
3232
import { useGridApiOptionHandler } from '../../utils';
@@ -397,6 +397,7 @@ export const useGridVirtualScroller = () => {
397397
if (!params.rows && !currentPage.range) {
398398
return [];
399399
}
400+
const rowTree = gridRowTreeSelector(apiRef);
400401

401402
let baseRenderContext = renderContext;
402403
if (params.renderContext) {
@@ -453,6 +454,17 @@ export const useGridVirtualScroller = () => {
453454

454455
rowIndexes.forEach((rowIndexInPage) => {
455456
const { id, model } = rowModels[rowIndexInPage];
457+
458+
// In certain cases, the state might already be updated and `currentPage.rows` (which sets `rowModels`)
459+
// contains stale data.
460+
// In that case, skip any further row processing.
461+
// See:
462+
// - https://github.com/mui/mui-x/issues/16638
463+
// - https://github.com/mui/mui-x/issues/17022
464+
if (!rowTree[id]) {
465+
return;
466+
}
467+
456468
const rowIndex = (currentPage?.range?.firstRowIndex || 0) + rowIndexOffset + rowIndexInPage;
457469

458470
// NOTE: This is an expensive feature, the colSpan code could be optimized.

0 commit comments

Comments
 (0)