-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
[Experimental] Add useInsertionEffect #21913
Conversation
Comparing: 81db4eb...ab6ad9f Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: Expand to show
|
2f933e4
to
60d379c
Compare
I’d say effect lists were the usual cause of bugs so you have a higher chance of doing it right with the current approach! |
a10210c
to
c6ceff6
Compare
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.
Looks good overall! Let's discuss the timing of the destroy function before merging
packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js
Show resolved
Hide resolved
@rickhanlonii just curious is the point of having this hook is to handle something even earlier than |
c6ceff6
to
9d9a3a7
Compare
@windmaomao I updated the description with the use case 👍 |
bac179d
to
36cbc67
Compare
Overview
Adds back the experimental
useMutationEffect
hook asuseInsertionEffect
, initially intended for stylesheet libraries.Semantics
This hook is called right before mutations are made to the host, to allow inserting dependencies of the soon-to-be mutated host nodes. The canonical example use case is for inserting styles into the DOM so that they're available before reading layout information from DOM nodes that they apply to (like reading height in
useLayoutEffect
). Since this hook is limited in scope, this hook does not have access to refs and cannot schedule updates.The lifecycle of this hook is a little different than other hooks.
useInsertionEffect
will interleave create and destroy while traversing the tree in the beforeMutation phase. This means that as we traverse the tree before mutations (e.g. the same time we callgetSnapshotBeforeUpdate
), and for each component we will destroy all of the insertion effects and then create all of the insertion effects for that component.The resulting in ordering is something like:
Contrast this with layout effects, which do a two passes. One to destroy all of the layout effects in every component, and a second to create them: