Skip to content

React Lists and Keys: Render Arrays with map, Choose Good Keys, Show Things Conditionally

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Almost every screen shows a list: products in a shop, lines in a cart, messages in a chat, rows in a report. In React you do not write a loop that appends elements; you turn an array of data into an array of JSX with map, and React draws it. Each item needs a key so React can tell items apart when the list changes. Screens also change shape with the data: an empty cart shows a message instead of a table, a sold-out product shows a badge, an error replaces a form. This lesson covers both: rendering lists with good keys, and the three ways to render something only when a condition is true.

Quick answer

Render a list by mapping the array to JSX inside curly braces: {list.map(x => <li key={x.id}>{x.name}</li>)}. Give each element a key that is unique among its siblings and stays the same for the same item, usually an id from your data; avoid the array index when items can be added, removed or reordered. For conditions, use a normal if above the return to choose what to show, a ternary cond ? <A /> : <B /> inside JSX for either-or, and cond && <A /> for show-or-nothing, making sure cond is a real boolean so a 0 is not printed.

35 min read and practice · Content checked on 22 November 2026

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.

Try it: A menu rendered from an array
Code
ResultUpdates as you type
Console
  1. 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.