Error: React Hook is called conditionally
Error message
React Hook useState is called conditionally. React Hooks must be called in the exact same order in every component render.
Quick summary
You placed a Hook call inside an if statement or other conditional, which breaks React's ability to track Hook order across renders.
Why this happens
Called a Hook inside an
ifstatementCalled a Hook inside a ternary expression
Called a Hook early-returned before other Hooks
Called a Hook inside a loop
Minimal example
function Component({ showCount }) {
if (showCount) {
const [count, setCount] = useState(0); // Conditional!
}
return <div />;
}function Component({ showCount }) {
const [count, setCount] = useState(0); // Always called
return showCount ? <div>{count}</div> : <div />;
}Move useState above the condition so it's always called. The condition now controls rendering, not whether the Hook runs.
How to diagnose
Look for any
if,?,for, orwhilewrapping a Hook callCheck for early returns before Hook calls
Use the eslint-plugin-react-hooks rules-of-hooks rule to auto-detect
How to fix
Move the Hook call to the top level (before any conditions)
Move the condition inside the Hook (e.g., inside useEffect)
If the Hook shouldn't always run, split into two components or use a custom Hook
How to prevent
Enable eslint-plugin-react-hooks rules-of-hooks rule
Always call Hooks at the top level of the component, before any early returns
Related Resources
Related Glossary
Related Lessons
- State
Learn the Rules of Hooks.