Bug: React's batching makes state comparison checks unreliable
See original GitHub issueIt seems that in some situations React’s batching makes comparison to change in state unreliable. In the provided example, focus/blur handlers produce different outcomes:
- when triggered via tabbing
- vs. when manually focusing on keydown
React version: 17.0.0
Steps To Reproduce
- Open the provided codesandbox
- Focus the 2 inputs back and forth by tabbing
- Note the
onChange
logs correspond to the updates and are in sync with the UI - Now instead, focus an input then press the down arrow to programmatically focus back and forth between the 2 inputs
- Note the
onChange
logs are now wrong and inconsistent with the UI (initiallytrue
and thenfalse
forever)
Link to code example: https://codesandbox.io/s/react-staleness-ishfn?file=/src/App.js
The current behavior
When tabbing through this is the output I get:
onChange true
onChange false
onChange true
onChange false
onChange true
onChange false
When pressing arrow down to focus the inputs back and forth I get:
onChange true
onChange false
onChange false
onChange false
onChange false
onChange false
Note that if you check the “Prevent React from batching updates?” checkbox, both examples behave the same. The only difference is that in this case, we wrap the
.focus()
calls in asetTimeout()
to avoid batching.
The expected behavior
We would expect the 2 examples to behave the same.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:5
Top Results From Across the Web
What I learned from React-Query, and why I won't use it again
Id of the new section is saved to the store as selectedSectionId (client state); Value of selectedSectionData is derived from the siteSections ...
Read more >The Mysterious Gotcha of gRPC Stream Performance | Ably Blog
gRPC is highly useful for fast, efficient data exchange and client/server state sync. Here's a performance gotcha we ran across.
Read more >Batch normalization in 3 levels of understanding
Batch -Normalization (BN) is an algorithmic method which makes the training of Deep Neural Networks (DNN) faster and more stable. It consists of ......
Read more >Batch Analytics | Emerson
The very nature of batch data can make data visualization of trends across multiple batches challenging due to the differences in batch length ......
Read more >Data Integrity: What You Read Is What You Wrote
When considering the reliability needs of a given system, it may seem that ... As is sometimes observed: No one really wants to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
ReactDOM.flushSync
! TIL 😮If this is just a workaround, are we saying that it is something that will be fixed?
I’m not sure how we’ll explain to our consumers that they need to do that 😅 Do we know when this predictably occurs and when this kind of fix is needed so we can maybe document it?
Thanks for the help @eps1lon
The workaround for this particular issue is similar to https://github.com/facebook/react/issues/18591:
– https://codesandbox.io/s/flushsync-semantics-eli4w?file=/src/App.js:1520-1580
Though it makes me wonder whether https://github.com/facebook/react/issues/18591 is not just related to concurrent mode. Intuitively I would not expect https://codesandbox.io/s/crimson-lake-gidmx?file=/src/App.js to work the way it does right now.