All Projects → hinok → React Router Last Location

hinok / React Router Last Location

Licence: mit
Provides access to the last location in react + react-router (v4.x, v5.x) applications. ❤️ Using hooks? useLastLocation | 💉 Using HOC? withLastLocation

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Router Last Location

Piwik React Router
Piwik analytics component for react-router
Stars: ✭ 53 (-78.88%)
Mutual labels:  history, react-router
rafagas
Daily geospatial links curated by Raf Roset
Stars: ✭ 17 (-93.23%)
Mutual labels:  history, location
async-react-router
Client side react router with async. It like next.js!
Stars: ✭ 21 (-91.63%)
Mutual labels:  react-router, history
Redux First History
🎉 Redux First History - Redux history binding support react-router - @reach/router - wouter
Stars: ✭ 163 (-35.06%)
Mutual labels:  history, react-router
Delete My Hisroy In Tieba
删除在百度贴吧的回复、主题帖、关注等(其实就是黑历史啦ww)
Stars: ✭ 235 (-6.37%)
Mutual labels:  history
React Course
Code for ui.dev's "React" course
Stars: ✭ 2,457 (+878.88%)
Mutual labels:  react-router
Sheet Router
fast, modular client-side router
Stars: ✭ 219 (-12.75%)
Mutual labels:  history
Music163 React
🔥基于React全家桶开发:「网易云音乐PC端项目」实战
Stars: ✭ 209 (-16.73%)
Mutual labels:  react-router
Arc
React starter kit based on Atomic Design
Stars: ✭ 2,780 (+1007.57%)
Mutual labels:  react-router
Timezonefinder
fast python package for finding the timezone of any point on earth (coordinates) offline
Stars: ✭ 242 (-3.59%)
Mutual labels:  location
Place Search Dialog
A place autocomplete search dialog which uses Google's places API for finding results.
Stars: ✭ 235 (-6.37%)
Mutual labels:  location
Actingweb firstapp
Starter app for Flutter that includes many different production app features; some not typically included in demo apps.
Stars: ✭ 224 (-10.76%)
Mutual labels:  location
Hstr
bash and zsh shell history suggest box - easily view, navigate, search and manage your command history.
Stars: ✭ 2,909 (+1058.96%)
Mutual labels:  history
Rick And Morty React
Stars: ✭ 221 (-11.95%)
Mutual labels:  react-router
React
Extremely simple boilerplate, easiest you can find, for React application including all the necessary tools: Flow | React 16 | redux | babel 6 | webpack 3 | css-modules | jest | enzyme | express + optional: sass/scss
Stars: ✭ 244 (-2.79%)
Mutual labels:  react-router
Universal React Router4
Demo app showing how to use react-router v4 for server- and client-side rendering
Stars: ✭ 216 (-13.94%)
Mutual labels:  react-router
Productivity Frontend
Productivity Application - Kanban Style Productivity Management Application with Customizable Boards, Lists and Cards to Make You More Productive.
Stars: ✭ 234 (-6.77%)
Mutual labels:  react-router
Redux Undo
♻️ higher order reducer to add undo/redo functionality to redux state containers
Stars: ✭ 2,744 (+993.23%)
Mutual labels:  history
Wordpress Simple History
🔍🕵️‍♀️ WordPress audit log that track user changes in WordPress admin using a nice activity feed.
Stars: ✭ 232 (-7.57%)
Mutual labels:  history
Jshistory Cn
🇨🇳 《JavaScript 二十年》中文版
Stars: ✭ 3,686 (+1368.53%)
Mutual labels:  history

Build Status Coverage Status

react-router-last-location

  • Provides access to the last location in react + react-router (v4.x, v5.x) applications.
  • ❤️ Using hooks? If yes, useLastLocation.
  • 💉 Using HOC? - If yes, withLastLocation.
  • Handle redirects.
  • Support TypeScript
  • Useful for handling internal routing.
  • Easily keep your users inside your app.

Demo

Edit react-router-last-location

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

  1. Visit /: last location = null, previous browser history state = null
  2. Visit /a: last location = /, previous browser history state = /
  3. Visit /b: last location = /a, previous browser history state = /a
  4. Reload (url will stay at /b): last location = null, previous browser history state = /a

Example 2

  1. Visit /: last location = null
  2. Visit /a: last location = /
  3. Visit /b: last location = /a
  4. Go back: last location = /b, previous browser history state = /

Example 3

  1. Visit /: last location = null
  2. Visit /a: last location = /
  3. Visit /b: last location = /a
  4. Visit /c: last location = /b
  5. 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, 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 [email protected]^2.0.0
# or
npm install [email protected]

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 HOC 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);

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 <Redirect /> 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 { Redirect } from 'react-router-dom';

const MyPage = () => (
  <Redirect
    to={{
      pathname: '/',
      state: { preventLastLocation: true },
    }}
  />
);

export default MyPage;

LastLocationProvider

Props

watchOnlyPathname, type: boolean, default: false

Stores the last route only when pathname has changed.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].