Alan H. Alan H. - 5 months ago 87
React JSX Question

Why do the React docs recommend doing AJAX in componentDidMount, not componentWillMount?

Title says it all. I understand why componentDidMount is appropriate for anything that requires DOM access, but an AJAX request doesn’t necessarily or usually need this.

What gives?


componentDidMount is for side effects. Adding event listeners, AJAX, mutating the DOM, etc.

componentWillMount is rarely useful; especially if you care about server side rendering (adding event listeners causes errors and leaks, and lots of other stuff that can go wrong).

There is talk about removing componentWillMount from class components since it serves the same purpose as the constructor. It will remain on createClass components.