Replies: 3 comments 16 replies
-
Does |
Beta Was this translation helpful? Give feedback.
8 replies
This comment has been hidden.
This comment has been hidden.
-
Currently after resuspending But the React doc is saying:
Is this expected behavior? Could we rely on it and expect that effect cleanup will not be called when the component gets hidden inside Suspense? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Last week I posted about a new "strict effects" mode and how it would impact effects for development builds. React 18 will also include a small change to how layout effects work with Suspense. This thread explains that change and how you can prepare for it.
What's changing?
If a component mounts within a Suspense boundary and is later hidden (because of something else suspending), React will cleanup that component’s layout effects by calling
useLayoutEffect
cleanup functions orcomponentWillUnmount
. After the suspended boundary resolves, React will recreate the component’s layout effects again by callinguseLayoutEffect
orcomponentDidMount
.This change is being made to avoid bugs that may occur from things like reading layout in a hidden tree.
Will this impact a lot of my code?
The scenario described above is not common and can be avoided entirely by using the new
useTransition
API to ensure that Suspense does not revert to its fallback state after being mounted.For example, here is a state update that might cause the
ProfilePage
component to re-suspend afer mounting:Wrap the state update in a "transition" to prevent the
ProfilePage
from being unmounted while suspending. This lets React know that it should wait for the update to complete:As a bonus, React also returns an
isPending
value which can be used for things like disabling the button while the previous transition is still in progress.We expect that most effects should "just work" with this change because of strict effects mode. Refer to this post for examples of the types of effects that may require changes.
Related posts
For more information on strict effects, see:
<StrictMode>
Beta Was this translation helpful? Give feedback.
All reactions