I have some questions about react usage and patterns.
Should I use
ponentDidMount
or
getInitialState
in loading data asynchronously? What is the difference between the two?
I am using Backbone for my frontend data structures
this.props.data = new BrandModel({ _id: this.props.params.brandId });
var that = this;
this.props.data.fetch({
success: function () {
that.setState({ brand: that.props.brand });
}
});
return null;
Update: thanks for the responses
This Question is suggesting to not us ponentWillMount, but I understand that its more expressive to use ponentDidMount in this case as getInitialState seems to be meant to be used synchronously
Update 2:
I've had to revert to using getInitialState as ponentDidMount fires after render and I need this.props.data to be pointing to an object
I have some questions about react usage and patterns.
Should I use
ponentDidMount
or
getInitialState
in loading data asynchronously? What is the difference between the two?
I am using Backbone for my frontend data structures
this.props.data = new BrandModel({ _id: this.props.params.brandId });
var that = this;
this.props.data.fetch({
success: function () {
that.setState({ brand: that.props.brand });
}
});
return null;
Update: thanks for the responses
This Question is suggesting to not us ponentWillMount, but I understand that its more expressive to use ponentDidMount in this case as getInitialState seems to be meant to be used synchronously
Update 2:
I've had to revert to using getInitialState as ponentDidMount fires after render and I need this.props.data to be pointing to an object
Share Improve this question edited May 23, 2017 at 11:46 CommunityBot 11 silver badge asked Jun 30, 2015 at 19:12 Dan BakerDan Baker 1,8273 gold badges22 silver badges36 bronze badges3 Answers
Reset to default 9ponentDidMount
would be called after ponent was mounted into browser DOM (it would be called after first render and it would not be called if you are rendering server-side(to string)
getInitialState
would be called when ponent is created, if you are using es6 class syntax you can place that logic in you ponent constructor directly assigning to this.state
There is also ponentWillMount
it would be called before first render for both server and client - it is more suitable in your case.
Componentndid mount is fired after the render and we load the Ajax inside that.While during the actual render we check if the object has data, else send empty div
ponentDidMount: function () {
console.log("========> FIring");
$.get("http://api.", function(response) {
if (this.isMounted()) {
this.setState({
Data: response
});
}
}.bind(this));
},
render: function() {
var data = this.state.Data;
if (this.state.promoData) {
return (<div>{data}</div>
);
} else {
return (<div className="divLoading">Loading...</div>);
}
}
I'm not sure about this Backbone usage but if you load data in ponentDidMount
that's fine as essentially that code will start executing after the ponent is initially rendered -- after the data is fetched and the state is set again, the ponent will re-render showing that correct data at that point. This kind of like lazy loading to me.
I'm not sure if getInitialState
is blocking, but if it is, then the ponent will not render until the state is loaded. But I think it isn't, so the data would probably not be fetched by the time the ponent is rendered.
ponentWillMount
may be what you want, but review the React lifecycle for what you think makes the most sense.