React Router Scroll Management
react-router-scroll is a middleware for React Router versions 2.x and 3.x, providing advanced scroll management capabilities for web applications. It allows developers to customize scroll behavior during route transitions, enabling features like scrolling to the top of the page, to specific pixel coordinates, or to named HTML elements based on routing events. The library internally uses `scroll-behavior` for its core functionality. The current stable version is 0.4.4, with the last notable release in 2017. Due to fundamental changes in React Router v4 and subsequent versions (which removed the router middleware concept), this library is not compatible with modern React Router setups. It is considered abandoned for new projects utilizing React Router v4, v5, or v6, with no active development or planned compatibility updates for newer React Router APIs.
Common errors
-
TypeError: (0, _reactRouterScroll.useScroll) is not a function
cause Attempting to import `useScroll` as a default export after v0.3.0, or using CommonJS `require` incorrectly.fixUpdate the import statement to use named import: `import { useScroll } from 'react-router-scroll';` -
TypeError: _reactRouter.Router.applyRouterMiddleware is not a function
cause This error occurs when trying to use `applyRouterMiddleware` with React Router v4 or newer, which has removed this API.fix`react-router-scroll` is incompatible with React Router v4+. Downgrade React Router to v2 or v3, or migrate your application's scroll management to a solution compatible with modern React Router versions. -
Invariant Violation: Module history does not have a module named default
cause `history` version incompatibility with `react-router-scroll` or `react-router`, particularly after `react-router-scroll` v0.4.0 made `history` a peer dependency.fixEnsure your project's `history` dependency (`npm list history`) matches the `^2.0.0 || ^3.0.0` requirement and is consistent with your installed `react-router` version (v2.x or v3.x).
Warnings
- breaking The `history` package became a peer dependency in v0.4.0. Projects must explicitly install a compatible `history` version (`^2.0.0 || ^3.0.0`) that aligns with their React Router v2/v3 installation.
- breaking In v0.3.0, `useScroll` changed from being a default export to a named export. Incorrect import statements will lead to runtime errors.
- gotcha This library is NOT compatible with React Router v4, v5, or v6. React Router v4 removed the router middleware concept that `react-router-scroll` relies on. Attempting to use it with newer React Router versions will result in errors.
- gotcha When performing server-side rendering (SSR), the `useScroll` middleware should not be applied to avoid issues related to DOM manipulation in a non-browser environment.
- gotcha The `<ScrollContainer>` component does not support dynamic changes to its `scrollKey` prop or to the underlying DOM node of its child. Changing these properties after initial render may lead to unexpected scroll behavior.
Install
-
npm install react-router-scroll -
yarn add react-router-scroll -
pnpm add react-router-scroll
Imports
- useScroll
import useScroll from 'react-router-scroll';
import { useScroll } from 'react-router-scroll'; - ScrollContainer
const ScrollContainer = require('react-router-scroll').ScrollContainer;import { ScrollContainer } from 'react-router-scroll'; - applyRouterMiddleware
import { applyRouterMiddleware } from 'react-router';
Quickstart
import React from 'react';
import ReactDOM from 'react-dom';
import { applyRouterMiddleware, browserHistory, Router } from 'react-router';
import { useScroll } from 'react-router-scroll';
// Define your routes (example)
const routes = {
path: '/',
component: ({ children }) => (
<div>
<h1>Welcome</h1>
{children}
</div>
),
indexRoute: { component: () => <p>Home Page Content</p> },
childRoutes: [
{ path: 'about', component: () => <p>About Us Content</p> },
{ path: 'contact', component: () => <p>Contact Us Content</p> }
]
};
ReactDOM.render(
<Router
history={browserHistory}
routes={routes}
render={applyRouterMiddleware(useScroll())}
/>,
document.getElementById('root')
);