What if your app could prepare itself perfectly every time a new screen shows up, without you lifting a finger?
Why Mounting phase in React? - Purpose & Use Cases
Imagine you want to show a welcome message only when a user first opens a page, and you have to manually add and remove HTML elements every time the page loads.
Manually adding and removing elements is slow, easy to forget, and can cause bugs like showing messages multiple times or not at all.
The mounting phase in React automatically runs code when a component appears on the screen, so you can set up things like messages or data loading without extra hassle.
document.body.innerHTML += '<p>Welcome!</p>' // run on page loaduseEffect(() => { console.log('Component mounted'); }, [])You can run setup code exactly once when a component appears, making your app more reliable and easier to manage.
Showing a welcome popup only once when a user visits a site, without repeating it on every action.
The mounting phase runs code when a component first appears.
It helps automate setup tasks like fetching data or showing messages.
This avoids manual, error-prone DOM changes.
Practice
Solution
Step 1: Understand React component lifecycle phases
The mounting phase is the first phase when a component is created and inserted into the DOM.Step 2: Match the phase to the description
Mounting means the component appears on screen for the first time, not updates or removal.Final Answer:
When a component first appears on the screen -> Option BQuick Check:
Mounting phase = first appearance [OK]
- Confusing mounting with updating
- Thinking mounting means removal
- Mixing mounting with re-rendering
Solution
Step 1: Recall useEffect dependency array usage
Passing an empty array [] runs the effect only once on mount.Step 2: Compare options
useEffect(() => { /* code */ }, []) uses [] so runs once; others run on every render or when dependencies change.Final Answer:
useEffect(() => { /* code */ }, []) -> Option AQuick Check:
Empty array means run once on mount [OK]
- Omitting dependency array runs effect every render
- Using state or props array runs effect multiple times
- Confusing useEffect with useState
function Timer() {
React.useEffect(() => {
console.log('Timer started');
}, []);
return <div>Timer</div>;
}Solution
Step 1: Analyze useEffect with empty dependency array
The effect runs once on mount, logging 'Timer started' once.Step 2: Confirm no repeated logs or errors
No dependencies means no repeated logs; no error occurs.Final Answer:
Timer started -> Option CQuick Check:
useEffect with [] logs once on mount [OK]
- Thinking it logs every render
- Expecting no logs without return
- Assuming error due to missing dependencies
function DataFetcher() {
React.useEffect(() => {
fetch('/api/data')
.then(response => response.json())
.then(data => console.log(data));
});
return <div>Loading...</div>;
}Solution
Step 1: Check useEffect dependencies
Without a dependency array, useEffect runs after every render, causing repeated fetch calls.Step 2: Identify correct fix
Adding an empty array [] runs fetch only once on mount, preventing repeated calls.Final Answer:
Missing dependency array causes fetch on every render -> Option AQuick Check:
No [] means effect runs every render [OK]
- Forgetting dependency array in useEffect
- Thinking fetch outside useEffect is needed
- Assuming useEffect cannot contain async calls
function UserProfile() {
const [user, setUser] = React.useState(null);
// Which useEffect is correct?
A) React.useEffect(() => {
fetch('/user')
.then(res => res.json())
.then(data => setUser(data));
}, []);
B) React.useEffect(() => {
fetch('/user')
.then(res => res.json())
.then(data => setUser(data));
});
C) React.useEffect(() => {
fetch('/user')
.then(res => res.json())
.then(data => setUser(data));
}, [user]);
D) React.useEffect(() => {
fetch('/user')
.then(res => res.json())
.then(data => setUser(data));
}, [setUser]);
return user ? <div>{user.name}</div> : <div>Loading...</div>;
}Solution
Step 1: Understand useEffect dependencies for fetching
Empty array [] runs effect once on mount; no dependencies cause repeated fetches.Step 2: Analyze each option
The useEffect with an empty dependency array fetches data only once on mount. Without any dependency array, the effect runs after every render. Depending onusercauses an infinite loop since updatingusertriggers re-renders. Depending onsetUserruns only once becausesetUseris stable, but it is unnecessary and may cause ESLint warnings.Final Answer:
useEffect with empty array to fetch once on mount -> Option DQuick Check:
Empty [] means fetch once on mount [OK]
- Leaving out dependency array causes infinite fetch
- Using state as dependency causing loops
- Misunderstanding stable functions as dependencies
Ready to prove it?
Pass the test and this pattern moves unknown
