ReactEasy

What is list rendering?

List rendering means turning an array of data into an array of elements, usually with .map(), and giving each element a stable, unique key. Combine it with .filter(), sorting and grouping to shape the data before rendering.

Pattern

  1. Prepare the data: filter, sort (with toSorted() to avoid mutating state), group.
  2. Map each item to JSX.
  3. Put key={item.id} on the outermost element of each item.
  4. Handle the empty case.

Keys recap

  • Use IDs from the data, not indexes, when items can be added, removed or reordered.
  • Keys must be unique among siblings.
  • Use <Fragment key> when each item renders several siblings.

Performance for large lists

  • Rendering thousands of DOM nodes is slow. Virtualize with TanStack Virtual or react-window, rendering only visible rows.
  • Paginate or use infinite scroll.
  • Memoize row components when parent updates are frequent (or rely on the React Compiler).
  • CSS content-visibility: auto can skip rendering offscreen content.

Avoid

  • Defining the row component inside the parent (remounts every render).
  • Doing expensive work per item on every render; precompute or memoize.
  • Mutating the source array with .sort() or .reverse().

Code example

tsx
1type Task = { id: string; title: string; done: boolean; priority: number };
2
3function TaskRow({ task }: { task: Task }) {
4 return (
5 <li className={task.done ? "line-through" : ""}>
6 {task.title} (P{task.priority})
7 </li>
8 );
9}
10
11export function TaskList({ tasks, showDone }: { tasks: Task[]; showDone: boolean }) {
12 const visible = tasks
13 .filter((t) => showDone || !t.done)
14 .toSorted((a, b) => a.priority - b.priority); // does not mutate props
15
16 if (visible.length === 0) return <p>Nothing to do 🎉</p>;
17
18 return (
19 <ul>
20 {visible.map((task) => (
21 <TaskRow key={task.id} task={task} />
22 ))}
23 </ul>
24 );
25}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app