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-last-location

v2.0.1

Published

Provides access to the last location in react + react-router (v4.x) apps. Useful for handling internal routing. Easily prevent leaving your app by users.

Downloads

93,862

Readme

Build Status Coverage Status

react-router-last-location

  • Provides access to the last location in react + react-router (v4.x, v5.x) applications.
  • ❤️ Using hooks? If yes, useLastLocation.
  • 💉 Using HOC? - If yes, withLastLocation.
  • Handle redirects.
  • Support TypeScript
  • Useful for handling internal routing.
  • Easily prevent leaving your app by users.

Demo

Edit react-router-last-location

Note: Last location != Previous browser history state

This library only returns the location that has been active before the recent location change in the current window lifetime.

This means, it is not equal to the "location that happened before navigating to this history state", or in other words "location to which you'll be redirected upon clicking browser back button".

Example 1

  1. Visit /: last location = null, previous browser history state = null
  2. Visit /a: last location = /, previous browser history state = /
  3. Visit /b: last location = /a, previous browser history state = /a
  4. Reload (url will stay at /b): last location = null, previous browser history state = /a

Example 2

  1. Visit /: last location = null
  2. Visit /a: last location = /
  3. Visit /b: last location = /a
  4. Go back: last location = /b, previous browser history state = /

Example 3

  1. Visit /: last location = null
  2. Visit /a: last location = /
  3. Visit /b: last location = /a
  4. Visit /c: last location = /b
  5. Go back to /a (by selecting that state explicitly in "Go back" browser dropdown that is visible upon clicking it with right mouse button): last location = /c, previous browser history state = /

How to use?

# Please remember that you should have installed react, prop-types and react-router-dom packages
# npm install react prop-types react-router-dom --save

npm install react-router-last-location --save

If you still use v1.x.x and would like to use hook useLastLocation, please upgrade to v2.x.x version (don't worry, no breaking changes).

npm install react-router-last-location@^2.0.0
# or
npm install react-router-last-location@latest

Declare <LastLocationProvider /> inside <Router />.

index.js

import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
import { LastLocationProvider } from 'react-router-last-location';
import Home from './pages/Home';
import About from './pages/About';
import Contact from './pages/Contact';
import Logger from './components/Logger';

const App = () => (
  <Router>
    <LastLocationProvider>
      <div>
        <ul>
          <li><Link to="/">Home</Link></li>
          <li><Link to="/about">About</Link></li>
          <li><Link to="/contact">Contact</Link></li>
        </ul>

        <hr />

        <Route exact path="/" component={Home} />
        <Route path="/about" component={About} />
        <Route path="/contact" component={Contact} />

        <hr />

        <Logger />
      </div>
    </LastLocationProvider>
  </Router>
);

render(<App />, document.getElementById('root'));

Use hook useLastLocation to get lastLocation.

./components/Logger, see example

import React from 'react';
import { useLastLocation } from 'react-router-last-location';

const Logger = () => {
  const lastLocation = useLastLocation();

  return (
    <div>
      <h2>Logger!</h2>
      <pre>
        {JSON.stringify(lastLocation)}
      </pre>
    </div>
  );
};

export default LoggerHooks;

Use HOC withLastLocation to get lastLocation prop.

./components/Logger, see example

import React from 'react';
import { withLastLocation } from 'react-router-last-location';

const Logger = ({ lastLocation }) => (
  <div>
    <h2>Logger!</h2>
    <pre>
      {JSON.stringify(lastLocation)}
    </pre>
  </div>
);

export default withLastLocation(Logger);

Use RedirectWithoutLastLocation to don't store redirects as last location

import React from 'react';
import { RedirectWithoutLastLocation } from 'react-router-last-location';

const MyPage = () => (
  <RedirectWithoutLastLocation to="/" />
);

export default MyPage;

You can still use regular <Redirect /> component from react-router but then you need to pass manually the state: { preventLastLocation: true }, like below:

import React from 'react';
import { Redirect } from 'react-router-dom';

const MyPage = () => (
  <Redirect
    to={{
      pathname: '/',
      state: { preventLastLocation: true },
    }}
  />
);

export default MyPage;

LastLocationProvider

Props

watchOnlyPathname, type: boolean, default: false

Stores the last route only when pathname has changed.