{"id":15501821,"url":"https://github.com/gadicc/redux-router-state","last_synced_at":"2025-04-05T10:26:01.822Z","repository":{"id":57351480,"uuid":"59825308","full_name":"gadicc/redux-router-state","owner":"gadicc","description":"Store router state in Redux and route via redux","archived":false,"fork":false,"pushed_at":"2016-06-13T08:28:43.000Z","size":21,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-11T04:48:03.345Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/gadicc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-27T10:07:19.000Z","updated_at":"2016-06-01T13:32:09.000Z","dependencies_parsed_at":"2022-08-31T03:53:31.070Z","dependency_job_id":null,"html_url":"https://github.com/gadicc/redux-router-state","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gadicc%2Fredux-router-state","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gadicc%2Fredux-router-state/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gadicc%2Fredux-router-state/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gadicc%2Fredux-router-state/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gadicc","download_url":"https://codeload.github.com/gadicc/redux-router-state/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247321104,"owners_count":20919920,"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":[],"created_at":"2024-10-02T09:05:57.512Z","updated_at":"2025-04-05T10:26:01.805Z","avatar_url":"https://github.com/gadicc.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# redux-router-state\n\nStore router state in Redux and route via redux\n\n[![npm](https://img.shields.io/npm/v/redux-router-state.svg?maxAge=2592000)](https://www.npmjs.com/package/redux-router-state) [![Circle CI](https://circleci.com/gh/gadicc/redux-router-state.svg?style=shield)](https://circleci.com/gh/gadicc/redux-router-state) [![Coverage Status](https://coveralls.io/repos/github/gadicc/redux-router-state/badge.svg?branch=master)](https://coveralls.io/github/gadicc/redux-router-state?branch=master) ![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)\n\nCopyright (c) 2016 Gadi Cohen \u0026lt;dragon@wastelands.net\u0026gt;, released under the MIT license.\n\n## Design Goals:\n\n* Storing router state in redux is a design principle, not an after thought\n* `Router.go()`, `Router.setParams()`, etc dispatch actions to the redux store.\n* `pushState` fires on changes to *redux* state (i.e. time travel works great out the box).\n* The (optional) react \u003cRouter\u003e component subscribes to redux router state for changes\n* See \"Related Projects\" at the bottom of the README for (dis)similar alternatives\n\n## Setup and Pure-JS (no react usage)\n\n*See below for React helpers*\n\n```js\nimport { combineReducers, createStore } from 'redux';\nimport Router from 'redux-router-state';\n\n// Add your routes BEFORE creating your Store with the reducer\n// This step could be done for you via the React helper, see below.\nRouter.add('home', '/');\nRouter.add('issue_id', '/issues/:id', optionalData);\nRouter.add()\n\n// Include Router.reducer when setting up your reducers\nconst reducers = combineReducers({\n  route: Router.reducer\n});\n\n// However you usually create your store\nconst Store = createStore(reducers, {},\n  window.devToolsExtension \u0026\u0026 window.devToolsExtension()\n);\n\n// Initialize the Router with your store.\nRouter.init(Store);\n```\n\nSample State:\n\n```js\n// http://x.com/issues/1/?action=edit#mode=markdown\n{\n  route: {\n    name: 'issue_id',\n    pathname: '/issues/1/'\n    params: {\n      id: 1\n    },\n    query: {\n      action: 'edit'\n    },\n    hash: {\n      mode: 'markdown'\n    },\n    data: optionalData\n  }\n}\n```\n\n## React\n\n### Optional react-router inspired config\n\n```js\nconst App = () =\u003e (\n  \u003cProvider store={Store} /\u003e\n    \u003cRouter\u003e\n      \u003cRoute name=\"home\" path=\"/\" component={Home} /\u003e\n\n      \u003cRoute name=\"issues\" path=\"/issues/:id\" component={ShowIssue} /\u003e\n\n      \u003cRoute name=\"users\" path=\"/users\" component={ShowUsers}\n        mapRouteToProps={route =\u003e {asc: route.queryParams.asc}} /\u003e\n\n      \u003cRoute name={null} component={NotFound} /\u003e\n    \u003c/Router\u003e\n  \u003c/Provider\u003e\n);\n\nconst ShowIssue({params}) =\u003e (\n  \u003ch1\u003eIssue #{params.id}\u003c/h1\u003e\n);\n\nconst ShowUsers({asc}) =\u003e (\n  \u003ch1\u003eUsers ({asc?\"Asceending\":\"Descending\")\u003c/h1\u003e\n);\n\n```\n\n### Creating links:\n\n```js\n\u003cLink to=\"issues\" params={{id: 1}}\u003eIssue #1\u003c/Link\u003e\n```\n\n### Accessing route info\n\n**NB: all params are provided as Strings**, since they come from the URL.  It's up to you to convert to Numbers, if needed (e.g. before comparisons).\n\nBy default, only the route params are passed down as individual props, to avoid unnecessary re-rendering.  You can override this with the `mapRouteToProps` attribute (as above), and/or, use the `connectRouter` HOC on individual compoents that works pretty much how you'd expect:\n\n`connectRouter([optionalMappingFunction], Component)`\n\n```js\nconst showIssue = routerConnect(\n  // optional mapping function, provides 'id' as the only prop\n  (route) =\u003e { id: route.params.id },\n\n  // display component, could be an existing constant\n  (id) =\u003e ( \u003ch1\u003eIssue #{id}\u003c/h1\u003e )\n);\n\n// Without the optional mapping function, a \"route\" prop is given, with the\n// entire router state.  This may lead to unnecessary re-rendering.\n```\n\nLike `react-redux`'s `connect` (which we use), it assumes a `\u003cProvider\u003e` ancestor.\n\n### Anything more complicated?\n\nDon't forget, the above are just convenience helpers.  Your entire route state is available in the redux store, that you can use just like any other state.\n\n## What about feature XXX?  What about forced login?\n\nGet out of the habit of thinking about the Router as a separate entity, and realize that it's now just like any other state in your store.  A lot of the the things we needed before as router features can now just be done by subscribing to state changes or with additional reducer functions.  e.g.\n\n```js\nRouter.add('inbox', '/inbox', { requiresLogin: true });\n\n// Not implemented yet, still planning...\nconst customRouteReducer = (routeState) =\u003e {\n  routeState = Router.reduce(routeState);\n  if (routeState.data.requiresLogin \u0026\u0026 !loggedIn)\n    Router.rewriteState(routerState, 'loginPage');\n  return routerState;\n};\n\n// With your other reducers...\nconst reducers = combineReducers({\n  route: customRouteReducer\n});\n```\n\nMay still go with groups, `onReduce`, and/or similar methods.\n\n## Related projects:\n\n* Similar to [universal-redux-router](https://www.npmjs.com/package/universal-redux-router) but works in a slightly different (and imho simpler) way, i.e. no need to define your own reducers for each part of every route.\n\n* Dissimilar to [react-router-redux](https://github.com/reactjs/react-router-redux) and react-router which stores state in a hidden redux store just for timetravel.\n\n## TODO\n\n* Router.add - should dispatch on matching route\n* Router.add component for \u003cRouter\u003e\n* \u003cRoute\u003e should call Router.add\n* \u003cRouter\u003e needs way to be refreshed on route add after render time\n* Pattern for forcing login\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgadicc%2Fredux-router-state","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgadicc%2Fredux-router-state","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgadicc%2Fredux-router-state/lists"}