question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

State updates from useState hook not always collapsed?

See original GitHub issue

Do you want to request a feature or report a bug? I’m hoping it’s a bug, it at least bit me very unexpectedly when trying out the new react hooks.

What is the current behavior? When multiple setters returned from useState are called in the same timeslice:

  • sometimes they are collapsed into a single update, and a single rerender is triggered, the ‘intermediate’ state is not observable
  • sometimes they are not collapsed into a single update, and multiple rerenders are triggered, the ‘intermediate’ state is observable from the first rerender

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. See https://codesandbox.io/s/74nm51wv00 The toggle triggered from the button click will collapse the state changes, so the a === b console.log will never trigger. However the toggle triggered from window.setInterval will not collapse the state changes, triggering the console.log.

What is the expected behavior? I would expect the state changes to always be collapsed, a little bit for performance (less rerenders), but mostly because this makes logically ‘impossible’ states observable during rendering, which was very unexpected for me.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? React 16.7.0-alpha.0 with the new hooks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Nov 12, 2018

And of course you can also useReducer to describe updates to multiple fields as a result of one dispatch. I’d say we recommend it over multiple state variables when the logic gets complex.

0reactions
MrHackycommented, Nov 12, 2018

Hmm, right. The ReactDOM.unstable_batchedUpdates indeed works around my issue, and the way forward with (more) automatic batching would be a perfect fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

state is not getting updated with usestate - Stack Overflow
**I have created a sample code you can try like this:** class Test extends Component { constructor(props) { super(props) this.state ...
Read more >
Steps to Solve Changes Not Reflecting When useState Set ...
When the “useState” set method is not reflecting a change immediately, it may be due to the current closure of the state variable....
Read more >
4 Examples of the useState Hook - Dave Ceddia
The useState hook lets you add state to function components. (Not sure what a “hook” is? Read this intro to hooks!) By calling...
Read more >
Concurrent UI Patterns (Experimental) - React
We've imported the useTransition Hook and used it in the component that updates the state. We've wrapped our state update into startTransition to...
Read more >
React Hooks Explained: useImperativeHandle
Posted on Apr 12, 2021 • Updated on Sep 30, 2021 ... on the other hand, do not have access to (and are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found