Skip to content

Lesson 6 of 9 · 26m

Virtualization and Large Lists: Render the Window, Not the World

Choose when to virtualize, implement windowing correctly with dynamic rows, and avoid the accessibility and measurement traps that come with fake DOM height.

A product once shipped a customer table that rendered every row "because React is fast." It was fast with fifty demo rows. A real tenant imported 30,000 accounts and the tab froze before paint. The answer was not cleverer rendering; it was not rendering most of the list. Virtualization is a lie told carefully: the DOM contains a window of rows, while spacer geometry preserves the illusion of the full list.

First decide whether the list should exist

Before reaching for a library, ask whether infinite scale is the right UX. Tables often need server pagination, filtering, grouping, or search rather than a longer scroll. Virtualization fits continuous scanning: logs, diffs, chat history, dense operational tables, and canvas-like feeds.

Understand the core mechanics

A virtualizer tracks scroll position, estimates item positions, renders a slice, and maintains total height:

import {useVirtualizer} from '@tanstack/react-virtual'

function TicketList({tickets}: {tickets: Ticket[]}) {
  const parentRef = useRef<HTMLDivElement>(null)
  const virtualizer = useVirtualizer({
    count: tickets.length,
    getScrollElement: () => parentRef.current,
    estimateSize: () => 56,
    overscan: 8,
  })

  return <div ref={parentRef} style={{height: 600, overflow: 'auto'}}>
    <div style={{height: virtualizer.getTotalSize(), position: 'relative'}}>
      {virtualizer.getVirtualItems().map(item => (
        <div key={item.key} data-index={item.index} ref={virtualizer.measureElement}
             style={{position: 'absolute', transform: `translateY(${item.start}px)`, width: '100%'}}>
          <TicketRow ticket={tickets[item.index]} />
        </div>
      ))}
    </div>
  </div>
}

The important details are estimateSize, measureElement, and overscan. Fixed-height rows are easy; dynamic-height rows require measurement as content resolves.

Overscan is a tradeoff

Overscan renders rows outside the viewport to reduce blank flashes during fast scrolling. More overscan costs more DOM work; less risks visual gaps. Tune it with real scrolling on throttled hardware.

Rows need stable keys

Do not use array index as a key when rows reorder, insert, or delete:

rows.map((row, index) => <Row key={index} row={row} />) // risky
rows.map(row => <Row key={row.id} row={row} />)          // stable

Rows constantly mount and unmount. Focus state, expanded rows, input drafts, and media loading all depend on correct identity.

Accessibility does not virtualize itself

A screen reader cannot navigate absent rows. Browser find-in-page cannot find unmounted content. Keyboard focus can be destroyed when its row unmounts. You may need roving focus, explicit paging controls, aria row metadata, or a downloadable representation.

Sometimes pagination is faster and better

Pagination reduces DOM size, memory, data fetching scope, and find confusion. It also gives users stable locations. Virtualization preserves flow but makes position harder to reason about. Choose deliberately.

Field notes

  • Virtualization fixes DOM scale, not expensive row rendering; profile row self time separately.
  • Dynamic images and expanding rows invalidate measurements unless real sizes are observed.
  • Large data still costs memory even when rows are unmounted; consider windowed data fetching.
  • If users mostly need the latest twenty records, do not build a cathedral for record 20,000.

Was this lesson helpful?

Sign in to rate