Lost Component State Between Renders
What you'll learn: How unstable keys cause React to unmount and remount components, destroying their internal state unexpectedly.
When a component's key changes between renders, React doesn't update it — it destroys the old component entirely and creates a brand new one from scratch. Any local state (useState, useRef, focus, animations) vanishes instantly.
The Problem in Action
Imagine you have a list of expandable items, each with an "open/closed" toggle tracked by local state. If you're using index-based keys and the user sorts or filters the list, the indexes shift. React sees different keys in different positions and treats them as completely different components:
// Before sort: Item A at index 0, expanded
<Item key={0} name="A" />
// After sort: Item B now at index 0
<Item key={0} name="B" />
React thinks "the component at key 0 changed from A to B" — so it unmounts the A component (losing its "expanded" state) and mounts a fresh B component. Your user's UI interactions just disappeared.
Why This Happens
Keys are React's only way to track component identity across renders. When the key changes, React assumes it's a different component that happens to live in the same spot. It's like giving someone a new ID card — to the system, they're a completely new person, with no memory of the past.
The Fix
Use stable, data-driven keys (like server IDs). When Item A moves from position 0 to position 5, its key stays "item-a-123", so React moves the existing component instance and preserves all its state.
Key Takeaway: Unstable keys break component identity — React destroys and recreates components instead of updating them, erasing all local state in the process. Always use stable identifiers as keys.