Marco Ferragina Marco Ferragina - 1 year ago 81
React JSX Question

How to get route name in handler using react-route?

As on subject how can I get the route name inside the handler?
For example:

var routes = <Route handler={App} path="/">
<Route name="home" path="/home" handler={HomePage} />
<DefaultRoute handler={HomePage} />
</Route>, function(Handler, state) {
var params = state.params;
React.render(<Handler params={params}/>, document.body);

Now suppose I have a component like this:

class HomePage extends React.Component {
render() {

how can I get the current route name? To be more specific I want to get the


attribute from

<Route name="home" path="/home" handler={HomePage} />

Answer Source

Before react-router 0.13 you can use this.getRoutes() using Router.State mixin.

For react-router 0.13 you can use this too:

var currentRoutes = this.context.router.getCurrentRoutes();
var lastRoute = currentRoutes[currentRoutes.length - 1];

For react-router v2.0.x you can use:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download