Open History to see the last commit
1---
2title: Introduction
3---
4β
5TanStack Virtual is a headless UI utility for virtualizing long lists of elements in JS/TS, React, Vue, Svelte, Solid, Lit, and Angular. It is not a component therefore does not ship with or render any markup or styles for you. While this requires a bit of markup and styles from you, you will retain 100% control over your styles, design and implementation.
6β
7## The Virtualizer
8β
9At the heart of TanStack Virtual is the `Virtualizer`. Virtualizers can be oriented on either the vertical (default) or horizontal axes which makes it possible to achieve vertical, horizontal and even grid-like virtualization by combining the two axis configurations together.
10β
11Here is just a quick example of what it looks like to virtualize a long list within a div using TanStack Virtual in React:
12β
13```tsx
14import { useVirtualizer } from '@tanstack/react-virtual';
15β
16function App() {
17 // The scrollable element for your list
18 const parentRef = React.useRef(null)
19β
20 // The virtualizer
21 const rowVirtualizer = useVirtualizer({
22 count: 10000,
23 getScrollElement: () => parentRef.current,
24 estimateSize: () => 35,
25 })
26β
27 return (
28 <>
29 {/* The scrollable element for your list */}
30 <div
31 ref={parentRef}
32 style={{
33 height: `400px`,
34 overflow: 'auto', // Make it scroll!
35 }}
36 >
37 {/* The large inner element to hold all of the items */}
38 <div
39 style={{
40 height: `${rowVirtualizer.getTotalSize()}px`,
41 width: '100%',
42 position: 'relative',
43 }}
44 >
45 {/* Only the visible items in the virtualizer, manually positioned to be in view */}
46 {rowVirtualizer.getVirtualItems().map((virtualItem) => (
47 <div
48 key={virtualItem.key}
49 style={{
50 position: 'absolute',
51 top: 0,
52 left: 0,
53 width: '100%',
54 height: `${virtualItem.size}px`,
55 transform: `translateY(${virtualItem.start}px)`,
56 }}
57 >
58 Row {virtualItem.index}
59 </div>
60 ))}
61 </div>
62 </div>
63 </>
64 )
65}
66```
67β
68Let's dig into some more examples!