Capítulo 7 de 20
Virtualizer is the core class behind the framework adapters. Its options define the data count, scroll source, estimates, range extraction, spacing, measurement, anchoring, and scroll behavior; its methods expose virtual items, geometry, and imperative control.
count, getScrollElement, and estimateSize establish the collection and its axis geometry.getScrollElement may return null before mount; a window adapter uses the browser window.measureElement observes rendered DOM size; resizeItem accepts a known size.getVirtualItems() returns records for rendering; getVirtualIndexes() returns indexes.paddingStart, paddingEnd, gap, and scroll padding affect geometry and scroll alignment.horizontal changes the measured axis; lanes creates parallel lanes.getItemKey provides stable keys; rangeExtractor customizes returned indexes.scrollToOffset, scrollToIndex, scrollBy, and scrollToEnd control position.scrollOffset, scrollDirection, isScrolling, and scrollRect expose current state.anchorTo, followOnAppend, scrollEndThreshold, getDistanceFromEnd, and isAtEnd support reverse feeds.measure() recalculates measurements after geometry changes; takeSnapshot() supports state capture.onChange(instance, sync) runs when the virtualizer changes and can support direct DOM updates.overscan trades render work for scroll continuity; enabled can disable observers and reset state.const virtualizer = useVirtualizer({
count: items.length,
getScrollElement: () => parentRef.current,
estimateSize: (index) => items[index].size,
getItemKey: (index) => items[index].id,
overscan: 5,
})
const items = virtualizer.getVirtualItems()
virtualizer.scrollToIndex(40)
estimateSize as geometry input, not merely a placeholder.rangeExtractor.scrollToFn.