EasyReact Fundamentals📖 Theory Question

What is the key prop and why is it critical in lists?

💡

Hint

Key lets React identify which item changed, was added, or removed — must be stable and unique

Full Answer

The key prop is React's mechanism to identify list items across renders. Without stable keys, React can't efficiently reconcile list changes.

// ❌ Using index as key — problematic when list reorders or items are inserted
{items.map((item, index) => (
  
))}

// ✅ Use a stable unique ID from the data
{items.map(item => (
  
))}

Why index keys fail:

// Initial:  [A(key=0), B(key=1), C(key=2)]
// Delete A: [B(key=0), C(key=1)]
// React sees key=0 CHANGED (not deleted) → updates B in place
// Key=2 REMOVED → destroys C
// Result: input state, focus, animations on wrong elements

When index keys are safe:

  • The list never reorders
  • Items are never inserted or deleted from the middle
  • Items have no state (text-only renders)
💡 Keys must be unique among siblings, not globally. Keys don't get passed as props — if you need the ID in the component, pass it explicitly as a separate prop.

More React Fundamentals Questions

EasyWhat is React and what problem does it solve?EasyWhat is JSX and how does it work under the hood?EasyWhat is the Virtual DOM and how does reconciliation work?EasyWhat is the difference between a controlled and uncontrolled component?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint