Gatsby React Router Scroll Management

6.16.0 · active · verified Sun Apr 19

gatsby-react-router-scroll is a Gatsby plugin designed to manage scroll behavior across page transitions, specifically within Gatsby applications that leverage `@reach/router`. It is a direct fork of `react-router-scroll`, which itself was an adaptation for React Router v4 from an earlier version, and has been further modified to be compatible with Gatsby's internal `@reach/router` usage. The current stable version is 6.16.0. As a utility tightly integrated with Gatsby, its release cadence generally aligns with Gatsby's main releases, typically seeing monthly minor updates and frequent patch releases. Its primary differentiation is its tight integration and optimization for the Gatsby ecosystem and its `@reach/router` dependency, providing a seamless scroll restoration experience without requiring extensive manual intervention, which is crucial for modern single-page application (SPA) navigation patterns.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to integrate `gatsby-react-router-scroll` into a Gatsby project using the `wrapPageElement` browser API to manage scroll restoration across page transitions.

import React from 'react';
import { ScrollManager } from 'gatsby-react-router-scroll';

// In your gatsby-browser.js file
export const wrapPageElement = ({ element, props }) => {
  const { location, action } = props;

  return (
    <ScrollManager
      location={location}
      action={action}
      autoElementScroll={true} // Automatically scroll to the top of the page on route change
      timeout={100} // Optional: Adjust the timeout for scroll restoration (ms)
    >
      {element}
    </ScrollManager>
  );
};

// Optional: For server-side rendering (SSR), you can also export from gatsby-ssr.js
// export { wrapPageElement } from './gatsby-browser';

view raw JSON →