# Lists & Keys

> How to render arrays of data into components, and why React demands a unique key for each item.

Subject: React | Lesson 12 of 48 | 3 min read
Interactive lesson: https://codewithanimation.in/react/lists-and-keys
Previous: [Conditional Rendering](https://codewithanimation.in/react/conditional-rendering.md) | Next: [The useEffect Hook](https://codewithanimation.in/react/useeffect-basics.md)

## What it is

To render a list of items, you use the standard JavaScript `Array.map()` function to transform an array of data into an array of JSX elements.

React requires you to provide a special `key` prop to the outermost element returned by the `.map()` function. This key must be a unique string or number that uniquely identifies that item among its siblings.

## Why keys matter

When a list changes (items are added, removed, or reordered), React needs to figure out exactly what changed so it can update the DOM efficiently.

Without keys, React doesn't know if an item was deleted or just moved. It assumes the array order represents the items' identities. Keys give React a stable identity for each element across renders.

## How to do it

If you have an array `const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}]`.

You render it like this: `users.map(user => <Profile key={user.id} name={user.name} />)`. Always use a unique database ID if you have one.

## Common mistake: The index-as-key trap

If you don't have a unique ID, it's tempting to use the array index: `.map((user, index) => <Profile key={index} />)`.

Never do this if the list can be reordered, filtered, or sorted! If you delete the first item, the second item becomes index 0. React will think the second item is actually the first item, and it will give it the first item's state (like text typed into an input). This causes bizarre UI bugs.

## Tip: Keys don't get passed as props

React uses the `key` internally for reconciliation. If you try to access `props.key` inside your component, it will be `undefined`. If you need the ID inside the component, pass it again: `<Profile key={user.id} id={user.id} />`.

## Try it: The Index Bug

Type something into the first item's input, then shuffle the list. Notice how the input text stays locked to the position (the index) rather than following the item it belongs to!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/lists-and-keys

## Check yourself

### Question 1

Which JavaScript array method is heavily used in React to render lists?

- A. Array.forEach()
- B. Array.map()
- C. Array.reduce()

Correct answer: B

You use `.map()` to transform an array of raw data objects into an array of React components.

- Why not A: `forEach` does not return a new array. You need a method that returns an array of JSX elements.
- Why not C: While technically possible, `.map()` is the idiomatic standard for lists.

### Question 2

Why does React require a `key` prop when rendering lists?

- A. To give the element a unique CSS ID for styling.
- B. To help React identify which items have changed, are added, or are removed.
- C. To ensure the array is sorted correctly before rendering.

Correct answer: B

Keys give elements a stable identity across renders, allowing React to efficiently reuse DOM nodes instead of destroying and recreating them.

- Why not A: The `key` is purely for React's internal diffing algorithm; it does not appear in the DOM.
- Why not C: Keys have nothing to do with sorting.

### Question 3

Why is it dangerous to use the array `index` as a key (e.g. `key={index}`) if the list can be reordered?

- A. Because React will throw a fatal error and crash the app.
- B. Because if the array is reordered, the indices change. React will mix up the elements and they might inherit the wrong state.
- C. Because indices are strings, and keys must be numbers.

Correct answer: B

If item B moves from index 1 to index 0, React thinks it is the exact same component as item A (which used to be index 0). It will reuse item A's state for item B, causing massive bugs.

- Why not A: React allows it, but warns against it. It won't crash.
- Why not C: Keys can be strings or numbers. Indices are actually numbers.

## Remember this

- Use `Array.map()` to render lists of elements.
- Every element immediately returned by `.map()` requires a unique `key` prop.
- The key should ideally be a unique ID from your data (like a database ID).
- Do not use the array `index` as a key if the list can ever be sorted, filtered, or reordered.
