Convert Figma logo to code with AI

bvaughn logoreact-window

React components for efficiently rendering large lists and tabular data

17,045
815
17,045
5

Top Related Projects

6,628

🤖 Headless UI for Virtualizing Large Element Lists in JS/TS, React, Solid, Vue and Svelte

React components for efficiently rendering large lists and tabular data

The most powerful virtual list component for React

6,626

🤖 Headless UI for Virtualizing Large Element Lists in JS/TS, React, Solid, Vue and Svelte

A tiny but mighty 3kb list virtualization library, with zero dependencies 💪 Supports variable heights/widths, sticky items, scrolling to index, and more!

Quick Overview

React Window is a lightweight, efficient windowing library for React applications. It enables rendering large lists and tabular data with improved performance by only rendering visible items. This library is particularly useful for handling large datasets in web applications.

Pros

  • Significantly improves performance for large lists and grids
  • Supports both fixed-size and variable-size lists and grids
  • Lightweight and has minimal dependencies
  • Easy to integrate with existing React applications

Cons

  • Limited built-in features compared to some other windowing libraries
  • May require additional configuration for complex use cases
  • Learning curve for developers new to windowing concepts
  • Some edge cases might require custom implementations

Code Examples

  1. Basic fixed-size list:
import { FixedSizeList } from 'react-window';

const Row = ({ index, style }) => (
  <div style={style}>Row {index}</div>
);

const Example = () => (
  <FixedSizeList
    height={150}
    itemCount={1000}
    itemSize={35}
    width={300}
  >
    {Row}
  </FixedSizeList>
);
  1. Variable-size grid:
import { VariableSizeGrid } from 'react-window';

const Cell = ({ columnIndex, rowIndex, style }) => (
  <div style={style}>
    Item {rowIndex},{columnIndex}
  </div>
);

const Example = () => (
  <VariableSizeGrid
    columnCount={1000}
    columnWidth={index => (index % 3 === 0 ? 100 : 50)}
    height={150}
    rowCount={1000}
    rowHeight={index => (index % 2 === 0 ? 50 : 30)}
    width={300}
  >
    {Cell}
  </VariableSizeGrid>
);
  1. Using with dynamic content:
import { FixedSizeList } from 'react-window';
import AutoSizer from 'react-virtualized-auto-sizer';

const Row = ({ index, style, data }) => (
  <div style={style}>{data[index]}</div>
);

const Example = ({ items }) => (
  <AutoSizer>
    {({ height, width }) => (
      <FixedSizeList
        height={height}
        itemCount={items.length}
        itemSize={35}
        width={width}
        itemData={items}
      >
        {Row}
      </FixedSizeList>
    )}
  </AutoSizer>
);

Getting Started

  1. Install the package:

    npm install react-window
    
  2. Import and use in your React component:

    import { FixedSizeList } from 'react-window';
    
    const MyList = () => (
      <FixedSizeList
        height={400}
        width={300}
        itemSize={50}
        itemCount={1000}
      >
        {({ index, style }) => (
          <div style={style}>Row {index}</div>
        )}
      </FixedSizeList>
    );
    
  3. Customize as needed for your specific use case, considering different list types (FixedSizeList, VariableSizeList) or grid components (FixedSizeGrid, VariableSizeGrid) based on your requirements.

Competitor Comparisons

6,628

🤖 Headless UI for Virtualizing Large Element Lists in JS/TS, React, Solid, Vue and Svelte

Pros of Virtual

  • Framework-agnostic, supporting React, Vue, Solid, and more
  • More flexible API with advanced features like dynamic item sizes and sticky items
  • Active development and community support

Cons of Virtual

  • Steeper learning curve due to more complex API
  • Potentially larger bundle size due to additional features

Code Comparison

React Window:

import { FixedSizeList } from 'react-window';

const Example = () => (
  <FixedSizeList
    height={150}
    itemCount={1000}
    itemSize={35}
    width={300}
  >
    {({ index, style }) => <div style={style}>Row {index}</div>}
  </FixedSizeList>
);

Virtual:

import { useVirtualizer } from '@tanstack/react-virtual';

const Example = () => {
  const virtualizer = useVirtualizer({
    count: 1000,
    getScrollElement: () => parentRef.current,
    estimateSize: () => 35,
  });

  return (
    <div ref={parentRef} style={{ height: '150px', overflow: 'auto' }}>
      <div style={{ height: `${virtualizer.getTotalSize()}px` }}>
        {virtualizer.getVirtualItems().map((virtualItem) => (
          <div key={virtualItem.key} style={{
            position: 'absolute',
            top: 0,
            left: 0,
            width: '100%',
            height: `${virtualItem.size}px`,
            transform: `translateY(${virtualItem.start}px)`,
          }}>
            Row {virtualItem.index}
          </div>
        ))}
      </div>
    </div>
  );
};

React components for efficiently rendering large lists and tabular data

Pros of react-virtualized

  • More comprehensive feature set, including support for various layouts (Grid, List, Table, etc.)
  • Offers additional utilities like CellMeasurer for dynamic content sizing
  • Mature project with extensive documentation and community support

Cons of react-virtualized

  • Larger bundle size due to its comprehensive nature
  • More complex API, which can lead to a steeper learning curve
  • Higher memory usage, especially for large datasets

Code Comparison

react-virtualized:

import { List } from 'react-virtualized';

<List
  width={300}
  height={300}
  rowCount={1000}
  rowHeight={20}
  rowRenderer={({ index, key, style }) => (
    <div key={key} style={style}>Row {index}</div>
  )}
/>

react-window:

import { FixedSizeList } from 'react-window';

<FixedSizeList
  height={300}
  width={300}
  itemCount={1000}
  itemSize={20}
>
  {({ index, style }) => <div style={style}>Row {index}</div>}
</FixedSizeList>

Both libraries serve similar purposes, but react-window is a more lightweight and focused alternative to react-virtualized. It offers improved performance and a simpler API, making it easier to use for basic virtualization needs. However, react-virtualized remains a powerful choice for complex use cases requiring advanced features and layouts.

The most powerful virtual list component for React

Pros of react-virtuoso

  • More flexible and feature-rich, supporting dynamic heights and complex layouts
  • Built-in support for grouping, sticky headers, and footer
  • Easier to use with less boilerplate code required

Cons of react-virtuoso

  • Slightly larger bundle size due to additional features
  • May have a steeper learning curve for simpler use cases
  • Less established in the React ecosystem compared to react-window

Code Comparison

react-window:

import { FixedSizeList } from 'react-window';

<FixedSizeList
  height={400}
  itemCount={1000}
  itemSize={35}
  width={300}
>
  {({ index, style }) => <div style={style}>Row {index}</div>}
</FixedSizeList>

react-virtuoso:

import { Virtuoso } from 'react-virtuoso';

<Virtuoso
  style={{ height: '400px', width: '300px' }}
  totalCount={1000}
  itemContent={index => <div>Row {index}</div>}
/>

Both libraries aim to efficiently render large lists in React applications, but react-virtuoso offers more built-in features and flexibility at the cost of a slightly larger bundle size. react-window is more lightweight and may be preferable for simpler use cases, while react-virtuoso shines in complex scenarios with dynamic content and layouts.

6,626

🤖 Headless UI for Virtualizing Large Element Lists in JS/TS, React, Solid, Vue and Svelte

Pros of Virtual

  • Framework-agnostic, supporting React, Vue, Solid, and more
  • More flexible API with advanced features like dynamic item sizes and sticky items
  • Active development and community support

Cons of Virtual

  • Steeper learning curve due to more complex API
  • Potentially larger bundle size due to additional features

Code Comparison

React Window:

import { FixedSizeList } from 'react-window';

const Example = () => (
  <FixedSizeList
    height={150}
    itemCount={1000}
    itemSize={35}
    width={300}
  >
    {({ index, style }) => <div style={style}>Row {index}</div>}
  </FixedSizeList>
);

Virtual:

import { useVirtualizer } from '@tanstack/react-virtual';

const Example = () => {
  const virtualizer = useVirtualizer({
    count: 1000,
    getScrollElement: () => parentRef.current,
    estimateSize: () => 35,
  });

  return (
    <div ref={parentRef} style={{ height: '150px', overflow: 'auto' }}>
      <div style={{ height: `${virtualizer.getTotalSize()}px` }}>
        {virtualizer.getVirtualItems().map((virtualItem) => (
          <div key={virtualItem.key} style={{
            position: 'absolute',
            top: 0,
            left: 0,
            width: '100%',
            height: `${virtualItem.size}px`,
            transform: `translateY(${virtualItem.start}px)`,
          }}>
            Row {virtualItem.index}
          </div>
        ))}
      </div>
    </div>
  );
};

A tiny but mighty 3kb list virtualization library, with zero dependencies 💪 Supports variable heights/widths, sticky items, scrolling to index, and more!

Pros of react-tiny-virtual-list

  • Smaller bundle size, making it more lightweight for simpler use cases
  • Easier to set up and use for basic virtualization needs
  • Supports both fixed and variable height items out of the box

Cons of react-tiny-virtual-list

  • Less actively maintained compared to react-window
  • Fewer features and customization options
  • Limited support for horizontal lists and grid layouts

Code Comparison

react-tiny-virtual-list:

import VirtualList from 'react-tiny-virtual-list';

<VirtualList
  width={300}
  height={600}
  itemCount={10000}
  itemSize={50}
  renderItem={({ index, style }) => (
    <div style={style}>Item {index}</div>
  )}
/>

react-window:

import { FixedSizeList } from 'react-window';

<FixedSizeList
  height={600}
  width={300}
  itemCount={10000}
  itemSize={50}
>
  {({ index, style }) => (
    <div style={style}>Item {index}</div>
  )}
</FixedSizeList>

Both libraries offer similar basic functionality for rendering large lists efficiently. react-tiny-virtual-list provides a simpler API, while react-window offers more advanced features and better performance for complex use cases. The choice between them depends on the specific requirements of your project and the level of customization needed.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

react-window logo

react-window is a component library that helps render large lists of data quickly and without the performance problems that often go along with rendering a lot of data. It's used in a lot of places, from React DevTools to the Replay browser.

Support

If you like this project there are several ways to support it:

The following wonderful companies and individuals have sponsored react-window:

Installation

Begin by installing the library from NPM:

npm install react-window

TypeScript types

TypeScript definitions are included within the published dist folder

FAQs

Frequently asked questions can be found here.

Documentation

Documentation for this project is available at react-window.vercel.app; version 1.x documentation can be found at react-window-v1.vercel.app.

List

Renders data with many rows.

Required props

Name Description
rowComponent

React component responsible for rendering a row.

This component will receive an index and style prop by default. Additionally it will receive prop values passed to rowProps.

ℹ️ The prop types for this component are exported as RowComponentProps

rowCount

Number of items to be rendered in the list.

rowHeight

Row height; the following formats are supported:

  • number of pixels (number)
  • percentage of the grid's current height (string)
  • function that returns the row height (in pixels) given an index and cellProps
  • dynamic row height cache returned by the useDynamicRowHeight hook

⚠️ Dynamic row heights are not as efficient as predetermined sizes. It's recommended to provide your own height values if they can be determined ahead of time.

rowProps

Additional props to be passed to the row-rendering component. List will automatically re-render rows when values in this object change.

⚠️ This object must not contain ariaAttributes, index, or style props.

Optional props

Name Description
className

CSS class name.

style

Optional CSS properties. The list of rows will fill the height defined by this style.

children

Additional content to be rendered within the list (above cells). This property can be used to render things like overlays or tooltips.

defaultHeight

Default height of list for initial render. This value is important for server rendering.

listRef

Ref used to interact with this component's imperative API.

This API has imperative methods for scrolling and a getter for the outermost DOM element.

ℹ️ The useListRef and useListCallbackRef hooks are exported for convenience use in TypeScript projects.

onResize

Callback notified when the List's outermost HTMLElement resizes. This may be used to (re)scroll a row into view.

onRowsRendered

Callback notified when the range of visible rows changes.

overscanCount

How many additional rows to render outside of the visible area. This can reduce visual flickering near the edges of a list when scrolling.

tagName

Can be used to override the root HTML element rendered by the List component. The default value is "div", meaning that List renders an HTMLDivElement as its root.

⚠️ In most use cases the default ARIA roles are sufficient and this prop is not needed.

Grid

Renders data with many rows and columns.

Required props

Name Description
cellComponent

React component responsible for rendering a cell.

This component will receive an index and style prop by default. Additionally it will receive prop values passed to cellProps.

ℹ️ The prop types for this component are exported as CellComponentProps

cellProps

Additional props to be passed to the cell-rendering component. Grid will automatically re-render cells when values in this object change.

⚠️ This object must not contain ariaAttributes, columnIndex, rowIndex, or style props.

columnCount

Number of columns to be rendered in the grid.

columnWidth

Column width; the following formats are supported:

  • number of pixels (number)
  • percentage of the grid's current width (string)
  • function that returns the row width (in pixels) given an index and cellProps
rowCount

Number of rows to be rendered in the grid.

rowHeight

Row height; the following formats are supported:

  • number of pixels (number)
  • percentage of the grid's current height (string)
  • function that returns the row height (in pixels) given an index and cellProps

Optional props

Name Description
className

CSS class name.

dir

Indicates the directionality of grid cells.

ℹ️ See HTML dir global attribute for more information.

style

Optional CSS properties. The grid of cells will fill the height and width defined by this style.

children

Additional content to be rendered within the grid (above cells). This property can be used to render things like overlays or tooltips.

defaultHeight

Default height of grid for initial render. This value is important for server rendering.

defaultWidth

Default width of grid for initial render. This value is important for server rendering.

gridRef

Imperative Grid API.

ℹ️ The useGridRef and useGridCallbackRef hooks are exported for convenience use in TypeScript projects.

onCellsRendered

Callback notified when the range of rendered cells changes.

onResize

Callback notified when the Grid's outermost HTMLElement resizes. This may be used to (re)scroll a cell into view.

overscanCount

How many additional rows/columns to render outside of the visible area. This can reduce visual flickering near the edges of a grid when scrolling.

tagName

Can be used to override the root HTML element rendered by the List component. The default value is "div", meaning that List renders an HTMLDivElement as its root.

⚠️ In most use cases the default ARIA roles are sufficient and this prop is not needed.

NPM DownloadsLast 30 Days