Challenge - 5 Problems
Mounting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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>; }
Attempts:
2 left
💡 Hint
Think about when useEffect runs and how state updates affect rendering.
✗ Incorrect
The component first renders with 'Loading...'. Then useEffect runs after mounting and updates the message to 'Hello, friend!'. This triggers a re-render showing the updated message.
❓ lifecycle
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about how dependency arrays control when useEffect runs.
✗ Incorrect
useEffect with an empty dependency array runs only once after the component mounts. Other hooks either run on every render or during initialization.
🔧 Debug
advanced2: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>; }
Attempts:
2 left
💡 Hint
Look at the useEffect dependencies and what triggers re-renders.
✗ Incorrect
Without a dependency array, useEffect runs after every render. Inside it, setCount updates state, causing another render, creating an infinite loop.
📝 Syntax
advanced1: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.
Attempts:
2 left
💡 Hint
Remember how dependency arrays control effect execution.
✗ Incorrect
Only an empty array as the second argument makes useEffect run once after mounting. Omitting it or using other values causes different behaviors.
❓ state_output
expert3: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>; }
Attempts:
2 left
💡 Hint
Consider how React batches state updates with functional updates inside useEffect.
✗ Incorrect
Both setCount calls use functional updates, so React applies them in order, resulting in count increasing by 2 after mounting.