typescriptadvanced
Virtualized List Component
Render large lists efficiently by only rendering visible items with calculated scroll positioning.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { useState, useRef, useCallback } from 'react';
interface VirtualizedListProps<T> {
items: T[];
itemHeight: number;
containerHeight: number;
renderItem: (item: T, index: number) => React.ReactNode;
}
export function VirtualizedList<T>({
items,
itemHeight,
containerHeight,
renderItem,
}: VirtualizedListProps<T>) {
const [scrollTop, setScrollTop] = useState(0);
const containerRef = useRef<HTMLDivElement>(null);
const startIndex = Math.floor(scrollTop / itemHeight);
const visibleCount = Math.ceil(containerHeight / itemHeight) + 1;
const endIndex = Math.min(startIndex + visibleCount, items.length);
const totalHeight = items.length * itemHeight;
const offsetY = startIndex * itemHeight;
const onScroll = useCallback(() => {
if (containerRef.current) setScrollTop(containerRef.current.scrollTop);
}, []);
return (
<div
ref={containerRef}
onScroll={onScroll}
style={{ height: containerHeight, overflow: 'auto' }}
>
<div style={{ height: totalHeight, position: 'relative' }}>
<div style={{ transform: `translateY(${offsetY}px)` }}>
{items.slice(startIndex, endIndex).map((item, i) =>
renderItem(item, startIndex + i)
)}
</div>
</div>
</div>
);
}Use Cases
- Rendering 10k+ item lists
- Log viewers
- Data tables
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
Virtual Scroll List
Virtualized list that only renders visible items for large datasets with fixed row height.
Best for: Large data lists
#virtualization#list
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