Capítulo 17 de 20
The React chat example combines end anchoring, stable message IDs, automatic history loading near the top, streaming output, and a latest-position control.
const virtualizer = useVirtualizer({
count: messages.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 74,
getItemKey: React.useCallback(
(index: number) => messages[index]!.id,
[messages],
),
anchorTo: 'end',
followOnAppend: true,
scrollEndThreshold: 80,
overscan: 6,
directDomUpdates: true,
})
React.useLayoutEffect(() => {
if (didInitialScroll) return
virtualizer.scrollToEnd()
setDidInitialScroll(true)
}, [didInitialScroll, virtualizer])
isAtEnd(80) to distinguish latest reading from history reading.