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.
npm install react-router-scrollVerified import paths — ran on the pinned version, not inferred.
This example demonstrates basic setup for `react-router-scroll` with React Router v2/v3, applying `useScroll` as a router middleware to manage default browser-style scroll behavior on route transitions.
Ensure `history` is installed as a dependency (`npm install history@^2.0.0 || ^3.0.0`) and that its version is compatible with your `react-router` version.
Update your import statements from `import useScroll from 'react-router-scroll';` to `import { useScroll } from 'react-router-scroll';`.For React Router v4+, consult the React Router documentation for scroll restoration or use alternative community packages designed for newer React Router APIs (e.g., `react-router-dom`'s built-in scroll restoration).
Conditionally apply `useScroll` only in client-side rendering environments (e.g., `typeof window !== 'undefined'`). `<ScrollContainer>` components will gracefully do nothing on the server.
Ensure `scrollKey` remains static for the lifetime of a `<ScrollContainer>` instance. If a different key or child DOM node is needed, remount the `<ScrollContainer>` component entirely.
Update the import statement to use named import: `import { useScroll } from 'react-router-scroll';``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.
Ensure 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).