0
0
Reactframework~10 mins

Mounting phase in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mounting phase
Component Function Called
JSX Evaluated -> Virtual DOM Created
React DOM Mounts Virtual DOM to Real DOM
useEffect with empty deps runs
Component Ready on Screen
The mounting phase starts when React calls the component function, creates the virtual DOM from JSX, mounts it to the real DOM, and runs effects for setup.
Execution Sample
React
import React, { useState, useEffect } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  useEffect(() => {
    console.log('Mounted');
  }, []);
  return <button onClick={() => setCount(count + 1)}>{count}</button>;
}

export default Counter;
This React component shows a button with a count starting at 0 and logs 'Mounted' once when first shown.
Execution Table
StepTriggerState BeforeActionState AfterWhat Re-rendersDOM Change
1Initial rendercount: undefinedCall Counter(), useState(0) sets count=0count: 0Counter componentButton with text '0' created
2JSX evaluatedcount: 0Return <button> with count 0count: 0Counter componentVirtual DOM button with '0' ready
3React mountscount: 0Mount virtual DOM to real DOMcount: 0Counter componentButton with '0' appears on screen
4useEffect runscount: 0Run effect callback, console.log('Mounted')count: 0No re-renderNo DOM change
5User clicks buttoncount: 0setCount(1) triggers state updatecount: 1Counter componentButton text updates to '1'
6Re-render after clickcount: 1Re-evaluate JSX with count=1count: 1Counter componentButton text changes to '1'
7No more mountingcount: 1No further mounting phasecount: 1No re-renderNo DOM change
💡 Mounting phase ends after initial render and useEffect with empty dependencies runs.
Variable Tracker
VariableStartAfter Step 1After Step 5Final
countundefined011
Key Moments - 3 Insights
Why does useEffect run only once during mounting?
Because useEffect has an empty dependency array [], React runs it only after the first render (see step 4 in execution_table).
Is the component function called again during mounting?
Yes, the component function is called once initially to create the virtual DOM (step 1), but not again until state changes.
What triggers the DOM to update after mounting?
State changes like setCount trigger re-renders and DOM updates, but these happen after mounting (step 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after step 1?
A1
B0
Cundefined
Dnull
💡 Hint
Check the 'State After' column in row for step 1.
At which step does React mount the virtual DOM to the real DOM?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the row where 'React mounts virtual DOM to real DOM' in the 'Action' column.
If the useEffect dependency array was not empty, when would the effect run?
AEvery time the component re-renders
BOnly once during mounting
CNever
DOnly when the component unmounts
💡 Hint
Recall how useEffect runs with dependencies on state changes, not just mounting.
Concept Snapshot
Mounting phase in React:
- Component function runs first time
- JSX creates virtual DOM
- React mounts virtual DOM to real DOM
- useEffect with [] runs once after mount
- Component appears on screen ready for interaction
Full Transcript
The mounting phase in React starts when the component function is called for the first time. React runs useState to set initial state, then evaluates the JSX to create a virtual DOM representation. Next, React mounts this virtual DOM to the real DOM, making the component visible on the screen. After mounting, useEffect hooks with empty dependency arrays run once for setup tasks. This phase ends when the component is fully rendered and ready. Later state changes cause re-renders but are not part of mounting.