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
- Prepare the data: filter, sort (with
toSorted()to avoid mutating state), group. - Map each item to JSX.
- Put
key={item.id}on the outermost element of each item. - 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: autocan 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 };23function TaskRow({ task }: { task: Task }) {4 return (5 <li className={task.done ? "line-through" : ""}>6 {task.title} (P{task.priority})7 </li>8 );9}1011export function TaskList({ tasks, showDone }: { tasks: Task[]; showDone: boolean }) {12 const visible = tasks13 .filter((t) => showDone || !t.done)14 .toSorted((a, b) => a.priority - b.priority); // does not mutate props1516 if (visible.length === 0) return <p>Nothing to do 🎉</p>;1718 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.