-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Specifying default sub-routes and redirecting when there is no activeRouteHandler match #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
<Router.Routes>
<Router.Route handler={App}>
<Router.Route name="user" handler={User}/>
<Router.Route name="user-details" path="user/:userId/details" handler={UserDetails} />
<Router.Route name="user-activity" path="user/:userId/activity" handler={UserActivity}/>
<!-- add a route to handle anything that doesn't match details or actiivty -->
<Router.Route path="user/*" handler={UserRedirect}/>
</Router.Route>
</Router.Route>
</Router.Routes> var UserRedirect = React.createClass({
statics: {
willTransitionTo: function(transition, params) {
transition.redirect('user-details', params);
}
}
}); (hopefully @mjackson's proposals in #142 don't ruin this solution) |
I wonder if |
Thanks @rpflorence for the solution, two questions though:
The second was what led me to this question in the first place. |
get rid of the name on the user route so it can't match if you don't want it to, and then probably do |
I've been doing: componentWillMount : function() {
if (! this.props.activeRouteHandler()) {
Router.replaceWith(...);
}
}, Didn't realize there was already a baked-in solution! Thanks @rpflorence. |
@rpflorence I still think that @JedWatson has some unanswered questions regarding how the handler will have knowledge of the |
@bobeagan is right - or alternatively, being able to specify a default |
@JedWatson @bobeagan alright, here are scenarios we're solving as I understand them. Scenario 1
You don't actually want to render anything. the route with Scenario 1
Our router config doesn't handle this yet, but lets add it: <Routes>
<Route handler={App}>
<!-- remove path from this handler, don't want it to ever match a url -->
<Route handler={User}/>
<Route name="user-details" path="user/:userId/details" handler={UserDetails} />
<Route name="user-activity" path="user/:userId/activity" handler={UserActivity}/>
<!-- match user/123 -->
<Route path="user/:userId" handler={UserRedirectToDetails}/>
<!-- match everything else -->
<Route path="user*" handler={UserRedirect}/>
</Route>
</Route>
</Routes>
|
I think this would work for <Route path="user/:userId" handler={UserRedirectToDetails}/>
<Route path="user/:userId/" handler={UserRedirectToDetails}/> |
Verbos-ilicious! :) |
yeah, we need For instance, if you jump into some code and see a route w/o the |
(still, I'd like a more elegant solution) |
We've gotten caught up on the optional If we remove the path from
how does it know what the
in the first place. Everything else, we can work around. |
if a route handler has no path, it only ever gets rendered because a child route path matched (or, the entire branch has no path and therefore the lowest child matches So, if somebody types in |
<Routes>
<!-- this will only ever get rendered if... -->
<Route handler={User}>
<!-- ... one of these match -->
<Route name="details" path="user/:userId/details" handler={Details}/>
<Route name="activity" path="user/:userId/activity" handler={Activity}/>
<!-- a default sub-route to render when the others don't match -->
<Route name="user-default" path="user/:userId" handler={UserDefault}/>
</Route>
<!-- this doesn't need to exist, because ... -->
<Route path="user" handler={NotFound}/>
<!-- ... this will catch it -->
<Route path="*" handler={NotFound}/>
</Routes> edit: added user-default and names. |
I'm not coming across clearly, and I apologise if I'm missing something in React that makes this unnecessary / an anti-pattern. Going with the whole "Nested UI" thing with Routes, my It works if the following setup is in place:
It seems I can:
Maybe the solution is actually around Hope this makes what I'm trying to solve clearer. |
<Route handler={User}>
<Route path="user/:userId/details" handler={Details}/>
<Route path="user/:userId/activity" handler={Activity}/>
</Route> Once upon a time the params were sent to So for now, you should use this route config: <Route name="user" path="user/:userId" handler={User}>
<Route name="user-details" path="user/:userId/details" handler={Details}/>
<Route name="user-activity" path="user/:userId/activity" handler={Activity}/>
</Route> And have your user handler do this: var User = React.createClass({
componentWillMount: function() {
if (!this.props.activeRouteHandler())
Router.replaceWith('user-details', this.props.params, this.props.query);
}
}); |
@JedWatson Please see my comments in #158. I'm curious to know what |
Hey guys, sorry for the delay coming back to this. @rpflorence that code for Will pick this up over in #158 |
For those coming in 2016, you'd use
|
Not sure if I missed something obvious about how to do this, but here's what I'm trying to achieve:
Say there's a
User
route that has two sub-routes:UserDetails
andUserActivity
.The
User
route draws common UI (name, navigation, etc) and needs to know theuserId
in order to do so.So I set them up like this:
App
draws the surrounding UI,User
draws the common UI for the current user (name, subnav), andUserDetails
andUserActivity
draw their specific UI.The
User
view doesn't have any 'inner' UI and requires either theUserDetails
orUserActivity
view to be rendered inside it, but it seems reasonable that a) someone may visit the url/user/1
, and b) the default redirect should be encapsulated somewhere.What I can't work out is how to best ensure that any requests to
/user/1
either render theUserDetails
handler by default, or automatically redirect to/user/1/details
.I've (sort of) been able to hack it by adding a
willTransitionTo
static on theUser
component, like this:...but that's messy, and breaks the encapsulation of the routes / paths, among other things.
It seems that there are a few additions to the API that could make it cleaner, including:
Route
that lets you specify a default sub-route handler (these could cascade, and wouldn't cause a redirect)willTransitionTo
that indicates this is the 'final' matched route; i.e. no child routes will also be renderedMaybe a nice thing to have available generally would be an array of the matched routes, starting with the upper parent; this would be useful in both
willTransitionTo
andwillTransitionFrom
.Alternatively, I may be going in completely the wrong direction here; If I could get the
:userId
param into theUser
route without specifying the path, I could make the path of theuser-details
route/user/:userId
, and that would be fine.The text was updated successfully, but these errors were encountered: