Vigyata.AI
Is this your channel?

React 19 Tutorial - 22 - setState using Previous State

5.5K views· 179 likes· 7:58· Nov 26, 2025

🛍️ Products Mentioned (2)

Github - https://github.com/gopinav/React-19-Tutorials Become a Fullstack Developer with Scrimba - https://scrimba.com/fullstack-path-c0fullstack?via=Codevolution Follow me + Twitter - https://twitter.com/CodevolutionWeb Business - codevolution.business@gmail.com setState using Previous State

About This Video

In this lesson, I pick up from the simple counter component where calling setCount multiple times inside the same click handler gives you a surprising result. All the logs inside the handler show count as 0, and even though I call setCount three times, the UI ends up showing only the last update. That’s because each call uses the same render “snapshot” value, so React queues updates like “set to 1”, “set to 5”, and “set to 10”, and the last one wins. To fix this when the new state depends on the previous state, I switch to the updater function pattern. Instead of passing a value, I pass a function to setCount, where I receive the previous state and return the next state (prev + 1, prev + 5, prev + 10). React queues these updater functions and runs them after the event handler finishes, feeding the latest computed value into the next updater. That’s how we go from 0 → 1 → 6 → 16, even though the event handler itself still “sees” 0. The rule is simple: if your next state depends on the previous state, use an updater function; otherwise, setting a value directly is fine.

Frequently Asked Questions

🎬 More from Codevolution