Lists and Keys
Learn how to render arrays of data with .map() in React and why a stable, unique key prop is required for every list item.
Introduction
A huge amount of UI is really just a list rendered over and over — a list of products, comments, todo items, or search results. React makes this easy by letting you use plain JavaScript array methods, especially .map(), directly inside JSX to turn an array of data into an array of elements.
But rendering lists comes with one rule you cannot skip: every item needs a special key prop. In this lesson you will learn how to render arrays with .map(), why React insists on keys, how to pick a good one, and what the console warning about missing keys actually means.
- How to transform an array of data into JSX using .map().
- Why the key prop is required on every item in a rendered list.
- How keys help React track which items changed, were added, or were removed.
- How to choose a stable, unique key — and why the array index is usually a poor choice.
- What the "missing key" console warning means and how to fix it.
Rendering an Array with .map()
To render a list in React, you call .map() on your array of data and return a piece of JSX for each item. The result is an array of elements, which React is happy to render directly inside your JSX.
function FruitList() {
const fruits = ["Apple", "Banana", "Mango"];
return (
<ul>
{fruits.map((fruit) => (
<li key={fruit}>{fruit}</li>
))}
</ul>
);
}- Apple
- Banana
- MangoThis same pattern works for arrays of objects too, which is far more common in real applications — you just pull out the properties you need inside the .map() callback.
function UserList() {
const users = [
{ id: "u1", name: "Amaya" },
{ id: "u2", name: "Kenji" },
{ id: "u3", name: "Priya" },
];
return (
<ul>
{users.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}- Amaya
- Kenji
- PriyaWhy Keys Are Required
When a list re-renders — an item is added, removed, or reordered — React needs a way to match up each element in the new list with the corresponding element in the previous list, so it knows which parts of the real DOM actually need to change. The key prop is exactly that: a stable identity for each item across renders.
Without keys, React has to guess based on position alone, which can lead to mismatched DOM nodes, lost input state, or unnecessary re-rendering of items that did not actually change.
Stable Identity
A key tells React "this is the same logical item" across different renders.
Tracks Additions
React can tell a brand-new key apart from ones it has already seen.
Tracks Removals
When a key disappears from the list, React knows to remove that exact element.
Tracks Reordering
If items with the same keys just change position, React moves the existing elements instead of recreating them.
The key prop is used by React itself, behind the scenes, to manage the list. It is not passed down to your component as a regular prop, so you cannot read it via props.key inside the component.
Choosing a Good Key
The best key is a value that is both unique among siblings and stable across re-renders — typically an ID that already exists in your data, like a database ID or a unique field from your API response.
const todos = [
{ id: "t1", text: "Buy milk" },
{ id: "t2", text: "Walk the dog" },
];
// Good: a stable, unique ID from the data itself
todos.map((todo) => <li key={todo.id}>{todo.text}</li>);It is tempting to just use the array index (map((item, index) => ...)) as the key, and it works fine for a static list that never reorders, gets filtered, or has items inserted or removed. But once the list changes shape, the index for a given item can shift, which breaks the whole point of a stable key.
// Risky if this list can be reordered, filtered, or have items removed
todos.map((todo, index) => <li key={index}>{todo.text}</li>);If you remove the first item from a list keyed by index, every remaining item shifts down one index. React then thinks the item at index 0 changed its content, rather than realizing the whole item was removed — this can cause stale input values, broken animations, or incorrect state tied to the wrong item.
The Missing Key Warning
If you render a list without a key prop, React does not throw an error — the list still displays — but it logs a console warning to let you know something important is missing.
function ColorList() {
const colors = ["Red", "Green", "Blue"];
return (
<ul>
{colors.map((color) => (
<li>{color}</li> // no key prop here
))}
</ul>
);
}Warning: Each child in a list should have a unique "key" prop.
Check the render method of `ColorList`.The fix is simple — add a unique key to the outermost element returned inside the .map() callback.
function ColorList() {
const colors = ["Red", "Green", "Blue"];
return (
<ul>
{colors.map((color) => (
<li key={color}>{color}</li>
))}
</ul>
);
}Common Mistakes
- Forgetting the key prop entirely and seeing the console warning, then ignoring it.
- Using the array index as a key for a list that can be reordered, filtered, or have items added or removed.
- Putting the key on the wrong element — it must go on the outermost element returned from the .map() callback.
- Generating a new random key on every render (e.g. with Math.random()), which defeats the purpose of a stable key.
Best Practices
- Prefer a stable, unique field from your data (like an id) as the key over the array index.
- Only use the array index as a key when the list is static and will never reorder, filter, or change length.
- Place the key prop directly on the element returned by .map(), not on a child inside it.
- Treat key as invisible to your component logic — never try to read it via props.
- Fix "missing key" console warnings right away; they usually point to a real, if subtle, bug.
Frequently Asked Questions
Do keys need to be globally unique across the whole app?
No, keys only need to be unique among siblings in the same array — the same key value can be reused in a completely different list elsewhere in your app.
Can I use the key prop for anything inside my component, like styling?
No, key is reserved for React's internal bookkeeping and is not passed to your component as a prop, so you cannot access it via props.key.
Is it ever fine to use the index as a key?
Yes, for a list that is guaranteed to stay in the same order and never has items added, removed, or filtered, using the index is harmless.
What happens if two siblings accidentally share the same key?
React will warn you in the console about duplicate keys, and list rendering can behave unpredictably since React can no longer tell the items apart.
Does adding a key make my list render faster?
Keys make React's reconciliation more accurate rather than simply "faster" — the real benefit is correctness: less unnecessary re-rendering and no mismatched DOM state.
Key Takeaways
- Use .map() to turn an array of data into an array of JSX elements.
- Every element in a rendered list needs a unique key prop among its siblings.
- Keys let React efficiently track which items changed, were added, or were removed.
- Prefer a stable ID from your data over the array index, especially for lists that can change.
- A missing key triggers a console warning and can lead to subtle rendering bugs.
Summary
Rendering lists is one of the most frequent tasks in any React app, and .map() combined with a well-chosen key makes it both simple and reliable. Keys are the small detail that lets React efficiently and correctly update lists as your data changes.
In this lesson, you learned how to render arrays with .map(), why the key prop matters, how to choose a stable and unique key, and what the missing-key warning is telling you. Next, you will learn how to build forms in React and handle their submission.
- You can render arrays of data using .map().
- You understand why React requires a key prop on list items.
- You know how to choose a stable, unique key over the array index.
- You can recognize and fix the missing-key console warning.