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:

  1. Use useDeferredValue for search queries - Prevents filtering from blocking scroll rendering. Use the resolved pattern to avoid stale results when clearing:

  2. Use nativeScroll on CommandVirtualizedList - Avoids custom scroll container overhead for smoother fast scrolling.

API Reference

Options

Prop

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

PropertyTypeDescription
rootPropsobjectSpread on root component. Includes virtualized, items, filteredItems, onItemHighlighted
scrollRef(el: HTMLDivElement | null) => voidRef callback for the scroll container
measureRef(el: HTMLDivElement | null) => voidRef callback for each item (enables dynamic heights)
totalSizenumberTotal height of all virtual items in pixels
virtualItemsVirtualItem[]Array of virtual items to render
getItem(virtualItem) => T | undefinedGet the data item for a virtual item
getItemStyle(virtualItem) => CSSPropertiesGet positioning styles for a virtual item
getItemProps(virtualItem) => objectGet aria attributes (aria-setsize, aria-posinset, data-index, index)

Used By