npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

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.

Readme

react-scroll-restore

npm version npm downloads types license GitHub stars

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 useLayoutEffect on mount—no listeners, no polling, no patched history.
  • 📐 Sticky-Header Aware: Honors the target's scroll-margin-top and the scroll container's scroll-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#comments directly and the anchor is ignored, or the page jumps before the target has rendered.
  • Your layout scrolls a container with overflow: auto instead 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 #target when 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/replaceState and 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:

  1. The element returned by the getContainer prop, when provided.
  2. The nearest scrollable ancestor of <ScrollRestore /> (a parent whose computed overflow-y is auto, scroll, or overlay).
  3. 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 play

The 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.