-
Notifications
You must be signed in to change notification settings - Fork 220
Switch hook on useMountedRef from useEffect to useLayoutEffect to support React 17 #1964
Conversation
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.
Checks out to me, but probably we want someone else to also review since I helped make it :P
IIRC recall useLayoutEffect doesn't work on the server-side. If we make this change then I think we're going get a lot of logspam or errors about that. #1813 seems to be a related issue where useEffect being async is a pain (though that was an issue in the mount rather than unmount phase). Perhaps that might offer some inspiration Perhaps just using |
@BPScott I'm not sure we need |
@TheMallen yep I agree this doesn't make much sense on the server as useLayoutEffect/useEffect doesn't do anything. however if you call useLayoutEffect on the server React emits the following warning to the console (while useEffect keeps quiet):
My "it should work on the server" claim was really "it should not trigger loads of those warning logs for consumers" - which means we should either use useEffect or a noop function on the server side |
Gave this a tick . I'd love to see a test for this but I'm not sure how that would work, so that's not an issue for now. |
@BPScott the existing tests fail without this changeset, so we're covered imo |
Oh perfect! in that case ignore me :) |
Description
useEffect
's cleanup function is no longer synchronous, so we switched touseLayoutEffect
to preserve the behaviour ofuseMountedRef
in React 17+.Source: https://reactjs.org/blog/2020/08/10/react-v17-rc.html#effect-cleanup-timing
Fixes #1959
Checklist
Tophat Instructions
On package.json, upgrade React and ReactDOM to the following:
And the resolutions to:
Then run
yarn && yarn test react-hooks
. The tests should pass!