useListVirtualizer
Virtualization hook for rendering large lists efficiently
Installation
Overview
useListVirtualizer provides virtualization for large lists using TanStack Virtual. It handles scroll positioning, item measurement, and keyboard navigation, making it easy to render thousands of items without performance issues.
When to use:
- Lists with 100+ items
- Command menus, autocompletes, or selects with large datasets
- Any scrollable list where DOM node count impacts performance
Usage
Example with Command
Usage with useFilteredItems
When using Base UI's useFilteredItems() hook, filtered items come from inside the Root component. This requires a child component pattern with virtualizerRef + useHighlightHandler:
This eliminates manual query state, useDeferredValue, useFilter(), and useMemo filtering. The Root handles filtering internally, and useFilteredItems() provides the result to the virtualizer.
Performance Tips
For optimal scroll performance with large lists:
-
Use
useDeferredValuefor search queries - Prevents filtering from blocking scroll rendering. Use the resolved pattern to avoid stale results when clearing: -
Use
nativeScrollonCommandVirtualizedList- Avoids custom scroll container overhead for smoother fast scrolling.
API Reference
Options
useHighlightHandler
Returns a stable onItemHighlighted callback that scrolls the virtualizer to the highlighted item on keyboard navigation. Use this on the Root component when the virtualizer lives in a child component.
ListVirtualizerInstance
Type alias for the virtualizer instance. Use to type the virtualizerRef.
Return Value
| Property | Type | Description |
|---|---|---|
rootProps | object | Spread on root component. Includes virtualized, items, filteredItems, onItemHighlighted |
scrollRef | (el: HTMLDivElement | null) => void | Ref callback for the scroll container |
measureRef | (el: HTMLDivElement | null) => void | Ref callback for each item (enables dynamic heights) |
totalSize | number | Total height of all virtual items in pixels |
virtualItems | VirtualItem[] | Array of virtual items to render |
getItem | (virtualItem) => T | undefined | Get the data item for a virtual item |
getItemStyle | (virtualItem) => CSSProperties | Get positioning styles for a virtual item |
getItemProps | (virtualItem) => object | Get aria attributes (aria-setsize, aria-posinset, data-index, index) |
Used By
- Command - For virtualized command menus
- Autocomplete - For virtualized autocomplete lists
- Combobox - For virtualized combobox lists