├── .browserslistrc
├── .DS_Store
├── .babelrc
├── .editorconfig
├── .eslintignore
├── .eslintrc
├── .gitignore
├── .npmrc
├── .travis.yml
├── LICENSE
├── README.md
├── example
└── src
│ ├── components
│ ├── AppLink
│ │ └── index.tsx
│ ├── Logger
│ │ └── index.tsx
│ ├── LoggerHOC
│ │ └── index.tsx
│ ├── LoggerHooks
│ │ └── index.tsx
│ ├── ManualHistory
│ │ └── index.tsx
│ └── PreviewLocation
│ │ └── index.tsx
│ ├── index.ejs
│ ├── index.tsx
│ ├── pages
│ ├── About.tsx
│ ├── Contact.tsx
│ ├── FixedRedirect.tsx
│ ├── FixedRedirectManual.tsx
│ ├── Home.tsx
│ ├── Page.tsx
│ └── RegularRedirect.tsx
│ └── styles.js
├── jest.config.json
├── package.json
├── src
├── LastLocationContext.ts
├── LastLocationProvider.tsx
├── RedirectWithoutLastLocation.spec.tsx
├── RedirectWithoutLastLocation.tsx
├── index.ts
├── prevent.ts
├── types.ts
├── useLastLocation.spec.tsx
├── useLastLocation.ts
├── withLastLocation.spec.tsx
└── withLastLocation.tsx
├── tests
└── setup.js
├── tsconfig.json
├── webpack.config.js
└── yarn.lock
/ .browserslistrc:
--------------------------------------------------------------------------------
1 | {
2 | "browserslist": [
3 | "defaults"
4 | ]
5 | }
6 |
--------------------------------------------------------------------------------
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hinok/react-router-last-location/2d57c676abb49f3f5755c44fbe2c24b01a8bb68b/.DS_Store
--------------------------------------------------------------------------------
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | [
4 | "@babel/preset-env",
5 | {
6 | "modules": false
7 | }
8 | ],
9 | "@babel/preset-typescript",
10 | "@babel/preset-react",
11 | "linaria/babel"
12 | ],
13 | "env": {
14 | "test": {
15 | "plugins": [
16 | "@babel/plugin-transform-modules-commonjs"
17 | ]
18 | }
19 | },
20 | "plugins": [
21 | "@babel/plugin-syntax-dynamic-import",
22 | "@babel/plugin-syntax-import-meta",
23 | "@babel/plugin-proposal-class-properties",
24 | "@babel/plugin-proposal-json-strings",
25 | [
26 | "@babel/plugin-proposal-decorators",
27 | {
28 | "legacy": true
29 | }
30 | ],
31 | "@babel/plugin-proposal-function-sent",
32 | "@babel/plugin-proposal-export-namespace-from",
33 | "@babel/plugin-proposal-numeric-separator",
34 | "@babel/plugin-proposal-throw-expressions"
35 | ]
36 | }
37 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | # This file is for unifying the coding style for different editors and IDEs.
2 | # More information at http://editorconfig.org
3 |
4 | # No .editorconfig files above the root directory
5 | root = true
6 |
7 | [*]
8 | indent_style = space
9 | end_of_line = lf
10 | charset = utf-8
11 | trim_trailing_whitespace = true
12 | insert_final_newline = true
13 | max_line_length = 100
14 |
15 | [*.{html,hbs,mustache,ejs,js,jsx,ts,tsx,rb,scss,xml,svg,json}]
16 | indent_size = 2
17 |
18 | [*.md]
19 | indent_size = 4
20 |
--------------------------------------------------------------------------------
/.eslintignore:
--------------------------------------------------------------------------------
1 | /coverage/*
2 | /dist/*
3 |
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "airbnb",
3 | "parserOptions": {
4 | "ecmaVersion": 7,
5 | "sourceType": "module",
6 | "ecmaFeatures": {
7 | "impliedStrict": true,
8 | "jsx": true
9 | }
10 | },
11 | "parser": "babel-eslint",
12 | "env": {
13 | "browser": true,
14 | "node": true,
15 | "commonjs": true,
16 | "es6": true,
17 | "jest": true
18 | },
19 | "rules": {
20 | "react/jsx-filename-extension": "off",
21 | "react/jsx-one-expression-per-line": "off",
22 | "jsx-a11y/anchor-is-valid": [
23 | "error",
24 | {
25 | "components": ["Link"],
26 | "specialLink": ["to"]
27 | }
28 | ]
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Project
2 | /dist
3 | /example/dist
4 | /coverage
5 | /.linaria-cache
6 |
7 | # Logs
8 | logs
9 | *.log
10 | npm-debug.log*
11 | yarn-debug.log*
12 | yarn-error.log*
13 |
14 | # Runtime data
15 | pids
16 | *.pid
17 | *.seed
18 | *.pid.lock
19 |
20 | # Directory for instrumented libs generated by jscoverage/JSCover
21 | lib-cov
22 |
23 | # Coverage directory used by tools like istanbul
24 | coverage
25 |
26 | # nyc test coverage
27 | .nyc_output
28 |
29 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
30 | .grunt
31 |
32 | # Bower dependency directory (https://bower.io/)
33 | bower_components
34 |
35 | # node-waf configuration
36 | .lock-wscript
37 |
38 | # Compiled binary addons (http://nodejs.org/api/addons.html)
39 | build/Release
40 |
41 | # Dependency directories
42 | node_modules/
43 | jspm_packages/
44 |
45 | # Typescript v1 declaration files
46 | typings/
47 |
48 | # Optional npm cache directory
49 | .npm
50 |
51 | # Optional eslint cache
52 | .eslintcache
53 |
54 | # Optional REPL history
55 | .node_repl_history
56 |
57 | # Output of 'npm pack'
58 | *.tgz
59 |
60 | # Yarn Integrity file
61 | .yarn-integrity
62 |
63 | # dotenv environment variables file
64 | .env
65 |
66 |
--------------------------------------------------------------------------------
/.npmrc:
--------------------------------------------------------------------------------
1 | scripts-prepend-node-path=true
2 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - 16
4 | install:
5 | - npm install -g yarn
6 | - yarn install
7 | script:
8 | - yarn run ci
9 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Dawid Karabin
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://travis-ci.org/hinok/react-router-last-location)
2 | [](https://coveralls.io/github/hinok/react-router-last-location?branch=master)
3 |
4 | # react-router-last-location
5 |
6 | - Provides access to the last location in `react` + `react-router (v4.x, v5.x)` applications.
7 | - ❤️ Using [`hooks`](https://reactjs.org/docs/hooks-overview.html)? If yes, `useLastLocation`.
8 | - 💉 Using [`HOC`](https://reactjs.org/docs/higher-order-components.html)? - If yes, `withLastLocation`.
9 | - Handle redirects.
10 | - Support 
11 | - Useful for handling internal routing.
12 | - Easily keep your users inside your app.
13 |
14 | ## Demo
15 |
16 | [](https://codesandbox.io/s/zn208l91zp)
17 |
18 | ## Note: Last location != Previous browser history state
19 |
20 | This library only returns the location that was active right before the recent location change, during the lifetime of the current window.
21 |
22 | This means, it is not equal to the "location you were at before navigating to this history state".
23 |
24 | In other words, the location this library provides is not necessarily the same as the one when you click the browser's back button.
25 |
26 | **Example 1**
27 |
28 | 1. Visit `/`: last location = `null`, previous browser history state = `null`
29 | 2. Visit `/a`: last location = `/`, previous browser history state = `/`
30 | 3. Visit `/b`: last location = `/a`, previous browser history state = `/a`
31 | 4. Reload (url will stay at `/b`): last location = `null`, previous browser history state = `/a`
32 |
33 | **Example 2**
34 |
35 | 1. Visit `/`: last location = `null`
36 | 2. Visit `/a`: last location = `/`
37 | 3. Visit `/b`: last location = `/a`
38 | 4. Go back: last location = `/b`, previous browser history state = `/`
39 |
40 | **Example 3**
41 |
42 | 1. Visit `/`: last location = `null`
43 | 2. Visit `/a`: last location = `/`
44 | 3. Visit `/b`: last location = `/a`
45 | 4. Visit `/c`: last location = `/b`
46 | 4. 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 = `/`
47 |
48 | ## How to use?
49 |
50 | ```bash
51 | # Please remember that you should have installed react, prop-types and react-router-dom packages
52 | # npm install react prop-types react-router-dom --save
53 |
54 | npm install react-router-last-location --save
55 | ```
56 |
57 | **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).**
58 |
59 | ```bash
60 | npm install react-router-last-location@^2.0.0
61 | # or
62 | npm install react-router-last-location@latest
63 | ```
64 |
65 | ### Declare `
84 |
88 |
89 |
90 |
91 |
96 |
97 |
121 | {JSON.stringify(lastLocation)} 122 |123 |
143 | {JSON.stringify(lastLocation)} 144 |145 |
43 | {JSON.stringify(data, undefined, 2)} 44 |45 |
{JSON.stringify(location, undefined, 2)}12 |
(WrappedComponent: React.ComponentType
) {
7 | return WrappedComponent.displayName || WrappedComponent.name || 'Component';
8 | }
9 |
10 | export interface WithLastLocationProps extends RouteComponentProps {
11 | lastLocation: ReturnType