Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More

@react-aria/virtualizer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-aria/virtualizer

Spectrum UI components in React

  • 3.0.0-nightly-a626c2596-240926
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
241K
decreased by-26.52%
Maintainers
2
Weekly downloads
 
Created

What is @react-aria/virtualizer?

@react-aria/virtualizer is a library that provides utilities for efficiently rendering large lists and collections in React applications. It leverages virtualization techniques to only render the visible items, improving performance and reducing memory usage.

What are @react-aria/virtualizer's main functionalities?

Virtualized List

This feature allows you to create a virtualized list where only the visible items are rendered. This improves performance for large lists by reducing the number of DOM elements.

import { useVirtualizer } from '@react-aria/virtualizer';
import { useRef } from 'react';

function VirtualizedList({ items }) {
  const parentRef = useRef();
  const { virtualItems, totalSize } = useVirtualizer({
    count: items.length,
    getScrollElement: () => parentRef.current,
    estimateSize: () => 35
  });

  return (
    <div ref={parentRef} style={{ overflow: 'auto', height: '400px' }}>
      <div style={{ height: totalSize }}>
        {virtualItems.map(virtualRow => (
          <div
            key={virtualRow.index}
            style={{
              position: 'absolute',
              top: 0,
              left: 0,
              width: '100%',
              transform: `translateY(${virtualRow.start}px)`
            }}
          >
            {items[virtualRow.index]}
          </div>
        ))}
      </div>
    </div>
  );
}

Virtualized Grid

This feature allows you to create a virtualized grid where only the visible items are rendered. This is useful for displaying large grids of items efficiently.

import { useVirtualizer } from '@react-aria/virtualizer';
import { useRef } from 'react';

function VirtualizedGrid({ items, columnCount }) {
  const parentRef = useRef();
  const { virtualItems, totalSize } = useVirtualizer({
    count: items.length,
    getScrollElement: () => parentRef.current,
    estimateSize: () => 100
  });

  return (
    <div ref={parentRef} style={{ overflow: 'auto', height: '400px' }}>
      <div style={{ height: totalSize }}>
        {virtualItems.map(virtualItem => (
          <div
            key={virtualItem.index}
            style={{
              position: 'absolute',
              top: 0,
              left: 0,
              width: `${100 / columnCount}%`,
              transform: `translate(${(virtualItem.index % columnCount) * 100}%, ${Math.floor(virtualItem.index / columnCount) * 100}px)`
            }}
          >
            {items[virtualItem.index]}
          </div>
        ))}
      </div>
    </div>
  );
}

Other packages similar to @react-aria/virtualizer

FAQs

Package last updated on 26 Sep 2024

Did you know?

Socket

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