Skip to content

Commit

Permalink
[DataGrid] Make instanceId required for state selectors (#11395)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherniavskii authored Dec 13, 2023
1 parent 892c08f commit 3578eb9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,18 @@ Below are described the steps you need to make to migrate from v6 to v7.
- The deprecated props `components` and `componentsProps` have been removed. Use `slots` and `slotProps` instead. See [components section](/x/react-data-grid/components/) for more details.
- The `slots.preferencesPanel` slot and the `slotProps.preferencesPanel` prop were removed. Use `slots.panel` and `slotProps.panel` instead.

<!-- ### State access
### State access

- -->
- Some selectors now require passing `instanceId` as a second argument:
```diff
- gridColumnFieldsSelector(apiRef.current.state);
+ gridColumnFieldsSelector(apiRef.current.state, apiRef.current.instanceId);
```
However, it's preferable to pass the `apiRef` as the first argument instead:
```js
gridColumnFieldsSelector(apiRef);
```
See [Direct state access](/x/react-data-grid/state/#direct-selector-access) for more info.

<!-- ### Events
Expand Down
5 changes: 1 addition & 4 deletions packages/grid/x-data-grid/src/models/controlStateItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ export interface GridControlStateItem<
stateId: string;
propModel?: GridEventLookup[E]['params'];
stateSelector:
| OutputSelector<
{ state: State; instanceId: string },
GridControlledStateEventLookup[E]['params']
>
| OutputSelector<State, GridControlledStateEventLookup[E]['params']>
| ((state: State) => GridControlledStateEventLookup[E]['params']);
propOnChange?: (
model: GridControlledStateEventLookup[E]['params'],
Expand Down
2 changes: 1 addition & 1 deletion packages/grid/x-data-grid/src/utils/createSelector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ createSelector(
createSelector(
(state: GridStateCommunity) => state.columns.orderedFields,
(fields) => fields,
)({} as GridStateCommunity);
)({} as GridStateCommunity, { id: 1 });

createSelector(
// @ts-expect-error Wrong state key
Expand Down
7 changes: 4 additions & 3 deletions packages/grid/x-data-grid/src/utils/createSelector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('createSelector', () => {
it('should warn if the instance ID is missing', () => {
const selector = createSelectorMemoized([], () => []);
const state = {} as GridStateCommunity;
// @ts-expect-error Need to test the warning
expect(() => selector(state)).toWarnDev(
'MUI: A selector was called without passing the instance ID, which may impact the performance of the grid.',
);
Expand All @@ -32,13 +33,13 @@ describe('createSelector', () => {
);
const state1 = {} as GridStateCommunity;
const state2 = {} as GridStateCommunity;
const value1 = selector(state1);
const value1 = selector(state1, { id: 1 });

// The default cache has maxSize=1, which forces a recomputation if another state is passed.
// Since the combiner function returns a new array, the references won't be the same.
selector(state2);
selector(state2, { id: 1 });

const value2 = selector(state1);
const value2 = selector(state1, { id: 1 });
expect(value1).not.to.equal(value2);
});
});
Expand Down
3 changes: 1 addition & 2 deletions packages/grid/x-data-grid/src/utils/createSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ interface CacheContainer {

export interface OutputSelector<State, Result> {
(apiRef: React.MutableRefObject<{ state: State; instanceId: GridCoreApi['instanceId'] }>): Result;
// TODO v6: make instanceId require
(state: State, instanceId?: GridCoreApi['instanceId']): Result;
(state: State, instanceId: GridCoreApi['instanceId']): Result;
acceptsApiRef: boolean;
}

Expand Down

0 comments on commit 3578eb9

Please sign in to comment.