Rules of Hooks and Compiler-aware ESLint
React relies on Hook call order to associate Hook state with a component across renders.
Call normal Hooks at the top level of React function components and custom Hooks.
Hook order is identity
slot 1 → useState
slot 2 → useEffect
slot 3 → useMemo
slot 1 → useState
slot 2 → useEffect
slot 3 → useMemo
If a Hook disappears conditionally, later slots shift and React can no longer associate the right state/effect with the right call site.
Invalid: conditional Hook
function Profile({ enabled }) {
if (enabled) {
const [name, setName] = useState(''); // wrong
}
return null;
}
Better:
function Profile({ enabled }) {
const [name, setName] = useState('');
if (!enabled) {
return null;
}
return <input value={name} onChange={e => setName(e.target.value)} />;
}
Normal Hooks must not move between call positions
Avoid calling them:
- inside
if/elsebranches; - inside loops;
- after conditional early returns;
- inside event handlers;
- inside arbitrary callbacks;
- inside class methods;
- at module scope;
- inside arbitrary async functions.
The rule is about stable React-controlled execution order.
Custom Hooks
A custom Hook is a function that follows Hook rules and uses a use prefix.
function useOnlineStatus() {
const [online, setOnline] = useState(true);
return online;
}
The prefix is meaningful to both humans and tooling.
The special use API exception
React's use API is special: it may be called conditionally and in loops.
function Comments({ shouldLoad, promise }) {
if (shouldLoad) {
const comments = use(promise);
return <CommentList comments={comments} />;
}
return null;
}
This does not make normal Hooks conditional.
useState, useEffect, useMemo, etc. require stable top-level call order.Do not generalize the exception.
ESLint is part of modern React correctness
eslint-plugin-react-hooks does more than enforce the classic Hook rules. Its recommended presets also surface Rules-of-React and React Compiler diagnostics.
npm install -D eslint-plugin-react-hooks@latest
The plugin is valuable even if Compiler is not enabled.
Core rules
rules-of-hooks
Checks Hook locations and ordering.
exhaustive-deps
Checks dependency arrays for dependency-aware Hooks.
Do not silence exhaustive-deps just to remove warnings. Dependencies describe which reactive values configure the synchronization process.
Compiler-aware diagnostics
Current recommended plugin presets include rules covering areas such as:
- component/Hook factories;
- Compiler configuration and gating;
- Error Boundary usage;
- globals and immutability;
- incompatible libraries;
- preservation of manual memoization;
- purity;
- ref usage;
- setting state during render/effects;
- static component definitions;
- unsupported syntax;
- invalid
useMemousage.
Lint first, compile second
A safe Compiler migration path is:
Do not treat Compiler opt-outs as a substitute for fixing broken Hook ordering or impure rendering.
Dependency arrays are not scheduling wishes
Wrong mental model:
“I want this Effect to run once, so I will hide the dependencies.”
Better mental model:
If dependencies feel wrong, first ask whether the Effect itself is necessary or whether the code belongs in render/event logic.
React must call components and Hooks
Do not bypass React's execution ownership by calling component functions manually.
Wrong:
const result = SomeComponent(props);
Prefer:
<SomeComponent {...props} />
React needs to own component/Hook execution so it can associate state, scheduling, identity, and error boundaries correctly.
Decision guide
Common mistakes
- calling
useStateoruseEffectconditionally; - calling Hooks after an early return;
- calling Hooks from events/callbacks;
- assuming the special
useexception applies to all Hooks; - disabling
rules-of-hooksorexhaustive-depsbroadly; - suppressing Compiler diagnostics instead of understanding them;
- manually calling component functions;
- treating lint rules as style-only warnings.
Team policy
For production codebases:
- run current
eslint-plugin-react-hooksin CI; - fail builds on new Rules-of-React violations;
- document narrow exceptions;
- avoid repo-wide disables;
- fix the architecture that creates dependency fights;
- review new custom Hooks for stable call structure and focused responsibility.
Interview questions
Junior: Why must Hooks be called in the same order every render?
Mid-level: Why can use() be conditional while useState() cannot?
Senior: Explain how the modern Hooks ESLint plugin supports both React correctness and React Compiler adoption in a large codebase.