{"id":13527629,"url":"https://github.com/hinok/react-router-last-location","last_synced_at":"2025-04-12T19:50:47.775Z","repository":{"id":21295180,"uuid":"92157932","full_name":"hinok/react-router-last-location","owner":"hinok","description":"Provides access to the last location in react + react-router (v4.x, v5.x) applications. ❤️ Using hooks? useLastLocation | 💉 Using HOC? withLastLocation","archived":false,"fork":false,"pushed_at":"2022-12-10T17:23:16.000Z","size":1602,"stargazers_count":284,"open_issues_count":18,"forks_count":24,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-30T00:36:46.262Z","etag":null,"topics":["history","last-location","location","react","react-router","reactjs"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hinok.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-23T10:02:12.000Z","updated_at":"2024-03-19T09:01:58.000Z","dependencies_parsed_at":"2023-01-12T03:45:25.256Z","dependency_job_id":null,"html_url":"https://github.com/hinok/react-router-last-location","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hinok%2Freact-router-last-location","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hinok%2Freact-router-last-location/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hinok%2Freact-router-last-location/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hinok%2Freact-router-last-location/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hinok","download_url":"https://codeload.github.com/hinok/react-router-last-location/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625501,"owners_count":21135513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["history","last-location","location","react","react-router","reactjs"],"created_at":"2024-08-01T06:01:54.609Z","updated_at":"2025-04-12T19:50:47.756Z","avatar_url":"https://github.com/hinok.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/hinok/react-router-last-location.svg?branch=master)](https://travis-ci.org/hinok/react-router-last-location)\n[![Coverage Status](https://coveralls.io/repos/github/hinok/react-router-last-location/badge.svg?branch=master)](https://coveralls.io/github/hinok/react-router-last-location?branch=master)\n\n# react-router-last-location\n\n- Provides access to the last location in `react` + `react-router (v4.x, v5.x)` applications.\n- ❤️ Using [`hooks`](https://reactjs.org/docs/hooks-overview.html)? If yes, `useLastLocation`.\n- 💉 Using [`HOC`](https://reactjs.org/docs/higher-order-components.html)? - If yes, `withLastLocation`.\n- Handle redirects.\n- Support ![TypeScript](https://user-images.githubusercontent.com/1313605/53197634-df9a6d00-361a-11e9-81ba-69f8a941f8a2.png)\n- Useful for handling internal routing.\n- Easily keep your users inside your app.\n\n## Demo\n\n[![Edit react-router-last-location](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/zn208l91zp)\n\n## Note: Last location != Previous browser history state\n\nThis library only returns the location that was active right before the recent location change, during the lifetime of the current window.\n\nThis means, it is not equal to the \"location you were at before navigating to this history state\".\n\nIn other words, the location this library provides is not necessarily the same as the one when you click the browser's back button.\n\n**Example 1**\n\n1. Visit `/`: last location = `null`, previous browser history state = `null`\n2. Visit `/a`: last location = `/`, previous browser history state = `/`\n3. Visit `/b`: last location = `/a`, previous browser history state = `/a`\n4. Reload (url will stay at `/b`): last location = `null`, previous browser history state = `/a`\n\n**Example 2**\n\n1. Visit `/`: last location = `null`\n2. Visit `/a`: last location = `/`\n3. Visit `/b`: last location = `/a`\n4. Go back: last location = `/b`, previous browser history state = `/`\n\n**Example 3**\n\n1. Visit `/`: last location = `null`\n2. Visit `/a`: last location = `/`\n3. Visit `/b`: last location = `/a`\n4. Visit `/c`: last location = `/b`\n4. 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 = `/`\n\n## How to use?\n\n```bash\n# Please remember that you should have installed react, prop-types and react-router-dom packages\n# npm install react prop-types react-router-dom --save\n\nnpm install react-router-last-location --save\n```\n\n**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).**\n\n```bash\nnpm install react-router-last-location@^2.0.0\n# or\nnpm install react-router-last-location@latest\n```\n\n### Declare `\u003cLastLocationProvider /\u003e` inside `\u003cRouter /\u003e`.\n\n`index.js`\n\n```jsx\nimport React from 'react';\nimport { render } from 'react-dom';\nimport { BrowserRouter as Router, Route, Link } from 'react-router-dom';\nimport { LastLocationProvider } from 'react-router-last-location';\nimport Home from './pages/Home';\nimport About from './pages/About';\nimport Contact from './pages/Contact';\nimport Logger from './components/Logger';\n\nconst App = () =\u003e (\n  \u003cRouter\u003e\n    \u003cLastLocationProvider\u003e\n      \u003cdiv\u003e\n        \u003cul\u003e\n          \u003cli\u003e\u003cLink to=\"/\"\u003eHome\u003c/Link\u003e\u003c/li\u003e\n          \u003cli\u003e\u003cLink to=\"/about\"\u003eAbout\u003c/Link\u003e\u003c/li\u003e\n          \u003cli\u003e\u003cLink to=\"/contact\"\u003eContact\u003c/Link\u003e\u003c/li\u003e\n        \u003c/ul\u003e\n\n        \u003chr /\u003e\n\n        \u003cRoute exact path=\"/\" component={Home} /\u003e\n        \u003cRoute path=\"/about\" component={About} /\u003e\n        \u003cRoute path=\"/contact\" component={Contact} /\u003e\n\n        \u003chr /\u003e\n\n        \u003cLogger /\u003e\n      \u003c/div\u003e\n    \u003c/LastLocationProvider\u003e\n  \u003c/Router\u003e\n);\n\nrender(\u003cApp /\u003e, document.getElementById('root'));\n```\n\n### Use hook `useLastLocation` to get `lastLocation`.\n\n`./components/Logger`, [see example](https://github.com/hinok/react-router-last-location/blob/eb552e0a82df6000ba140d8f20627b8bc68716b6/example/src/components/LoggerHooks/index.js)\n\n```jsx\nimport React from 'react';\nimport { useLastLocation } from 'react-router-last-location';\n\nconst Logger = () =\u003e {\n  const lastLocation = useLastLocation();\n\n  return (\n    \u003cdiv\u003e\n      \u003ch2\u003eLogger!\u003c/h2\u003e\n      \u003cpre\u003e\n        {JSON.stringify(lastLocation)}\n      \u003c/pre\u003e\n    \u003c/div\u003e\n  );\n};\n\nexport default LoggerHooks;\n\n```\n\n### Use HOC `withLastLocation` to get `lastLocation` prop.\n\n`./components/Logger`, [see example](https://github.com/hinok/react-router-last-location/blob/eb552e0a82df6000ba140d8f20627b8bc68716b6/example/src/components/LoggerHOC/index.js)\n\n```jsx\nimport React from 'react';\nimport { withLastLocation } from 'react-router-last-location';\n\nconst Logger = ({ lastLocation }) =\u003e (\n  \u003cdiv\u003e\n    \u003ch2\u003eLogger!\u003c/h2\u003e\n    \u003cpre\u003e\n      {JSON.stringify(lastLocation)}\n    \u003c/pre\u003e\n  \u003c/div\u003e\n);\n\nexport default withLastLocation(Logger);\n```\n\n### Use `RedirectWithoutLastLocation` to not store redirects as last location\n\n```jsx\nimport React from 'react';\nimport { RedirectWithoutLastLocation } from 'react-router-last-location';\n\nconst MyPage = () =\u003e (\n  \u003cRedirectWithoutLastLocation to=\"/\" /\u003e\n);\n\nexport default MyPage;\n```\n\nYou can still use a regular `\u003cRedirect /\u003e` component from `react-router`.\n\nIf you do, you'll  then you need to manually pass the `state: { preventLastLocation: true }`, like below:\n\n```jsx\nimport React from 'react';\nimport { Redirect } from 'react-router-dom';\n\nconst MyPage = () =\u003e (\n  \u003cRedirect\n    to={{\n      pathname: '/',\n      state: { preventLastLocation: true },\n    }}\n  /\u003e\n);\n\nexport default MyPage;\n```\n\n## LastLocationProvider\n\n### Props\n\n**`watchOnlyPathname`**, type: `boolean`, default: `false`\n\nStores the last route only when `pathname` has changed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhinok%2Freact-router-last-location","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhinok%2Freact-router-last-location","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhinok%2Freact-router-last-location/lists"}