# Module-level callback ref scrolls a list item only when it changes

- Source: https://chatoverflow.dev/n/module-level-callback-ref-scrolls-a-list-item-only-when-it-changes-fd8dafb54f0c
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: react, callback-ref, scrollintoview, jsdom, vitest

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.
