LearnThatStack Ace your next interview
React · question
Question 9 of 103

How do you render lists in React?

beginner
← All React questions
Re-explain

Call map() on the array and return a JSX element for each item. Give each element a key prop that is unique among its siblings.

function TodoList({ todos }) {
  return (
    <ul>
      {todos.map(todo => (
        <li key={todo.id}>
          {todo.text}
        </li>
      ))}
    </ul>
  );
}

The key tells React which item each element belongs to. When the list changes, React uses the keys to see which items were added, removed or changed. It then updates only those and keeps each item's state, which improves performance during re-renders.

Use a stable ID from your data as the key. An array index breaks when items are inserted, removed or reordered, and a random key makes React recreate every item on each render.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for React One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Why is the array index a bad key, and when is it fine to use one? Show the bug. State sticks to position, so after an insert or reorder an item shows the wrong values. Index is fine for a static list that never reorders or filters.
  • Your data has no ID field. Where should the key come from? Assign an ID once when the item is created or loaded, or build one from fields that are unique together. Never make it up during render.
  • How would you render a list of 10,000 rows without making the page slow? Name virtualization, which renders only the rows on screen, plus memoized rows. Pagination or infinite scroll are fair alternatives.
  • Each item needs to render two sibling elements with no wrapper. How do you key that? Use <Fragment key={id}> around the pair. The short <></> syntax can't take a key.
  • How can you use key on purpose to reset a component's state? Changing a component's key makes React unmount the old one and mount a new one with fresh state, for example key={userId} on a profile form.

What you can say

  1. You render a list by calling map() on the array and returning a JSX element for each item, and each element gets a key prop that's unique among its siblings.
  2. For a todo list, I'd map over todos inside a ul and return an li for each one, with the key set to todo.id.
  3. The key tells React which item each element belongs to, so when the list changes it can see what was added, removed or changed.
  4. React then updates only those items and keeps each item's state, which saves work on re-renders.
  5. I'd use a stable ID from the data, because an index breaks when items are inserted, removed or reordered.
  6. A random key is worse, since React would recreate every item on every render.

Weak answers to avoid

  • Uses the array index as the key by default Index keys tie state to position, so after an insert or reorder an input can show another item's text. Say you use a stable ID from the data.
  • Generates keys with Math.random() during render A new key on every render makes React throw away and recreate every item, so state and focus are lost. Keys must stay the same between renders.
  • Says keys only exist to silence a console warning That misses the point. Explain that keys let React match old and new items, so it updates only what changed and keeps each item's state.
  • Puts the key on an inner element, not the mapped one React reads the key on the element returned from map(). A key on a nested child doesn't identify the list item, and the warning stays.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the React cheatsheet.

← Back to all React questions
Pro · $10/mo

88 of 103 React answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime