Virtualizer

The Virtualizer renders only the window of rows that is currently visible, which is what makes lists of tens of thousands of items affordable.

import {ListVirtualizer, Virtualizer} from '@gravity-ui/uikit/virtualizer';

This entry point needs @tanstack/react-virtual: it is an optional peer dependency, so install it next to @gravity-ui/uikit.

Two things live here. Virtualizer is the engine — a scroll container that positions and measures rows you render yourself. ListVirtualizer is the virtualization layer of the List: it wraps a <List>, and the list renders through the engine without any other change.

Virtualizer

The element the component renders is the scroll container itself, so its height has to be limited. getItemSize is the estimate used before a row is rendered; unless measure is off, the measured height of a top-level row replaces it after mount, which is why rows of variable height need no configuration.

import {Virtualizer} from '@gravity-ui/uikit/virtualizer';

const ROW_HEIGHT = 32;

function Log({records}) {
  return (
    <Virtualizer
      style={{height: 400}}
      count={records.length}
      getItemSize={() => ROW_HEIGHT}
      getItemKey={(index) => records[index].id}
      renderRow={({index}) => <div style={{height: ROW_HEIGHT}}>{records[index].message}</div>}
    />
  );
}

ListVirtualizer

The wrapper is the only thing that changes — the list inside stays as it was. All of its configuration props are optional: without estimateItemSize the row heights come from the size of the list.

import {List} from '@gravity-ui/uikit';
import {ListVirtualizer} from '@gravity-ui/uikit/virtualizer';

function TrackList({tracks}) {
  return (
    <ListVirtualizer estimateItemSize={28}>
      <List
        aria-label="Archive"
        style={{maxHeight: 480}}
        items={tracks}
        getItemContent={(track) => track.title}
      />
    </ListVirtualizer>
  );
}

The root of the List becomes the scroll container, so limiting its height is up to you — otherwise the window degenerates into the full list. The details of the layer are in the Virtualization section of the List.

One wrapper for the whole app

The wrapper is a context provider: it covers every List and Select below it, the popups of the Selects included. With minItemsCount it can stand once next to the theme Provider and virtualize only the long lists — a list of fewer rows renders as if there were no wrapper, every row in the DOM.

import {Provider} from '@gravity-ui/uikit';
import {ListVirtualizer} from '@gravity-ui/uikit/virtualizer';

root.render(
  <Provider theme="light">
    <ListVirtualizer minItemsCount={50}>
      <App />
    </ListVirtualizer>
  </Provider>,
);

A List counts its rows, section headers included. A Select counts its options without the group headers and without the filter, so typing into the filter does not switch the list from one mode to the other.

A wrapper inside another one changes only what it is given and takes the rest from the one above, the way a nested theme Provider does. Around a single list under the root wrapper:

  • <ListVirtualizer overscan={20}> keeps the threshold of the root and changes the buffer;
  • <ListVirtualizer minItemsCount={0}> virtualizes the list whatever its size;
  • <ListVirtualizer minItemsCount={Infinity}> keeps it plain, and the chunk is not loaded for it.

Loading on demand

The code of the window — @tanstack/react-virtual included — is a chunk of its own: importing ListVirtualizer does not bring it into the bundle of the app. The chunk is loaded when the first list long enough for minItemsCount renders, and a Select that is going to be virtualized starts loading it on mount, before its popup opens. Until the chunk arrives, the list shows its first rows in the same root: the page is not left empty, and the window replaces them as soon as it can be rendered, scrolled to the active row the list was showing. If the chunk fails to load, the error reaches the nearest error boundary of the list.

[!NOTE] In server-rendered HTML a virtualized list has its first rows: there is no viewport to measure the window against, so the window takes over after hydration. The rest of the rows are not in the markup — leave a list unvirtualized when all of its content has to be there.

Properties

Virtualizer

The rest of the props go to the scroll container element.

NameDescriptionTypeDefault
countThe number of first level items in the listnumber
getItemSizeThe height of an item, including its children; children arrive with the parentKey(index, parentKey?) => number
getItemKeyThe key of an item; children arrive with the parentKey(index, parentKey?) => Key
renderRowRenders a row: the item, the key of its parent and a callback rendering its children(item, parentKey, renderChildren) => React.ReactNode
measureMeasure the actual heights of the top-level rows after mount instead of trusting getItemSizebooleantrue
overscanThe number of rows rendered above and below the visible windownumber0
disableVirtualizationRender every row; useful for a list known to be smallboolean
persistedIndexesRows kept in the window even when scrolled out; each entry is a path of indexes in the hierarchyArray<number[]>
loadingWhether more items are loading: until it turns back to false, onLoadMore is not called againboolean
onLoadMoreCalled when the scroll gets near the end() => void
apiRefRef to the imperative API: scrollToIndex, scrollToOffset, scrollOffset, scrollRectReact.Ref<VirtualizerApi>
containerRefRef of the scroll container elementReact.Ref<HTMLElement>

ListVirtualizer

NameDescriptionTypeDefault
childrenThe <List> insideReactNode
estimateItemSizeThe height estimate of a row before it is rendered — a constant or a function of the row contextnumber | (ctx) => numberby the size of the list
measureMeasure the actual heights of the rows after mount (rows of variable height out of the box)booleantrue
minItemsCountA list of fewer rows renders as if there were no wrapper; a Select counts its options, without the group headers and without the filternumber0
overscanThe buffer of rows outside of the visible windownumber5

A wrapper inside another one takes the props it is not given from the one above: the defaults in the table are those of the outermost wrapper.