5 ms·
Yeah I'm not a fan at all of react-router because it's too dependent on React. For redirecting, for example, they recommend you render a <Redirect> component. W
by thatswrong0 7y ago
Yeah I'm not a fan at all of react-router because it's too dependent on React. For redirecting, for example, they recommend you render a <Redirect> component. Which is just bananas to me.
- arthurdenture 7y agoThe alternative would be to write a side effect inside your `render()` function, which is illegal -- it breaks the new Concurrent mode rendering, which can call render() speculatively. I mean, I see why it's intuitively unappealing to you, but there are perfectly good reasons for the design.
- thatswrong0 7y agoThere are "perfectly good reasons" in the sense that if you artificially limit yourself to using the React component tree to manage routes, yes it makes sense how they arrived at this solution. But after dealing with it for a while, it's just so obviously not the correct approach, and it's crazy to me that it's the defacto router. Cargo culting at its finest. The router could exist outside of or at a higher level than the component tree.
- jaquers 7y agoIt's the defacto router because it has gone through many iterations, has lots of users, is easy to bring into a React app, and doesn't fight w/ React, it works with it. Plus there are a variety of approaches to declaring routes. [0] I'm not sure I see how obvious it is that a <Redirect/> is wrong. But that's fine, I take your point that it's not particularly intuitive. I'm of the opinion that routing, in general, is a function of application state - and I like to manage my application state with Redux - so I will often also mix in connected-react-router [1]. This lets you do navigation w/ an imperative API [2]. [0] https://github.com/ReactTraining/react-router/tree/master/packages/react-router-config https://github.com/ReactTraining/react-router/tree/master/pa... [1] https://github.com/supasate/connected-react-router https://github.com/supasate/connected-react-router [2] https://github.com/supasate/connected-react-router/blob/master/index.d.ts#L80 https://github.com/supasate/connected-react-router/blob/mast...
- Silhouette 7y agoThe alternative would be to write a side effect inside your `render()` function, which is illegal That's one alternative. Another is not to try implementing behaviour that has nothing to do with rendering using a rendering library in the first place. A horrible amount of accidental complexity has been created in the React ecosystem when people have tried to use it like a full framework. If all you have is a hammer, maybe it's time to consider using other tools as well.
- allover 7y agoIt's not really bananas when you actually start to think of everything 'as components', and consider that 'render' can neatly, declaratively describe behaviour, not just the DOM.
- thatswrong0 7y agoThat _is_ how I think about components, but it's still bananas. Render does not neatly describe this behavior because it necessitates setting unnecessary state. That's gross. Usually when I need to trigger a redirect, I'm in some business-level function. So to redirect this way, I would need to set some state in my store, re-render, then hit the conditional, which would redirect, probably unset that state, and then probably trigger some additional business logic. When really all I want to do is in the business function, directly trigger the redirect and maybe some other logic without any indirection. redux-react-router exists, but it's API is still obtuse compared to something like redux-first-router.
- jaquers 7y ago> When really all I want to do is in the business function, directly trigger the redirect and maybe some other logic without any indirection. Heh, didn't see this comment before I left a novel up the tree. Seems we have similar ideas about state management. I think the default RR API is straight React, and that is by design.
- Tenemo 7y agoSince you are talking about using Redux, you most definitely can dispatch push actions with libs like connected-react-router or just use the history API directly (for things like replacing state instead of pushing). Most of the redirects I write are inside business logic and I don't like mixing <Redirect /> in there, too.
- allover 7y agoThen you can use `useHistory` (or `withRouter` if you're not hooks-ready). I somewhat agree that the <Redirect /> component is not actually that useful, it's only useful in very simple cases like "based on one route plus conditional redirect to another", e.g.: <Route path="/"> {usersPreviousShoppingGender === "women" ? ( <Redirect to="/women" /> ) : ( ... )} ... </Route> <Route path="/women">
- jtms 7y agoIt’s bananas to you that a library with the word react in the name would be dependent on react? It’s an add on you would literally never use unless you are building a react app