Web · Components
VirtualList
List showing tens of thousands of items without stutter: only the visible items (and a few extra) are in the DOM.
Live example
Preview
Code
Events
- Events appear here when you interact with the preview.
Import
List showing tens of thousands of items without stutter: only the visible items (and a few extra) are in the DOM. Contact lists, records, chats, notifications. For your own layout use the useVirtualizer hook.
- Item heights can differ; they are measured as they render, and content doesn't jump even if an item above the visible area changes size.
- With
windowScrollit works with the page's own scrolling. - Infinite scrolling with
onLoadMore+hasMore; "Tekrar dene" on error. - Announced to screen readers as a list; each item reports its position within the total (
aria-posinset/aria-setsize). - The first items are rendered on the server (the first paint isn't empty).
- While the box scrolls itself the library's overlay scrollbar is used (see
ScrollArea);className/stylego to the outer box, other props (aria-label,onScroll…) to the element that actually scrolls.
Examples
useVirtualizer
functionVirtualization hook showing thousands of items by rendering only the visible ones; the foundation of VirtualList. You can use it directly for your own layout (grid cards, table rows).
- Item sizes can differ: it starts with an estimate and rendered items are measured with
ResizeObserver. - When an item above the visible area grows or shrinks, the scroll position is corrected; content doesn't jump.
- Scrolling is handled in the same frame; it re-renders only when the visible range changes.
Examples
UseVirtualizerOptions
interfaceOptions of the useVirtualizer hook.
| Name | Type | Default | Description |
|---|---|---|---|
count | number | Number of items. | |
estimateSize | number | ((index: number) => number) | Estimated size of an item not measured yet (px): a number or a function by index. Real sizes are measured once items render; the closer the estimate, the less the scrollbar jumps. | |
scrollElement | () => HTMLElement | "window" | null | The scrolled element. If "window", the page itself scrolls (feed, timeline); the list is virtualized from its position on the page. | |
listElement? | () => HTMLElement | null | The list itself (to find its position on the page in "window" mode). If omitted, the list counts as at the very start of the scrolled element. | |
overscan? | number | 6 | Number of extra items drawn before and after the visible area. |
horizontal? | boolean | false | Horizontal list. |
gap? | number | 0 | Gap between items (px). |
paddingStart? | number | 0 | Padding at the start of the list (px). |
paddingEnd? | number | 0 | Padding at the end of the list (px). |
getKey? | (index: number) => string | number | index | Persistent key of an item. Measurements are stored by this key; adding items to the start of the list doesn't shift measurements. |
initialViewport? | number | 800 | Default visible area size (px) before the scroll area is measured (on the server, on first render). |
VirtualItem
interfaceAn item to render.
| Name | Type | Description |
|---|---|---|
index | number | Index of the item. |
key | string | number | Key of the item (getKey). |
start | number | Distance from the start of the list (px). |
size | number | Its size (px; the estimate if not measured). |
VirtualAlign
typeAlignment of VirtualList and scrollToIndex.
Virtualizer
interfaceWhat the useVirtualizer hook returns.
| Name | Type | Description |
|---|---|---|
items | VirtualItem[] | Items that should be rendered now. |
totalSize | number | Size of the whole list (px); the inner container's height/width should be this. |
measureElement | (element: HTMLElement | null) => (() => void) | undefined | Function to pass to an item's ref to measure it. The item must have a data-index attribute. |
scrollToIndex | (index: number, options?: { align?: VirtualAlign; smooth?: boolean; }) => void | Scrolls to the item. auto: doesn't scroll if already visible. |
reset | () => void | Clears measurements (e.g. when the size of all items changed). |
VirtualListHandle
interfaceFunctions of VirtualList accessed with ref.
| Name | Type | Description |
|---|---|---|
scrollToIndex | (index: number, options?: { align?: VirtualAlign; smooth?: boolean; }) => void | Scrolls to the item. auto: doesn't scroll if already visible. |
element | HTMLDivElement | null | The element that actually scrolls (the list itself in windowScroll mode too). |
VirtualListClassNames
interfaceClasses that can be passed to the inner parts of VirtualList.
| Name | Type | Description |
|---|---|---|
content? | string | Inner container carrying the total size of the items. |
item? | string | Wrapper of each item. |
footer? | string | Loading/error area at the end of the list. |
VirtualListProps
interfaceProps of the VirtualList component.
Extends: Omit<ComponentProps<"div">, "children" | "ref">
| Name | Type | Default | Description |
|---|---|---|---|
items | readonly T[] | Items. | |
renderItem | (item: T, index: number) => ReactNode | Renders an item. | |
getKey? | (item: T, index: number) => string | number | index | Persistent key of an item (e.g. (u) => u.id). |
estimateSize? | number | ((index: number) => number) | 48 | Estimated size of an item (px). Items can be of different sizes; they are measured once rendered. |
overscan? | number | 6 | Number of extra items rendered outside the visible area. |
gap? | number | 0 | Gap between items (px). |
horizontal? | boolean | false | Horizontal list. |
windowScroll? | boolean | false | The list scrolls with the page instead of in its own box (feed, timeline). No need to give a height. |
height? | number | string | Height of the scroll area (width when horizontal). Can be given with a class too (h-96). | |
empty? | ReactNode | Content shown when the list is empty (e.g. EmptyState). | |
onLoadMore? | () => unknown | Loads the next page when nearing the end. See InfiniteScroll. | |
onError? | (error: unknown) => void | Called when onLoadMore throws; the error is caught and "Tekrar dene" appears. | |
hasMore? | boolean | false | Is there another page to load? |
loading? | boolean | Loading state from outside; while true an indicator shows at the end of the list. | |
threshold? | number | 400 | Start loading when this much remains to the end (px). |
ref? | Ref<VirtualListHandle> | Access for scrollToIndex. See VirtualListHandle. | |
classNames? | VirtualListClassNames | Classes added to the inner parts. |