npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-router-scroll-4

v1.0.0-beta.2

Published

React Router 4 scroll management

Downloads

13,039

Readme

react-router-scroll for React-router-dom v4

React Router scroll management.

react-router-scroll is a React Router component that adds scroll management using scroll-behavior. By default, the component adds browser-style scroll behavior, but you can customize it to scroll however you want on route transitions.

This is a fork of the original React Router scroll made to support Reat Router v4. Currently all the original features should be working but it is still in early development, tests have not been completely updated yet and there are probably bugs to be encountered. Do not hesitate to signal them, or fix them through pull requests.

Usage

import { BrowserRouter } from 'react-router-dom';
import { ScrollContext } from 'react-router-scroll-4';

/* ... */

ReactDOM.render(
  <BrowserRouter>
    <ScrollContext>
      <MyApp />
    </ ScrollContext>
  </ BrowserRouter>,
  container
);

Guide

Installation

$ yarn add react-router-scroll-4

Basic usage

Use the ScrollContext wrapper as in the example above. ScrollContext Should always have only one child, same as all the Router components.

You can override [scroll-behavior] by sending custom costructor through the property scrollBehavior to ScrollContext:

<ScrollContext scrollBehavior={newScrollBehavior}>
  <MyApp />
</ ScrollContext>

Custom scroll behavior

You can provide a custom shouldUpdateScroll as a property of ScrollContext. This function is called with the previous and the current router props. Those properties correspond to those accessible through withRouter.

The function 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 position
  • a truthy value to emulate the browser default scroll behavior
shouldUpdateScroll = (prevRouterProps, { location, history }) => (
  prevRouterProps && location.pathname !== prevRouterProps.location.pathname
);

shouldUpdateScroll = (prevRouterProps, { location, history }) => {
  if (history.action === 'POP') {
    return false;
  }

  if (location.state["MY-USER-KEY"] === "NoScroll") {
    return [0, 0];
  }

  return true;
};

Scrolling elements other than window

Use <ScrollContainer> in components rendered by a ScrollContext to manage the scroll behavior of elements other than window. Each <ScrollContainer> must be given a unique scrollKey, and can be given an optional shouldUpdateScroll callback that behaves as above. ScrollContainer should have exactly one child, which will be the node managed.

import { ScrollContainer } from 'react-router-scroll';

function Page() {
  /* ... */

  return (
    <ScrollContainer
      scrollKey={scrollKey}
      shouldUpdateScroll={shouldUpdateScroll}
    >
      <MyScrollableComponent />
    </ScrollContainer>
  );
}

<ScrollContainer> does not support on-the-fly changes to scrollKey or to the DOM node for its child.

Notes

Server side rendering

Both <ScrollContainer> and <ScrollContext> are fine to use in server side rendering context. They just renturn their child without changing them in any way.