https://github.com/gadicc/redux-router-state
Store router state in Redux and route via redux
https://github.com/gadicc/redux-router-state
Last synced: about 1 year ago
JSON representation
Store router state in Redux and route via redux
- Host: GitHub
- URL: https://github.com/gadicc/redux-router-state
- Owner: gadicc
- License: mit
- Created: 2016-05-27T10:07:19.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-06-13T08:28:43.000Z (almost 10 years ago)
- Last Synced: 2025-03-11T04:48:03.345Z (about 1 year ago)
- Language: JavaScript
- Size: 20.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# redux-router-state
Store router state in Redux and route via redux
[](https://www.npmjs.com/package/redux-router-state) [](https://circleci.com/gh/gadicc/redux-router-state) [](https://coveralls.io/github/gadicc/redux-router-state?branch=master) 
Copyright (c) 2016 Gadi Cohen <dragon@wastelands.net>, released under the MIT license.
## Design Goals:
* Storing router state in redux is a design principle, not an after thought
* `Router.go()`, `Router.setParams()`, etc dispatch actions to the redux store.
* `pushState` fires on changes to *redux* state (i.e. time travel works great out the box).
* The (optional) react component subscribes to redux router state for changes
* See "Related Projects" at the bottom of the README for (dis)similar alternatives
## Setup and Pure-JS (no react usage)
*See below for React helpers*
```js
import { combineReducers, createStore } from 'redux';
import Router from 'redux-router-state';
// Add your routes BEFORE creating your Store with the reducer
// This step could be done for you via the React helper, see below.
Router.add('home', '/');
Router.add('issue_id', '/issues/:id', optionalData);
Router.add()
// Include Router.reducer when setting up your reducers
const reducers = combineReducers({
route: Router.reducer
});
// However you usually create your store
const Store = createStore(reducers, {},
window.devToolsExtension && window.devToolsExtension()
);
// Initialize the Router with your store.
Router.init(Store);
```
Sample State:
```js
// http://x.com/issues/1/?action=edit#mode=markdown
{
route: {
name: 'issue_id',
pathname: '/issues/1/'
params: {
id: 1
},
query: {
action: 'edit'
},
hash: {
mode: 'markdown'
},
data: optionalData
}
}
```
## React
### Optional react-router inspired config
```js
const App = () => (
{asc: route.queryParams.asc}} />
);
const ShowIssue({params}) => (
Issue #{params.id}
);
const ShowUsers({asc}) => (
Users ({asc?"Asceending":"Descending")
);
```
### Creating links:
```js
Issue #1
```
### Accessing route info
**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).
By 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:
`connectRouter([optionalMappingFunction], Component)`
```js
const showIssue = routerConnect(
// optional mapping function, provides 'id' as the only prop
(route) => { id: route.params.id },
// display component, could be an existing constant
(id) => (
Issue #{id}
)
);
// Without the optional mapping function, a "route" prop is given, with the
// entire router state. This may lead to unnecessary re-rendering.
```
Like `react-redux`'s `connect` (which we use), it assumes a `` ancestor.
### Anything more complicated?
Don'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.
## What about feature XXX? What about forced login?
Get 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.
```js
Router.add('inbox', '/inbox', { requiresLogin: true });
// Not implemented yet, still planning...
const customRouteReducer = (routeState) => {
routeState = Router.reduce(routeState);
if (routeState.data.requiresLogin && !loggedIn)
Router.rewriteState(routerState, 'loginPage');
return routerState;
};
// With your other reducers...
const reducers = combineReducers({
route: customRouteReducer
});
```
May still go with groups, `onReduce`, and/or similar methods.
## Related projects:
* 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.
* 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.
## TODO
* Router.add - should dispatch on matching route
* Router.add component for
* should call Router.add
* needs way to be refreshed on route add after render time
* Pattern for forcing login