-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Get current route names #119
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
Can you explain a use-case or two that you're after? |
I got a handler that uses different route names depending on where it is, i.e. if it is mounted under one route it provides one routename to a link and if it is mounted under another route it provides a different routename to the link. The main part of the route is nested inside this handler. Is that clear enough? Right now I run a regexp against the current hash to detect which route I'm in. |
While I'm at it, another method that would be usefull is an isActive(name, props, query) method to check if a given route is active. Usage example:
It is not always the right thing to have this on the Link element. |
Use the Router.ActiveState mixin. |
:close:? |
Yeah, I think when there's a use case that doesn't boil down to "am I active?" We can reopen. |
Please reopen. The usecase in my first comment still stands. It is not "am I active?" but "in what context am I in?" which is different. |
@tarjei I'm not totally understanding, can you post some code? From what I can gather (and its probably not correct) you could share a mixin but use two different handlers with different props but the same render. var SharedMixin = {
render: function() {
var link = <Link to={this.props.linkTo}/>
// ...
}
}
var A = React.createClass({
mixins: [SharedMixin],
getDefaultProps: {
return { linkTo: 'something' }
}
});
var B = React.createClass({
mixins: [SharedMixin],
getDefaultProps: {
return { linkTo: 'something-else' }
}
}); <Routes>
<Route handler={A}/>
<Route handler={B}/>
</Routes> Am I way off? Is this terrible? |
Hmm ... actually <Routes>
<Route name="foo" otherProp="foo" handler={SharedHandler}/>
<Route name="bar" otherProp="bar" handler={SharedHandler}/>
</Routes> Then you can branch on This gets me thinking that we ought to just pass the I also think |
Hi, Ryan, thanks for reopening. Your last comment captures the usecase well. Here's an example that shows my problem with a bit more detail: var SharedHandler = React.createClass({
render: function() {
var myRouteName = ? ;
return <div><Link to={myRouteName} someProp="foo">Next foo!</a></div>
}
}); <Routes>
<Route name="someFOO" handler={SomeFooHandler}>
<Route name="someFOO-bar" otherProp="foo" handler={SharedHandler}/>
</Route>
<Route name="otherFoo" handler="OtherFooHandler">
<Route name="otherFoo-bar" otherProp="foo" handler={SharedHandler}/>
</Route>
</Routes> So I might need not to just know the current route name, but the route name of the parent route as well, but just knowing the current route is a great boon - I can always work from there. |
Interesting. For now you can just put whatever names in the route props and access in the handler. Can you explain what you're trying to do in the application, rather than explaining the kind of solution you're imagining? Maybe there's a different solution. |
@tarjei would you consider a bread crumb to be a similar use case? |
@karlmikko, yes it might be. You might want to keep the breadcrumb outside of the main application for example. Something like: // in main handler @rpflorence : The main handlers are different ways that I list videos. The Shared handler is a modal that I use to show the videos. |
@tarjei the reason I asked was to wonder if you could take the same approach to know what route you were on. from what i can see in your example you are tying to link to a relative route path akin to /whatever/5.html |
Another option could be something like this <Routes>
<Route name="someFOO" handler={SomeFooHandler}>
<Route name="someFOO-bar" otherProp="foo" handler={SharedHandler} parentHandler={SomeFooHandler}/>
</Route>
<Route name="otherFoo" handler={OtherFooHandler}>
<Route name="otherFoo-bar" otherProp="foo" handler={SharedHandler} parentHandler={OtherFooHandler}/>
</Route>
</Routes> then access However this won't let you see the route you are on. |
@tarjei I think dynamic routes for the different ways to list your videos might work also <Routes>
<Route name="video" path="/:method" handler={SomeHandler}>
<Route name="modal" path="/:method/view" handler={SharedHandler} />
</Route>
</Routes> Where SomeHandler changes if SomeFooHandler is rendered or OtherFooHandler. Then you will always have the current route. |
closed via #157 |
Hi, is there a way to get the current route name(s) from the router?
Something like
Is that possible? Would that be something to add to the ActiveStore?
The text was updated successfully, but these errors were encountered: