DEV Community

Discussion on: Why doesn't useState has a dependency array?

Collapse
 
tylim88 profile image
Acid Coder • Edited

because it happens in different render

when you setLocalState, you update your state to 0 and pass it to the hook, that is one render

useEffect only run at the next render and if there is a change in dependencies, so the first render has to run first (with old value), then it trigger useEffect that set another state and trigger another render, only by that you have the correct value

but i have to say this is a very odd way to update the state, there is pattern that is much better