reactjsreact-lifecycle-hooks

Where the component gets mounted into, when we hit componentDidMount in react? is it real DOM or VirtualDom?


When will you hit the componentDidMount hook in react? is it when the component is mounted to the virtual dom or actual dom?


Solution

  • The component is mounted to the Virtual DOM on render(), at the "Render phase".

    The componentDidMount lifecycle, which is part of the "Commit phase", is mounted to the actual DOM.

    Notice that you can have another render phase if you call setState in the componentDidMount.

    You may call setState() immediately in componentDidMount(). It will trigger an extra rendering, but it will happen before the browser updates the screen.