Find: Conditional Hook Call
Find the Mistake Medium
Question
What is wrong with this code?
Code
function Component({ showCount }) {
if (showCount) {
const [count, setCount] = useState(0);
}
return <div />;
}
Hint
Hooks must not be called conditionally.
Explanation
useState is called inside an if statement, which violates the Rules of Hooks. Hooks must be called at the top level, not inside conditions, loops, or nested functions. The fix is to move useState above the if and use the condition for rendering instead.
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.