Course contentsShow
← React
Lesson 362 of 2,39610. useState BasicsFree lesson

State Lives in the React Tree, Not the Component Function

State persists between re-renders because React stores it in the tree node, not in function scope or closures.

State Lives in the React Tree, Not the Component Function

You'll learn: Where React actually stores your component's state and why it persists across re-renders.

The Core Idea

When you call useState, you might think the state value lives inside your function component—maybe in some closure or local variable. But that's not how it works. React stores state in the tree node, not in the function itself.

Every time your component renders, React calls your function again from scratch. All local variables are recreated. But your state values persist because React keeps them in a separate data structure associated with that component's position in the component tree.

Think of it like a filing cabinet: each drawer (tree node) has a label (component name + position), and inside that drawer are folders (state values). When React needs to render your component, it pulls the values from the correct drawer, passes them to your function, and then files away any updates you make.

Why This Matters

This explains several behaviors:

  • State survives re-renders: Your function runs many times, but state doesn't reset because it's stored externally.
  • Each instance has its own state: Two <Counter /> components have two separate drawers in the tree.
  • State is tied to position: If you unmount and remount a component at a different position, it gets fresh state.

The function component is stateless—it's just a recipe that React follows. The actual state lives in React's internal tree structure.

Key Takeaway: State doesn't live in your function or in closures—it lives in the React tree node, which is why it persists between renders and why each component instance maintains separate state.