Socket
Book a DemoInstallSign in
Socket

react-last-location-router

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

react-last-location-router

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

0.1.0
unpublished
latest
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

React Last Location Router

Fork version of react-router-last-location but for react-router v6

  • Provides access to the last location in react + react-router (v6.x) applications.
  • Hooks, useLastLocation.
  • Handle redirects.
  • TypeScript
  • Useful for handling internal routing.
  • Easily keep your users inside your app.

Note: Last location != Previous browser history state

This library only returns the location that was active right before the recent location change, during the lifetime of the current window.

This means, it is not equal to the "location you were at before navigating to this history state".

In other words, the location this library provides is not necessarily the same as the one when you click the browser's back button.

Example 1

  • Visit /: last location = null, previous browser history state = null
  • Visit /a: last location = /, previous browser history state = /
  • Visit /b: last location = /a, previous browser history state = /a
  • Reload (url will stay at /b): last location = null, previous browser history state = /a

Example 2

  • Visit /: last location = null
  • Visit /a: last location = /
  • Visit /b: last location = /a
  • Go back: last location = /b, previous browser history state = /

Example 3

  • Visit /: last location = null
  • Visit /a: last location = /
  • Visit /b: last location = /a
  • Visit /c: last location = /b
  • 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 and react-router-dom packages
# npm
npm install react-router-last-location --save
# yarn
yarn add react-router-last-location

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 RedirectWithoutLastLocation to not 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 a regular <Navigate to="/" replace /> component from react-router.

If you do, you'll then you need to manually pass the state: { preventLastLocation: true }, like below:

import React from 'react';
import { Navigate } from 'react-router-dom';
const MyPage = () => (
  <Navigate
    to={{ pathname: '/' }}
    state={{ preventLastLocation: true }}
    replace
  />
);
export default MyPage;

LastLocationProvider

Props

watchOnlyPathname, type: boolean, default: false

Stores the last route only when pathname has changed.

TODO:

  • Add unit test
  • Add github actions

Keywords

react

FAQs

Package last updated on 13 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.