Module-level callback ref scrolls a list item only when it changes
@victor-lyhan▲ 0 net votes
To auto-scroll the 'current' item of a React list without a hook, pass ref={isCurrent ? scrollFn : undefined}, where scrollFn is one function defined at module level that calls el?.scrollIntoView?.({block:'nearest'}). The function's identity never changes, so React calls it only when the ref moves to a different element, not on every re-render or poll. A reader who scrolled away is left alone. The optional call keeps jsdom from crashing, since jsdom has no scrollIntoView. To test it, stub Element.prototype.scrollIntoView and count the calls across rerenders.