[Experimental] Add useInsertionEffect#21913
Merged
rickhanlonii merged 1 commit intofacebook:mainfrom Sep 14, 2021
Merged
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
Collaborator
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
acdlite
reviewed
Jul 20, 2021
Collaborator
acdlite
left a comment
There was a problem hiding this comment.
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
Member
Author
|
@windmaomao I updated the description with the use case 👍 |
bac179d to
36cbc67
Compare
acdlite
reviewed
Sep 10, 2021
This was referenced Nov 24, 2025
This was referenced Jan 11, 2026
This was referenced Jan 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Adds back the experimental
useMutationEffecthook 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.
useInsertionEffectwill 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: