Skip to content

Commit

Permalink
[DataGrid] Remove apiRef.current.forceUpdate() method (#16560)
Browse files Browse the repository at this point in the history
  • Loading branch information
MBilalShafi authored Feb 13, 2025
1 parent 7b1f088 commit f1fd8e2
Show file tree
Hide file tree
Showing 20 changed files with 4 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Below are described the steps you need to make to migrate from v7 to v8.

- The `rowPositionsDebounceMs` prop was removed.
- The `apiRef.current.resize()` method was removed.
- The `apiRef.current.forceUpdate()` method was removed. Use selectors combined with `useGridSelector()` hook to react to changes in the state.
- The `<GridOverlays />` component is not exported anymore.
- The `sanitizeFilterItemValue()` utility is not exported anymore.
- `gridRowsDataRowIdToIdLookupSelector` was removed. Use `gridRowsLookupSelector` in combination with `getRowId()` API method instead.
Expand Down
1 change: 0 additions & 1 deletion docs/pages/x/api/data-grid/grid-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"type": { "description": "(params?: GridExportStateParams) =&gt; InitialState" },
"required": true
},
"forceUpdate": { "type": { "description": "() =&gt; void" }, "required": true },
"getAllColumns": { "type": { "description": "() =&gt; GridStateColDef[]" }, "required": true },
"getAllGroupDetails": {
"type": { "description": "() =&gt; GridColumnGroupLookup" },
Expand Down
3 changes: 0 additions & 3 deletions docs/translations/api-docs/data-grid/grid-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
"exportState": {
"description": "Generates a serializable object containing the exportable parts of the DataGrid state.<br />These values can then be passed to the <code>initialState</code> prop or injected using the <code>restoreState</code> method."
},
"forceUpdate": {
"description": "Forces the grid to rerender. It&#39;s often used after a state update."
},
"getAllColumns": {
"description": "Returns an array of <a href=\"/x/api/data-grid/grid-col-def/\">GridColDef</a> containing all the column definitions."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export const useGridCellSelection = (
return;
}
apiRef.current.setState((prevState) => ({ ...prevState, cellSelection: newModel }));
apiRef.current.forceUpdate();
},
[apiRef, props.cellSelection],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export const useGridRowGrouping = (
if (currentModel !== model) {
apiRef.current.setState(mergeStateWithRowGroupingModel(model));
setStrategyAvailability(apiRef, props.disableRowGrouping);
apiRef.current.forceUpdate();
}
},
[apiRef, props.disableRowGrouping],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export const useGridColumnReorder = (
...state,
columnReorder: { ...state.columnReorder, dragCol: params.field },
}));
apiRef.current.forceUpdate();

removeDnDStylesTimeout.current = setTimeout(() => {
dragColNode.current!.classList.remove(classes.columnHeaderDragging);
Expand Down Expand Up @@ -340,7 +339,6 @@ export const useGridColumnReorder = (
...state,
columnReorder: { ...state.columnReorder, dragCol: '' },
}));
apiRef.current.forceUpdate();
},
[
apiRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export const useGridDetailPanel = (
}
}, [apiRef, props.detailPanelExpandedRowIds]);

const updateCachesAndForceUpdate = React.useCallback(() => {
const updateCaches = React.useCallback(() => {
if (!props.getDetailPanelContent) {
return;
}
Expand All @@ -245,10 +245,9 @@ export const useGridDetailPanel = (
},
};
});
apiRef.current.forceUpdate();
}, [apiRef, props.getDetailPanelContent, props.getDetailPanelHeight]);

useGridApiEventHandler(apiRef, 'sortedRowsSet', updateCachesAndForceUpdate);
useGridApiEventHandler(apiRef, 'sortedRowsSet', updateCaches);

const previousGetDetailPanelContentProp =
React.useRef<DataGridProProcessedProps['getDetailPanelContent']>(undefined);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,8 @@ export const useGridStateInitialization = <PrivateApi extends GridPrivateApiComm
[apiRef],
);

const forceUpdate = React.useCallback(() => {
// @deprecated - do nothing
}, []);

const publicStateApi: Omit<GridStateApi<PrivateApi['state']>, 'state'> = {
setState,
forceUpdate,
};

const privateStateApi: GridStatePrivateApi<PrivateApi['state']> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,6 @@ export const useGridColumnResize = (
...state,
columnResize: { ...state.columnResize, resizingColumnField: field },
}));
apiRef.current.forceUpdate();
},
[apiRef],
);
Expand All @@ -635,7 +634,6 @@ export const useGridColumnResize = (
...state,
columnResize: { ...state.columnResize, resizingColumnField: '' },
}));
apiRef.current.forceUpdate();
}, [apiRef]);

const handleColumnResizeMouseDown: GridEventListener<'columnSeparatorMouseDown'> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export function useGridColumns(
apiRef.current.setState(mergeColumnsState(columnsState));
apiRef.current.publishEvent('columnsChange', columnsState.orderedFields);
apiRef.current.updateRenderContext?.();
apiRef.current.forceUpdate();
},
[logger, apiRef],
);
Expand Down Expand Up @@ -156,7 +155,6 @@ export function useGridColumns(
}),
}));
apiRef.current.updateRenderContext?.();
apiRef.current.forceUpdate();
}
},
[apiRef],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ export const useGridCellEditing = (

return { ...state, editRows: newEditingState };
});
apiRef.current.forceUpdate();
},
[apiRef],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ export const useGridRowEditing = (

return { ...state, editRows: newEditingState };
});
apiRef.current.forceUpdate();
},
[apiRef],
);
Expand All @@ -404,7 +403,6 @@ export const useGridRowEditing = (

return { ...state, editRows: newEditingState };
});
apiRef.current.forceUpdate();
},
[apiRef],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,6 @@ export const useGridFilter = (
/**
* API METHODS
*/
const applyFilters = React.useCallback<GridFilterApi['unstable_applyFilters']>(() => {
updateFilteredRows();
apiRef.current.forceUpdate();
}, [apiRef, updateFilteredRows]);

const upsertFilterItem = React.useCallback<GridFilterApi['upsertFilterItem']>(
(item) => {
const filterModel = gridFilterModelSelector(apiRef);
Expand Down Expand Up @@ -339,7 +334,7 @@ export const useGridFilter = (

const filterApi: GridFilterApi = {
setFilterLogicOperator,
unstable_applyFilters: applyFilters,
unstable_applyFilters: updateFilteredRows,
deleteFilterItem,
upsertFilterItem,
upsertFilterItems,
Expand Down Expand Up @@ -519,11 +514,8 @@ export const useGridFilter = (
visibleRowsLookup: getVisibleRowsLookupState(apiRef, state),
};
});
apiRef.current.forceUpdate();
}, [apiRef]);

// Do not call `apiRef.current.forceUpdate` to avoid re-render before updating the sorted rows.
// Otherwise, the state is not consistent during the render
useGridApiEventHandler(apiRef, 'rowsSet', updateFilteredRows);
useGridApiEventHandler(apiRef, 'columnsChange', handleColumnsChange);
useGridApiEventHandler(apiRef, 'activeStrategyProcessorChange', handleStrategyProcessorChange);
Expand Down
8 changes: 0 additions & 8 deletions packages/x-data-grid/src/hooks/features/focus/useGridFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export const useGridFocus = (
},
};
});
apiRef.current.forceUpdate();

// The row might have been deleted
if (!apiRef.current.getRow(id)) {
Expand Down Expand Up @@ -129,8 +128,6 @@ export const useGridFocus = (
},
};
});

apiRef.current.forceUpdate();
},
[apiRef, logger, publishCellFocusOut],
);
Expand Down Expand Up @@ -159,8 +156,6 @@ export const useGridFocus = (
},
};
});

apiRef.current.forceUpdate();
},
[apiRef, logger, publishCellFocusOut],
);
Expand Down Expand Up @@ -195,8 +190,6 @@ export const useGridFocus = (
},
};
});

apiRef.current.forceUpdate();
},
[apiRef],
);
Expand Down Expand Up @@ -398,7 +391,6 @@ export const useGridFocus = (
columnGroupHeader: null,
},
}));
apiRef.current.forceUpdate();

// There's a focused cell but another element (not a cell) was clicked
// Publishes an event to notify that the focus was lost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export const useGridHeaderFiltering = (
},
};
});
apiRef.current.forceUpdate();
},
[apiRef, props.signature, props.headerFilters],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ export const useGridRowSelection = (
...state,
rowSelection: props.rowSelection ? model : [],
}));
apiRef.current.forceUpdate();
}
},
[apiRef, logger, props.rowSelection, props.signature, canHaveMultipleSelection],
Expand Down
5 changes: 0 additions & 5 deletions packages/x-data-grid/src/hooks/features/rows/useGridRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ export const useGridRows = (
}),
}));
apiRef.current.publishEvent('rowsSet');
apiRef.current.forceUpdate();
};

timeout.clear();
Expand Down Expand Up @@ -289,7 +288,6 @@ export const useGridRows = (
},
};
});
apiRef.current.forceUpdate();
apiRef.current.publishEvent('rowExpansionChange', newNode);
},
[apiRef],
Expand Down Expand Up @@ -591,7 +589,6 @@ export const useGridRows = (
};
});
apiRef.current.publishEvent('rowsSet');
apiRef.current.forceUpdate();
}, [apiRef, props.rowCount]);

useGridRegisterPipeApplier(apiRef, 'hydrateRows', applyHydrateRowsProcessor);
Expand Down Expand Up @@ -638,7 +635,6 @@ export const useGridRows = (
rows: { ...state.rows, loading: props.loading },
}));
apiRef.current.caches.rows!.loadingPropBeforePartialUpdates = props.loading;
apiRef.current.forceUpdate();
}

if (!isNewRowCountAlreadyInState) {
Expand All @@ -651,7 +647,6 @@ export const useGridRows = (
},
}));
apiRef.current.caches.rows.rowCountPropBeforePartialUpdates = props.rowCount;
apiRef.current.forceUpdate();
}
if (!isRowCountPropUpdated) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ export const useGridSorting = (
});

apiRef.current.publishEvent('sortedRowsSet');
apiRef.current.forceUpdate();
}, [apiRef, logger, props.sortingMode]);

const setSortModel = React.useCallback<GridSortApi['setSortModel']>(
Expand All @@ -179,7 +178,6 @@ export const useGridSorting = (
apiRef.current.setState(
mergeStateWithSortModel(model, props.disableMultipleColumnsSorting),
);
apiRef.current.forceUpdate();
apiRef.current.applySorting();
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ export const useGridStatePersistence = (apiRef: RefObject<GridPrivateApiCommunit
response.callbacks.forEach((callback) => {
callback();
});

apiRef.current.forceUpdate();
},
[apiRef],
);
Expand Down
5 changes: 0 additions & 5 deletions packages/x-data-grid/src/models/api/gridStateApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ export interface GridStateApi<State extends GridStateCommunity> {
* Property that contains the whole state of the grid.
*/
state: State;
/**
* Forces the grid to rerender. It's often used after a state update.
* @deprecated no longer needed.
*/
forceUpdate: () => void;
/**
* Sets the whole state of the grid.
* @param {GridState | (oldState: GridState) => GridState} state The new state or the callback creating the new state.
Expand Down

0 comments on commit f1fd8e2

Please sign in to comment.