react-scroll-restore
v0.2.0
Published
A lightweight, framework-agnostic React component for seamless scroll restoration and hash anchor positioning—works with any router.
Maintainers
Readme
react-scroll-restore
A lightweight, router-agnostic scroll restoration and hash positioning library for React.
Unlike router-specific solutions (like React Router's <ScrollRestoration>), react-scroll-restore reads the URL hash once, when it mounts, and then does nothing else: it patches no history method, adds no event listener, and runs no timer. This means it works seamlessly with any routing solution—whether you are using Wouter, React Router, TanStack Router, HashRouter, or even a custom setup.
✨ Features
- 🛡️ Router-Agnostic: Zero coupling with router libraries. Drop it into any React application.
- 🔄 Predictable Positioning: Scrolls to the top when mounted on a URL without a hash and to the matching anchor when a hash is present.
- ⚓ Built-in Hash Navigation: Handles URL hash fragments (
#target-id) smoothly, with built-in retry logic for async-rendered content. - 🧹 Zero Side Effects: A single
useLayoutEffecton mount—no listeners, no polling, no patchedhistory. - 📐 Sticky-Header Aware: Honors the target's
scroll-margin-topand the scroll container'sscroll-padding-top, so anchors are not hidden behind fixed headers. - 🪶 Lightweight & Modern: Built with React Hooks, zero heavy dependencies, and optimized for performance.
🤔 Why scroll restoration?
Client-side routing replaces the content of the page without a full browser navigation. The browser does not know that the "page" changed, so it keeps the scroll offset of the previous route and does not scroll to the new URL's hash. The result is easy to reproduce:
- You scroll to the bottom of a long list, click a link, and the next page opens already scrolled to the bottom instead of the top.
- You open
/posts#commentsdirectly and the anchor is ignored, or the page jumps before the target has rendered. - Your layout scrolls a container with
overflow: autoinstead of the document, and native anchor navigation scrolls the wrong element or nothing at all.
react-scroll-restore handles these cases for you:
- Resets to the top when it mounts on a URL without a hash.
- Scrolls to
#targetwhen the URL has a hash, retrying while the target is still rendering. - Attaches to the nearest scrollable container, or to the element returned by
getContainer. - 🔌 Non-Invasive: Never patches or wraps
history.pushState/replaceStateand never subscribes to navigation events, so native history behavior, routers, and other libraries are untouched.
🚀 Installation
npm install --save react-scroll-restore📖 Usage
Place <ScrollRestore /> inside the element you want to scroll. When it mounts, it inspects window.location.hash and either scrolls to the matching element or, when there is no hash, back to the top. It never reacts to later URL changes by itself.
Because the component only acts on mount, mount it per navigation: render it inside the route component, or remount it by keying it with the router location.
import { ScrollRestore } from 'react-scroll-restore';
export function App() {
return (
<div style={{ height: '100vh', overflowY: 'auto' }}>
<ScrollRestore />
<main>
<h1>Page title</h1>
<section id='features'>...</section>
</main>
</div>
);
}The scroll container is resolved in this order:
- The element returned by the
getContainerprop, when provided. - The nearest scrollable ancestor of
<ScrollRestore />(a parent whose computedoverflow-yisauto,scroll, oroverlay). - The document scrolling element.
If the hashed element is rendered asynchronously (for example after data loading), <ScrollRestore /> keeps looking for it for up to 30 animation frames.
Anchor positioning follows native fragment navigation: the target's scroll-margin-top and the scroll container's scroll-padding-top are subtracted from the scroll position. Set one of them (for example scroll-margin-top: 5rem on headings) to keep anchors clear of sticky headers.
getContainer
Use getContainer to scroll a specific element instead of relying on the nearest scrollable ancestor:
import { useRef } from 'react';
import { ScrollRestore } from 'react-scroll-restore';
export function Page() {
const containerRef = useRef<HTMLDivElement>(null);
return (
<div ref={containerRef} style={{ height: '100vh', overflowY: 'auto' }}>
<ScrollRestore getContainer={() => containerRef.current} />
<main>...</main>
</div>
);
}Remounting per navigation
Give the component a key that changes with the router location, so React remounts it on every navigation:
// React Router
import { useLocation } from 'react-router';
export function App() {
const location = useLocation();
return (
<div style={{ height: '100vh', overflowY: 'auto' }}>
<ScrollRestore key={location.key} />
<main>...</main>
</div>
);
}// Wouter
import { useLocation } from 'wouter';
export function App() {
const [location] = useLocation();
return (
<div style={{ height: '100vh', overflowY: 'auto' }}>
<ScrollRestore key={location} />
<main>...</main>
</div>
);
}Sandboxed routers (memory or hash based) work the same way: any location value that changes per navigation is enough.
Same-page anchor links need no remount—plain <a href="#section"> links are scrolled by the browser itself, which also applies scroll-margin-top and scroll-padding-top.
Props
| Prop | Type | Default | Description |
| -------------- | --------------------------- | -------- | ----------------------------------------------------------------------------- |
| getContainer | () => HTMLElement \| null | — | Returns the element to scroll. Falls back to the nearest scrollable ancestor. |
| behavior | ScrollBehavior | 'auto' | Scroll behavior passed to scrollTo ('auto' or 'smooth'). |
🎮 Playground
pnpm playThe playground uses Wouter to cover three scenarios:
- Navigating to a new page without a hash scrolls to the top.
- Navigating to a new page with a hash (e.g.
/about#about-3) scrolls to that anchor. - Clicking a same-page hash link (e.g.
#home-3) is left to the browser's native fragment navigation.
Deployment
Every push to main builds the playground and publishes it to GitHub Pages at https://guoyunhe.github.io/react-scroll-restore/ via .github/workflows/pages.yaml. The build uses PLAYGROUND_BASE=/react-scroll-restore/, and index.html is copied to 404.html so client-side routes survive deep links on GitHub Pages.
Enable Pages once in Settings → Pages → Source: GitHub Actions before the first deployment.
