react / react/react

Bug: Updating state during render when using `useSyncExternalStore` throws `Cannot update a component (Component) while rendering a different component (Component)`

Open
#31,730 14 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Status: Unconfirmed
Dominant language
JavaScript
Stars
251k
Forks
51.4k
Avg merge
2d 4h
Merged PRs (30d)
53

Description

React version: 19.0.0

Steps To Reproduce

  1. Open the Console before you work with the page
  2. Click the "increment" button 5 times
  3. You will see the error in the Console

Link to code example:

https://codesandbox.io/p/sandbox/polished-wind-d9dyvj

The current behavior

When updating the store it throws Cannot update a component (Component) while rendering a different component (Component).

The expected behavior

It should work the same way useState works when calling setState during render.

Notes

This is coming from a user of use-local-storage-state hook that has around ~500k monthly downloads — https://github.com/astoilkov/use-local-storage-state/issues/77.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue in the linked CodeSandbox and compare the useSyncExternalStore behavior with the stated useState expectation. Read the linked use-local-storage-state issue for context; done means updating the store during render no longer produces the reported cross-component update error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.