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

The Key Prop Requirement

Why React requires a unique 'key' prop on list items and what happens when you omit it.

The Key Prop Requirement

What you'll learn: Why React demands a unique key prop on list items and what breaks when you skip it.

When you use .map() to render lists in React, you'll notice the browser console often screams with a warning: "Each child in a list should have a unique 'key' prop." This isn't just React being picky—it's protecting your app from subtle bugs.

Why React Needs Keys

React uses keys to track which items in a list are which. Without them, React can't tell if you've added, removed, or reordered items—it just sees "an array of components" with no identity.

The analogy: Imagine a classroom where every student is named "Student." When the teacher calls roll after someone leaves, chaos ensues. Keys are like name tags—they let React identify who's who even when the list changes.

What Happens When You Omit Keys

Without keys, React falls back to using array indices as implicit keys. This causes problems:

  • Wrong components update: When you reorder items, React might update the wrong DOM nodes
  • State confusion: Component state (like form inputs) can attach to the wrong items
  • Performance hits: React can't optimize efficiently
// ❌ Missing keys - you'll see a warning
{users.map(user => <UserCard name={user.name} />)}

// ✅ With keys - React can track each item
{users.map(user => <UserCard key={user.id} name={user.name} />)}

The key must be unique among siblings and stable (doesn't change between renders). Usually, you'll use IDs from your data. We'll cover what makes a good key in the next lesson.

Key Takeaway: The key prop isn't optional decoration—it's how React maintains the identity and state of list items across renders. Always provide a stable, unique key when mapping arrays.