Why React doesn't update state immediately - LogRocket Blog
https://blog.logrocket.com/why-react-doesnt-update-state-immediately/
Understanding React’s Asynchronous State UpdatesHow React Performs State UpdatesCarrying Out Operations with Class Components setState() callbackThe second parameter to setState() is an optional callback function. This argument will be executed once setState()is completed and the component is re-rendered. The callback function is guaranteed to run after the state update has been applied: componentDidUpdateThe componentDidUpdatefunction is invoked immediately after a state update occurs. To avoid an infinite loop, you should always use a conditional statement to be sure that the previous state and the current state are not the same:
setState() callbackThe second parameter to setState() is an optional callback function. This argument will be executed once setState()is completed and the component is re-rendered. The callback function is guaranteed to run after the state update has been applied:
componentDidUpdateThe componentDidUpdatefunction is invoked immediately after a state update occurs. To avoid an infinite loop, you should always use a conditional statement to be sure that the previous state and the current state are not the same:
DA: 39 PA: 23 MOZ Rank: 83