Lists & Keys
Learn how to render arrays in React with map, why the key prop matters, and what goes wrong when you use the array index as a key.
On this page
1. What are lists and keys?
Data often lives in an array: products, users, comments. map() turns each item into JSX. A key is like a small name tag that tells React which JSX belongs to which item.
2. Why does it matter?
| No key or a wrong key | A stable unique key |
|---|---|
| React identifies items only by position | React identifies each item by its id |
| When an item is deleted, state moves to the wrong row | State stays with the right item |
| A key warning in the console | No warnings |
| More DOM work | Only the changed item is updated |
3. Where is it used?
| Use case | What the key should be |
|---|---|
| Products grid | product.id |
| Chat messages | message.id |
| Todo list | crypto.randomUUID() when the todo is created |
| Table rows | The database primary key |
| Static menu | A unique href or slug |
4. How does it work?
A key only needs to be unique among siblings, not across the whole app. The key is not passed to the component as a prop, it is only for React.
5. Code example
Basic list
const products = [
{ id: "p1", name: "Keyboard", price: 4500 },
{ id: "p2", name: "Mouse", price: 1800 },
{ id: "p3", name: "Monitor", price: 32000 },
];
export default function ProductList() {
return (
<ul>
{products.map((product) => (
<li key={product.id}>
{product.name}: Rs. {product.price}
</li>
))}
</ul>
);
}Filter, then map
function CheapProducts({ products }) {
const cheap = products.filter((p) => p.price < 5000);
if (cheap.length === 0) {
return <p>No cheap products found.</p>;
}
return (
<ul>
{cheap.map((p) => (
<ProductRow key={p.id} product={p} />
))}
</ul>
);
}How does the code work?
| Part | Meaning |
|---|---|
products.map(...) | Each object becomes one li |
key={product.id} | A stable unique id that comes from the data |
filter then map | Filter first, then render |
<ProductRow key=... /> | The key goes on the component, not on the li inside it |
| Empty check | Shows a message when the list is empty |
6. Common mistakes
Mistake: Using the array index as a key when the list can be reordered, deleted from or inserted into
Why? When an item is removed, the indexes shift, and React gives the old state and input value to the wrong item.
Better: Use the data's own id. The index is only fine when the list never changes.
Mistake: Creating keys with Math.random() or randomUUID() during render
Why? Every render produces a new key, so React treats every item as new and rebuilds the DOM and state.
Better: Generate the id once when the data is created and store it with the item.
Mistake: Putting the key on an element inside the component
Why? React needs the key on the outermost element returned from map. Putting it inside causes a warning.
Better: Put the key where the map callback returns JSX.
Interview tip
Answer "Why is index as key bad?" with an example: in a list of inputs, delete an item in the middle and the typed text shifts to the wrong row. This is a reconciliation problem.
7. What to learn before and after
| Stage | Chapters |
|---|---|
| Before | Conditional Rendering, Props |
| Alongside | How React Renders |
| After | Forms & Controlled Inputs, React.memo & Performance |
Final quick revision
| Point | Detail |
|---|---|
| Render a list | array.map() |
key | Unique and stable among siblings |
| Best key | The id from your data |
| Index as key | Only for static lists |
| Random key | Never |