react-router: [v4] Breaking Changes
The same idea was reflected in #4697, but that part was ignored, therefore opening a new issue to specifically address that.
I can see that v4 is coming up with the pre-releases from the past days to which I can only say congrats š
However, I cannot see anything about the very useful hooks that were available in v3:
Furthermore, I do not see anything to reflect the above decision in the change log, nor cannot find a roadmap that describes this methods are deprecated. Should I expect v4 will break all my code?
I find this hooks very useful, for example, I was using the onEnter hook to validate if the user is authenticated in a very elegant way compared to the way is illustrated in the v4 docs. In my opinion, my solution works better because I need the user to be authenticated on all pages except /login.
Another thing I cannot find with v4 is Configuration with Plain Routes, where, why did that go?
Maybe letās not close this right away and use this forum to debate as this package is essential in my projects and, hopefully, this will end in a clear roadmap for all of us.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 20
- Comments: 15 (3 by maintainers)
v4 is a complete rewrite. As such, there is no singular breaking change. We have some similar-looking things (
<Route path="/foo" component={Foo} />), but the behaviors are completely different. You should expect none of your existing react-router usage to work under 4.0.As for the on* hooks, they were removed because they already exist in React as the lifecycle methods. We were reimplementing them inside the Router, but they didnāt exactly behave like they should. So, why provide an inferior or conflicting API? Everything is way more React-y now, and itās way better as a result. You no longer have to mix paradigms or code for two different systems. Itās just React now. Itās a much lower cognitive load.
And for āplain routesā, weāve extracted that to a separate package: https://github.com/ReactTraining/react-router/tree/master/packages/react-router-config You can see the reasoning for this here: https://github.com/ReactTraining/react-router/issues/4410#issuecomment-276993829
@timdorr how do you prevent a route change now then?
onEnterisnt the same ascomponentWillMountbecauseonEnterwas called before the mounting process began as far as I know.This seems to be a large loss in functionality as far as the
replaceandcallbackpairing goes that you cant replicate withcomponentWillMount.Yeah, did a dry run of converting v3 -> v4 and itās going to be super-painful on my project. The nice thing about the onEnter/onLeave handlers is that I would write my containers in my redux app so that they were only dependent upon the store state. Now I have to extract the path variables (e.g. something like itemId in /items/42) from the āmatchā prop and update the store that way. On top of the lifecycle methods require that I can no longer use stateless functional components and need to implement React lifecycle methods just to make the router integration work. I think Iām staying with v3 for the foreseeable future. The v4 code is just a lot uglier.
@timdorr - re: āthey were removed because they already exist in React as the lifecycle methodsā
What in the world are you talking about? Are we supposed to subclass Route and override componentDidMount and componentWillReceiveProps or something? It sure doesnāt seem like thereās an easy way to implement an onEnter or onLeave hook in V4.
The simple answer.
Usage:
On the topic of breaking changes: what happened to the
location.queryobject? I see that the query string itself is there aslocation.search, but not the parsed object. Am I missing something or was this completely removed from v4? Seems an important feature that wouldnāt make sense to removeā¦@jfconde Looks pretty good IMO - I donāt see any issue in having all route components connected to redux.
You can fix the ādirtyā part by using the
connectdirectly inwithBreadcrumband exporting that afterwards:@Johnius that seems helpful for prompts, but not for what I had in mind. A pattern that I use, and I think is common, is to wrap my routes that require login in a
Route, such as:And the
Authenticated.enter(store)function might return something like:I assume that v4 expects me to have instead have a wrapper component in the
Authenticatedcomponentāsrender()that implements something similar in itscomponentWillMount. But that still leaves me wondering about the functionality ofreplaceandcallbackEdit
Looking over the docs more - it seems like
replacecan be replaced with<Redirect />and composing theRoutecomponent. Not sure ifRoutecomposition can be used to the same effect forcallback(ie. performing an action before the page rendering starts to change), Iāll have to experiment with it.What I would recommend to the maintainers is to elaborate more on the on* section of the migration doc. Itās a bit sparse and could use examples of how to handle
replaceandcallback@johnomalley The lifecycle methods get placed on the component rendered by the
<Route>.@MrXyfir You can read about that decision here