Capítulo 20 de 20
The window example lets the browser window own scrolling instead of a nested list element. It measures the list's page offset and subtracts scrollMargin from each item transform.
const listOffsetRef = React.useRef(0)
React.useLayoutEffect(() => {
listOffsetRef.current = listRef.current?.offsetTop ?? 0
}, [])
const virtualizer = useWindowVirtualizer({
count: 10000,
estimateSize: () => 35,
overscan: 5,
scrollMargin: listOffsetRef.current,
})
{virtualizer.getVirtualItems().map((item) => (
<div
key={item.key}
style={{
position: 'absolute',
height: `${item.size}px`,
transform: `translateY(${item.start - virtualizer.options.scrollMargin}px)`,
}}
>
Row {item.index}
</div>
))}
scrollMargin synchronized with the list's position on the page.getTotalSize().