Skip to content
Shubham JangleResources
React

Explore topics

DOM recycling / Windowing / List Virtualization

Watch the walkthrough
Open on Instagram

Quick revision

Virtualization renders a small visible window of a long list, plus overscan, instead of mounting every record. It reduces DOM nodes, layout, and rendering work. Infinite loading changes how many records are fetched; virtualization changes how many are mounted. They solve different costs and can be combined.

Read the copied comparison

Virtualisation.tsx is the baseline: it maps all 100,000 records despite its title. VirtualisationWindow.tsx uses FixedSizeList and AutoSizer. Apply the supplied row style so the library can position each row. The source declares 84 px rows, so the content and spacing must actually fit that height.

This example targets react-window v1 (^1.8.10 in the repository package.json), not the newer API. Keep the matching dependency when reproducing it. AutoSizer needs a parent with usable dimensions. The 100,000-record array is still allocated: reducing DOM size does not remove data-memory cost.

Revision checks

Compare DOM node count and scroll traces against the baseline. Check focus, long text, resizing, stable item keys for reordering, and screen-reader behavior. Browser find-in-page may not find rows that are not mounted. Variable-height rows require measurement and a compatible list strategy.

Reference: react-window v1 documentation.

Original repository source

These are the supplied checkout's React 18/Vite examples. Code is preserved apart from line endings; revision recommendations above are separate. Use the original project dependencies and configuration when running them.

Virtualisation.tsx

Original file on GitHub

/* eslint-disable @typescript-eslint/no-unused-vars */
import { useState } from "react";

const generateData = (start: number, end: number) => {
  return Array.from({ length: end - start }, (_, index) => ({
    id: start + index,
    title: `Card Title ${start + index}`,
    description: `This is a description for card ${start + index}`,
  }));
};

const Virtualisation = () => {
  const [items, setItems] = useState(() => generateData(0, 100000)); // Generate initial data for virtualization

  return (
    <div className="container mx-auto p-6">
      <h1 className="text-2xl font-bold mb-4">
        Virtualized Infinite Scroll Demo
      </h1>
      <div style={{ height: 600, overflowY: "auto" }}>
        {items.map((item, index) => (
          <Row key={index} item={item} />
        ))}
      </div>
    </div>
  );
};

const Row = ({ item }: { item: { title: string; description: string } }) => {
  return (
    <div className="p-4 bg-orange-100 rounded-lg shadow-md mb-2">
      <h2 className="text-lg font-bold">{item.title}</h2>
      <p className="text-sm text-gray-600">{item.description}</p>
    </div>
  );
};

export default Virtualisation;

VirtualisationWindow.tsx

Original file on GitHub

/* eslint-disable @typescript-eslint/no-unused-vars */
import React, { useState } from "react";
import { FixedSizeList as List } from "react-window";
import AutoSizer from "react-virtualized-auto-sizer";

const generateData = (start: number, end: number) => {
  return Array.from({ length: end - start }, (_, index) => ({
    id: start + index,
    title: `Card Title ${start + index}`,
    description: `This is a description for card ${start + index}`,
  }));
};

const Row = ({
  index,
  style,
  data,
}: {
  index: number;
  style: React.CSSProperties;
  data: { items: Array<{ title: string; description: string }> };
}) => {
  const item = data.items[index];
  return (
    <div style={style} className="p-4 bg-orange-100 rounded-lg shadow-md mb-2">
      <h2 className="text-lg font-bold">{item.title}</h2>
      <p className="text-sm text-gray-600">{item.description}</p>
    </div>
  );
};

const VirtualisationWindow = () => {
  const [items, setItems] = useState(() => generateData(0, 100000));

  return (
    <div className="container mx-auto p-6">
      <h1 className="text-2xl font-bold mb-4">
        Virtualized Infinite Scroll Demo
      </h1>
      <div style={{ height: 600, width: "100%" }}>
        <AutoSizer>
          {({ height, width }) => (
            <List
              height={height}
              width={width}
              itemCount={items.length}
              itemSize={84} // Approximate height of each row
              itemData={{ items }}
            >
              {Row}
            </List>
          )}
        </AutoSizer>
      </div>
    </div>
  );
};

export default VirtualisationWindow;