-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Given Emotion's Global
component is used in a component that is lazy loaded and wrapped in Suspense
, and the parent component is rendered upon initial page load, the global styles are not removed when that component is unmounted
#3038
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
Comments
Global
component is used in a component that is lazy loaded and wrapped in Suspense
, and the parent component is rendered upon initial page load, the global styles _are not_ removed when that component is unmountedGlobal
component is used in a component that is lazy loaded and wrapped in Suspense
, and the parent component is rendered upon initial page load, the global styles are not removed when that component is unmounted
I think that this is actually a React issue, you can find the tracking ticket here. I'm not sure if as such it's worth fixing it in Emotion - especially since there is actually no clear way of dealing with this situation. I guess that we could track disconnecting/reapparing of those components through a combination of |
Just ran into this, and came to the same conclusion as suggested by @Andarist I could see creating a flag to bypass this, possibly, but yeah, it's ugly. Any chance you just use useLayoutEffect (or an isomorphic variant to support SSR) instead? |
I'm also getting this a lot recently after I start using suspense. |
Bump up the issue, as we lazy component for every react-router route, and the GlobalStyles's sheet isn't flushed (removed) when the component is unmounted (route change), affecting the style of subsequent pages |
What version of React are you on? I think this was fixed by facebook/react#30954 |
@Andarist That PR is on Sep 2024, which is likely on React 19. We're still on React 18.3.1 (Apr 2024) and doesn't intend to upgrade to React 19 yet due to some legacy code. |
That is understandable - but I don't have a good way to workaround this on our side. And given the bug is fixed upstream, it's a reasonable resolution of this issue (in my eyes) to call it fixed. |
Current behavior:
Given Emotion's
Global
component is used in a component that is lazy loaded and wrapped inSuspense
, and the parent component is rendered upon initial page load, the global styles are not removed when that component is unmounted.Note that when
Global
is not in a component that is rendered on initial page load (while wrapped inSuspense
), global styles are correctly removed when the parent component is unmounted.Seps to reproduce:
Failing Test Case
Using this example: https://stackblitz.com/edit/react-ts-agivwh?file=App.tsx
Page1
componentPassing Test Case
Using the same example at https://stackblitz.com/edit/react-ts-agivwh
App.tsx
, set the initial value ofcurrentPage
to the value of/page2
, then reload web viewPage2
componentGlobal body style was correctly added and removed.
I tested this in an example without using suspense and everything works as expected following the same steps above
https://stackblitz.com/edit/react-ts-xwa9w5?file=App.tsx
The text was updated successfully, but these errors were encountered: