The Index-Based Hook Registry
What you'll learn: Why React tracks hooks by their position in the call order, not by names or keys.
The Core Mechanism
When your component renders, React doesn't store hooks in a named map like { counterState: 0, userName: 'Alice' }. Instead, it creates an ordered list indexed by numbers: [0, 'Alice'] — where the first useState call always maps to index 0, the second to index 1, and so on.
This is why calling hooks in different orders across renders breaks everything. React relies on sequential consistency: the third hook called must always be the same hook, every single render.
Why Indices Instead of Names?
React can't use variable names because JavaScript doesn't preserve them at runtime. After compilation, const [count, setCount] = useState(0) doesn't tell React "this is the count state." React only sees a function call.
Using indices is fast and simple: no hashing, no string comparisons, just array access. When your component re-renders, React walks through the same sequence, matching each hook call to its stored value by position.
The Visual Model
Think of it like a cafeteria line where you always take the same items in order:
- First tray slot → your main dish
- Second slot → your drink
- Third slot → your dessert
If you suddenly skip the drink one day, you'll grab dessert thinking it's your drink. React's hook registry works the same way — order matters, not labels.
Key Takeaway: React uses numeric indices to track hooks because it's fast, simple, and the only reliable way to match hook calls across renders without runtime variable names.