What Happens on Re-render
You'll learn: How React walks through your hooks in the exact same order during every re-render to match up the right state with the right hook call.
The Re-render Walk-Through
When your component re-renders, React doesn't start with a blank slate. It already has an internal list of hook data from the first render—that index-based registry we just learned about.
During re-render, React walks through this existing list in order, expecting to encounter the exact same sequence of hook calls.
Think of it like a waiter returning to your table with your order. They've written down "Table 5: pasta, salad, water" on their notepad. When they come back, they expect to find the same people at Table 5, in the same seats. If you've suddenly switched seats or left the table entirely, they're confused about who gets what.
The Matching Process
- Your component function runs again
- React encounters the first
useStatecall → matches it with index 0 in the hook list → returns the current state value - React encounters the second
useStatecall → matches it with index 1 → returns that state value - React encounters a
useEffectcall → matches it with index 2 → checks if dependencies changed
This is why the order is non-negotiable. If you add a conditional that skips a hook on some renders, React's walk-through gets out of sync. It expects three hooks but encounters two, or encounters them in a different order. The index positions no longer match up correctly.
Key Takeaway: On re-render, React walks the existing hook list in sequential order, matching each hook call by position to retrieve the correct stored data—which is why changing the order or count of hooks breaks everything.