r/reactjs • • Dec 07 '18

React Team Comments React Hooks setState Gotcha

ran into a React Hooks setState Gotcha today:

https://www.youtube.com/watch?v=8NDSr9Vz6H4

Depending on how you view it, this is either a closure or a concurrency issue. Easy to run into if you are doing consecutive setStates. Because useState's setter doesnt offer a generic callback (it does offer a specific callback), you may need to "lift common variables up".

EDIT: NOT a concurrency issue - see Dan's reply below. as always its slightly scary to post up my failures for pple to see but i hope people understand i am just sharing my own pain points that i am bumping into. defintely not react's fault.

https://codesandbox.io/s/67zo2knpn

happy to take suggestions on how to improve

9 Upvotes

26 comments sorted by

View all comments

Show parent comment

2

u/gaearon React core team Dec 07 '18

Can you create an example that doesn't use names like state1 and state2, and actually roughly reflects what you were trying to do? It's a bit difficult to suggest a proper solution because it's hard to understand which parts of your solution are important to the user and which are just a consequence of how you put things together.