Horizontal and Grid Virtualization
You'll learn how to apply virtualization to horizontal scrolling lists and two-dimensional grids using multiple virtualizer instances.
Beyond Vertical Lists
So far we've virtualized vertical lists—scrolling up and down. But sometimes you need to scroll horizontally (like a carousel of cards) or in two dimensions (like a spreadsheet or image gallery grid). TanStack Virtual handles both by letting you change the direction or combine multiple virtualizers.
Horizontal Virtualization
For horizontal scrolling, simply switch the horizontal option:
const rowVirtualizer = useVirtualizer({
count: items.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 200, // width instead of height
horizontal: true, // changes scrolling axis
});
The virtual items now represent columns instead of rows. You'll apply their measurements to the left position and width instead of top and height.
Grid Virtualization: Two Virtualizers
For a grid, you need virtualization in both directions. Create two separate virtualizers—one for rows, one for columns:
const rowVirtualizer = useVirtualizer({
count: rowCount,
getScrollElement: () => parentRef.current,
estimateSize: () => 100,
});
const columnVirtualizer = useVirtualizer({
count: columnCount,
getScrollElement: () => parentRef.current,
estimateSize: () => 150,
horizontal: true,
});
Then loop through both virtual item arrays to render only the visible cells in the grid. Each cell gets positioned using both the row's top/height and column's left/width.
The Grid Pattern
Think of it like rendering a chessboard—but instead of drawing all 64 squares, you only draw the 12 squares currently visible in the viewport window.
Key Takeaway: Horizontal virtualization uses horizontal: true and column measurements, while grid virtualization combines two virtualizers (one per axis) to render only visible cells in a two-dimensional space.