-
-
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][utils] Fix useTimeout clear lifecycle #44897
base: master
Are you sure you want to change the base?
[core][utils] Fix useTimeout clear lifecycle #44897
Conversation
Netlify deploy previewhttps://deploy-preview-44897--material-ui.netlify.app/ Bundle size reportDetails of bundle changes (Toolpad) |
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useEnhancedEffect(timeout.disposeEffect, []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use a stable deps like useOnMount
does:
const EMPTY = [] as unknown[];
// ...
useEnhancedEffect(effect, EMPTY);
import useLazyRef from '../useLazyRef/useLazyRef'; | ||
import useOnMount from '../useOnMount/useOnMount'; | ||
import useLazyRef from '../useLazyRef'; | ||
import useEnhancedEffect from '../useEnhancedEffect'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not the first time I see useEnhancedEffect
cause such issue, the hook makes it less apparent that useLayoutEffect
is used, and that it runs on a different cycle than useEffect
.
As far as I know, using
useOnMount()
is wrong in this context. We need to clear the timeout synchronously with the component unmounts.See facebook/react#19671, with https://codesandbox.io/p/sandbox/unruffled-banzai-7zzr6k?file=%2Fsrc%2FDemo.tsx that illustrates the difference in behavior:
It seems the root cause of why mui/mui-x#14987. This fix is upstream of: mui/mui-x#16031, but it might be best to first merge downstream, so we test it with a smaller userbase.