Skip to content
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

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

Open
jmca opened this issue May 20, 2023 · 3 comments

Comments

@jmca
Copy link

jmca commented May 20, 2023

Current behavior:

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.

Note that when Global is not in a component that is rendered on initial page load (while wrapped in Suspense), 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

  • Page initially loads Page1 component
  • Notice red body background
  • Click on the link "CLICK ME TO PAGE 2"
  • Page 2 is now rendered
    • Expected Result
      • Body should be white
    • Actual Result
      • Body is still red
        image
  • Click on the link "CLICK ME TO PAGE 1"
  • Page 1 is now rendered
    • Expected Result
      • Body has 1 red style entry
    • Actual Result
      • Body has 2 red style entries (because the original one was never removed)
        image

Passing Test Case

Using the same example at https://stackblitz.com/edit/react-ts-agivwh

  • In App.tsx, set the initial value of currentPage to the value of /page2, then reload web view
  • Page initially loads Page2 component
  • Notice the white background
  • Click on the link "CLICK ME TO PAGE 1"
  • Page 1 in now rendered
  • Notice the red background
  • Click on the link "CLICK ME TO PAGE 2"
  • Page 2 is loaded
  • Notice the white background

Global 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


image

@jmca jmca changed the title 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 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 May 20, 2023
@Andarist
Copy link
Member

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 useInsertionEffect+useLayoutEffect... but that's an ugly workaround 😬 I could consider accepting a PR with such a fix though.

@lpolito
Copy link

lpolito commented Dec 13, 2023

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?

@tien
Copy link

tien commented Jan 29, 2024

I'm also getting this a lot recently after I start using suspense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants