ReactHard

Optimized list rendering

To render large lists efficiently: give every row a stable key, keep row components cheap and memoized (or rely on the React Compiler) with stable props, avoid passing new objects or callbacks per row, keep fast-changing state out of the list's parent, defer expensive filtering with useDeferredValue, and virtualize so only visible rows are in the DOM. Measure with the Profiler before and after.

Techniques

  1. Stable keys: IDs, never indexes or random values.
  2. Memoized rows: memo(Row) so unchanged rows skip rendering when one row changes. Pass primitives or stable references; use one stable callback that takes an ID instead of a new arrow per row.
  3. Normalize data: pass item objects whose references only change when that item changes (immutable updates do this naturally).
  4. Colocate state: a row's hover or edit state lives in the row, not the parent.
  5. Defer expensive work: useDeferredValue for filter text, useMemo for sorted or filtered arrays.
  6. Virtualize: TanStack Virtual or react-window render only the visible window plus overscan, keeping DOM size constant for 10,000+ items.
  7. CSS containment: content-visibility: auto with contain-intrinsic-size lets the browser skip offscreen rendering for moderately long lists without virtualization.
  8. Pagination or infinite loading to avoid fetching everything.
  9. Avoid layout thrash: fixed or estimated row heights for virtualization.

React Compiler

With it enabled, memoization of rows and callbacks happens automatically; virtualization is still needed for very large lists, because the cost is DOM size, not React rendering.

Code example

tsx
1import { memo, useCallback, useDeferredValue, useMemo, useRef, useState } from "react";
2import { useVirtualizer } from "@tanstack/react-virtual";
3
4type Item = { id: string; name: string; done: boolean };
5
6const Row = memo(function Row({ item, onToggle }: { item: Item; onToggle: (id: string) => void }) {
7 return (
8 <label style={{ display: "flex", gap: 8, height: 36, alignItems: "center" }}>
9 <input type="checkbox" checked={item.done} onChange={() => onToggle(item.id)} /> {item.name}
10 </label>
11 );
12});
13
14export function BigList({ initial }: { initial: Item[] }) {
15 const [items, setItems] = useState(initial);
16 const [query, setQuery] = useState("");
17 const deferredQuery = useDeferredValue(query);
18 const parentRef = useRef<HTMLDivElement>(null);
19
20 const visible = useMemo(() => items.filter((i) => i.name.toLowerCase().includes(deferredQuery.toLowerCase())), [items, deferredQuery]);
21 const onToggle = useCallback((id: string) => setItems((prev) => prev.map((i) => (i.id === id ? { ...i, done: !i.done } : i))), []);
22
23 const virtualizer = useVirtualizer({ count: visible.length, getScrollElement: () => parentRef.current, estimateSize: () => 36, overscan: 8 });
24
25 return (
26 <div>
27 <input value={query} onChange={(e) => setQuery(e.target.value)} placeholder={`Filter ${items.length} items`} />
28 <div ref={parentRef} style={{ height: 400, overflow: "auto" }}>
29 <div style={{ height: virtualizer.getTotalSize(), position: "relative" }}>
30 {virtualizer.getVirtualItems().map((v) => (
31 <div key={visible[v.index].id} style={{ position: "absolute", top: 0, left: 0, right: 0, transform: `translateY(${v.start}px)` }}>
32 <Row item={visible[v.index]} onToggle={onToggle} />
33 </div>
34 ))}
35 </div>
36 </div>
37 </div>
38 );
39}

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