redux-react-router
npm install --save redux-react-routerWhy
Example
import React from 'react';
import { combineReducers, applyMiddleware, compose } from 'redux';
import { Provider } from 'react-redux';
import { reduxReactRouter, routerStateReducer, ReduxRouter } from 'redux-react-router';
// Configure routes like normal
const routes = (
<Route path="/" component={App}>
<Route path="parent" component={Parent}>
<Route path="child" component={Child} />
<Route path="child/:id" component={Child} />
</Route>
</Route>
);
// Configure reducer to store state at state.router
// You can store it elsewhere by specifying a custom `routerStateSelector`
// in the store enhancer below
const reducer = combineReducers({
router: routerStateReducer
});
// Compose reduxReactRouter with other store enhancers
const store = compose(
applyMiddleware(m1, m2, m3),
reduxReactRouter({
routes,
createHistory
}),
devTools()
)(createStore)(reducer);
// Elsewhere, in a component module...
import { connect } from 'react-redux';
import { pushState } from 'redux-react-router';
connect(
// Use a selector to subscribe to state
state => ({ q: state.router.location.query.q }),
// Use an action creator for navigation
{ pushState }
)(SearchBox);Works with Redux Devtools (and other external state changes)
API
reduxReactRouter({ routes, createHistory, routerStateSelector })
reduxReactRouter({ routes, createHistory, routerStateSelector })routerStateReducer(state, action)
routerStateReducer(state, action)<ReduxRouter>
<ReduxRouter>pushState(state, pathname, query)
pushState(state, pathname, query)replaceState(state, pathname, query)
replaceState(state, pathname, query)Bonus: Reacting to state changes with redux-rx
Last updated