The rules of hooks
Why hooks must only be called at a component's top level and only from components or hooks, how React tracks hook state by call order, and ESLint enforcement.
The rules of hooks
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Hooks must be called at a component's top level, never inside a loop, an if statement, a nested function, or after an early return — the call has to happen unconditionally, every single render.
- Hooks may only be called from a React function component or from another custom hook, never from a plain helper function or from outside React's rendering process entirely.
- React does not track hook state by name; it tracks it by the order the hooks were called in during render, keeping an ordered list per component instance and matching the Nth call this render to the Nth slot from last time.
- Calling a hook conditionally can shift every hook call after it into the wrong slot on some renders but not others, silently mixing up state and effects between hooks that have nothing to do with each other.
- The fix for conditional logic is to call the hook unconditionally every time and move the condition inside it or into the code that uses its result, rather than around the hook call itself.
- `eslint-plugin-react-hooks` statically checks both rules while you type and flags a violation immediately, which is why nearly every React project has it enabled from the start rather than relying on catching the bug by hand.
Two rules, and why they exist
React hooks come with exactly two non-negotiable rules:
- Only call hooks at the top level of a component — never inside a loop, a condition, a nested function, or after a
return. - Only call hooks from a React function component or from another custom hook — never from a regular function.
Both rules exist for the same underlying reason: React doesn’t know your variable names. It has no idea that you called your state count or your effect “sync with the server.” What it actually does is simpler and stricter — it keeps an ordered list of hook calls per component instance, and matches the first useState call this render to the first slot from last render, the second call to the second slot, and so on, purely by position.
What breaks when a hook is conditional
function Profile({ showBio }) {
if (showBio) {
const [bio, setBio] = useState(""); // breaks rule #1
}
const [name, setName] = useState("");
// ...
}
On a render where showBio is true, this component calls useState twice, in the order bio, then name. On a render where showBio is false, it calls useState only once — name — but React’s bookkeeping doesn’t know why a call is missing, only that call #1 this render doesn’t match call #1 from an earlier render where showBio was true. The state React hands back for “the first hook call” can end up being the stored bio state attached to a component that now has no bio call at all, or vice versa on the next toggle. The bug isn’t a crash you can always count on — it’s silently wrong data in a hook that has nothing to do with the one you meant to read, and it’s maddening to track down because the component’s own code reads perfectly reasonably.
The fix is always to call the hook unconditionally and put the condition inside it, or around how you use the result:
function Profile({ showBio }) {
const [bio, setBio] = useState("");
const [name, setName] = useState("");
return (
<div>
<p>{name}</p>
{showBio && <p>{bio}</p>}
</div>
);
}
Now both hooks run on every render, in the same order every time, and the conditional logic only decides what gets rendered, not whether a hook call happens.
Hooks belong to components and other hooks only
The second rule is about where a call is legal at all, independent of ordering:
// Breaks rule #2 — this is a plain function, not a component or hook
function getUserCount() {
const [count] = useState(0);
return count;
}
React’s hook bookkeeping is tied to a specific render of a specific component instance. A plain function called from anywhere, at any time, has no such render to attach state to — there’s no component instance for React to track the state against. The two places where that render context genuinely exists are inside a component function’s body while React is rendering it, and inside a custom hook (identifiable by the use naming convention) that is itself called from one of those two valid places, forming a chain that always traces back to an actual component render.
Letting a tool catch it instead of your eyes
Both rules are mechanical enough to check with static analysis, which is exactly what eslint-plugin-react-hooks does — it’s bundled into virtually every modern React starter (Vite’s React template included) specifically because manually spotting a hook called inside a condition during code review is unreliable. The plugin flags both rule violations as lint errors the moment you write them, before the bug ships and well before it has a chance to produce the kind of confusing, state-swapped symptom described above.
What the video covers
Codevolution’s episode walks through both rules with current React 19 examples — top-level-only calls, and calls restricted to components or custom hooks — and explains the call-order mechanism behind rule #1 directly, plus notes that the ESLint setup already present in a Vite project catches violations automatically. It mentions the plugin rather than dwelling on it, which the linked Cosden Solutions video does in more focused detail.
Where this fits
With the rules clear, the next lesson returns to the question of where complex, multi-part state should live — in useReducer for complex state.
Check yourself
Why is calling useState inside an if block considered unsafe, even if the condition happens to be true on every render so far?
React matches hook calls to their stored state by the order they run in during each render, not by name. If the condition can ever be false on some later render, that render calls one fewer hook, shifting every hook called after it into the wrong slot — so the rule is enforced on the call's position, not on whether the condition has misbehaved yet.
Can a plain JavaScript utility function, not a component or hook, call useState internally?
No. Hooks can only be called from a React function component's body or from another custom hook (a function whose name starts with use that itself follows the rules). Calling a hook from an ordinary helper function breaks the second rule, since React has no component render to attach the resulting state to.
How does React know which stored state belongs to which useState call when a component calls useState three times?
React keeps an ordered list of hook state per component instance and relies on the fact that the same component always calls its hooks in the same order on every render. The first useState call always reads and updates the first slot, the second call always the second slot, and so on — purely by call order, not by any name or label.
What does eslint-plugin-react-hooks actually check?
It statically analyzes your code for violations of the two rules: a hook called somewhere other than a component's top level (inside a condition, loop, or nested function), and a hook called from something other than a component or custom hook. It flags these as lint errors while you write the code, before the bug ever runs.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.