Where to make an initial AJAX request from ReactJS

+7
javascript reactjs frontend
source share
3 answers

Since the reaction is intended to be used as a representation, your ajax requests should be placed in your model.

Otherwise, if for some reason you need to do this in the view, the difference between componentDidMount and componentWillMount is that the first one is called once , the element is re-displayed and you have access to it through this.getDOMNode() , and the second - once before the start of render() .

+4
source share

When using server rendering, componentWillMount is called, but componentDidMount not. Because of this, I try to do any initialization that requires a browser (including Ajax and DOM manipulations) in componentDidMount .

+6
source share

In componentDidMount() , you have access to the DOM if you need it; in componentWillMount() you still do not have access (although you may not need it if all you want to do is call setState ).

Make sure that in your render() method you can gracefully display an β€œempty” state (that is, before returning the Ajax results). getInitialState() can be useful here for setting the state of the base database.

+4
source share

All Articles