typescriptadvanced
Virtual Scroll List
Virtualized list that only renders visible items for large datasets with fixed row height.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { useState, useRef, useCallback, CSSProperties, ReactNode } from 'react';
interface VirtualListProps<T> {
items: T[];
rowHeight: number;
containerHeight: number;
overscan?: number;
renderItem: (item: T, index: number) => ReactNode;
}
export function VirtualList<T>({ items, rowHeight, containerHeight, overscan = 5, renderItem }: VirtualListProps<T>) {
const [scrollTop, setScrollTop] = useState(0);
const containerRef = useRef<HTMLDivElement>(null);
const totalHeight = items.length * rowHeight;
const startIndex = Math.max(0, Math.floor(scrollTop / rowHeight) - overscan);
const endIndex = Math.min(items.length, Math.ceil((scrollTop + containerHeight) / rowHeight) + overscan);
const visibleItems = items.slice(startIndex, endIndex);
const handleScroll = useCallback(() => {
if (containerRef.current) setScrollTop(containerRef.current.scrollTop);
}, []);
return (
<div ref={containerRef} onScroll={handleScroll} style={{ height: containerHeight, overflow: 'auto' }}>
<div style={{ height: totalHeight, position: 'relative' }}>
{visibleItems.map((item, i) => {
const index = startIndex + i;
const style: CSSProperties = {
position: 'absolute',
top: index * rowHeight,
height: rowHeight,
width: '100%',
};
return <div key={index} style={style}>{renderItem(item, index)}</div>;
})}
</div>
</div>
);
}Use Cases
- Large data lists
- Log viewers
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
Virtualized List Component
Render large lists efficiently by only rendering visible items with calculated scroll positioning.
Best for: Rendering 10k+ item lists
#virtualization#performance
typescriptadvanced
React Virtual List Component
Render large lists efficiently with windowing to only mount visible items in the viewport.
Best for: Rendering thousands of list items efficiently
#react#virtualization
typescriptintermediate
useScrollPosition Hook
Tracks window scroll position with throttling for performance-sensitive scroll effects.
Best for: Sticky headers
#hooks#scroll
typescriptbeginner
useDebounce — Debounced Value Hook
Debounce any rapidly-changing value with a configurable delay. Useful for search inputs and resize handlers.
Best for: Search-as-you-type
#hooks#debounce