-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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
Clarify lifecycle (renderComponent vs. renderComponentToString) #968
Comments
I think we could probably get away with not calling |
@petehunt The only issue I see with that is that it breaks the expectation of |
One possible solution is to actually replace componentWillMount with the constructor. Since the descriptor change, the constructor and componentWillMount is called at the same time. It also clarifies why there is no componentDidUnmount. |
@sebmarkbage Sounds reasonable, my only spontaneous reaction is that I really like the obvious nature of And in-terms of |
The idea is to replace getInitialState with a property initializer, which doesn't currently exist in ES6 but we're proposing it for ES7 (similar to TypeScript). The idea is to follow the future idiomatic way to handle initialization. class Foo {
state = { counter: 0 };
constructor() {
this.state.counter; // === 0
}
} If a destruction primitive like .NET's The mental model is that you just follow the normal initialization process of a class. We just add the notion of mount/unmount. class Foo {
state = { counter: 0 };
constructor() {
this.state.counter; // === 0
}
didMount() {
}
willUpdate() {
}
didUpdate() {
}
willUnmount() {
}
} Since there's no distinction between did and will in this model, we might be able to just shorten it to mount/unmount but that probably creates other problems such as what to do with will/didUpdate. |
@sebmarkbage Ah that makes a lot of sense. PS. I'm preally impressed with the overall discussion that is going on now with the API, exciting stuff! |
This may deserve its own thread, but it's related to @sebmarkbage's comments so I figured I'd post here. The fact that var c = MyComponent({
onReady: function() {
var rendered = React.renderComponentToString(c);
// Do whatever with it
}
}).initialize(); This illustrates how you might create a custom initializer and invoke it manually, since the de facto initializer— It seems to me there's a pretty strong argument for this kind of initialization (decoupled from synchronous rendering) to be formalized as part of the life cycle. |
We do want to support async server rendering, but I'm going to close this issue out as the original docs it asked for are done now and I think we have other issues open to track async server rendering. |
As discussed on IRC, the lifecycle functions are misleading when using
renderComponentToString
. Namely, becausecomponentWillMount
is called, butcomponentDidMount
is not.At the least, this should be documented on the lifecycle docs page. There was also discussion about changing the method names. (Note: there's clearly a need for both hooks; the only issue is that name of the former conveys that the component will mount when, in fact, it won't.)
One possibility is to add another always-run phase before the mounting phase which is run when rendered to string or DOM (e.g. "prepare" or "initialize"). The lifecycle would then be easily explained as follows:
I'd be happy to contribute a PR for either docs, method names, or both once a decision is reached.
The text was updated successfully, but these errors were encountered: