-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[data grid] Reduce the use of useGridSelector
in root-level hooks
#16001
base: master
Are you sure you want to change the base?
[data grid] Reduce the use of useGridSelector
in root-level hooks
#16001
Conversation
Deploy preview: https://deploy-preview-16001--material-ui-x.netlify.app/ |
136ecee
to
3ac7422
Compare
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
0e5d227
to
3ac7422
Compare
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
Signed-off-by: Lauri <lauri.lehtmaa@gmail.com>
Out of curiosity, I moved It broke a few tests, most notably anything related to tree data. Revealing that `useGridVisibleRows' is a problematic hook – it currently only works due to side-effects of other state updating it seems. Again, it will be an easy fix once #15666 gets merged (fyi @romgrk). But this confirms my hunch that avoiding |
I'm not sure if this function is legacy, but I cannot see any case where the So, I simplified it, and got rid of the last root-level |
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
@MBilalShafi, out of curiosity, why did you change the below from an event (was previously triggered by mui-x/packages/x-data-grid-pro/src/hooks/features/dataSource/useGridDataSourceBase.ts Lines 403 to 413 in a2c7b00
|
As discussed in #15986 (comment)
The use of
useGridSelector
should be avoided whenever possible in root level (feature) hooks, as it sends the whole grid root and all sibling hooks to re-evaluate on every time selecto state changes. There's many instances, whereuseGridSelector
values are passed touseCallback
, which re-creates callback functions and event listeners on state changes. If the callbacks are indeed callbacks, the state they access is identical to just evaluating the selector within the callback itself.This instance is a bit more difficult to refactor for the time being, but will be very easy once #15666 gets merged as it'll be refactored as a pure selector then:
The only instances where
useGridSelector
is valid truly valid and necessary areuseGridDimensions
anduseGridRowsMeta
as they need to perform calculations in response to many different parts of the state. If those were to be optimised further, they could be moved to a separate component, so they wouldn't affect all the other hooks any time the state they depend on changes.Two other instances that were difficult to refactor currently:
mui-x/packages/x-data-grid-pro/src/hooks/features/dataSource/useGridDataSource.ts
Line 91 in b061f55
mui-x/packages/x-data-grid/src/hooks/features/columnResize/useGridColumnResize.tsx
Line 135 in b061f55
But they are pretty minor.
However, if we could get rid of them completely, I think it would make for a good convention not to allow selector hooks in root-level feature hooks. Makes it easier to write more isolated features, and safer to introduce experimental features. Alternative is:
#15986 (comment)