According to this page http://busypeoples.github.io/post/react-component-lifecycle/ The render method for a component is called right in between the componentWillMount
and componentDidMount
methods amongst other places.
But the react.js documentation for component lifecycles here https://facebook.github.io/react/docs/component-specs.html says that the componentDidMount
methods of all child activities are called before the parent. I can understand that componentDidMount
is ok to call after any child components are rendered but how does the runtime know which children to call the componentWillMount
function on before they are rendered? Or am I right in assuming that componentWillMount
is called for the parent activity first and then for the children (unlike componentDidMount
)?
Thanks!
OK. so here goes. If you have a simple structure with a parent and 2 children like this:
Then the sequence of events firing will be:
<Parent> componentWillMount()
<Parent> render()
, which starts to render children<Child> componentWillMount()
of the first child<Child> render()
of the first child<Child> componentWillMount()
of the second child<Child> render()
of the second child<Child> componentDidMount()
of the first child (these will start only after the last render in the tree has run)<Child> componentDidMount()
of the second child<Parent> componentDidMount()
(this one will run only after its last child has runcomponentDidMount
)You can find a codepen example here.
You could put a console.log() in each of the methods to see their order printed in the console.