A zero-config, fast and small (~3kB) virtual list (and grid) component for React, Vue, Solid and Svelte.
If you want to check the difference with the alternatives right away, see comparison section.
This project is a challenge to rethink virtualization. The goals are...
- Zero-config virtualization: This library is designed to give the best performance without configuration. It also handles common hard things in the real world (dynamic size measurement, scroll position adjustment while reverse scrolling and imperative scrolling, iOS support, etc).
- Fast: Natural virtual scrolling needs optimization in many aspects (eliminate frame drops by reducing CPU usage and GC and layout recalculation, reduce visual jumps on repaint, optimize with CSS, optimize for frameworks, etc). We are trying to combine the best of them.
- Small: Its bundle size should be small as much as possible to be friendly with modern web development. Currently each components are ~3kB gzipped and tree-shakeable. The total size for React is ~5kB gzipped.
- Flexible: Aiming to support many usecases - fixed size, dynamic size, horizontal scrolling, reverse scrolling, RTL, mobile, infinite scrolling, scroll restoration, DnD, keyboard navigation, sticky, placeholder and more. See live demo.
- Framework agnostic: React, Vue, Solid and Svelte are supported. We could support other frameworks in the future.
https://inokawa.github.io/virtua/
npm install virtua
If you use this lib in legacy browsers which does not have ResizeObserver, you should use polyfill.
react >= 16.14
is required.
If you use ESM and webpack 5, use react >= 18 to avoid Can't resolve react/jsx-runtime
error.
import { VList } from "virtua";
export const App = () => {
return (
<VList style={{ height: 800 }}>
{Array.from({ length: 1000 }).map((_, i) => (
<div
key={i}
style={{
height: Math.floor(Math.random() * 10) * 10 + 10,
borderBottom: "solid 1px gray",
background: "white",
}}
>
{i}
</div>
))}
</VList>
);
};
import { VList } from "virtua";
export const App = () => {
return (
<VList style={{ height: 400 }} horizontal>
{Array.from({ length: 1000 }).map((_, i) => (
<div
key={i}
style={{
width: Math.floor(Math.random() * 10) * 10 + 10,
borderRight: "solid 1px gray",
background: "white",
}}
>
{i}
</div>
))}
</VList>
);
};
VList
is a recommended solution which works like a drop-in replacement of simple list built with scrollable div
(or removed virtual-scroller element). For more complicated styling or markup, use Virtualizer
.
import { Virtualizer } from "virtua";
export const App = () => {
return (
<div style={{ overflowY: "auto", height: 800 }}>
<div style={{ height: 40 }}>header</div>
<Virtualizer startMargin={40}>
{Array.from({ length: 1000 }).map((_, i) => (
<div
key={i}
style={{
height: Math.floor(Math.random() * 10) * 10 + 10,
borderBottom: "solid 1px gray",
background: "white",
}}
>
{i}
</div>
))}
</Virtualizer>
</div>
);
};
import { WindowVirtualizer } from "virtua";
export const App = () => {
return (
<div style={{ padding: 200 }}>
<WindowVirtualizer>
{Array.from({ length: 1000 }).map((_, i) => (
<div
key={i}
style={{
height: Math.floor(Math.random() * 10) * 10 + 10,
borderBottom: "solid 1px gray",
background: "white",
}}
>
{i}
</div>
))}
</WindowVirtualizer>
</div>
);
};
import { experimental_VGrid as VGrid } from "virtua";
export const App = () => {
return (
<VGrid style={{ height: 800 }} row={1000} col={500}>
{({ rowIndex, colIndex }) => (
<div
style={{
width: ((colIndex % 3) + 1) * 100,
border: "solid 1px gray",
background: "white",
}}
>
{rowIndex} / {colIndex}
</div>
)}
</VGrid>
);
};
This library is marked as a Client Component. You can render RSC as children of VList
, Virtualizer
or WindowVirtualizer
.
// page.tsx in App Router of Next.js
export default async () => {
const articles = await fetchArticles();
return (
<div>
<div>This is Server Component</div>
<VList style={{ height: 300 }}>
{articles.map((a) => (
<div key={a.id} style={{ border: "solid 1px gray", height: 80 }}>
{a.content}
</div>
))}
</VList>
</div>
);
};
vue >= 3.2
is required.
<script setup>
import { VList } from "virtua/vue";
const sizes = [20, 40, 180, 77];
const data = Array.from({ length: 1000 }).map((_, i) => ({
id: i,
size: sizes[i % 4] + "px",
}));
</script>
<template>
<VList :data="data" :style="{ height: '800px' }" #default="item">
<div
:key="item.id"
:style="{
height: item.size,
background: 'white',
borderBottom: 'solid 1px #ccc',
}"
>
{{ item.id }}
</div>
</VList>
</template>
solid-js >= 1.0
is required.
import { VList } from "virtua/solid";
export const App = () => {
const sizes = [20, 40, 80, 77];
const data = Array.from({ length: 1000 }).map((_, i) => sizes[i % 4]);
return (
<VList data={data} style={{ height: "800px" }}>
{(d, i) => (
<div
style={{
height: d + "px",
"border-bottom": "solid 1px #ccc",
background: "#fff",
}}
>
{i}
</div>
)}
</VList>
);
};
svelte >= 4.0
is required.
<script lang="ts">
import { VList } from "virtua/svelte";
const sizes = [20, 40, 180, 77];
const data = Array.from({ length: 1000 }).map((_, i) => ({ id: i, size: sizes[i % 4] + "px" }));
</script>
<VList {data} let:item style={`height: 100vh;`} getKey={(d) => d.id}>
<div
style={`
height: ${item.size};
background: white;
border-bottom: solid 1px #ccc;
`}
>
{item.id}
</div>
</VList>
- API reference
- Storybook examples for more usages
In complex usage, especially if you re-render frequently the parent of virtual scroller or the children are tons of items, children element creation can be a performance bottle neck. That's because creating React elements is fast enough but not free and new React element instances break some of memoization inside virtual scroller.
One solution is memoization with useMemo
. You can use it to reduce computation and keep the elements' instance the same. And if you want to pass state from parent to the items, using context
instead of props may be better because it doesn't break the memoization.
const elements = useMemo(
() => tooLongArray.map((d) => <Component key={d.id} {...d} />),
[tooLongArray]
);
const [position, setPosition] = useState(0);
return (
<div>
<div>position: {position}</div>
<VList onScroll={(offset) => setPosition(offset)}>{elements}</VList>
</div>
);
The other solution is using render prop
as children to create elements lazily. It will effectively reduce cost on start up when you render many items (>1000). An important point is that newly created elements from render prop
will disable optimization possible with cached element instances. We recommend using memo
to reduce calling render function of your item components during scrolling.
const Component = memo(HeavyItem);
<VList count={items.length}>
{(i) => {
const item = items[i];
return <Component key={item.id} data={item} />;
}}
</VList>;
Decreasing overscan
prop may also improve perf in case that components are large and heavy.
Virtua try to suppress glitch caused by resize as much as possible, but it will also require additional work. If your item contains something resized often, such as lazy loaded image, we recommend to set height or min-height to it if possible.
It may be dispatched by ResizeObserver in this lib as described in spec. If it bothers you, you can safely ignore it.
Especially for webpack-dev-server
, you can filter out the specific error with devServer.client.overlay.runtimeErrors
option.
viewportSize
will be calculated by ResizeObserver so it's 0 until the first measurement.
This package uses exports of package.json for entry point of Vue/Solid/Svelte adapter. This field can't be resolved in TypeScript with moduleResolution: node
. Try moduleResolution: bundler
or moduleResolution: nodenext
instead.
virtua | react-virtuoso | react-window | react-virtualized | @tanstack/react-virtual | react-tiny-virtual-list | react-cool-virtual | |
---|---|---|---|---|---|---|---|
Bundle size | |||||||
Vertical scroll | β | β | β | β | π (needs customization) | β | π (needs customization) |
Horizontal scroll | β | β | β (may be dropped in v2) | β | π (needs customization) | β | π (needs customization) |
Horizontal scroll in RTL direction | β | β | β (may be dropped in v2) | β | β | β | β |
Grid (Virtualization for two dimension) | π (experimental_VGrid) | β | β (FixedSizeGrid / VariableSizeGrid) | β (Grid) | π (needs customization) | β | π (needs customization) |
Table | π (needs customization) | β (TableVirtuoso) | π (needs customization) | π (Table but it's built with div) | π (needs customization) | β | π (needs customization) |
Window scroller | β (WindowVirtualizer) | β | β | β (WindowScroller) | β (useWindowVirtualizer) | β | β |
Dynamic list size | β | β | π (needs AutoSizer) | π (needs AutoSizer) | β | β | β |
Dynamic item size | β | β | π (needs additional codes and has wrong destination when scrolling to item imperatively) | π (needs CellMeasurer and has wrong destination when scrolling to item imperatively) | π (has wrong destination when scrolling to item imperatively) | β | π (has wrong destination when scrolling to item imperatively) |
Reverse scroll | β | β | β | β | β | β | β |
Reverse scroll in iOS Safari | π (user must release scroll) | π (has glitch with unknown sized items) | β | β | β | β | β |
Infinite scroll | β | β | π (needs react-window-infinite-loader) | π (needs InfiniteLoader) | β | β | β |
Reverse (bi-directional) infinite scroll | β | β | β | β | β | β | π (has startItem method but its scroll position can be inaccurate) |
Scroll restoration | β | β (getState) | β | β | β | β | β |
Smooth scroll | β | β | β | β | β | β | β |
SSR support | β | β | β | β | β | β | β |
Render React Server Components (RSC) as children | β | β | β | β | π (needs customization) | β | π (needs customization) |
Display exceeding browser's max element size limit | β | β | β | β | β | β | β |
- β - Built-in supported
- π - Supported but partial, limited or requires some user custom code
- β - Not officially supported
WIP
All contributions are welcome. If you find a problem, feel free to create an issue or a PR. If you have a question, ask in discussions.
- Fork this repo.
- Run
npm install
. - Commit your fix.
- Make a PR and confirm all the CI checks passed.