← Back to React, page 3

react-0050 · Hard senior

How do you render a very large list efficiently in React, and how does virtualization (windowing) work?

Virtualization renders only the rows that are visible in the scroll container, plus a few extra, instead of all of them. It keeps the DOM small by calculating which items fall in the viewport from the scroll position and the row height, and by placing them with padding or absolute positioning inside a container as tall as the full list. Libraries such as TanStack Virtual and react-window handle the details, including variable heights.

Full answer

Rendering 10,000 rows creates 10,000 DOM nodes plus the work for React to create and update them. Mounting is slow, memory use is high, and every update that touches the list costs more. Most of those rows are off-screen.

Virtualization (or windowing) fixes this. The container has a fixed height and overflow: auto. Inside it, an element is given the height of the entire list, which is the item count times the row height, so the scrollbar is the right size. The component reads scrollTop, calculates the first visible index as Math.floor(scrollTop / rowHeight) and the last from the container height, adds an overscan of a few rows to avoid blank flashes, and renders only that slice. Each row is positioned at index * rowHeight, with absolute positioning or a transform.

With fixed heights the maths is trivial. Variable heights require measuring rows as they render and keeping a cache of sizes, which is what libraries do. Grids need the same idea in two dimensions.

Use a library in production. TanStack Virtual (a hook with no markup) and react-window are common. Hand-written versions miss keyboard focus, find-in-page, accessibility semantics, dynamic sizes and scroll restoration.

Trade-offs to mention. Browser find-in-page cannot find text that is not in the DOM. Screen readers need proper roles and counts, for example aria-rowcount. Items need stable keys so recycled rows keep the correct state.

Alternatives for lighter cases: pagination, infinite scroll with a limit, or the CSS property content-visibility: auto, which lets the browser skip rendering off-screen content, though it still keeps all nodes.

The example is a fixed-height windowed list of 10,000 rows, written by hand to show the arithmetic.

jsx
import { useState } from 'react';

const ROW_HEIGHT = 30;
const VIEWPORT_HEIGHT = 120;
const OVERSCAN = 2;
const rows = Array.from({ length: 10000 }, (_, i) => 'Student ' + (i + 1));

export default function VirtualList() {
  const [scrollTop, setScrollTop] = useState(0);

  const first = Math.max(0, Math.floor(scrollTop / ROW_HEIGHT) - OVERSCAN);
  const last = Math.min(
    rows.length - 1,
    Math.ceil((scrollTop + VIEWPORT_HEIGHT) / ROW_HEIGHT) + OVERSCAN
  );

  const visible = [];
  for (let index = first; index <= last; index++) {
    visible.push(
      <div
        key={index}
        role="listitem"
        style={{ position: 'absolute', top: index * ROW_HEIGHT, height: ROW_HEIGHT }}
      >
        {rows[index]}
      </div>
    );
  }

  return (
    <div
      role="list"
      style={{ height: VIEWPORT_HEIGHT, overflow: 'auto', position: 'relative' }}
      onScroll={(event) => setScrollTop(event.currentTarget.scrollTop)}
    >
      <div style={{ height: rows.length * ROW_HEIGHT }}>{visible}</div>
    </div>
  );
}

Only the rows near the viewport are in the DOM, whatever the scroll position. The check scrolled to 3000 pixels, which is the row at index 100 (Student 101) with a row height of 30. With an overscan of two rows the DOM starts at Student 99. Output of the run (jsdom does no layout, so the scroll position was set by the check): at the top: 7 rows in the DOM, first is Student 1 after scrolling to 3000px: 9 rows in the DOM, first is Student 99

Key points

  • Virtualization renders only visible rows plus overscan, keeping the DOM small.
  • The container has the full list height. Row positions come from index times row height.
  • Variable heights need measurement and a size cache, which libraries provide.
  • Use TanStack Virtual or react-window in production, and handle accessibility and find-in-page limits.
  • content-visibility and pagination are lighter alternatives.

Common mistakes

  • Rendering all rows and trying to fix the slowness with memo alone.
  • Using the index as a key when the underlying data can reorder.
  • Forgetting overscan, which shows blank space during fast scrolling.
  • Hand-writing dynamic heights instead of using a library.
  • Ignoring accessibility roles and the loss of find-in-page.

Follow-ups

  • How would you support rows with variable height?
  • How would you keep keyboard focus on a row that scrolls out of the window?
  • When would pagination or content-visibility be better than virtualization?

Tips

  • Show the arithmetic (scrollTop divided by row height). It proves you understand the mechanism.
  • Recommend a library for production and name two.
  • Mention find-in-page and accessibility trade-offs. Seniors are expected to know them.