-
Notifications
You must be signed in to change notification settings - Fork 47.6k
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
Bug: useInsertionEffect()
cleanup function does not fire if a component is wrapped in React.lazy
#26670
Comments
I was just about to report the same issue (my repro case can be found here, it's prepared based on @jmca's repro for the issue caused by this - but it's essentially the same repro as yours). I think that |
Seems like a bug. Anyone wants to look into fixing it? |
@gaearon yes! I actually started to work on a PR locally - it's very draft-ish and I only fixed one of the discrepancies so far. Gonna open a draft PR later today. |
To rephrase what the observed issue here is:
|
This is intentional to avoid layout thrash when things are hidden and restored. It's also to allow for hidden trees to be able to do early preemptive layout calculations for hidden trees so that they can be instantly restored. |
Hmm, but that cleanup has to be called at some point in time - when that is? |
When the Suspense boundary itself is committed and deleted or if an update changes it to no longer be visible. I could imagine the second case being a bug since it’s a thing we typically don’t model at all. |
unless i'm misunderstanding, this doesn't seem to be the case Repro: press "Go to Page 1", "Go to Page 2", "Just unmount the whole thing". the suspense boundary and the suspending children below all get destroyed, but the background stays pink (i.e. Page1's Interestingly, this only acts "strange" when Page1 or Page2 suspend, not when they're already available. reproducible like this:
maybe i'm missing something, but i'd expect insertion cleanups to behave the same regardless of whether something suspensey happened (i.e Page2 suspended, Page1 got hidden, and never came back) or not. |
Was there any movement on this? I ran into the same issue linked by @Andarist , specifically with Emotion's Global component not calling the clean up on unmount. In my case all components are wrapped inside of React.lazy() because that's how we have our page route components set up (all pages being lazily loaded). |
For what it's worth, styled-components worked around this bug by simply using |
Any movement on this one? |
I had to downgrade to React 17.0.2 to resolve this for now. |
Insertion effects do not unmount when a subtree goes offscreen, this means we still have to traverse the subtree in that case. We could potentially use a new subtree bit flag to avoid the traversal if there's no insertion effects, but I'm not sure that's worth the tradeoff of using a new flag? Likely also fixes facebook#26670
Insertion effects do not unmount when a subtree goes offscreen, this means we still have to traverse the subtree in that case. We could potentially use a new subtree bit flag to avoid the traversal if there's no insertion effects, but I'm not sure that's worth the tradeoff of using a new flag? Likely also fixes facebook#26670
Insertion effects do not unmount when a subtree goes offscreen, this means we still have to traverse the subtree in that case. Likely also fixes facebook#26670
Insertion effects do not unmount when a subtree goes offscreen, this means we still have to traverse the subtree in that case. Likely also fixes facebook#26670
Insertion effects do not unmount when a subtree is removed while offscreen. Current behavior for an insertion effect is if the component goes - *visible -> removed:* calls insertion effect cleanup - *visible -> offscreen -> removed:* insertion effect cleanup is never called This makes it so we always call insertion effect cleanup when removing the component. Likely also fixes #26670 --------- Co-authored-by: Rick Hanlon <rickhanlonii@fb.com>
Insertion effects do not unmount when a subtree is removed while offscreen. Current behavior for an insertion effect is if the component goes - *visible -> removed:* calls insertion effect cleanup - *visible -> offscreen -> removed:* insertion effect cleanup is never called This makes it so we always call insertion effect cleanup when removing the component. Likely also fixes #26670 --------- Co-authored-by: Rick Hanlon <rickhanlonii@fb.com> DiffTrain build for commit d3d4d3a.
Insertion effects do not unmount when a subtree is removed while offscreen. Current behavior for an insertion effect is if the component goes - *visible -> removed:* calls insertion effect cleanup - *visible -> offscreen -> removed:* insertion effect cleanup is never called This makes it so we always call insertion effect cleanup when removing the component. Likely also fixes #26670 --------- Co-authored-by: Rick Hanlon <rickhanlonii@fb.com> DiffTrain build for [d3d4d3a](d3d4d3a)
@rickhanlonii I somewhat doubt that a PR to Relay fixed this issue 😅 EDIT:// Oh, this might be a byproduct of some of your internal infra. There are some changes in React related to this too: d3d4d3a |
React version: 18.2.0
Reproduction
Click the button twice.
Link to code example:
https://codesandbox.io/s/focused-burnell-ewgso1?file=/src/App.js:0-773
The current behavior
Logs:
The expected behavior
This is also the behavior of
useLayoutEffect
if not in strict mode.The text was updated successfully, but these errors were encountered: