Virtualize Long React Lists Without Breaking UX
A practical guide to React list virtualization: when it helps, how to preserve stable identity and scrolling, and what to test before shipping.
A table that feels fine with 80 rows can become sluggish when a customer opens a report with 20,000. The browser has to create, style, lay out, and often update every rendered row—even when most are nowhere near the screen. Virtualization reduces that work by rendering only the visible items and a small buffer around them. It can make a large interface responsive, but it also changes the relationship between the data and the DOM. That is where selection bugs, jumpy scrolling, and keyboard-navigation problems tend to appear.
Virtualization is a rendering trade-off
Pagination and virtualization solve different problems. Pagination limits how much data the client loads or presents at once; virtualization limits how many loaded items are mounted in the DOM. A product may need one, both, or neither. If the API already returns manageable pages, adding a virtual list may create complexity without a meaningful benefit. Start by measuring the slow interaction and identifying whether the bottleneck is DOM size, data fetching, expensive row rendering, or something else. The broader web performance guide explains why profiling the actual bottleneck matters more than applying optimizations by habit.
Start with identity and the simplest row model
A virtualizer recycles or unmounts rows as the viewport moves. That makes stable item identity essential: use a durable record ID, not the current array index, as the key. If a row owns selection, editing, or expanded state, store that state by record ID outside the row component. Otherwise, a recycled row can briefly display the previous item’s state. React keys are part of component identity; see why unstable keys cause state bugs.
type RowProps = {
item: { id: string; name: string };
selected: boolean;
onToggle: (id: string) => void;
};
function Row({ item, selected, onToggle }: RowProps) {
return (
<button
aria-pressed={selected}
onClick={() => onToggle(item.id)}
>
{item.name}
</button>
);
}Keep the row component focused on presenting one item and dispatching actions by ID. This separation also makes it easier to test the row independently from the scrolling container.
Fixed heights are simpler; variable heights need measurement
When every item has a predictable height, the virtualizer can calculate each row’s position directly from its index. This is the simplest and most reliable setup. If rows can grow because of wrapping text, expandable details, or user-generated content, the list needs measurements and a way to update them when content changes. Incorrect or stale measurements accumulate into visible offsets: the scrollbar may feel wrong, and scrolling to an item may land above or below it. Avoid assuming that a CSS height will remain fixed if the product allows content to change.
Choose an overscan buffer deliberately
Overscan renders a few items outside the viewport. Too little can reveal blank space during fast scrolling; too much reduces the main performance gain by mounting more elements. There is no universally correct number: tune it against the list’s row cost, scroll behavior, and target devices. Use real interaction profiling rather than judging only from a static screenshot. If rendering is still expensive, inspect row-level work—formatting, derived data, and broad context updates—before increasing the buffer.
Keep scrolling, focus, and accessibility intact
A virtualized list is not a normal list with fewer elements. Items outside the rendered window may not exist in the DOM, so browser find-in-page, screen-reader navigation, and code that queries every row can behave differently. Preserve meaningful list semantics, expose the total count where appropriate, and ensure keyboard users can move through items even as the rendered window changes. Test focus when an item scrolls out of view: unmounting a focused control can leave focus in an unexpected place. For a data-heavy interface, test with actual keyboard and assistive-technology flows, not just pointer scrolling.
Be explicit about the distinction between a virtualized list and API pagination. A virtualizer can render a large client-side collection smoothly, but it does not make that collection small to download or retain in memory. Infinite loading also needs a clear loading and error state, plus a stable cursor strategy; the cursor pagination guide covers avoiding gaps and duplicates as data changes.
A practical rollout checklist
- Profile the slow interaction and confirm that DOM rendering is a meaningful part of the cost.
- Record a baseline: dataset size, interaction latency, memory use, and scroll behavior on representative devices.
- Choose a virtualizer that fits the layout, row sizing, and accessibility needs; avoid adding one to a small list without evidence.
- Use stable record IDs, keep durable UI state outside recyclable rows, and verify how updates affect measurements.
- Test fast scrolling, filtering, sorting, empty and loading states, keyboard focus, and assistive-technology navigation.
- Compare the same measurements after implementation and remove the optimization if its complexity does not earn its keep.
Virtualization is useful when DOM work is demonstrably limiting an interface—not as a default decoration for every array. For a site with performance concerns, Site Analyzer can help check the broader page experience, while application profiling is still needed to diagnose a specific list. When large data surfaces sit inside a more complex product, WebMriya’s complex application development work can help shape the interface and its rendering constraints together.
Frequently asked questions
When should I virtualize a React list?
Consider it when profiling shows that rendering many DOM elements is hurting a real user interaction. If the list is small or the bottleneck is elsewhere, virtualization may add complexity without improving the experience.
Does virtualization replace pagination?
No. Virtualization limits how many loaded items are mounted in the DOM, while pagination controls how data is fetched or presented. A large dataset may need both.
Why can selection state appear on the wrong row?
Rows may be unmounted or reused as the viewport moves. Use stable record IDs for keys and store durable selection or editing state by ID outside the row.