Skip to main content

Overview

Virtualized lists render only the visible items in the viewport, enabling smooth drag-and-drop with thousands of items. This example shows how to integrate dnd-kit with popular virtualization libraries.

Why Virtualization?

Rendering large lists (1000+ items) causes performance issues:
  • Slow initial render
  • Laggy scrolling
  • High memory usage
  • Poor drag-and-drop responsiveness
Virtualization solves this by rendering only visible items plus a small buffer.

Using @tanstack/react-virtual

Installation

Basic Implementation

Key Concepts

Window Virtualizer

The useWindowVirtualizer hook virtualizes items based on the window scroll position:
Important parameters:
  • estimateSize: Should match your item height for accurate scrollbar sizing
  • getItemKey: Use the item’s unique ID, not the index
  • scrollMargin: Accounts for fixed headers or other offsets

Measuring Elements

The virtualizer needs to measure items for accurate positioning:
This allows the virtualizer to:
  • Calculate actual item sizes
  • Adjust scroll position dynamically
  • Handle variable-height items

Virtual Items Positioning

The virtual items need special positioning:
This creates:
  • A container with the full height of all items (for scrollbar)
  • An absolutely positioned inner container that translates to show visible items

Container Virtualizer

For scrollable containers (not the window), use useVirtualizer:
The main difference:
  • Use useVirtualizer instead of useWindowVirtualizer
  • Pass getScrollElement to specify the scrollable container
  • No scrollMargin needed

Variable Height Items

For items with different heights:

Feedback Plugin

For virtualized lists, always use the clone feedback mode:
This ensures:
  • The drag preview is visible even when the original item scrolls out of view
  • Smooth dragging across the entire list
  • Proper visual feedback

Performance Tips

1. Optimize Re-renders

2. Use Stable Keys

3. Debounce State Updates

For extremely large lists (10,000+ items), debounce updates:

4. Overscan Configuration

Adjust the number of items rendered outside the viewport:
Higher overscan:
  • Smoother scrolling
  • Higher memory usage
Lower overscan:
  • Lower memory usage
  • Possible visual glitches during fast scrolling

Horizontal Virtualization

For horizontal lists:

Virtualized Grid

For 2D grids with virtualization:

Common Issues

Items Jump During Drag

Problem: Items jump to incorrect positions while dragging. Solution: Ensure getItemKey uses stable IDs:

Scrollbar Size Incorrect

Problem: Scrollbar shows wrong total size. Solution: Provide accurate estimateSize:

Poor Performance

Problem: Still laggy with virtualization. Solutions:
  1. Memoize components with React.memo
  2. Reduce overscan value
  3. Use feedback: 'clone' plugin
  4. Avoid heavy computations in render

Next Steps