title: struggling to understand componentDidMount() method when it comes to nested components
body: <p>I'm trying to understand react lifecycle hooks and have faced a problem with componentDidMount() lifecycle hook.</p>
  
  <p>Suppose you are rendering a list of components. componentDidMount() method for each list item is always fired at the end after all list items have rendered. Why it did not fire at the beginning for each item?</p>
  
  <p>This is my render() method in my root App component</p>
  
  <pre><code>  render() {
      return (
        &lt;div className="App"&gt;
          &lt;A&gt;
            &lt;A1 /&gt;
            &lt;A2 /&gt;
          &lt;/A&gt;
          &lt;B&gt;
            &lt;B1 /&gt;
            &lt;B2 /&gt;
          &lt;/B&gt;
        &lt;/div&gt;
      );
    }
  }
  </code></pre>
  
  <p>structure of a single component A,A1,A2,B,B1,B2 all looks the same, only the name is different.</p>
  
  <pre><code>import React, { Component } from "react";
  
  class A extends Component {
    constructor(props) {
      super(props);
  
      this.state = {};
  
      console.log(" [A.js] constructor()");
    }
  
    componentWillMount() {
      console.log(" [A.js] ComponentWillMount()");
    }
  
    componentDidMount() {
      console.log(" [A.js] ComponentDidMount()");
    }
  
    render() {
      console.log(" [A.js] render()");
      return &lt;div&gt;{this.props.children}&lt;/div&gt;;
    }
  }
  
  export default A;
  
  </code></pre>
  
  <p>and I have written console logs inside each method in all components. And below is a screenshot of I got.
  <a href="https://snag.gy/wyi5Ta.jpg" rel="nofollow noreferrer">https://snag.gy/wyi5Ta.jpg</a></p>
  
  <p>in the screenshot we get</p>
  
  <pre><code>[App.js] constructor()
  [App.js] componentWillMount()
  [App.js] render()
   [A.js] constructor()
   [A.js] ComponentWillMount()
   [A.js] render()
    [A1.js] constructor()
    [A1.js] ComponentWillMount()
    [A1.js] render()
    [A2.js] constructor()
    [A2.js] ComponentWillMount()
    [A2.js] render()
   [B.js] constructor()
   [B.js] ComponentWillMount()
   [B.js] render()
    [B1.js] constructor()
    [B1.js] ComponentWillMount()
    [B1.js] render()
    [B2.js] constructor()
    [B2.js] ComponentWillMount()
    [B2.js] render()
    [A1.js] ComponentDidMount()
    [A2.js] ComponentDidMount()
   [A.js] ComponentDidMount()
    [B1.js] ComponentDidMount()
    [B2.js] ComponentDidMount()
   [B.js] ComponentDidMount()
  [App.js] componentDidMount()
  </code></pre>
  
  <p>i was expecting</p>
  
  <pre><code>[App.js] constructor()
  [App.js] componentWillMount()
  [App.js] render()
   [A.js] constructor()
   [A.js] ComponentWillMount()
   [A.js] render()
    [A1.js] constructor()
    [A1.js] ComponentWillMount() &lt;---
    [A1.js] render()
    [A1.js] ComponentDidMount()
    [A2.js] constructor()
    [A2.js] ComponentWillMount() &lt;---
    [A2.js] render()
    [A2.js] ComponentDidMount()
   [A.js] ComponentDidMount() &lt;---
   [B.js] constructor()
   [B.js] ComponentWillMount()
   [B.js] render()
    [B1.js] constructor()
    [B1.js] ComponentWillMount()
    [B1.js] render()
    [B1.js] ComponentDidMount() &lt;---
    [B2.js] constructor()
    [B2.js] ComponentWillMount()
    [B2.js] render()
    [B2.js] ComponentDidMount() &lt;---
   [B.js] ComponentDidMount()
  [App.js] componentDidMount()
  </code></pre>
  
  <p>I have put arrows where I expected the componentDidMount() method to fire. What's the reason for this? </p>
  
  <p>I couldn't find the exact answer to this question. I also found that react follows "Depth First Search" traversal method. I watched some videos, but couldn't understand this mystery. Can someone please explain? I have been struggling hours now to figure this out. Thanks.</p>