Skip to content

feat: move style injection from render to useInsetionEffect - #3821

Closed
alexandernanberg wants to merge 1 commit into
styled-components:mainfrom
alexandernanberg:use-insertion-effect
Closed

alexandernanberg wants to merge 1 commit into
styled-components:mainfrom
alexandernanberg:use-insertion-effect

Conversation

@alexandernanberg

Copy link
Copy Markdown
Member

Atm this is just a quick experiment if we can utilize useInsertionEffect instead of injecting styles in render which isn't the best for performance. E.g. following the guidelines described in reactwg/react-18#110

From my limited and quick testing this seems to work fine, is there anything I'm missing and does this sound like a good idea to you? If that is the case I'll clean up the PR a little more, e.g. renaming generateAndInjectStyles to something more fitting

@quantizor

Copy link
Copy Markdown
Contributor

I'll have to look at this one later. I switched us to use the new effect type for global styles but I'm not sure about doing it for component styles since they need to be injected as fast as possible on first render

@alexandernanberg

Copy link
Copy Markdown
Member Author

One possible solution could be to inject styles in useInsertionEffect if React 18, otherwise fallback to doing it in render like before

@agriffis

Copy link
Copy Markdown
Contributor

@probablyup

they need to be injected as fast as possible on first render

I haven't looked at the code in React to verify, but the doc claims "it fires synchronously before all DOM mutations."

That seems to satisfy "as fast as possible" but are there situations where this still isn't enough?

Otherwise this LGTM, pending whatever cleanup @alexandernanberg wanted to do.

@alexandernanberg

Copy link
Copy Markdown
Member Author

Superseded by #4332

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants