Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

react-cool-virtual

> 前端框架
Open source

♻️ A tiny React hook for rendering large datasets like a breeze.

1.2K stars0 likes3 views
WebsiteGitHub

About

♻️ A tiny React hook for rendering large datasets like a breeze.

A tiny React hook for rendering large datasets like a breeze.

Features

  • ♻️ Renders millions of items with highly performant way, using DOM recycling.
  • Easy to use, based on React hook.
  • Apply styles without hassle, just few setups.
  • Supports fixed, variable, dynamic, and real-time heights/widths.
  • Supports responsive web design (RWD) for better UX.
  • Supports sticky headers for building on-trend lists.
  • Built-ins load more callback for you to deal with infinite scroll + skeleton screens.
  • Imperative scroll-to methods for offset, items, and alignment.
  • Out-of-the-box smooth scrolling and the effect is DIY-able.
  • It's possible to implement stick to bottom and pre-pending items for chat, feeds, etc.
  • ⛳ Provides isScrolling indicator to you for UI placeholders or performance optimization.
  • ️ Supports server-side rendering (SSR) for a fast FP + FCP and better SEO.
  • Supports TypeScript type definition.
  • Super flexible API design, built with DX in mind.
  • Tiny size (~ 3.1kB gzipped). No external dependencies, aside from the react.

Why?

When rendering a large set of data (e.g. list, table, etc.) in React, we all face performance/memory troubles. There're some great libraries already available but most of them are component-based solutions that provide well-defineded way of using but increase a lot of bundle size. However, a library comes out as a hook-based solution that is flexible and headless but using and styling it can be verbose (because it's a low-level hook). Furthermore, it lacks many of the useful features.

React Cool Virtual is a tiny React hook that gives you a better DX and modern way for virtualizing a large amount of data without struggle .

Docs

  • Getting Started
    • Requirement
    • Installation
    • CDN
    • Basic Usage
  • Examples
    • Fixed Size
    • Variable Size
    • Dynamic Size
    • Real-time Resize
    • Responsive Web Design (RWD)
    • Sticky Headers
    • Scroll to Offset / Items
    • Smooth Scrolling
    • Infinite Scroll
    • Pre-pending Items
    • Filtering Items
    • Sticking to Bottom
    • Working with Input Elements
    • Dealing with Dynamic Items
    • Server-side Rendering (SSR)
  • API
    • Options
    • Return Values
  • Others
    • Performance Optimization
    • How to Share A ref?
    • Layout Items
    • Working in TypeScript
    • ResizeObserver Polyfill

Getting Started

Requirement

To use React Cool Virtual, you must use [email protected] or greater which includes hooks.

Installation

This package is distributed via npm.

bash
$ yarn add react-cool-virtual
# or
$ npm install --save react-cool-virtual

⚠️ This package using ResizeObserver API under the hook. Most modern browsers support it natively, you can also add polyfill for full browser support.

CDN

If you're not using a module bundler or package manager. We also provide a UMD build which is available over the unpkg.com CDN. Simply use a `


Once you've added this you will have access to the `window.ReactCoolVirtual.useVirtual` variable.

### Basic Usage

Here's the basic concept of how it rocks:

```js
import useVirtual from "react-cool-virtual";

const List = () => {
  const { outerRef, innerRef, items } = useVirtual({
    itemCount: 10000, // Provide the total number for the list items
    itemSize: 50, // The size of each item (default = 50)
  });

  return (
    

        ))}
      
    
  );
};

✨ Pretty easy right? React Cool Virtual is more powerful than you think. Let's explore more use cases through the examples!

Examples

Fixed Size

This example demonstrates how to create a fixed size row. For column or grid, please refer to CodeSandbox.

javascript
import useVirtual from "react-cool-virtual";

const List = () => {
  const { outerRef, innerRef, items } = useVirtual({
    itemCount: 1000,
  });

  return (
    

        ))}
      
    
  );
};

Variable Size

This example demonstrates how to create a variable size row. For column or grid, please refer to CodeSandbox.

javascript
import useVirtual from "react-cool-virtual";

const List = () => {
  const { outerRef, innerRef, items } = useVirtual({
    itemCount: 1000,
    itemSize: (idx) => (idx % 2 ? 100 : 50),
  });

  return (
    

        ))}
      
    
  );
};

Dynamic Size

This example demonstrates how to create a dynamic (unknown) size row. For column or grid, please refer to CodeSandbox.

javascript
import useVirtual from "react-cool-virtual";

const List = () => {
  const { outerRef, innerRef, items } = useVirtual({
    itemCount: 1000,
    itemSize: 75, // The unmeasured item sizes will refer to this value (default = 50)
  });

  return (
    

        ))}
      
    
  );
};

The scrollbar is jumping (or unexpected position)? It's because the total size of the items is gradually corrected along with an item that has been measured. You can tweak the itemSize to reduce the phenomenon.

Real-time Resize

This example demonstrates how to create a real-time resize row (e.g. accordion, collapse, etc.). For column or grid, please refer to CodeSandbox.

javascript
import { useState, forwardRef } from "react";
import useVirtual from "react-cool-virtual";

const AccordionItem = forwardRef(({ children, height, ...rest }, ref) => {
  const [h, setH] = useState(height);

  return (
    

  );
});

const List = () => {
  const { outerRef, innerRef, items } = useVirtual({
    itemCount: 50,
  });

  return (
    

    
  );
};

Responsive Web Design (RWD)

This example demonstrates how to create a list with RWD to provide a better UX for the user.

javascript
import useVirtual from "react-cool-virtual";

const List = () => {
  const { outerRef, innerRef, items } = useVirtual({
    itemCount: 1000,
    // Use the outer's width (2nd parameter) to adjust the item's size
    itemSize: (_, width) => (width > 400 ? 50 : 100),
    // The event will be triggered on outer's size changes
    onResize: (size) => console.log("Outer's size: ", size),
  });

  return (
    

        ))}
      
    
  );
};

If the item size is specified through the function of itemSize, please ensure there's no the measureRef on the item element. Otherwise, the hook will use the measured (cached) size for the item. When working with RWD, we can only use either of the two.

Sticky Headers

This example demonstrates how to make sticky headers with React Cool Virtual.

javascript
import useVirtual from "react-cool-virtual";

const List = () => {
  const { outerRef, innerRef, items } = useVirtual({
    itemCount: 1000,
    itemSize: 75,
    stickyIndices: [0, 10, 20, 30, 40, 50], // The values must be provided in ascending order
  });

  return (
    

          );
        })}
      
    
  );
};

…

js
const { scrollTo, scrollToItem } = useVirtual();

const scrollToOffset = () => {
  // Scrolls to 500px
  scrollTo(500, () => {
    //  Do whatever you want through the callback
  });
};

const scrollToItem = () => {
  // Scrolls to the 500th item
  scrollToItem(500, () => {
    //  Do whatever you want through the callback
  });

  // We can control the alignment of the item with the `align` option
  // Acceptable values are: "auto" (default) | "start" | "center" | "end"
  // Using "auto" will scroll the item into the view at the start or end, depending on which is closer
  scrollToItem({ index: 10, align: "auto" });
};

Smooth Scrolling

React Cool Virtual provides the smooth scrolling feature out of the box, all you need to do is turn the smooth option on.

javascript
const { scrollTo, scrollToItem } = useVirtual();

// Smoothly scroll to 500px
const scrollToOffset = () => scrollTo({ offset: 500, smooth: true });

// Smoothly scroll to the 500th item
const scrollToItem = () => scrollToItem({ index: 10, smooth: true });

…


>  For more cool easing effects, please [check it out](https://easings.net).

### Infinite Scroll

It's possible to make a complicated infinite scroll logic simple by just using a hook, no kidding! Let's see how possible .
#### Working with [Skeleton Screens](https://uxdesign.cc/what-you-should-know-about-skeleton-screens-a820c45a571a)

…


#### Working with A Loading Indicator

…


### Pre-pending Items

This example demonstrates how to pre-pend items and maintain scroll position for the user.

…


### Filtering Items

When working with filtering items, we can reset the scroll position when the `itemCount` is changed by enabling the [resetScroll](#resetscroll) option.
```js
import { useState } from "react";
import useVirtual from "react-cool-virtual";

const List = () => {
  const [itemCount, setItemCount] = useState(100);
  const { outerRef, innerRef, items } = useVirtual({
    itemCount,
    // Resets the scroll position when the `itemCount` is changed (default = false)
    resetScroll: true,
  });

  return (
    

        ))}
      
    
  );
};

Sticking to Bottom

This example demonstrates the scenario of sticking/unsticking the scroll position to the bottom for a chatroom.

…

Working with Input Elements

This example demonstrates how to handle input elements (or form fields) in a virtualized list.

…

When dealing with forms, we can use React Cool Form to handle the form state and boost performance for use.

javascript
import useVirtual from "react-cool-virtual";
import { useForm } from "react-cool-form";

const defaultValues = new Array(20).fill(false);

const Form = () => {
  const { outerRef, innerRef, items } = useVirtual({
    itemCount: defaultValues.length,
  });
  const { form } = useForm({
    defaultValues: { todo: defaultValues },
    removeOnUnmounted: false, // To keep the value of unmounted fields
    onSubmit: (formData) => alert(JSON.stringify(formData, undefined, 2)),
  });

  return (
    
      

          ))}
        
      
      
    
  );
};

Dealing with Dynamic Items

React requires keys for array items. I'd recommend using an unique id as the key as possible as we can, especially when working with reordering, filtering, etc. Refe

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptcarouselchatroomdom-recycledynamic-size

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架