The useVirtualizer hook is the recommended way to implement virtualization. It provides a scrollElementRef to attach to your container, a range object containing visible item data, and utility methods for scrolling.
Hook Options
itemCount (required): Total number of items.estimateSize (required): Function returning item height: (index: number) => number.overscan (default: 5): Extra items to render above/below the visible area.horizontal (default: false): Enables horizontal scroll mode.height (default: '100%'): Container height.
Return Values
scrollElementRef: Ref to attach to the scroll container.range: An object of type VirtualRange containing current visible data.totalHeight: Total scrollable height.isLoading: Boolean indicating WASM loading state.error: Error object if initialization failed.scrollToIndex: Function to scroll to a specific index: (index, behavior?) => void.scrollToOffset: Function to scroll to a pixel offset: (offset, behavior?) => void.
import { useVirtualizer } from '@itsmeadarsh/warper';
function MyList() {
const { scrollElementRef, range, totalHeight } = useVirtualizer({
itemCount: 1_000_000,
estimateSize: () => 50,
overscan: 5,
});
return (
<div ref={scrollElementRef} style={{ height: 500, overflow: 'auto' }}>
<div style={{ height: totalHeight, position: 'relative' }}>
{range.items.map((index, i) => (
<div
key={index}
style={{
position: 'absolute',
top: 0,
transform: `translateY(${range.offsets[i]}px)`,
height: range.sizes[i],
width: '100%',
}}
>
Row {index}
</div>
))}
</div>
</div>
);
}