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

3

u/gaearon React core team Dec 07 '18 edited Dec 07 '18

Don't know how to do it in hooks

You can do it in exactly the same way if those two values are part of the same object. If they're not, you could either combine two states together in a single useState or jump straight to useReducer (which is the idiomatic way to avoid these problems).

In either case, the proper solution is to not "sync" two states — that usually means you don't have a single source of truth:

https://www.reddit.com/r/reactjs/comments/a3y76f/react_hooks_setstate_gotcha/ebabg32/

The solution at the end of the video (with capturing next state explicitly and using it twice) is also a reasonable one in many cases.