Virtualized List
UI Coding
Hard
50 min
performance
virtualization
scroll
accessibility
memoization
The starter renders 10,000 rows into a scrollable box, and it's slow to mount and to scroll. Build a reusable VirtualList that renders only the rows in view (plus a few extra), without using a library.
API
export function VirtualList({
items, // any array
itemHeight, // fixed row height in px
height, // viewport height in px
overscan = 3, // extra rows rendered above and below the viewport
renderItem, // (item, index) => ReactNode
label, // accessible name of the list
})
Requirements
- The scroll container has a fixed
heightand scrolls through the full content height (items.length × itemHeight). The scrollbar must look and behave as if every row were there. - Only the rows that intersect the viewport, plus
overscanrows on each side, are in the DOM. With 10,000 rows of 30px in a 300px viewport and an overscan of 3, that's 13 rows at the top of the list. - Scrolling updates the rendered window. Rows are absolutely positioned, or offset, at
index × itemHeight. - A Jump to item control (a number input plus a Go button) scrolls row N to the top of the viewport.
- Accessibility:
- The container is
role="list"with an accessible name. Rendered rows arerole="listitem"witharia-posinset(1-based) andaria-setsize(the total), so screen readers report "item 5,000 of 10,000" even though most rows aren't in the DOM. - The scroll container is keyboard-focusable (
tabIndex=0), so keyboard users can scroll it with the arrow and Page keys.
- The container is
- Keep scrolling cheap: don't re-render rows that didn't change, and don't create layout thrash in the scroll handler.
Follow-up
- Support variable row heights that are unknown until rendered.
- Ctrl+F (find in page) doesn't find off-screen rows. What are your options?
- When would you use
content-visibility: autoinstead of virtualising?
Spotted a mistake or a rights concern? Report an issue