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
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
Prop
Type
Default
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