Skip to content

[DataGridPro] Fix duplicate nested rows for dynamically updated row IDs #18526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/x-data-grid/src/hooks/features/rows/gridRowsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ export function computeRowsUpdates(
apiRef: RefObject<GridApiCommunity>,
updates: GridRowModelUpdate[],
getRowId: DataGridProcessedProps['getRowId'],
insertedNodes?: Set<GridRowId>,
) {
const nonPinnedRowsUpdates: GridRowModelUpdate[] = [];

Expand All @@ -416,6 +417,10 @@ export function computeRowsUpdates(
}
} else {
nonPinnedRowsUpdates.push(update);
// eslint-disable-next-line no-underscore-dangle
if (update._action !== 'delete') {
insertedNodes?.add(id);
}
}
});
return nonPinnedRowsUpdates;
Expand Down
50 changes: 48 additions & 2 deletions packages/x-data-grid/src/hooks/features/rows/useGridRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,56 @@ export const useGridRows = (

const updateNestedRows = React.useCallback<GridRowProPrivateApi['updateNestedRows']>(
(updates, groupKeys) => {
const nonPinnedRowsUpdates = computeRowsUpdates(apiRef, updates, props.getRowId);
const insertedNodes = new Set<GridRowId>();
const nonPinnedRowsUpdates = computeRowsUpdates(
apiRef,
updates,
props.getRowId,
insertedNodes,
);

const tree = gridRowTreeSelector(apiRef);
const removedNodes = new Set<GridRowId>();

if (groupKeys && groupKeys.length > 0) {
const rootNode = tree[GRID_ROOT_GROUP_ID];
let parentNode = rootNode;
for (let i = 0; i < groupKeys.length; i += 1) {
const childrenFromPath = (parentNode as GridGroupNode).childrenFromPath;
const nodeId = childrenFromPath[Object.keys(childrenFromPath)[0]]?.[groupKeys[i]];
if (nodeId) {
parentNode = tree[nodeId];
}
}

const traverse = (node: GridGroupNode) => {
Comment on lines +229 to +240
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this whole traverse necessary?

Copy link
Member Author

@MBilalShafi MBilalShafi Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is to check if the children of the row being fetched are outdated and need to be removed before adding new ones.

Regarding the part where parent group's ID is derived from groupKeys: we could update the function signature to directly accept parent id. However, since the current approach isn’t expensive (by leveraging childrenFromPath), I chose not to modify the signature. What do you think about this approach vs changing the function signature (will be a non-breaking change as the function is private)?

if (!node) {
return;
}
for (const childId of node.children) {
if (!insertedNodes.has(childId)) {
removedNodes.add(childId);
if (tree[childId].type === 'group') {
traverse(tree[childId] as GridGroupNode);
}
}
}
};

if (parentNode !== rootNode) {
traverse(parentNode as GridGroupNode);
}
}

const cacheUpdates = nonPinnedRowsUpdates.concat(
Array.from(removedNodes).map((rowId) => ({
id: rowId,
_action: 'delete' as const,
})),
);

const cache = updateCacheWithNewRows({
updates: nonPinnedRowsUpdates,
updates: cacheUpdates,
getRowId: props.getRowId,
previousCache: apiRef.current.caches.rows,
groupKeys: groupKeys ?? [],
Expand Down