redux-react-router
Documentation is in progress. Please refer to the basic example in the meantime.
Redux bindings for React Router.
Keep your router state inside your Redux Store.
Interact with the Router with the same API you use to interact with the rest of your app state.
Completely interoperable with existing React Router API.
<Link />
,router.transitionTo()
, etc. still work.Serialize and deserialize router state.
Works with time travel feature of Redux Devtools!
Why
React Router is a fantastic routing library, but one downside is that it abstracts away a very crucial piece of application state — the current route! This abstraction is super useful for route matching and rendering, but the API for interacting with the router to 1) trigger transitions and 2) react to state changes within the component lifecycle leaves something to be desired.
It turns out we already solved these problems with Flux (and Redux): We use action creators to trigger state changes, and we use higher-order components to subscribe to state changes.
This library allows you to keep your router state inside your Redux store. So getting the current pathname, query, and params is as easy as selecting any other part of your application state.
Example
Works with Redux Devtools (and other external state changes)
redux-react-router will notice if the router state in your Redux store changes from an external source other than the router itself — e.g. the Redux Devtools — and trigger a transition accordingly!
API
reduxReactRouter({ routes, createHistory, routerStateSelector })
reduxReactRouter({ routes, createHistory, routerStateSelector })
A Redux store enhancer that adds router state to the store.
routerStateReducer(state, action)
routerStateReducer(state, action)
A reducer that keeps track of Router state.
<ReduxRouter>
<ReduxRouter>
A component that renders a React Router app using router state from a Redux store.
pushState(state, pathname, query)
pushState(state, pathname, query)
An action creator for history.pushState()
.
replaceState(state, pathname, query)
replaceState(state, pathname, query)
An action creator for history.replaceState()
.
Bonus: Reacting to state changes with redux-rx
This library pairs well with redux-rx to trigger route transitions in response to state changes. Here's a simple example of redirecting to a new page after a successful login:
A more complete example is forthcoming.
Last updated