useEffect Empty Deps
Predict Output Easy
Question
What does this code print?
Code
function App() {
const [c, setC] = useState(0);
useEffect(() => { console.log("mount"); }, []);
return <button onClick={() => setC(c+1)}>+</button>;
}
// Click button 3 times, how many "mount" logs?
Hint
Empty array runs effect only once on mount.
Explanation
With an empty dependency array, useEffect runs only once after the initial mount. Clicking the button causes re-renders but the effect does not re-run. Total: 1 log.
Related Resources
Related Lessons
- useEffect
Learn useEffect.
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.