Skip to content

Key

In one line

A key is a special string prop that uniquely identifies a list item, helping React match elements across re-renders for efficient updates.

In simple words

The key prop is a special attribute React uses to identify which items in a list have changed. When React reconciles a list, it uses keys to match old elements to new ones. If a key exists in both the old and new list, React updates that element in place; if a key is new, React mounts a new element; if a key is gone, React unmounts the element.

Always use stable, unique keys — the best choice is a unique ID from your data (a database ID, a UUID). Avoid using array indices as keys when the list can be reordered, items can be inserted at the beginning, or items can be deleted — index-based keys cause React to incorrectly reuse DOM nodes, leading to subtle state bugs.

Keys only need to be unique among siblings, not globally. They are not passed to the component as a prop — React consumes them internally.

Example

jsx
// Good: stable unique IDs
{items.map(item => <ListItem key={item.id} item={item} />)}

// Bad: index keys (causes bugs on reorder)
{items.map((item, index) => <ListItem key={index} item={item} />)}

Using item.id as the key is stable and unique. Using the array index can cause incorrect DOM reuse when items move or are removed.

Common confusions

  • Confused with: id prop

    The difference: key is a React-internal prop used for reconciliation and is not passed to the component. id is a regular HTML attribute that ends up on the DOM element. They serve different purposes.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary