-
-
Notifications
You must be signed in to change notification settings - Fork 32.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
[core] Switch back to useCallback to avoid ESLint error #44592
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: wilhelmlofsten <[email protected]>
Netlify deploy previewhttps://deploy-preview-44592--material-ui.netlify.app/ Bundle size reportDetails of bundle changes (Toolpad) |
Can you clarify this? I don't understand. |
We based this pull request mostly off of the ESLint warning given by eslint-plugin-react-compiler. From our understanding, it tries to catch the pitfall mentioned on this page: https://react.dev/reference/react/useRef. So the inconsistencies we were alluding to were the breaking of React's expectation that the body of a component behaves like a pure function. But we are not entirely sure this expectation is broken, we base it entirely on the ESLint warning, but that might be a false positive. Perhaps you know more on this topic? |
Kinda misleading terminology. Hooks are fundamentally impure functions, because they depend on hidden global state that isn't passed through parameters. So I'm guessing they're meaning "pure with the exception of React hooks". But either way, I still don't understand how accessing |
We assume the issue would be the one mentioned on the documentation we sent. We admit we do not fully understand it, but earlier in the code when useEnhancedEffect(() => {
ref.current = fn;
}); So wouldn't the usage of return React.useRef((...args: Args) =>
// @ts-expect-error hide `this`
(0, ref.current!)(...args),
).current; // < -- This current is not inside of a hook If there is a good reason for this, we understand this pull request might not be necessary, but it looks like something is potentially strange with the current implementation. |
Aren't you mixing element refs with general-purpose refs? Because for an e.g. |
Yes, it is entirely possible that both eslint-plugin-react-compiler and we are confusing the two refs. So the switch to a useCallback might not be entirely necessary. With your explanation, it makes us question whether the hook that updates the ref.current is necessary if it is already initialized to fn? const ref = React.useRef(fn);
useEnhancedEffect(() => {
ref.current = fn;
}); Other than that, we are also curious about the comma operator |
Yes, those things are necessary for the hook to update & behave properly, see the react docs for Note that if the compiler is choking on this pattern and produces otherwise worst code, that could be a valid reason to make the change anyway, but I'd validate that assertion first and check upstream for a fix if that's the case. |
Revert back to previous implementation from #39078, with some minor adjustments. The reason for this is that eslint-plugin-react-compiler gives an ESLint error when ref.current is being accessed inside the useRef which from our understanding can cause inconsistencies. If this inconsistency is already handled in other ways, then the leaner version from #39078 can be kept, and an explicit ignore of this error could be added. This warning was noticed when working on #44591, and we felt it might be relevant to get rid of this ESLint warning as well.