JS-JMS-WEB
JS-JMS-WEB

Reputation: 2625

react router pass data

I have a simple react router:

<Route handler={AppRoot}>
    <Route name="about" handler={About} />
    <Route path="/projects" handler={Projects} />
    <Route path="/projects/experiments" handler={Projects} />
</Route> 

I'm using same handler for two different routes. In components I want to do different things accordingly. How can I achieve this? Should I do window.location to check current url. That doesn't sound right. Please note, I don't want to use params here.

My second question is about passing data:

<Route path="/projects" handler={Projects} projects={projects} />

Above example doesn't work, how can I pass collection from router to component?

Upvotes: 0

Views: 790

Answers (1)

const314
const314

Reputation: 1630

You can access router in your Projects component by using context and then get router's current path:

var Projects = React.createClass({
    contextTypes : {
        router : React.PropTypes.func
    },
    render : function () {
        var routerPath = this.context.router.getCurrentPath();
        ...
    }
});

As for passing the collection, it is not quite clear what scenario you are trying to implement, give us some details. Anyway, I believe that there is a better way than passing collection to the router.

Upvotes: 1

Related Questions