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.

Bug: React's batching makes state comparison checks unreliable

See original GitHub issue

It 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

  1. Open the provided codesandbox
  2. Focus the 2 inputs back and forth by tabbing
  3. Note the onChange logs correspond to the updates and are in sync with the UI
  4. Now instead, focus an input then press the down arrow to programmatically focus back and forth between the 2 inputs
  5. Note the onChange logs are now wrong and inconsistent with the UI (initially true and then false 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 a setTimeout() to avoid batching.

The expected behavior

We would expect the 2 examples to behave the same.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:5

github_iconTop GitHub Comments

3reactions
jjenzzcommented, Nov 27, 2020

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

2reactions
eps1loncommented, Nov 27, 2020

The workaround for this particular issue is similar to https://github.com/facebook/react/issues/18591:

React.useLayoutEffect(() => {
  ref.current = value;
});
....
<div onBlur={() => {
  ReactDOM.flushSync(() => {
    setValue(false)
  })
}}>

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.

Read more comments on GitHub >

github_iconTop 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 >

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