React Router scroll management
npm install react-router-scrollReact Router scroll management.
react-router-scroll is a React Router middleware that adds scroll management using scroll-behavior. By default, the middleware adds browser-style scroll behavior, but you can customize it to scroll however you want on route transitions.
This library does not currently support React Router v4, because React Router v4 has no concept of router middlewares. See ongoing discussion in #52. For an interim solution for just scrolling to top on navigation, see the React Router documentation on scroll restoration.
``js
import { applyRouterMiddleware, browserHistory, Router } from 'react-router';
import { useScroll } from 'react-router-scroll';
/ ... /
ReactDOM.render(
routes={routes}
render={applyRouterMiddleware(useScroll())}
/>,
container
);
`
`shell`
$ npm i -S react react-dom react-router
$ npm i -S react-router-scroll
Apply the useScroll router middleware using applyRouterMiddleware, as in the example above.
You can provide a custom shouldUpdateScroll callback as an argument to useScroll. This callback is called with the previous and the current router props.
The callback can return:
- a falsy value to suppress updating the scroll position
- a position array of x and y, such as [0, 100], to scroll to that positionid
- a string with the or name of an element, to scroll to that element
- a truthy value to emulate the browser default scroll behavior
`js
useScroll((prevRouterProps, { location }) => (
prevRouterProps && location.pathname !== prevRouterProps.location.pathname
));
useScroll((prevRouterProps, { routes }) => {
if (routes.some(route => route.ignoreScrollBehavior)) {
return false;
}
if (routes.some(route => route.scrollToTop)) {
return [0, 0];
}
return true;
});
`
You can customize useScroll even further by providing a configuration object with a createScrollBehavior callback that creates the scroll behavior object. This allows using a custom subclass of ScrollBehavior from scroll-behavior with custom logic. When using a configuration object, you can specify the shouldUpdateScroll callback as above under the shouldUpdateScroll key.
`js`
useScroll({
createScrollBehavior: (config) => new MyScrollBehavior(config),
shouldUpdateScroll,
});
Use in components rendered by a router with the useScroll middleware to manage the scroll behavior of elements other than window. Each must be given a unique scrollKey, and can be given an optional shouldUpdateScroll callback that behaves as above.
`js
import { ScrollContainer } from 'react-router-scroll';
function Page() {
/ ... /
return (
shouldUpdateScroll={shouldUpdateScroll}
>
);
}
`
does not support on-the-fly changes to scrollKey or to the DOM node for its child.
#### Minimizing bundle size
If you are not using , you can reduce your bundle size by importing the useScroll module directly.
`js`
import useScroll from 'react-router-scroll/lib/useScroll';
#### Server rendering
Do not apply the useScroll middleware when rendering on a server. You may use
[build-badge]: https://img.shields.io/travis/taion/react-router-scroll/master.svg
[build]: https://travis-ci.org/taion/react-router-scroll
[npm-badge]: https://img.shields.io/npm/v/react-router-scroll.svg
[npm]: https://www.npmjs.org/package/react-router-scroll