Performance Degradation from Remounting
What you'll learn: How unstable keys force React to destroy and recreate components instead of efficiently updating them, causing measurable performance problems.
The Unmount/Mount Cycle Tax
When React encounters a component with a changed key, it doesn't update the existing DOM node—it completely unmounts the old component and mounts a new one from scratch. This is far more expensive than a simple update.
Think of it like remodeling a house versus demolishing and rebuilding it. Updating existing DOM nodes (remodeling) is cheap: change some text, swap an attribute. But unmounting triggers cleanup effects, destroys DOM nodes, then mounting creates new DOM from scratch, runs initialization effects, and recalculates layout.
The Performance Impact
With unstable keys (like array indices on filtered/sorted lists), every re-render can trigger this expensive cycle for multiple items:
// Bad: indices shift after filtering
items.filter(predicate).map((item, index) => (
<ExpensiveComponent key={index} data={item} />
))
Each filtered item gets a new index, so React unmounts components at old positions and mounts "new" ones, even though the actual data hasn't changed.
What Gets Re-Run
The unmount/mount cycle repeats:
- Component initialization logic
- useEffect cleanup and setup
- Layout calculations
- Image loading and network requests
- Third-party library initialization
- Animation resets
With stable keys (IDs), React recognizes the same component and just updates props—orders of magnitude faster.
Key Takeaway: Unstable keys turn cheap updates into expensive unmount/mount cycles, causing unnecessary re-initialization, layout thrashing, and visible performance degradation in lists with dynamic ordering or filtering. Always use stable IDs to let React's reconciliation algorithm work efficiently.