-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
accessing statics
of activeRoute
#107
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
Shouldn't your deepest route handler control the title, and therefore have access to its own statics? You can also pass down your title to the active handler |
That said, maybe there are some interesting things people could do if we exposed the current active routes var title = Router.getActiveRouteHandlers().map(function(handler) {
return handler.constructor.title;
}).join(' : '); |
It could, but then you open the possibility for out-of-sync titles. You want to the title to be the default if the current route doesn't define a title, otherwise set the custom title. If the deepest route sets a custom title, and you navigate away from it, the title should be reset to the default title. That means every single route needs to do the title setting, so it's currently in sync. I could use a mixin, I suppose. But your right in your second comment; it feels like something that would be neat in the top-level component, allowing you to do more complex things like that. |
Actually, I figured it out. I just noticed that you automatically create a ref
It's up to the component's |
@jlongster Maybe it would be useful for us to include a |
I prefer |
I could see a |
That'd be fine with me! Any reason to not just name the ref Also, shouldn't the name be |
Reopening as it sounds like there might be an action item here? |
yeah, we'd want the handler, not the route descriptor. |
Sure, I guess we could just have |
activeRouteHandlerInstance |
activeRouteHandlerTheFactThatWereExposingThisMakesMeFeelLikeTheresSomeBiggerProblemWithTheAPI |
Perfect. I still think every component just tells something else to deal with the title |
ooh, I just realized you can do this:
Child routes can now send a title up to the parent, allowing you to send it up and down as far as you like. Its just data from a child like anything else, right? |
I've run into the same issue with trying to set the page title from the activeNestedRouteComponent. This is the only case that I would want to do this that I can think of. Is there any chance we can add a title to the Route config. . That way I don't have to add ANY code to my application and the title is updated for me.
|
Isn't title dependent on data in the handler? <Route path="/topics/:id" handler={DiscussionTopic} title="hmm ..."/> You'd probably want to set the document title to the discussion topic title when your discussion topic data lands. Additionally, we really want to make |
Agree about #65. We probably wouldn't be having this conversation if we had a |
yeah, after we settle #112, |
The title is dependent on data, yes. Agreed that extensibility > lots of built-in declarations.
That would work, but again introduces the problem of a "default" title. All routes would have to call it, even if they didn't want to set a title, to make sure it gets reset to the default. Also, I'm not sure when you would call it? Changing the title isn't really an event, it's just something that needs to stay in sync with the current route. |
I agree that there should probably be some way to set document.title declaratively. |
I think we just need more extensible primitives and let somebody make a mixin that does it. |
Regarding Titles Titles are a lot like rendering, I don't think we can do anything generic that works for everybody, seems to me you make a Regarding Access to Since you can now pass down props and you can pass your static data back up through a function, I think we can close this ticket. @jlongster please reopen if you're like "bummer" |
There's a big problem with that, as I described in #107 (comment) Can't we just make a method to get the handle ref or just simply name is something better than |
Can't you mix something like this into route handlers that you want to set the title? var TitleSetter = {
propTypes: {
documentTitle: React.PropTypes.string
},
getDefaultProps: function () {
return {
documentTitle: 'THE DEFAULT TITLE'
};
},
componentDidMount: function () {
document.title = this.props.documentTitle;
}
}; It's not perfect, but you get the idea. |
@mjackson I suppose so, that's probably the best alternative solution. It feels better to handle it in the top-level app component, and leverage our nested routing behavior though. If you don't want to add a better way to get the active handler, that's fine, but it seems like there would be other uses for it too. |
var TitleSetter = {
propTypes: {
documentTitle: React.PropTypes.string
},
getDefaultProps: function () {
return {
documentTitle: 'THE DEFAULT TITLE'
};
},
componentDidMount: function () {
// via <this.props.activeRouteHandler sendTitle={this.handleTitle} /> all
// the way down the hierarchy so you can send it all the way back up and
// handle in your top-level app component
this.props.sendTitle(this.props.documentTitle);
// though I'd do:
titleStore.push(this.props.documentTitle);
// and titleStore.pop(this.props.documentTitle) in `willUnmount`
// and let the title store deal with it, you get the hierarchy by
// the position in the array
}
}; |
@jlongster I'm just hesitant to couple together parents with child routes so strongly. React itself has a secret (public) |
You now know the route's name, which helps a bit. I still think you make a title module and route handlers tell it they have mounted and it decides what to do about the title. <3 and ;_; @jlongster |
Using something like https://github.com/karlmikko/react-router/blob/master/modules/stores/TitleStore.js If the title is set on every render all the way down, the last rendered item will what is set on the nextTick. The only issue with this approach I can think of is if the render forks and a shallow path re-renders and changes the title. |
Current activeRoute is a wrapper function that creats the child component:
This makes it so that I can't access the actual component directly, so I can't access the any static properties defined with
statics
. Right now my use case is to specify a title of the page, and the outer app handler will get it and set the title.I really enjoy how much this router does for me, but I also feel like it's pretty aggressive at hiding components from me. I guess that forces you to work through the router though. This is the only use-case so far where I feel like it's unnecessary. Can we expose the raw component type with another property, like
activeComponent
or something?The text was updated successfully, but these errors were encountered: