How to Render a List in React with map and key

Turning arrays into elements in React and picking the right key.

When building React components you'll almost always need to show a list of something, like todos, products or comments. React doesn't have a special loop syntax for this. You use the array's map method right inside your JSX.

Rendering a List with map

map turns each item in an array into something else. In React, that something else is an element:

const todos = ["Buy milk", "Walk the dog", "Write a blog post"]
const TodoList = () => (
<ul>
{todos.map((todo) => (
<li key={todo}>{todo}</li>
))}
</ul>
)

map returns an array of <li> elements, and React renders an array of elements the same as if you had written them out one by one.

Why React Needs a key

If you leave off the key prop, React will warn you in the console:

Warning: Each child in a list should have a unique "key" prop.

When a list changes, React uses the key to figure out which items were added, removed or moved. Without one, it has to guess, which can cause it to update the wrong elements.

A key should be a value that's unique within the list and stays the same for each item every time the component renders.

Using an id as the key

Most data from an API or database already has an id, which makes a great key:

const TodoList = ({ todos }) => (
<ul>
{todos.map((todo) => (
<li key={todo.id}>
<input type="checkbox" checked={todo.done} readOnly />
{todo.title}
</li>
))}
</ul>
)

The key goes on the outermost element returned from map, not on something nested inside it.

Why Not Use the Index?

map gives you the index as its second argument, so it's tempting to use that:

<ul>
{todos.map((todo, index) => (
<li key={index}>{todo.title}</li>
))}
</ul>

This works fine for a list that never changes. But if items get added to the top, removed or sorted, the index of each item changes. React then thinks the item at index 0 is the same item it was before, even when it's a completely different todo. This shows up as inputs keeping the wrong value or checkboxes staying checked on the wrong item.

Use the index only when the list is static and the items have nothing unique to use instead.

Lists of Components

The same idea works when each item is its own component. The key goes on the component:

const TodoList = ({ todos }) => (
<ul>
{todos.map((todo) => (
<TodoItem key={todo.id} todo={todo} />
))}
</ul>
)

Note that key isn't passed down as a prop. If TodoItem needs the id, pass it separately, like id={todo.id}.

Keys on Fragments

If each item needs to render more than one element without a wrapper, use React.Fragment with a key. The short <> syntax doesn't accept a key:

import React from "react"
const Glossary = ({ terms }) => (
<dl>
{terms.map((term) => (
<React.Fragment key={term.id}>
<dt>{term.word}</dt>
<dd>{term.definition}</dd>
</React.Fragment>
))}
</dl>
)

As long as each item has a stable key on its outer element, React can keep track of the list as it changes.

Have some feedback on this post? .