A list can have 50,000 rows and room to show six. A plain .map() still asks React to create all 50,000 row elements before you scroll. If each row has a few children, the DOM gets large well before the list looks complicated.
List virtualization keeps a window of rows mounted around the viewport. The scrollbar still covers the whole list, so you can drag it halfway down to reach a distant row. TanStack Virtual works out which rows belong in that window and where to place them. You supply the markup and styles. TanStack Virtual introduction
Keep the scrollbar the size of the whole list
The first demo has 50,000 rows at 40 pixels each. Its viewport is 240 pixels tall, so six rows fit at the top. Turn off Overscan 4 rows to see those six mounted by themselves. Turn it back on and jump to row 25,000. There are four extra rows on either side of the visible range, and the scroll height stays at 2,000,000 pixels.
JavaScript enables the interactive list. The 50,000 rows are each 40 pixels tall, giving the list a scroll height of 2,000,000 pixels.
There are three parts to the layout. The outer element is the scroll container. Inside it, a relatively positioned list takes up the height of all 50,000 rows. The mounted rows are absolutely positioned inside that list, so each can sit at its own offset without adding to the list’s height.
For a fixed-height list, the offset is easy to work out. Row 25,000 has a zero-based index of 24,999, so it starts at 24_999 * 40, or 999,960 pixels. When the scroll container reaches that offset, the row appears at the top of the viewport.
Overscan mounts a buffer just outside the viewport. It gives the browser some already-rendered rows to scroll into before React commits the next set. A larger buffer also means more rows for React and the browser to keep around. I’d start with a few and try a fast scroll with the actual row contents before changing it. Virtualizer options
Render fixed-height rows with useVirtualizer
Install @tanstack/react-virtual in a React project. Here’s the fixed-height layout without the demo controls.
import { useRef } from 'react';
import { useVirtualizer } from '@tanstack/react-virtual';
export function FixedRows() {
const viewport = useRef<HTMLDivElement>(null);
const count = 50_000;
const virtualizer = useVirtualizer({
count,
getScrollElement: () => viewport.current,
estimateSize: () => 40,
overscan: 4,
});
return (
<div
ref={viewport}
role="region"
aria-label="Fixed-height rows"
tabIndex={0}
style={{ height: 240, overflow: 'auto' }}
>
<ol
role="list"
style={{
position: 'relative',
height: virtualizer.getTotalSize(),
margin: 0,
padding: 0,
listStyle: 'none',
}}
>
{virtualizer.getVirtualItems().map((row) => (
<li
key={row.key}
aria-posinset={row.index + 1}
aria-setsize={count}
style={{
position: 'absolute',
inset: '0 0 auto',
height: row.size,
transform: `translateY(${row.start}px)`,
}}
>
Row {row.index + 1}
</li>
))}
</ol>
</div>
);
}getTotalSize() gives the list its height. getVirtualItems() gives you the rows to render, and each row’s start is its offset inside that list. Keep that offset relative to the inner element. The browser already moves the entire element as you scroll.
The jump button calls virtualizer.scrollToIndex(24_999, { align: 'start' }). It can go straight to the row because the virtualizer knows its offset even before the row has a DOM element. Virtualizer API
Measure rows that wrap
If the row contains text that can wrap, the height depends on the content and the available width. In the second demo, switch on Narrow list. The longer notes take up more lines, their neighbors move down, and the estimated scroll height changes.
JavaScript enables the interactive list. Narrowing the list wraps the notes and changes the height of the rows.
Keep the same scroll container and inner list. Give estimateSize a starting height and let the content set the rendered height of each row. This component accepts an array of notes with unique IDs.
import { useCallback, useRef } from 'react';
import { useVirtualizer } from '@tanstack/react-virtual';
type Note = { id: string; text: string };
export function MeasuredNotes({ notes }: { notes: Note[] }) {
const viewport = useRef<HTMLDivElement>(null);
const getItemKey = useCallback((index: number) => notes[index].id, [notes]);
const virtualizer = useVirtualizer({
count: notes.length,
getScrollElement: () => viewport.current,
getItemKey,
estimateSize: () => 120,
overscan: 4,
});
return (
<div
ref={viewport}
role="region"
aria-label="Notes"
tabIndex={0}
style={{ height: 240, overflow: 'auto', overflowAnchor: 'none' }}
>
<ol
role="list"
style={{
position: 'relative',
height: virtualizer.getTotalSize(),
margin: 0,
padding: 0,
listStyle: 'none',
}}
>
{virtualizer.getVirtualItems().map((row) => (
<li
key={row.key}
data-index={row.index}
ref={virtualizer.measureElement}
aria-posinset={row.index + 1}
aria-setsize={notes.length}
style={{
position: 'absolute',
inset: '0 0 auto',
padding: '12px 16px',
overflowWrap: 'anywhere',
transform: `translateY(${row.start}px)`,
}}
>
{notes[row.index].text}
</li>
))}
</ol>
</div>
);
}data-index tells the virtualizer which position the element occupies. The ref registers the element for measurement, including size changes while it’s mounted. If a row comes in 32 pixels taller than its estimate, the rows after it need to start 32 pixels farther down. The scrollable height grows by the same amount. TanStack Virtual dynamic example
The total uses measurements where they’re available and estimates for rows the virtualizer hasn’t measured. A width change can also leave old measurements for offscreen rows until they’re mounted and measured again. That’s why I label this number Estimated scroll height in the demo. The estimate matters most when jumping to a distant row whose neighbors are still unmeasured.
I use the record’s ID as the key so the measurement belongs to the same note after an insert or a sort. The index still tells me where the note is in the current order. I also put space inside rows with padding. For space between rows, use the virtualizer’s gap option so it can include that space in its offsets. Virtualizer options
Decide what needs to stay in the DOM
Virtualization reduces the work of rendering rows. Fetching, filtering, and sorting the data still happen separately. I’d profile the list with realistic row contents and check which part is taking the time. If the data is expensive to fetch, pagination can limit how much you request. You can also fetch in pages and virtualize the rows you’ve loaded.
I’d test browser Find and keyboard focus before virtualizing a list full of links or inputs. Find can only match content that’s mounted, and focus can be lost if its row unmounts. If a list needs search across every record, build that search against the data. For a list with editable rows, keep the edits outside the row component so scrolling away doesn’t discard them.
If you’re trying this in an existing list, start by checking whether the rows already have a reliable height. That gives you the simpler setup and an exact scroll range. For rows that wrap, try the measured version at the narrowest width you support, then jump far enough down the list to see how your estimates behave. React adapter documentation