Skip to content

emotion style does not work when using renderToString in React18 #2906

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

Open
vmaerten opened this issue Sep 29, 2022 · 5 comments
Open

emotion style does not work when using renderToString in React18 #2906

vmaerten opened this issue Sep 29, 2022 · 5 comments

Comments

@vmaerten
Copy link

Current behavior:

With React 18 (works in React 17), when we use ReactDOMServer.renderToString(EMOTION_COMPONENT) the style is not injected in the head.

To reproduce:

  1. Open this Sandbox: https://codesandbox.io/s/bug-rendertostring-with-react-18-7ty15q

Expected behavior:

Div BlockColorGreen should be green but no style is applied (class is present but no style injected)

Works fine with React 17, please see: https://codesandbox.io/s/not-bug-rendertostring-with-react-17-04vtp2

Environment information:

  • react version: 18.0.0
  • react-dom version: 18.0.0
  • @emotion/react version: 11.9.0
@srmagura
Copy link
Contributor

@Andarist Uhh.... do we support server-side rendering in the browser? 😅

@cforycki
Copy link

cforycki commented Nov 5, 2022

Probably the same underlying issue as this one #2691

@chandlervdw
Copy link

@srmagura @Andarist @cforycki any chance this will be addressed?

@shivamchamoli09
Copy link

Facing the same issue. Any update on this?

@cforycki
Copy link

cforycki commented Jan 5, 2023

@chandlervdw I'm not a contributor to emotion, I'm working with @vmaerten that's why I commented 😅

By the way, on our side we have a temporary solution, because we wanted to update to React 18 and this was the only thing that blocked us.

What we did was to render the original component in our page, inside a display: none so that it injects the style in the page like any other component but doesn't display it. And because the renderToString result string will be using the same class names, the style is applied.

See this codesandbox: https://codesandbox.io/s/bug-rendertostring-with-react-18-forked-m2un7b?file=/src/App.tsx

We are not working on performance sensible apps so for us it's enough

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

5 participants