Provides access to the last location in react + react-router (v4.x, v5.x) applications. ❤️ Using hooks? useLastLocation | 💉 Using HOC? withLastLocation
react+
react-router (v4.x, v5.x)applications.
hooks? If yes,
useLastLocation.
HOC? - If yes,
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 =
/
# Please remember that you should have installed react, prop-types and react-router-dom packages # npm install react prop-types react-router-dom --savenpm install react-router-last-location --save
If you still use
v1.x.xand would like to use hook
useLastLocation, please upgrade to
v2.x.xversion (don't worry, no breaking changes).
npm install [email protected]^2.0.0 # or npm install [email protected]
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 = () => (
- Home
- About
- Contact
);<hr> <route exact path="/" component="{Home}"></route> <route path="/about" component="{About}"></route> <route path="/contact" component="{Contact}"></route> <hr> <logger></logger> </div> </lastlocationprovider>
render(, document.getElementById('root'));
useLastLocationto get
lastLocation.
./components/Logger, see example
import React from 'react'; import { useLastLocation } from 'react-router-last-location';const Logger = () => { const lastLocation = useLastLocation();
return (
); };Logger!
{JSON.stringify(lastLocation)}export default LoggerHooks;
withLastLocationto get
lastLocationprop.
./components/Logger, see example
import React from 'react'; import { withLastLocation } from 'react-router-last-location';const Logger = ({ lastLocation }) => (
);Logger!
{JSON.stringify(lastLocation)}export default withLastLocation(Logger);
RedirectWithoutLastLocationto not store redirects as last location
import React from 'react'; import { RedirectWithoutLastLocation } from 'react-router-last-location';const MyPage = () => ( );
export default MyPage;
You can still use a regular
component fromreact-router.
If you do, you'll then you need to manually pass the
state: { preventLastLocation: true }, like below:
import React from 'react'; import { Redirect } from 'react-router-dom';const MyPage = () => ( );
export default MyPage;
watchOnlyPathname, type:
boolean, default:
false
Stores the last route only when
pathnamehas changed.