Lesson 4 of 10
Virtualization for Long Lists
Rendering 10,000 rows in the DOM kills performance - even if they're off-screen. Virtualization (windowing) renders only the rows currently visible, maintaining a constant DOM size regardless of data set length. This is essential for data grids, feeds, and large lists.
react-window - List Virtualization
import { FixedSizeList, VariableSizeList } from 'react-window';
import AutoSizer from 'react-virtualized-auto-sizer';
interface DataRow { id: string; name: string; value: number; status: string; }
// FixedSizeList - all rows same height (most common, highest performance)
function VirtualizedTable({ rows }: { rows: DataRow[] }) {
// Row renderer - called only for rows in the viewport
function Row({ index, style }: { index: number; style: React.CSSProperties }) {
const row = rows[index];
return (
// MUST apply the style prop - it contains the row's position and height
<div style={{ ...style, display: 'flex', alignItems: 'center', gap: 16, padding: '0 16px', borderBottom: '1px solid #e2e8f0' }}>
<span style={{ flex: 2 }}>{row.name}</span>
<span style={{ flex: 1, textAlign: 'right' }}>{row.value.toLocaleString()}</span>
<span style={{ flex: 1, color: row.status === 'active' ? 'green' : 'red' }}>{row.status}</span>
</div>
);
}
return (
// AutoSizer fills parent container - critical for responsive layouts
<AutoSizer>
{({ height, width }) => (
<FixedSizeList
height={height}
width={width}
itemCount={rows.length}
itemSize={52} // exact row height in pixels
overscanCount={5} // render 5 extra rows above/below viewport
>
{Row}
</FixedSizeList>
)}
</AutoSizer>
);
}
// VariableSizeList - rows with different heights (e.g. chat messages)
function ChatMessages({ messages }: { messages: Message[] }) {
// Must pre-calculate heights
const getItemSize = (index: number) => {
const msg = messages[index];
const baseHeight = 56;
const textLines = Math.ceil(msg.text.length / 60);
return baseHeight + (textLines - 1) * 20;
};
return (
<VariableSizeList
height={600}
width={800}
itemCount={messages.length}
itemSize={getItemSize}
estimatedItemSize={72}
>
{({ index, style }) => (
<div style={style}>
<MessageBubble message={messages[index]} />
</div>
)}
</VariableSizeList>
);
}
// TanStack Virtual - more flexible alternative
import { useVirtualizer } from '@tanstack/react-virtual';
function TanStackList({ items }: { items: Item[] }) {
const parentRef = useRef<HTMLDivElement>(null);
const rowVirtualizer = useVirtualizer({
count: items.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 45,
overscan: 5,
});
return (
<div ref={parentRef} style={{ height: 400, overflow: 'auto' }}>
<div style={{ height: rowVirtualizer.getTotalSize(), position: 'relative' }}>
{rowVirtualizer.getVirtualItems().map(virtualItem => (
<div
key={virtualItem.key}
style={{ position: 'absolute', top: 0, left: 0, width: '100%', transform: `translateY(${virtualItem.start}px)` }}
>
{items[virtualItem.index].name}
</div>
))}
</div>
</div>
);
}Common Mistakes - Virtualization
- Forgetting the `style` prop on row components - react-window positions rows via absolute CSS; not spreading `style` breaks the layout completely
- Using variable height without pre-measurement - VariableSizeList needs accurate sizes upfront; if you measure dynamically (ResizeObserver), you need to call `listRef.current.resetAfterIndex(0)` after measurement
- Virtualizing small lists - virtualization has overhead; for lists shorter than 50-100 items, just render normally
- Inline styles inside the Row component - creating style objects inside the row function causes every row to re-render; memoize with React.memo or extract styles outside the function
Tip
Tip
Practice Virtualization for Long Lists in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and flow for Virtualization for Long Lists
Practice Task
Note
Practice Task - (1) Write a working example of Virtualization for Long Lists from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Virtualization for Long Lists is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.
Key Takeaways
- Rendering 10,000 rows in the DOM kills performance - even if they're off-screen.
- Forgetting the `style` prop on row components - react-window positions rows via absolute CSS; not spreading `style` breaks the layout completely
- Using variable height without pre-measurement - VariableSizeList needs accurate sizes upfront; if you measure dynamically (ResizeObserver), you need to call `listRef.current.resetAfterIndex(0)` after measurement
- Virtualizing small lists - virtualization has overhead; for lists shorter than 50-100 items, just render normally
Finished this lesson?