React State Snapshots Explained: Why State Values Don't Change During Renders

Understand how React state snapshots work and why state values remain constant within a single render, with practical examples and explanations.

Cet article n'est pas encore disponible en français. Vous lisez la version anglaise.

Partie 11 sur 11React fundamentals
  1. Creating A React App in 2026 is EASY with Vite
  2. Just Created Your React Project? Don't PANIC, here's every file explained
  3. Creating Your First React Component is EASY
  4. Master JSX in 8 Minutes (Before You Write Your Next React Component) - React Tutorial
  5. PROPS in React explained.
  6. React Conditional Rendering: Complete Guide with If Statements and JSX
  7. React Rendering Lists Tutorial: Complete Guide to Map Method and Keys
  8. React Event Handlers Tutorial: Complete Guide to onClick and Event Handling
  9. React useState Hook Tutorial: Complete Guide to Component State Management
  10. React's Three Phases: Complete Guide to Trigger, Render, and Commit
  11. React State Snapshots Explained: Why State Values Don't Change During Renders

Introduction

Increment a counter, then log its value five seconds later inside the same click handler — you might expect the logged value to reflect whatever the counter has become by then. It doesn't.

This isn't a bug. It's a direct consequence of how React's state actually works: every render gets its own frozen snapshot of state, and the code attached to that render — including any deferred callbacks — only ever sees that one snapshot.

This article walks through exactly why a setTimeout inside an event handler logs the state value from the render it was created in, not whatever the state has since become, and what that means for how you should think about state and renders in React.


function App(){
  const [counter,seCounter] =useState(1)
return (
  <div>
    <p>{counter}</p>
    <button onClick={()=>{

      setCounter(counter + 1); 
      setTimeout(() => { 
    console.log(counter);
      }, 5000);

    }}></button>
  </div>
)
}

You may get shocked to know that this code, will not print 2.

Despite incrementing the counter by 1, and then deferring the execution of the console log by 5 seconds using the setTimeout browser API,which means that by the time the console.log is executed the state will have been already incremented by 1.

But reality shows nothing but that, in fact, a 1 gets printed.

Here we'll clarify what's exactly happening under the scene while explaining the concept of a state snapshot in React.

As you might know, State doesn't live inside the function component but it's stored within the React package itself!.

And provided as a snapshot or in other terms, as a copy of the original state via the useState hook.

In fact, useState is named a hook because **it's hooking into the external state stored in React itself! **

Components re-renders can be triggered via a state updates. Therefore when a given component re-renders or gets called by React a new snapshot mirroring the latest updated state value is given to it, and then based on that value the whole JSX including the attached event handlers gets re-created again.

So the deferred console.log in the previous code was printing 1, because it was referencing the state snapshot of the first render.

By the time the second render happens, the whole JSX including its corresponding attached event handlers will be re-created again with the new counter state value which will be equal to 2.

Therefore, Every render is associated with:

  • A state snapshot.
  • And a JSX code including event handlers.

Similarly, If you re-tried to re-click on the button again the counter state will be incremented to 3.

Then the component will re-render displaying 3 on the UI. After 5 seconds are elapsed 2 will be printed, because as we said previously the event handler was attached to the previous render hence the callback function is referencing the old state snapshot.

In other words, we can briefly say that:

The state that is returned by the useState hook is constant during every render, even if the event handler that is attached to one of the returned JSX elements was executing asynchronously, in the future when many potential renders may have already occurred.

Every render is allocated its own constant state snapshot, that never change before the next render.

All the derived JSX code including event listeners is tied to that specific render.

In more specific words, renders are totally isolated.

Render 1 can never access state snapshots in render 2.

Summary

State is a snapshot, not a live variable

const [counter, setCounter] = useState(1);
  • useState doesn't give you a live reference to React's internal state — it gives you a snapshot of its value as of the current render.

Every render has its own snapshot

<button onClick={() => {
  setCounter(counter + 1);
  setTimeout(() => console.log(counter), 5000);
}}>
  • The event handler attached to a given render closes over that render's counter value — even a callback that runs 5 seconds later still logs the value from the render it was created in, not whatever the state has become since.

Renders are isolated

  • Each render gets its own state snapshot and its own JSX, including event handlers built from that snapshot.
  • A closure created during one render can never see a different render's state — render 1 can't access render 2's snapshot.

Conclusion

To sum up, you can think of a re-render as a component starting a brand new life with a brand new state snapshot, JSX code and event handlers.

Now, imagine if we try to update the counter state using setCounter setter function two times sequentially. What would be the value of the counter state after the second render?

// counter = 1
setCounter(counter +1)
setCounter(counter +1)
// counter = ?? 

Well, that's what we are going to discover in the next article, where we introduce React State Batching.

Thank you for your attentive reading and happy coding.