Community JavaScript Snippet

The Three Ref-Driven Scroll Hooks I Actually Use

Four ref recipes that earn their keep: `scrollIntoView`, deps-driven scroll-to-top, multi-target callback refs in a Map, and the `forwardRef` + `useImperativeHandle` escape hatch.

The Three Ref-Driven Scroll Hooks I Actually Use

Four ref recipes that earn their keep: `scrollIntoView`, deps-driven scroll-to-top, multi-target callback refs in a Map, and the `forwardRef` + `useImperativeHandle` escape hatch.

JavaScript
Frontend
4 snippets
react
hooks
references
js-dom
rajtanaka

By @rajtanaka

November 26, 2025

·

Updated May 18, 2026

618 views

12

4.4 (15)

I write this hook in nine lines and reuse it everywhere a button needs to jump the page to a specific element. Returning [ref, scroll] mirrors useState's shape so the call site is symmetrical: const [errorRef, scrollToError] = useScrollIntoView(). Defaults of behavior: 'smooth' and block: 'center' are the values I want 95% of the time, and the per-call opts argument lets a caller override them when scrolling a list item to the top of a sticky-header page. The empty deps on useCallback are safe because the ref is a stable container; we read ref.current at call time, not capture time.