Skip to content

[changed] handlers receive route name #157

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

Merged
merged 1 commit into from
Aug 6, 2014
Merged

Conversation

ryanflorence
Copy link
Member

There are a lot of requests to introspect in handlers on the route name. We transfer other "static" props to the handler, why not name?

Some requests: #150, #119.

Use cases:

  • single handler for multiple routes can branch on name
  • introspection in general allows for building convention in an app
  • bread crumbs
  • managing document title

But really, best argument for passing it down to the handler is that we pass other props down and there is no harm in the handler knowing the name of the route its handling.

If we don't pass it in, then people can/will just do this instead:

<Route name="someName" gimmeabreak="someName"/>

@sophiebits
Copy link
Contributor

I kind of want to just pass route and have people do this.props.route.props.name but that's probably too wordy.

@ryanflorence
Copy link
Member Author

I think I agree, it should go to the transition hooks too.

@sorentwo
Copy link

sorentwo commented Aug 3, 2014

I ran into this within 30 minutes of setting up an extremely simple master/detail app. directly with the I want to know the current route to display the name in the header but there isn't any easy way to introspect it (this coincides with the identified use case of "managing document title").

Considering that the name has been manually defined on the route it makes sense that it would be passed through. Reaching through the route and its props to grab the name seems extremely brittle and easily broken with much refactoring.

@ryanflorence
Copy link
Member Author

@mjackson seems good?

@mjackson
Copy link
Member

mjackson commented Aug 6, 2014

Yeah, I'm 👍. We should probably talk a little more about what things you'd like to pass through to transition hooks as well, but in a separate issue.

mjackson added a commit that referenced this pull request Aug 6, 2014
[changed] handlers receive route name
@mjackson mjackson merged commit 6665391 into master Aug 6, 2014
@ryanflorence ryanflorence deleted the send-name-to-handlers branch August 6, 2014 05:44
@erupenkman
Copy link

for google searchers, this can now be accomplished like so:

module.exports = React.createClass({
        mixins:[Router.State],  
        render: function() {
        var routeName = this.getRoutes()[this.getRoutes().length-1].name;
        return (
                <div id={routeName}>
                        <Router.RouteHandler/>
                </div>
        );
});

@adjavaherian
Copy link

//insert sardonic comment here...
var routeName = this.getRoutes()[this.getRoutes().length-1].name;

This works, but don't you think it should just be router.name?

@henryboldi
Copy link

In React Router 1.0.0, inside of a route component you can do this without importing anything:

this.props.location.pathname

See https://github.com/rackt/react-router/blob/8cae5a12d149ede1bc6863e0211b33064d1396e4/UPGRADE_GUIDE.md#state-mixin

@vladmiller
Copy link

@9o This won't work well for urls with parameters, e.g. /page/:slug will just return current pathname, not a route name

@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants