
Security News
New CVE Forecasting Tool Predicts 47,000 Disclosures in 2025
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
react-router-last-location
Advanced tools
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.
react
+ react-router (v4.x, v5.x)
applications.hooks
? If yes, useLastLocation
.HOC
? - If yes, withLastLocation
.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
/
: last location = null
, previous browser history state = null
/a
: last location = /
, previous browser history state = /
/b
: last location = /a
, previous browser history state = /a
/b
): last location = null
, previous browser history state = /a
Example 2
/
: last location = null
/a
: last location = /
/b
: last location = /a
/b
, previous browser history state = /
Example 3
/
: last location = null
/a
: last location = /
/b
: last location = /a
/c
: last location = /b
/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 = /
# 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
<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'));
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;
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);
RedirectWithoutLastLocation
to don't store redirects as last locationimport 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;
watchOnlyPathname
, type: boolean
, default: false
Stores the last route only when pathname
has changed.
FAQs
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.
The npm package react-router-last-location receives a total of 18,949 weekly downloads. As such, react-router-last-location popularity was classified as popular.
We found that react-router-last-location demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.