このドキュメントは、React アプリケーションで大規模なリストや無限スクロールを実装する際の主要なライブラリを比較しています。react-virtualized、react-window、react-virtual などの仮想化ライブラリから、react-infinite、react-list などの従来の無限スクロールコンポーネントまで、それぞれのアーキテクチャ、API デザイン、メンテナンス状況を分析します。パフォーマンス、柔軟性、開発者体験の観点から、プロジェクトに適した技術選定を行うための指針を提供します。
大規模なリストを扱う際、すべてのアイテムを一度にレンダリングすると、ブラウザが重くなりユーザー体験が損なわれます。これを解決するため、表示されている部分だけを描画する「ウィンドウイング(仮想化)」技術や、スクロールに応じてデータを読み込む「無限スクロール」実装が必要になります。
今回は、react-infinite、react-list、react-virtual、react-virtualized、react-window、react-window-infinite-loader の 6 つのパッケージを比較し、それぞれの技術的特徴と適切な使いどころを明確にします。
ライブラリを選ぶ際、まず「完成されたコンポーネントを提供するか」それとも「ロジックだけを提供するか(ヘッドレス)」という違いを理解する必要があります。
react-virtualized と react-window は、高度に最適化されたコンポーネントを提供します。
// react-window: 完成されたコンポーネントを使用
import { FixedSizeList } from 'react-window';
const Row = ({ index, style }) => (
<div style={style}>Row {index}</div>
);
const List = () => (
<FixedSizeList height={600} itemCount={1000} itemSize={35}>
{Row}
</FixedSizeList>
);
react-virtual はヘッドレスアプローチを採用しています。
// react-virtual: 仮想化ロジックだけをフックで取得
import { useVirtual } from 'react-virtual';
import { useRef } from 'react';
const List = () => {
const parentRef = useRef();
const virtual = useVirtual({
size: 1000,
parentRef,
estimateSize: () => 35,
});
return (
<div ref={parentRef} style={{ height: '600px', overflow: 'auto' }}>
<div style={{ height: `${virtual.totalSize}px` }}>
{virtual.virtualItems.map(item => (
<div key={item.key} style={{ position: 'absolute', top: 0, transform: `translateY(${item.start}px)` }}>
Row {item.index}
</div>
))}
</div>
</div>
);
};
react-infinite と react-list は、より単純なコンポーネント型ですが、内部最適化は古いです。
// react-infinite: 単純なコンポーネント構成
import Infinite from 'react-infinite';
const List = () => (
<Infinite elementHeight={50} containerHeight={600}>
<div>Item 1</div>
<div>Item 2</div>
{/* 子要素をすべて渡す必要がある場合が多い */}
</Infinite>
);
ライブラリの長期的なメンテナンスは、プロジェクトの安定性に直結します。
react-virtualized は事実上メンテナンスモードに入っています。
react-window への移行が推奨されています。react-window が現在のデファクトスタンダードです。
react-virtualized の欠点を解消するために作られました。react-infinite と react-list は更新頻度が低いです。
react-virtual は活発にメンテナンスされています。
リストが無限に続く場合(ページネーションなしでスクロールするとデータが追加される)、追加のロジックが必要になります。
react-window-infinite-loader は、react-window のために作られた専用ラッパーです。
react-window と組み合わせることで、高性能な無限スクロールが実現できます。// react-window-infinite-loader: 無限ローディングの管理
import InfiniteLoader from 'react-window-infinite-loader';
import { FixedSizeList } from 'react-window';
const isItemLoaded = (index) => !!items[index];
const loadMoreItems = (startIndex, stopIndex) => {
// API を叩いてデータを取得し、items 配列を更新する
};
const List = () => (
<InfiniteLoader
isItemLoaded={isItemLoaded}
itemCount={items.length}
loadMoreItems={loadMoreItems}
>
{({ onItemsRendered, ref }) => (
<FixedSizeList
itemCount={items.length}
itemSize={35}
onItemsRendered={onItemsRendered}
ref={ref}
>
{Row}
</FixedSizeList>
)}
</InfiniteLoader>
);
react-virtual にも同様の機能がありますが、より低レベルです。
// react-virtual: 自前でローディングロジックを制御
const virtual = useVirtual({
size: items.length,
parentRef,
estimateSize: () => 35,
overscan: 5,
});
// onScroll などで末端に達したことを検知し、loadMoreItems を発火させる必要がある
react-infinite は無限スクロールが主目的です。
// react-infinite: 無限スクロールイベントをトリガーに使用
<Infinite
useWindowAsScrollContainer
onInfiniteLoad={loadMoreItems}
loadingSpinnerContainer={<div>Loading...</div>}
>
{items.map(item => <div key={item.id}>{item.name}</div>)}
</Infinite>
大量のデータを扱う場合、いかに DOM 操作を減らすかが鍵になります。
| 特徴 | react-window / react-virtualized | react-virtual | react-infinite / react-list |
|---|---|---|---|
| レンダリング | 表示領域のみレンダリング(ウィンドウイング) | 表示領域のみレンダリング | 全要素を保持しようとする傾向 |
| DOM 更新 | 最小限に抑えられた更新 | 開発者の実装に依存 | 頻繁な更新が発生しやすい |
| スクロール | 専用コンテナでのスクロール制御 | 任意のコンテナで制御可能 | ウィンドウスクロールに依存しがち |
| メモリ | 効率的 | 効率的 | 高負荷になりやすい |
react-window は、アイテムのサイズが固定か変動かに応じてコンポーネントを使い分けます。
FixedSizeList: 高速。すべてのアイテムが同じ高さの場合。VariableSizeList: 柔軟。アイテムの高さが異なる場合(計算コストが少し増える)。// 固定サイズの場合(最速)
<FixedSizeList itemSize={50} ... />
// 可変サイズの場合(柔軟)
<VariableSizeList itemSize={index => getHeight(index)} ... />
react-virtualized も同様の機能を持ちますが、API が複雑です。
List、Grid、Table など用途別にコンポーネントが細分化されています。実装のしやすさや、デザインのカスタマイズ自由度も重要な選定基準です。
react-window はバランスが良いです。
react-virtual はカスタマイズ性が最高です。
// react-virtual: 自由な HTML 構造が可能
<div ref={parentRef}>
{virtualItems.map(item => (
<div key={item.key} style={{ ...item.measurements }}>
{/* 中に何をいれても自由 */}
</div>
))}
</div>
react-infinite は簡単ですが制限があります。
プロジェクトの要件に応じて、以下のように選定するのが合理的です。
| 要件 | 推奨ライブラリ | 理由 |
|---|---|---|
| 標準的なリスト | react-window | 軽量、高速、メンテナンスが活発 |
| 無限スクロール | react-window + react-window-infinite-loader | 組み合わせることで最強の性能と機能 |
| 完全なカスタマイズ | react-virtual | UI 制約がなく、ロジックのみ借用できる |
| レガシー維持 | react-virtualized | 既存実装がある場合のみ。新規は非推奨 |
| シンプルな実装 | react-infinite | 非推奨。データ量少ならありだがリスク大 |
| 他フレームワーク | react-virtual | 核心ロジックがフレームワーク非依存 |
2024 年以降の React プロジェクトにおいて、リストの仮想化が必要な場合、react-window が最も堅実な選択です。特に react-window-infinite-loader と組み合わせることで、無限スクロールの課題も解決できます。API が直感的で、コミュニティのサポートも厚いため、トラブルが起きた際の解決も容易です。
一方、デザインシステムを自作していたり、リスト以外の複雑な仮想化(例えば、巨大なカレンダーやグリッド)が必要な場合は、react-virtual が優れた選択肢となります。ヘッドレスであるため、コンポーネントの設計に縛られず、パフォーマンスとデザインを両立できます。
react-virtualized、react-infinite、react-list については、既存プロジェクトのメンテナンス以外で新規に採用する理由はほとんどありません。これらは過去の技術的アプローチを反映しており、現代の React のパフォーマンス要件やエコシステムに最適化されていません。
技術選定では「できること」だけでなく「維持できること」も重要です。アクティブにメンテナンスされ、現代の Web 標準に沿ったライブラリを選ぶことが、長期的なプロジェクトの成功につながります。
既存のレガシーコードベースで既に導入されており、移行コストが大きい場合を除き、新規プロジェクトでは避けるべきです。react-window によって置き換えられた経緯があり、バンドルサイズや API の複雑さで劣ります。
react-window を使用していて、さらに無限ローディング(スクロールに応じたデータ取得)機能が必要な場合に選択します。単体ではなく react-window と組み合わせて使用する専用コンポーネントです。
フレームワークに依存しないロジック(ヘッドレス)を求めたり、Vue や Svelte などの他フレームワークとの共用を想定する場合に最適です。TanStack ecosystem との親和性が高く、カスタマイズ性を重視するプロジェクトに向いています。
シンプルな実装で十分な場合や、非常に古いコードベースの維持が必要な場合に限り考慮します。機能性やパフォーマンス面で現代的な仮想化ライブラリに劣るため、新規開発では他の選択肢を優先してください。
レガシープロジェクトのメンテナンスを行う場合を除き、新規プロジェクトでの使用は推奨しません。メンテナンスが停滞しており、現代的な React のパターン(Hooks など)に準拠していないため、代替ライブラリへの移行を検討すべきです。
React 環境で標準的な仮想化リストを実装する際のファーストチョイスです。Brian Vaughn によって維持されており、API がシンプルで軽量、かつパフォーマンスが優れています。ほとんどのユースケースでバランスが最も取れています。
React components for efficiently rendering large lists and tabular data. Check out the demo for some examples.
The following wonderful companies have sponsored react-virtualized:
Learn more about becoming a sponsor!
react-windowIf you're considering adding react-virtualized to a project, take a look at react-window as a possible lighter-weight alternative. Learn more about how the two libraries compare here.
Install react-virtualized using npm.
npm install react-virtualized --save
ES6, CommonJS, and UMD builds are available with each distribution. For example:
// Most of react-virtualized's styles are functional (eg position, size).
// Functional styles are applied directly to DOM elements.
// The Table component ships with a few presentational styles as well.
// They are optional, but if you want them you will need to also import the CSS file.
// This only needs to be done once; probably during your application's bootstrapping process.
import 'react-virtualized/styles.css';
// You can import any component you want as a named export from 'react-virtualized', eg
import {Column, Table} from 'react-virtualized';
// But if you only use a few react-virtualized components,
// And you're concerned about increasing your application's bundle size,
// You can directly import only the components you need, like so:
import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer';
import List from 'react-virtualized/dist/commonjs/List';
Note webpack 4 makes this optimization itself, see the documentation.
If the above syntax looks too cumbersome, or you import react-virtualized components from a lot of places, you can also configure a Webpack alias. For example:
// Partial webpack.config.js
{
alias: {
'react-virtualized/List': 'react-virtualized/dist/es/List',
},
...rest
}
Then you can just import like so:
import List from 'react-virtualized/List';
// Now you can use <List {...props} />
You can also use a global-friendly UMD build:
<link rel="stylesheet" href="path-to-react-virtualized/styles.css" />
<script src="path-to-react-virtualized/dist/umd/react-virtualized.js"></script>
Now you're ready to start using the components. You can learn more about which components react-virtualized has to offer below.
React Virtualized has very few dependencies and most are managed by NPM automatically.
However the following peer dependencies must be specified by your project in order to avoid version conflicts:
react,
react-dom.
NPM will not automatically install these for you but it will show you a warning message with instructions on how to install them.
By default all react-virtualized components use shallowCompare to avoid re-rendering unless props or state has changed.
This occasionally confuses users when a collection's data changes (eg ['a','b','c'] => ['d','e','f']) but props do not (eg array.length).
The solution to this is to let react-virtualized know that something external has changed. This can be done a couple of different ways.
The shallowCompare method will detect changes to any props, even if they aren't declared as propTypes.
This means you can also pass through additional properties that affect cell rendering to ensure changes are detected.
For example, if you're using List to render a list of items that may be re-sorted after initial render- react-virtualized would not normally detect the sort operation because none of the properties it deals with change.
However you can pass through the additional sort property to trigger a re-render.
For example:
<List {...listProps} sortBy={sortBy} />
Grid and Collection components can be forcefully re-rendered using forceUpdate.
For Table and List, you'll need to call forceUpdateGrid to ensure that the inner Grid is also updated. For MultiGrid, you'll need to call forceUpdateGrids to ensure that the inner Grids are updated.
API documentation available here.
There are also a couple of how-to guides:
Examples for each component can be seen in the documentation.
Here are some online demos of each component:
And here are some "recipe" type demos:
react-virtualized aims to support all evergreen browsers and recent mobile browsers for iOS and Android. IE 9+ is also supported (although IE 9 will require some user-defined, custom CSS since flexbox layout is not supported).
If you find a browser-specific problem, please report it along with a repro case. The easiest way to do this is probably by forking this Plunker.
Here are some great components built on top of react-virtualized:
Use GitHub issues for requests.
I actively welcome pull requests; learn how to contribute.
Changes are tracked in the changelog.
react-virtualized is available under the MIT License.