Predict useEffect Execution
Predict Output Medium
Question
How many times does the effect run after initial mount (no clicks)?
Code
function Component() {
const [count, setCount] = useState(0);
useEffect(() => {
console.log("effect ran");
}, [count]);
return <button onClick={() => setCount(c => c + 1)}>Click</button>;
}
Hint
The effect depends on count. How many times does count change on mount?
Explanation
On initial mount, the effect runs once because count is a dependency and this is the first render. Since no clicks happen, count doesn't change, so the effect doesn't run again. Total: 1 time.
Related Resources
Related Lessons
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.