Sub topic 14
Grids and horizontal virtualization
This chapter assumes you have built the vertical list from earlier chapters. You should know the scroll container and spacer pattern, overscan, and the basics of useVirtualizer from TanStack Virtual.
Horizontal lists are the same idea on another axis
Horizontal virtualization uses the same windowing logic as a vertical list. It swaps the axis. The virtualizer reads scrollLeft instead of scrollTop, and it uses widths instead of heights.
With TanStack Virtual, you set horizontal: true. Each virtual item then reports start and size along the x-axis. You position items with translateX(v.start) and make the spacer as wide as getTotalSize().
Grids use two virtualizers
A grid scrolls in both directions. The simplest approach is to run one virtualizer for rows and one for columns on the same scroll element. A cell is rendered only when both its row and its column are in range. The number of rendered cells is roughly visible rows times visible columns, plus overscan on each axis.
A complete grid
This grid has 10,000 rows and 1,000 columns. That is 10 million cells, but the browser holds only a few hundred at a time.
import { useRef } from "react";
import { useVirtualizer } from "@tanstack/react-virtual";
const ROWS = 10_000;
const COLS = 1_000;
export function Grid() {
const parentRef = useRef<HTMLDivElement>(null);
const rowVirtualizer = useVirtualizer({
count: ROWS,
getScrollElement: () => parentRef.current,
estimateSize: () => 35,
overscan: 5,
});
const columnVirtualizer = useVirtualizer({
horizontal: true,
count: COLS,
getScrollElement: () => parentRef.current,
estimateSize: () => 100,
overscan: 3,
});
return (
<div
ref={parentRef}
role="grid"
aria-rowcount={ROWS}
aria-colcount={COLS}
style={{ height: 400, width: 600, overflow: "auto" }}
>
<div
style={{
height: rowVirtualizer.getTotalSize(),
width: columnVirtualizer.getTotalSize(),
position: "relative",
}}
>
{rowVirtualizer.getVirtualItems().map((row) => (
<div key={row.key} role="row" aria-rowindex={row.index + 1}>
{columnVirtualizer.getVirtualItems().map((col) => (
<div
key={col.key}
role="gridcell"
aria-colindex={col.index + 1}
style={{
position: "absolute",
top: 0,
left: 0,
width: col.size,
height: row.size,
transform: `translate(${col.start}px, ${row.start}px)`,
}}
>
{row.index},{col.index}
</div>
))}
</div>
))}
</div>
</div>
);
}The spacer gets both a height and a width, so both scrollbars reflect the full grid. Each cell is positioned with a single translate(x, y). The aria-rowindex and aria-colindex values are 1-based, and they tell assistive technology where each cell sits in the full grid even though most cells are not in the DOM.
Gotchas
- Measure one axis at most. Dynamic measurement with
measureElementworks well for row heights. If you also let column widths vary by content, you must measure every rendered cell in both dimensions, and scrollbar size can jump. Prefer fixed or known column widths. - Overscan multiplies. An overscan of 5 rows and 3 columns adds a border of extra cells on all sides. Keep overscan small on grids.
- Sticky headers and first columns. A pinned header or column must stay rendered even when its index is out of range. Render it separately outside the virtualized cells, or use a custom
rangeExtractorto add its index to the range. - Memoize cells. A scroll event can re-render hundreds of cells. Wrap the cell in
React.memoand give it stable props, as covered in the keys and state chapter.
Exercise
In the grid above, the viewport shows 12 rows and 6 columns. Row overscan is 5 and column overscan is 3. Roughly how many cells are rendered when you are scrolled to the middle of the grid?
Show answer
About 18 rows (12 + 5 above and 5 below would be 22, but overscan is applied on each side, so 12 + 10 = 22 rows) times 12 columns (6 + 3 + 3). That is roughly 22 × 12 = 264 cells. Overscan applies on both sides of each axis, which is why overscan values should stay small on grids.