Is it possible to get the route name (the ones set when setting up the router)?
Hey there.
I have a little question. Is it possible to get the route name of the current route?
<Route name="user" path="user/:id" handler={UserLayout} />
This is an example route. I have the path there, but it's the name property I'm interested in. There are certain times I want to check that I'm on the route of any user page, so the name property of user is more important than the actual path, parameters, or query strings.
I've looked at the Router.State mixin and it seems to add these:
this.getParams()
this.getPath()
this.getPathname()
this.getQuery()
this.getRoutes()
I was hoping that this.getPathname() would give me the value of the name property but that seems to give me the same value as this.getPath() both give me back user/123, for example.
Any way to just get the user value from the name I set in the routes?
Also, another question... Why is there also a context added to my component? It seems to contain the same stuff as I detailed above.
Thank you! Great work! James
pathname is simply path without query.
You can get the names of the matched routes from state.routes or this.getRoutes():
router.run(function(Handler, state){
state.routes[0].name; // name of topmost matched route
});
this.getRoutes()[0].name; // name of topmost matched route
Seems cumbersome, how would I know which level in the array it was at any one time?
Is it completely bizarre to have something like getRouteName()?
What do you want to with the route name exactly? Are you using the same handler for different routes?
There are certain times I want to check that I'm on the route of any user page
The best way for that would be this.isActive("user") I think.
There has been a couple of times that I want my stores (I'm using Reflux by the way) to know the current route. I don't want to send back the whole path /user/123, I just want to know if the user is on a user page, regardless of it's IDs.
I still don't quite understand. Sounds like this.isActive("user") is what you're looking for, isn't it? The equivalent within run callback would be state.routes.some((route) => route.name === 'user') .
There has been a couple of times that I want my stores (I'm using Reflux by the way) to know the current route.
It's always a set of routes that's current, not just a single route. If you're referring to the innermost route (leaf node) you can get that with state.routes[state.routes.length - 1].name.
Easiest thing to do is put a name on your handlers instead (and don't reuse them across routes).
Otherwise, mixin RouteHandler (its new, no documentation yet) and then call this.getRouteDepth() to know how deep you are, then use State's this.getRoutes[this.getRouteDepth()].name.
I'd like to revisit this, my use case is slightly different than that of @moreofmorris
I think it makes sense to add a getRouteName method to the State mixin to work along with the getParams and getQuery mehtods.
I think the logic should be that if I do transitionTo('foo'), or click on <Link to="foo" /> then getRouteName should return foo.
Right now I can do
var routes = this.getRoutes();
var routeName = routes[ routes.length - 1 ].name;
bud I'd prefer if there was a mixin method for this and I am not actually sure if my code will work in all cases.
Is it unreasonable to add this method?
I also vote for adding this method with a different use case: analytics. I want to track pageviews, and it's much simpler to use the route name compared to using the path, where I have to strip away any path parameters.