Let react-router do all the work ✨
Redux is awesome. React Router is cool. The problem is that react-router manages an important piece of your application state: the URL. If you are using redux, you want your app state to fully represent your UI; if you snapshotted the app state, you should be able to load it up later and see the same thing.
react-router does a great job of mapping the current URL to a component tree, and continually does so with any URL changes. This is very useful, but we really want to store this state in redux as well.
The entire state that we are interested in boils down to one thing: the URL. This is an extremely simple library that just puts the URL in redux state and keeps it in sync with any react-router changes. Additionally, you can change the URL via redux and react-router will change accordingly.
npm install redux-simple-router
If you want to install the next major version, use redux-simple-router@next
. Run npm dist-tag ls redux-simple-router
to see what next
is aliased to.
View the CHANGELOG for recent changes.
Read the API docs farther down this page.
redux-router is another project which solves the same problem. However, it's far more complex. Take a quick look at the code for this library—it's extremely minimal. redux-router is much bigger and more complex.
That said, redux-router is a fine project and has features this doesn't provide. Use it if you like it better.
Compared with redux-router:
- Much smaller and simpler. You don't need to learn another library on top of everything else.
- We encourage direct access of react-router APIs. Need server-side rendering, or something else advanced? Just read react-router's docs.
- We only store current URL and state, whereas redux-router stores the entire location object from react-router.
The idea of this library is to use react-router's functionality exactly like its documentation tells you to. You can access all of its APIs in routing components. Additionally, you can use redux like you normally would, with a single app state and "connected" components. It's even possible for a single component to be both if needed.
redux (store.routing
) ↔ redux-simple-router ↔ history (history.location
) ↔ react-router
We only store current URL and state, whereas redux-router stores the entire location object from react-router. You can read it, and also change it with an action.
Let's take a look at a simple example.
import React from 'react'
import ReactDOM from 'react-dom'
import { compose, createStore, combineReducers, applyMiddleware } from 'redux'
import { Provider } from 'react-redux'
import { Router, Route, browserHistory } from 'react-router'
import { syncHistory, routeReducer } from 'redux-simple-router'
import reducers from '<project-path>/reducers'
const reducer = combineReducers(Object.assign({}, reducers, {
routing: routeReducer
}))
// Sync dispatched route actions to the history
const reduxRouterMiddleware = syncHistory(browserHistory)
const createStoreWithMiddleware = applyMiddleware(reduxRouterMiddleware)(createStore)
const store = createStoreWithMiddleware(reducer)
// Required for replaying actions from devtools to work
reduxRouterMiddleware.listenForReplays(store)
ReactDOM.render(
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/" component={App}>
<Route path="foo" component={Foo}/>
<Route path="bar" component={Bar}/>
</Route>
</Router>
</Provider>,
document.getElementById('mount')
)
Now you can read from state.routing.location.pathname
to get the URL. It's far more likely that you want to change the URL more often, however. You can use the push
action creator that we provide:
import { routeActions } from 'redux-simple-router'
function MyComponent({ dispatch }) {
return <Button onClick={() => dispatch(routeActions.push('/foo'))}/>;
}
This will change the state, which will trigger a change in react-router. Additionally, if you want to respond to the path update action, just handle the UPDATE_LOCATION
constant that we provide:
import { UPDATE_LOCATION } from 'redux-simple-router'
function update(state, action) {
switch(action.type) {
case UPDATE_LOCATION:
// do something here
}
}
- examples/basic - basic reference implementation
Examples from the community:
- davezuko/react-redux-starter-kit - popular redux starter kit
- tip: migrating from redux-router? use this commit as a reference
- freeqaz/redux-simple-router-example - example implementation
- choonkending/react-webpack-node - boilerplate for universal redux and react-router
- tj/frontend-boilerplate
- bdefore/universal-redux - npm package for universally rendered redux applications
- yangli1990/react-redux-isomorphic - boilerplate for universal redux and redux-simple-router
- StevenIseki/redux-simple-router-example
Have an example to add? Send us a PR!
Call this to create a middleware that can be applied with Redux's applyMiddleware
to allow actions to call history methods. The middleware will look for route actions created by push
, replace
, etc. and applies them to the history.
By default, the syncing logic will not respond to replaying of actions, which means it won't work with projects like redux-devtools. Call this function on the middleware object returned from syncHistory
and give it the store to listen to, and it will properly work with action replays. Obviously, you would do that after you have created the store and everything else has been set up.
Supply an optional function selectRouterState
to customize where to find the router state on your app state. It defaults to state => state.routing
, so you would install the reducer under the name "routing". Feel free to change this to whatever you like.
Call this on the middleware returned from syncHistory
to stop the syncing process set up by listenForReplays
.
A reducer function that keeps track of the router state. You must to add this reducer to your app reducers when creating the store.
An action type that you can listen for in your reducers to be notified of route updates.
An object that contains all the actions creators you can use to manipulate history:
push(nextLocation: LocationDescriptor)
replace(nextLocation: LocationDescriptor)
go(n: number)
goForward()
goBack()
A location descriptor can be a descriptive object (see the link) or a normal URL string. The most common action is to push a new URL via routeActions.push(...)
. These all directly call the analogous history methods.