
Product
Secure Your AI-Generated Code with Socket MCP
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
react-router-dom-last-location
Advanced tools
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.
Fork version of react-router-last-location but for react-router v6
react
+ react-router (v6.x)
applications.useLastLocation
.withLastLocation
.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
/
: 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 = /
# npm
npm install react react-router-dom history react-router-dom-last-location --save
# yarn
yarn add react react-router-dom history react-router-dom-last-location
<LastLocationProvider />
inside <Router />
.index.js
// layout/Main.jsx
import { Outlet } from "react-router";
import { LastLocationProvider } from "react-router-dom-last-location";
export function MainLayout() {
return (
<>
<LastLocationProvider>
<Outlet />
</LastLocationProvider>
</>
)
}
// App.jsx
import { Route, Routes } from "react-router";
import { BrowserRouter } from "react-router-dom";
import { LoggerLayout } from "./layout/Logger";
import { MainLayout } from "./layout/Main";
import { About } from "./pages/About";
import { Foo } from "./pages/Foo";
import { Home } from "./pages/Home";
function App() {
return (
<BrowserRouter >
<Routes>
<Route element={<MainLayout />}>
<Route element={<LoggerLayout />}>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/foo" element={<Foo />} />
</Route>
</Route>
</Routes>
</BrowserRouter>
);
}
export default App;
useLastLocation
to get lastLocation
.// layout/Logger.jsx
import { Outlet, useLocation } from "react-router";
import { useLastLocation } from "react-router-dom-last-location";
export function LoggerLayout() {
const location = useLocation()
const lastLocation = useLastLocation();
return (
<>
<Outlet />
<div>
<h3> Last Location state</h3>
<pre>{JSON.stringify(lastLocation)}</pre>
</div>
<div>
<h3> Current Location state</h3>
<pre>{JSON.stringify(location)}</pre>
</div>
</>
)
}
withLastLocation
to get lastLocation
.// layout/HocLogger.jsx
import { Outlet, useLocation } from "react-router";
import { withLastLocation } from "react-router-dom-last-location";
export const HocLoggerLayout = withLastLocation( ({lastLocation}) => {
const location = useLocation()
return (
<>
<Outlet />
<div>
<h3> Last Location state</h3>
<pre>{JSON.stringify(lastLocation)}</pre>
</div>
<div>
<h3> Current Location state</h3>
<pre>{JSON.stringify(location)}</pre>
</div>
</>
)
})
RedirectWithoutLastLocation
to not store redirects as last locationimport { RedirectWithoutLastLocation } from 'react-router-dom-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 { Navigate } from 'react-router-dom';
const MyPage = () => (
<Navigate
to="/"
state={{ preventLastLocation: true }}
replace
/>
);
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 (v6.x) apps. Useful for handling internal routing. Easily prevent leaving your app by users.
The npm package react-router-dom-last-location receives a total of 539 weekly downloads. As such, react-router-dom-last-location popularity was classified as not popular.
We found that react-router-dom-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.
Product
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.