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

Prevent automatic batching updates in ThemeProvider to ensure client use right colorMode #2732

Merged

Conversation

renbaoshuo
Copy link
Contributor

@renbaoshuo renbaoshuo commented Jan 3, 2023

Closes #2731.

Use ReactDOM.flushSync to prevent automatic batching of state updates since React 18, which causes error described in #2731.

Screenshots

None.

Merge checklist

  • Added/updated tests
  • Added/updated documentation
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge

Take a look at the What we look for in reviews section of the contributing guidelines for more information on how we review PRs.

@renbaoshuo renbaoshuo requested review from a team and mperrotti January 3, 2023 02:21
@changeset-bot
Copy link

changeset-bot bot commented Jan 3, 2023

🦋 Changeset detected

Latest commit: 83a95e7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@renbaoshuo renbaoshuo force-pushed the theme-provider-prevent-automatic-batch-update branch from f4cde90 to 8e90fb6 Compare January 4, 2023 07:26
@joshblack
Copy link
Member

Thanks for taking the time to make Pull Request @renbaoshuo! 🙌 In your opinion, would there be a good test that we could add to make sure this doesn't happen again in the future?

@rezrah rezrah requested a review from joshblack January 9, 2023 16:58
setColorMode(resolvedColorModeOnClient)
// use ReactDOM.flushSync to prevent automatic batching of state updates since React 18
// ref: https://github.com/reactwg/react-18/discussions/21
ReactDOM.flushSync(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking, would this lead to an error with React 17?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

facebook/react#10225
facebook/react#11527 (comment)

It seems that ReactDOM.flushSync(batch) is already introduced before React 17.

@renbaoshuo
Copy link
Contributor Author

Thanks for taking the time to make Pull Request @renbaoshuo! 🙌 In your opinion, would there be a good test that we could add to make sure this doesn't happen again in the future?

Sorry, I have no idea.

@renbaoshuo renbaoshuo force-pushed the theme-provider-prevent-automatic-batch-update branch from 8e90fb6 to 7214e0b Compare January 10, 2023 00:53
@joshblack joshblack enabled auto-merge (squash) January 17, 2023 21:54
@joshblack joshblack merged commit 3f70b2e into primer:main Jan 17, 2023
@primer-css primer-css mentioned this pull request Jan 17, 2023
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.

Theming issue when using SSR
3 participants