The Mental Model: Hook Array Indexing
What You'll Learn
You'll understand why React depends on hook call order by visualizing the internal array that tracks your hooks.
How React Tracks Your Hooks Internally
Behind the scenes, React maintains an array of hook values for each component instance. When your component renders, React walks through this array sequentially, matching each hook call to its position in the array.
Think of it like a coat check: you hand in items in a specific order and get numbered tickets. When you return, you must ask for them in the same order using those tickets. If you skip ticket #2 or ask for #3 before #2, the whole system breaks.
The First Render
Your code: React's internal array:
useState(0) → [0, ...]
useState(false) → [0, false, ...]
useEffect(...) → [0, false, effectData]
React assigns each hook to an index: 0, 1, 2...
Every Subsequent Render
React doesn't know which useState call is which by name or variable. It only knows "the first hook call gets array[0], the second gets array[1]," and so on.
If you conditionally skip the second useState, React still gives you array[1] on your third call—but now it's mismatched! The effect data goes where boolean state should be. Chaos ensues.
Why Order Matters
React uses positional matching, not magical variable tracking. Calling hooks in the same order every render ensures the array indices stay aligned with the right hook values.
Key Takeaway: React's hook system relies on a fixed-order internal array—each hook call grabs the next array slot, so consistent call order is absolutely critical for correct state-to-hook matching.