0
0
Reactframework~20 mins

Mounting phase in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mounting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when this React component mounts?
Consider this React functional component using hooks. What will be displayed on the screen after it mounts?
React
import React, { useState, useEffect } from 'react';

export default function Welcome() {
  const [message, setMessage] = useState('Loading...');

  useEffect(() => {
    setMessage('Hello, friend!');
  }, []);

  return <h1>{message}</h1>;
}
A<h1></h1>
B<h1>Loading...</h1>
CComponent throws an error
D<h1>Hello, friend!</h1>
Attempts:
2 left
💡 Hint
Think about when useEffect runs and how state updates affect rendering.
lifecycle
intermediate
1:30remaining
Which React hook runs only once during the mounting phase?
In React functional components, which hook runs exactly once right after the component mounts?
AuseEffect with an empty dependency array []
BuseMemo
CuseEffect without dependency array
DuseState
Attempts:
2 left
💡 Hint
Think about how dependency arrays control when useEffect runs.
🔧 Debug
advanced
2:30remaining
Why does this component cause an infinite loop during mounting?
Examine the code below. Why does it cause an infinite loop when the component mounts?
React
import React, { useState, useEffect } from 'react';

export default function Loop() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    setCount(count + 1);
  });

  return <p>{count}</p>;
}
ABecause useEffect runs after every render and updates state without dependencies, causing continuous re-renders
BBecause setCount is called before the component mounts
CBecause useState is used incorrectly without initial value
DBecause the component returns a paragraph instead of a div
Attempts:
2 left
💡 Hint
Look at the useEffect dependencies and what triggers re-renders.
📝 Syntax
advanced
1:30remaining
Which option correctly uses useEffect to run code only once on mount?
Select the code snippet that correctly runs the effect only once when the component mounts.
AuseEffect(() => { console.log('Mounted'); });
BuseEffect(() => { console.log('Mounted'); }, [{}]);
CuseEffect(() => { console.log('Mounted'); }, []);
DuseEffect(() => { console.log('Mounted'); }, null);
Attempts:
2 left
💡 Hint
Remember how dependency arrays control effect execution.
state_output
expert
3:00remaining
What is the final rendered output after mounting this component?
Analyze this React component. What will be displayed after it mounts?
React
import React, { useState, useEffect } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    setCount(c => c + 1);
    setCount(c => c + 1);
  }, []);

  return <div>{count}</div>;
}
A<div>0</div>
B<div>2</div>
C<div>1</div>
DComponent throws an error
Attempts:
2 left
💡 Hint
Consider how React batches state updates with functional updates inside useEffect.