Socket
Socket
Sign inDemoInstall

@tanstack/react-virtual

Package Overview
Dependencies
6
Maintainers
2
Versions
72
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @tanstack/react-virtual

Headless UI for virtualizing scrollable elements in React


Version published
Weekly downloads
1.5M
decreased by-6.66%
Maintainers
2
Install size
193 kB
Created
Weekly downloads
 

Package description

What is @tanstack/react-virtual?

The @tanstack/react-virtual npm package is designed to help developers efficiently render large lists and tabular data by virtualizing rows and columns. This means only the items in the viewport plus a small buffer are rendered, significantly improving performance for large datasets.

What are @tanstack/react-virtual's main functionalities?

Virtualized Lists

This feature allows for the rendering of large lists by virtualizing the rows, meaning only the items that are currently in the viewport (plus a small buffer) are rendered. This significantly improves performance for lists with a large number of items.

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

function MyVirtualList() {
  const parentRef = React.useRef();

  const rowVirtualizer = useVirtualizer({
    count: 10000, // Number of items
    getScrollElement: () => parentRef.current,
  });

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

Virtualized Grids

This feature extends the concept of virtualization to grids, allowing for efficient rendering of large tabular data. Both rows and columns are virtualized, ensuring that only the cells in the viewport are rendered.

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

function MyVirtualGrid() {
  const parentRef = React.useRef();

  const rowVirtualizer = useVirtualizer({
    count: 10000, // Number of items in a row
    getScrollElement: () => parentRef.current,
  });

  const columnVirtualizer = useVirtualizer({
    count: 100, // Number of columns
    getScrollElement: () => parentRef.current,
  });

  return (
    <div ref={parentRef} style={{ height: `150px`, width: `300px`, overflow: 'auto' }}>
      <div style={{ height: rowVirtualizer.getTotalSize() + `px`, width: columnVirtualizer.getTotalSize() + 'px', position: 'relative' }}>
        {rowVirtualizer.getVirtualItems().map(row => (
          {columnVirtualizer.getVirtualItems().map(column => (
            <div key={`${row.index}-${column.index}`} style={{ position: 'absolute', top: `${row.start}px`, left: `${column.start}px`, width: '100%', height: '20px' }}>
              Cell {row.index}, {column.index}
            </div>
          ))}
        ))}
      </div>
    </div>
  );
}

Other packages similar to @tanstack/react-virtual

Keywords

FAQs

Last updated on 29 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc