r/reactjs • u/dance2die • Aug 25 '18
Accessing React State right after setting it - Sung's Technical Blog
https://www.slightedgecoder.com/2018/08/25/accessing-react-state-right-after-setting-it/
1
Upvotes
r/reactjs • u/dance2die • Aug 25 '18
2
u/joesb Aug 26 '18
You may have multiple places that call
setState, each setting different fields of the state.You may want to centralize your logic so that the only source of truth is the state.
If you use
setStatewith an updater function then you won't have the value used to set the state. For example,setState({counter} => ({ counter: counter + 1}))