React Virtual (Legacy)

2.10.4 · abandoned · verified Sun Apr 19

React Virtual (version 2.10.4) is a JavaScript library providing a single headless hook (`useVirtual`) for efficiently virtualizing scrollable elements in React applications. It enables the rendering of massive lists, grids, and tables by only mounting the visible items in the DOM, drastically improving performance and memory usage for large datasets. This version, last updated over three years ago, was the predecessor to the more actively maintained and feature-rich `@tanstack/react-virtual` (version 3.x.x), part of the broader TanStack ecosystem. While still functional, it is no longer actively developed or recommended for new projects, with development having shifted entirely to the TanStack-branded successor.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates basic row virtualization with fixed item heights, showing how to set up the scroll container and render only the visible rows using the `useVirtual` hook.

import React, { useRef, useCallback, CSSProperties } from 'react';
import { useVirtual } from 'react-virtual';

const RowVirtualizerFixed = () => {
  const parentRef = useRef<HTMLDivElement>(null);
  const rowCount = 10000;

  const rowVirtualizer = useVirtual({
    size: rowCount,
    parentRef,
    estimateSize: useCallback(() => 35, []), // Fixed item height
    overscan: 5,
  });

  return (
    <div
      ref={parentRef}
      style={{
        height: `300px`,
        width: `100%`,
        overflow: `auto`,
      }}
    >
      <div
        style={{
          height: `${rowVirtualizer.totalSize}px`,
          width: `100%`,
          position: `relative`,
        }}
      >
        {rowVirtualizer.virtualItems.map(virtualRow => (
          <div
            key={virtualRow.index}
            style={{
              position: `absolute`,
              top: 0,
              left: 0,
              width: `100%`,
              height: `${virtualRow.size}px`,
              transform: `translateY(${virtualRow.start}px)`,
              background: virtualRow.index % 2 ? '#eee' : '#fafafa',
              display: 'flex',
              alignItems: 'center',
              paddingLeft: '10px',
              borderBottom: '1px solid #ddd',
              boxSizing: 'border-box'
            }}
          >
            Row {virtualRow.index}
          </div>
        ))}
      </div>
    </div>
  );
};

export default RowVirtualizerFixed;

view raw JSON →