Frontend Blocks typescript Advanced

Virtual List Component

Render only visible rows in a large list for smooth performance with thousands of items.

typescript
import { useRef, useState } from 'react';
export function VirtualList({ items, rowHeight = 48, visibleRows = 10, renderItem }) {
  const [scrollTop, setScrollTop] = useState(0);
  const startIdx = Math.floor(scrollTop / rowHeight);
  const endIdx   = Math.min(startIdx + visibleRows + 1, items.length);
  return (
    <div style={{ height: visibleRows * rowHeight, overflowY: 'auto' }}
         onScroll={e => setScrollTop(e.currentTarget.scrollTop)}>
      <div style={{ height: items.length * rowHeight, position: 'relative' }}>
        {items.slice(startIdx, endIdx).map((item, i) => (
          <div key={startIdx + i}
               style={{ position: 'absolute', top: (startIdx + i) * rowHeight, height: rowHeight }}>
            {renderItem(item, startIdx + i)}
          </div>
        ))}
      </div>
    </div>
  );
}

Prerequisites

  • React >= 17

Pro Tips

  • → Use react-window for production-grade virtual lists.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
typescript
Level
Advanced
Read time
6 min
Published
Nov 2024
License
Commercial License

Newsletter

New modules every week, directly to your inbox.