Capítulo 6 de 20
Chat, AI streams, logs, and reverse feeds append at the end but often prepend older history at the start. End anchoring preserves the visible message and follows new output only when the reader is already at the latest position.
anchorTo: 'end': Enables end-oriented anchoring for prepend and growth behavior.getItemKey must return the message ID, not its changing array index.followOnAppend: Follows newly appended items only when pinned near the end.scrollEndThreshold: Defines how close to the end counts as pinned.scrollToEnd(): Starts the view at the latest item after mount.isAtEnd() can drive a "jump to latest" control.const virtualizer = useVirtualizer({
count: messages.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 72,
getItemKey: (index) => messages[index]!.id,
anchorTo: 'end',
followOnAppend: true,
scrollEndThreshold: 80,
overscan: 6,
})