Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
React Router Redux recently incorporated the changes originally suggested in #1362. The RC of 4.x is out, so I encourage you to start migrating from the old versions.
The 4.x release is motivated by the desire to fix a few long-standing hard-to-fix bugs, become closer to Redux way of doing things, and by API changes in React Router 2.0. I know this is a lot of churn but it comes out of the lessons we learned from the real world usage. After this change, the project should be much more stable.
In case you haven’t been following, here is what changed:
You May Remove the Middleware
Since React Router 2.0 gives us convenient history singletons (
hashHistory
andbrowserHistory
), the middleware to handle history actions is no longer necessary. It still exists for those who prefer todispatch(push())
rather thanhistory.push()
. In fact it is as straightforward as middleware gets.Feel free to use it if you still like it. However we encourage you to just use the singletons provided by React Router instead:
Many people said that they used those action creators so they are logged for analytics. This no longer installing middleware or using those action creators. With 4.x, when you
push()
to a history, a special action will still be dispatched on every history change, so you can log that instead. And of course you can alwaysbrowserHistory.listen(location => ...)
yourself.Why would you still want the middleware then? I’ve heard that it can be helpful for some use cases but unless you know you need it, don’t use it.
I removed the usage of middleware in this example.
Prefer to Read the Routing State from Props
This example used to read routing state from
state.routing
. This was actually a bad idea and something that React Router Redux README had a warning for. If you use asynchronous transitions in React Router, you might get into infinite loops if you readstate.routing
in components. Why? The location instate.routing
describes the location before any transitions—the one obtained from the URL bar. Router is stateful and lets you do async work before showing the related UI, but this is why you should always be reading the location from theprops
supplied to you by the router.This is why do this:
We still keep the routing state in
state.routing
(the reducer key is up to you) so you can read it from your custom middleware (for example, for analytics). However you will notice that the actual location is instate.routing.locationBeforeTransitions
. This is our way of warning you that this is not the location you want to use in your UI.Synchronizing with the Store
So why use this library at all then? It only buys you a few things:
If you don’t care about these features, just don’t use it, and use React Router 2.0 directly.
If you do care, the last thing you need to change is to wrap your history before passing it to the router. This is how we translate history changes into Redux actions, and make the store a source of truth for the router. You only need to do it once wherever you render the
<Router>
:This is it. See the master branch of React Router Redux for a normal and a server rendering example.