DEV Community

Discussion on: componentWillMount() vs componentDidMount()

Collapse
 
merri profile image
Vesa Piittinen

From developer standpoint I'd add that componentWillMount is roughly the equivalent of just doing stuff in constructor. The difference between using these is very small. I guess componentWillMount only existed because of historical React.createClass reasons when you didn't yet have a constructor in React classes - and once constructor became available with ES6 it pretty much removed any need to having componentWillMount.

The way I think this is that constructor is the place where you put together the initial internal state of your component. This will be the same state for both server rendered and initial browser render. Once browser kicks in then you have all the other lifecycle methods to live with from componentDidMount all the way to componentWillUnmount - all of which are not available when rendering server stuff.