useEffect No Deps
Predict Output Easy
Question
What does this code print?
Code
function App() {
const [c, setC] = useState(0);
useEffect(() => { console.log("run"); });
return <button onClick={() => setC(c+1)}>+</button>;
}
// Click button once, how many "run" logs?
Hint
No dep array means effect runs after every render.
Explanation
Without a dependency array, useEffect runs after every render. Initial render: 1 log. Clicking the button triggers a state change and re-render: 1 more log. Total: 2.
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.