- Provides access to the last location in
react
+react-router (v4.x)
applications. - ❤️ Using
hooks
? If yes,useLastLocation
. - 💉 Using
HOC
? - If yes,withLastLocation
. - Support
- Useful for handling internal routing.
- Easily prevent leaving your app by users.
# 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-beta.3
# or
npm install react-router-last-location@latest
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'));
./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;
./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);
watchOnlyPathname
, type: boolean
, default: false
Stores the last route only when pathname
has changed.