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.
| Name | Description | Type | Default |
|---|---|---|---|
| count | The number of first level items in the list | number | |
| getItemSize | The height of an item, including its children; children arrive with the parentKey | (index, parentKey?) => number | |
| getItemKey | The key of an item; children arrive with the parentKey | (index, parentKey?) => Key | |
| renderRow | Renders a row: the item, the key of its parent and a callback rendering its children | (item, parentKey, renderChildren) => React.ReactNode | |
| measure | Measure the actual heights of the top-level rows after mount instead of trusting getItemSize | boolean | true |
| overscan | The number of rows rendered above and below the visible window | number | 0 |
| disableVirtualization | Render every row; useful for a list known to be small | boolean | |
| persistedIndexes | Rows kept in the window even when scrolled out; each entry is a path of indexes in the hierarchy | Array<number[]> | |
| loading | Whether more items are loading: until it turns back to false, onLoadMore is not called again | boolean | |
| onLoadMore | Called when the scroll gets near the end | () => void | |
| apiRef | Ref to the imperative API: scrollToIndex, scrollToOffset, scrollOffset, scrollRect | React.Ref<VirtualizerApi> | |
| containerRef | Ref of the scroll container element | React.Ref<HTMLElement> |
§ListVirtualizer
| Name | Description | Type | Default |
|---|---|---|---|
| children | The <List> inside | ReactNode | |
| estimateItemSize | The height estimate of a row before it is rendered — a constant or a function of the row context | number | (ctx) => number | by the size of the list |
| measure | Measure the actual heights of the rows after mount (rows of variable height out of the box) | boolean | true |
| minItemsCount | A list of fewer rows renders as if there were no wrapper; a Select counts its options, without the group headers and without the filter | number | 0 |
| overscan | The buffer of rows outside of the visible window | number | 5 |
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.