Bug: Updating state during render when using `useSyncExternalStore` throws `Cannot update a component (Component) while rendering a different component (Component)`
Nobody has claimed this yet.
- 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
- Open the Console before you work with the page
- Click the "increment" button 5 times
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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