Know about my route, and my active RouteHandler
In v0.10.x handlers were able to ask about "my route" and "my child route".
// 0.10
<Route handler={App}>
<Route name="foo" handler={SharedHandler}/>
<Route name="bar" handler={SharedHandler}/>
</Route>
var App = React.createClass({
render () {
var childName = this.props.activeRouteHandler().props.name;
// ...
}
});
var SharedHandler = React.createClass({
render () {
return <div>{this.props.name}</div>;
}
});
In v0.11.x you can solve this with mixins or closing over information you need.
// close over information
<Route handler={App}>
<Route name="foo" handler={makeSharedHandler("foo")}/>
<Route name="bar" handler={makeSharedHandler("bar")}/>
</Route>
function makeSharedHandler(name) {
return React.createClass({
render () {
return <div>{name}</div>;
}
});
}
var App = React.createClass({
mixins: [ Router.State ],
render () {
// I have to know the depth is `1`
var childName = this.getRoutes()[1].name;
// ...
}
});
// shared mixin
<Route handler={App}>
<Route name="foo" handler={Foo}/>
<Route name="bar" handler={Bar}/>
</Route>
var SharedMixin = {
render () {
return <div>{this.name}</div>;
}
};
var Foo = React.createClass({
mixins: [ SharedMixin ],
name: 'foo'
});
var Bar = React.createClass({
mixins: [ SharedMixin ],
name: 'bar'
});
// if `App` were shared, I could have a `this.depth` on the classes and then
// the render method would find the right child route
It was also possible to find out if you even had an active child route, but now I don't think there is:
var child = this.props.activeRouteHandler();
var hasChild = !!child;
// can we do anything like this now?
It is possible for us to add two properties to route handlers: route and childRoute, which would be much more convenient for these kinds of tasks.
I like that we don't add any magical properties to handlers anymore, but these two properties seem really appealing.
Thoughts?
I miss being able to detect the currently handled route's name too, as I just upgraded from 0.10.x.
Are you referring only to the default react-router props (name, handler, path) or to user-defined props as well?
I think it would be great to have a way to access those props from within the corresponding handler. Should make them easier to reuse and saves you from passing those props down the hierarchy. I'm not so sure about the child route though, since it would make your route config more fragile (depending on the usage of course). If, for example, I had a component using this.childRoute to get a header title from its child routes that display some content, and I wanted to add another route level inbetween (e.g. to split into categories), I couldn't really use childRoute anymore.
Perhaps this a different topic, but I'd also like to see support for accessing custom route props via Router.run callback (state argument). I think this could solve problems like passing route props to handlers further up the hierarchy more elegantly.
Since this is my first comment since 0.11: Awesome job, guys. I love the route you took :+1:
I need to call transitionTo from a mixin to change just the query. Here's how I did it:
var query = assign({}, this.props.query, {
p: formatPages(pages)
});
this.replaceWith(this.props.name, this.props.params, query);
How do I do this now if I don't know route depth?
It sounds like it would be useful to have something like a Router.Meta mixin with:
getRouteDepthgetRoutegetChildRoute(possibly)
I'm a little iffy on getChildRoute because I don't like my components to know anything about their children. It's one thing to know about your context, but knowing about your children could lead to doing silly things. I like that <RouteHandler> is opaque. If there is a child, it renders something. Otherwise, it doesn't. It's kind of like this.props.children in that regard.
Answering my own question, it's still pretty trivial:
var routeName = last(this.getRoutes()).name,
params = this.getParams(),
query = this.getQuery();
query = assign({}, query, {
p: formatPages(pages)
});
this.replaceWith(routeName, params, query);
I'm a little iffy on getChildRoute because I don't like my components to know anything about their children.
Don't owners know everything about their children?
var div = <div className="stuff"/>
div.props.className
// I know everything
The difference is that we are going to render a thing inside of <RouteHandler/> and I'd like to know something about it. I can get all the matched routes and know my depth, etc. etc. to find it, so we already provide access to this information, its just not obvious how to get to it.
Also, you want to know stuff like this in order to render the <RouteHandler/> in different places, think of the iTunes interface, when you click an album, it expands beneath the row of albums. The only way to do something like that you have to know something about what you're going to render.
If you really want to know something about your route handler, couldn't you already just use refs?
<RouteHandler ref="theHandler"/>
We currently use our own ref to get a handle on the component that <RouteHandler> renders because we need it in willTransitionFrom. But users should be able to specify their own ref if they want to do something with it as well and everything should still work.
As far as components knowing about their children, I don't think it's generally a good idea to do something different in a parent element based on the kind of children it has. React gives you refs for the times when you need to circumvent this rule, but it's not a strange rule to have. e.g. think about CSS. There is no selector for "style this <p> differently if it has any <a>s inside it".
Hmm, thinking about this a little more I think you might have to use two levels of refs if you were going that route, e.g.
this.refs.theHandler.refs.theHandler
because refs use owners, not parents.
+1 on some API for this, I just created this Gist to ask a question about how I should get a hold of the component <RouterHandler/> will render - I just copied some stuff out of RouterHandleritself for now:
https://gist.github.com/insin/f8f04d46a82c2832a3e0
Is there any reason not to put the <div> into the ***Thing components? And in case your div got more complex, put that into a separate <Layout [fluid]> component?
tl;dr: I like that all of my child components are a pure function of props and I'd rather not introduce intermediate view-controllers into my tree and keep the number of stateful components minimal. Therefore, I need a way of determining which props should be passed through to <RouteHandler/> unless I want to just pass potentially irrelevant props to children.
@mjackson,
As far as components knowing about their children, I don't think it's generally a good idea to do something different in a parent element based on the kind of children it has.
I agree with this sentiment if we're talking about child placement. However, I have a question about <RouterHandler/> props. Let's say my routes are declared as follows:
<Route handler={App}>
<DefaultRoute handler={Dashboard}/>
<Route name="productivity" handler={Productivity}/>
<Route name="contacts" handler={Contacts}/>
<Route name="todos" handler={Todos}/>
<Route name="inbox" handler={Inbox}>
<DefaultRoute handler={InboxList}/>
<Route name="inbox-message" handler={InboxMessage}/>
</Route>
<Route/>
</Route>
I'm implementing Flux and all my state is contained within the root component, i.e., Application is my only view-controller. The routes I defined may want a drastically different set of props, e.g., "contacts" may need this.state.contacts passed down from Application while "inbox" needs both this.state.contacts and this.state.messages. My initial thought was to build a helper method on any component that renders a <RouteHandler/>, this helper can determine which props should be passed through as a function of the current active RouterHandler. In order to do this I would need to know the name of the Route that corresponds with the <RouteHandler/> at each level.
I'm wondering if, given this architecture, react-router solves a real problem for me. Currently, I have a Backbone router invoking NavigationActions in the route callbacks. These NavigationActions drop the current nav state into a NavigationStore, the store emits a change, and my UI re-renders, branching on the current nav "name", query params, etc. I think that if I keep this architecture but try to implement react-router, I'd still have to do this branching logic, not to determine which component to render (since react-router does this bit for me) but to determine which props to forward.
In this example, it looks like Contact is stateful as a side effect of using react-router. In any other example React app, the contact data used to render the widget would be passed via props and thus enforcing best practices.
Sorry for the wall of text. I think there is a bigger question at the root of all this text, does react-router lend itself well to React applications where all mutable state is kept at the root?
/cc @rpflorence
@gusvargas
Why not just forward all props if there's no effort in computing them?
Yeah, that's definitely something I considered. I don't have any good reason not to do that, it would definitely be easier than branching on the Route name. There was just something that irked me about passing the kitchen sink to all components.
its undocumented, but you can use the RouteHandler mixin's getRouteDepth along with State.activeRoutes to see if you've got an active child route. I'm going to close until there's more noise around this.
getRouteDepth is depracated in the latest version... any replacements ? @ryanflorence