In this lesson you will
- Turn an array of data into elements with map
- Choose stable, unique keys and explain why they matter
- Filter and sort lists derived from state without changing it
- Render parts of the screen conditionally and avoid the 0 trap
Rendering an array with map
JSX can show an array of elements directly, so the job is to turn your data into one. The array method map does exactly that: it calls a function for every item and collects what it returns. Put it inside braces where the list should appear: <ul>{products.map(...)}</ul>, where each item becomes <li key={p.id}>{p.name}</li>. The function can return any JSX, including your own components, <ProductRow key={p.id} product={p} />. When the arrow function body is long, use braces and an explicit return, or move the item into its own component so the list stays easy to read.
A common slip is forgetting that an arrow function with braces needs return. p => { <li>{p.name}</li> } returns nothing, so the list is empty with no error. Write p => <li>...</li> or p => { return <li>...</li>; }. Another is calling forEach instead of map: forEach always returns undefined, so nothing is shown. Data usually comes in a shape you control, such as objects with id, name and price, and the rest of this lesson builds on that shape.
Console
- Console messages, errors, form data and clicked links appear here.
Add a fifth dish to the array and it appears with no other change. The key goes on <MenuItem>, the element returned by map, not on the <li> inside it. Delete key={item.id} and React prints a warning about a missing "key" prop in the Console.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
Why does React warn that each child in a list should have a unique key?
Because an element returned from map has no key, or two siblings share one. Without keys React matches items by position, which breaks when the list changes. Add a stable id from your data as the key.
Is it ever fine to use the array index as a key?
Yes, for a static list that is never reordered, filtered or edited, such as fixed steps in a guide. For anything users can change, use a real id.
Can two different lists on one page use the same keys?
Yes. Keys only need to be unique among siblings in the same array. A product list and an order list can both have an item with the key "1".
How do I render a table from an array in React?
The same way: put rows.map(r => <tr key={r.id}>...</tr>) inside <tbody>, and inside each row map the columns to <td> elements, each with its own key if they also come from an array.
How do I return several elements per item without a wrapper div?
Use Fragment with a key: import { Fragment } from "react", then <Fragment key={item.id}>...</Fragment>. The short <>...</> form cannot take a key.
Sources
Facts in this lesson were checked against these official pages on 22 November 2026.
- React docs: Rendering Lists
- React docs: Conditional Rendering
- React docs: Preserving and Resetting State
- MDN: Array.prototype.toSorted()
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.