Bug(not sure): Strange ordering of setState calls

This issue has been tracked since 2022-09-11.

React version: 16

Steps To Reproduce

  1. Click the "Test App" div
  2. See valueB never gets logged

Link to code example: https://stackblitz.com/edit/react-hooks-demo-f69yc7?file=App.js

The current behavior

When clicked the "Test App" div valueB never gets logged.

The expected behavior

Not sure what the expected behavior should be, just it seemed strange valueB was never logged.

After putting some console.logs and using functional setState, I can see why valueB was not logged, because it was overwritten by setB(null) from previous renders useEffect return function. Just still this behavior seemed a bit strange to me, so wanted to share to make sure this is not a bug. Is it?

More details: https://stackoverflow.com/questions/68080085/state-update-being-overridden-by-another-previous-state-update

rickhanlonii wrote this answer on 2022-09-12

Yeah, that's the behavior in 16. If you check out the behavior in 18 below you can see that it's been updated to batch the updates with a more predictable outcome (automatic batching):

https://stackblitz.com/edit/react-hooks-demo-nd2wau?file=App.js

gmoniava wrote this answer on 2022-09-12

Yeah I had already checked that it worked in 18. It was somewhat strange behavior isn't it?

More Details About Repo
Owner Name facebook
Repo Name react
Full Name facebook/react
Language JavaScript
Created Date 2013-05-24
Updated Date 2022-10-05
Star Count 195619
Watcher Count 6647
Fork Count 40518
Issue Count 1110

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date