react-router4-redux-example icon indicating copy to clipboard operation
react-router4-redux-example copied to clipboard

A demo of integrating Redux & React Router v4

Deprecated

React Router's API changed between the v4 alphas and the release version, making this code irrelevant. If you're looking for Redux-React Router v4 bindings I recommend checking out Connected React Router.


React Router v4 w/ React Router Redux Demo

An example for illustrating a possible technical direction for integrating Redux & React Router v4.

Differences

The modified version of react-router-redux is in src/react-router-redux/. The differences are in reducer.js and sync.js.

reducer.js

reducer.js exports a factory function createReducer that takes history. The reducer needs access to the history instance so it can set its initial state properly.

sync.js

Syncing the store with the history is now much simpler thanks to a one-way flow of state. It has an order of magnitude less code (~10 lines vs ~100 lines), and there's no more internal state. We simply subscribe to the history and dispatch an action to the store with the given location and action.

Dev Instrumentation

I've instrumented the app with a few handy hooks for playing with the app state:

  • dispatch - the store's dispatch is available globally as dispatch
  • Router redux actions - the router redux action creators (push, replace, go, goBack, & goForward) are available globally. Try something like dispatch(push('/bar')) or dispatch(goBack()).
  • history - the history instance is available globally as h. Try something like h.push('/foo').
  • Redux Dev Tools - the store is instrumented to dev tools so you can play around with time travel debugging