@sketchpixy/react-router-scroll

0.3.2 • Public • Published

react-router-scroll Travis npm

React Router scroll management.

Usage

import { applyRouterMiddleware, browserHistory, Router } from 'react-router';
import useScroll from 'react-router-scroll';

/* ... */

ReactDOM.render(
  <Router
    history={browserHistory}
    routes={routes}
    render={applyRouterMiddleware(useScroll())}
  />,
  container
);

Guide

Installation

$ npm i -S react react-dom react-router
$ npm i -S react-router-scroll

Basic usage

Apply the useScroll router middleware, as in the example above.

Custom scroll behavior

You can provide a custom shouldUpdateScroll callback as an argument to useScroll. This callback is called with both the previous and the current router props.

You can return:

  • a falsy value to suppress the scroll update
  • a position array such as [0, 100] to scroll to that position
  • a truthy value to get normal scroll behavior
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;
});

Package Sidebar

Install

npm i @sketchpixy/react-router-scroll

Weekly Downloads

104

Version

0.3.2

License

MIT

Last publish

Collaborators