Course contentsShow
← React
Lesson 166 of 2,3965. Lists and KeysFree lesson

Performance Implications of Bad Keys

Why React can't optimize properly with index keys and may destroy/recreate components instead of moving them.

Performance Implications of Bad Keys

You'll learn how using index-based or unstable keys prevents React from optimizing re-renders, causing unnecessary component destruction and recreation.

Why React Optimizes with Keys

When you provide stable, unique keys, React can quickly identify which items have moved, been added, or been removed. It uses this information to reuse existing DOM nodes and preserve component instances — just shifting them to new positions.

Think of it like rearranging furniture in a room. With good labels (stable keys), you simply move the couch from one wall to another. With bad labels (index keys), you throw away the old couch and build a new one in the new spot.

What Happens with Index Keys

When you use array indices as keys and the list changes:

// ❌ Index keys during reordering
items.map((item, index) => <Item key={index} {...item} />)

React sees the keys (0, 1, 2...) stay in the same positions, but the data changes. It assumes the components in those positions need updating, not moving. So React:

  1. Destroys the component instances that were there
  2. Re-creates new instances with the new data
  3. Loses internal state unnecessarily
  4. Re-runs initialization logic (effects, animations, focus)

This is slower than simply moving existing components and can cause visual glitches, lost form state, or animations restarting.

The Optimization Lost

With stable keys like database IDs, React recognizes "this is the same item, just in a different position" and efficiently moves it. With index keys, React thinks "position 0 is a different item now" and does expensive teardown/setup work.

Key Takeaway: Bad keys force React to destroy and recreate components instead of reusing them, wasting performance and breaking user experience during list updates.