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

Position in the Tree Determines State Identity

React tracks state by component type and position in the tree—same component at the same spot keeps its state.

Position in the Tree Determines State Identity

What you'll learn: How React decides whether to preserve or reset a component's state based on where it appears in the component tree.

The Core Idea

React doesn't track state by variable name or component instance reference. Instead, it uses position in the tree to identify which state belongs to which component. When React sees the same component type at the same position across renders, it preserves that component's state. Move the component to a different position or swap it with a different type, and React treats it as a brand new component—destroying the old state.

Why Position Matters

Think of React's component tree like numbered parking spots. Each spot (position) can hold a car (component). If you park a Honda in spot #3, leave, then come back and park the same Honda in spot #3 again, it keeps its contents (state). But if you park a Toyota in spot #3, or move your Honda to spot #7, React sees it as a different situation entirely.

// Same component type, same position → state preserved
{showCounter && <Counter />}

// Same component type, different position → new state
{showLeft ? <Counter /> : null}
{showRight ? <Counter /> : null}

In the second example, the Counter appears in two different positions in the tree (left branch vs. right branch). Each position gets its own independent state.

Tree Structure, Not Variable Names

React doesn't care what you call the component or how you render it—only its type and where it sits relative to its parent and siblings matters.

Key Takeaway: React identifies component state by the component's type and position in the tree structure, not by variable names or references—same spot means same state, different spot means fresh state.