Skip to main content

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​

Every render must preserve the same Hook slot sequence
Render 1

slot 1 → useState
slot 2 → useEffect
slot 3 → useMemo

Render 2

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)} />;
}
Condition the behavior, not the Hook call
Stable Hook call
Always allocate slot
Conditional UI / effect behavior

Normal Hooks must not move between call positions​

Avoid calling them:

  • inside if/else branches;
  • 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.

Custom Hooks compose Hook slots; they do not create a separate runtime
Component
Custom Hook
Built-in Hooks in stable order

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.

Normal Hooks
useState, useEffect, useMemo, etc. require stable top-level call order.
use(resource)
Special React API with documented conditional/loop usage semantics.

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
Linting is the static safety layer before runtime and Compiler
1Source code
2eslint-plugin-react-hooks
3Rules / Compiler diagnostics
4Fix or isolate
5Safer runtime + Compiler coverage

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 useMemo usage.
Many Compiler diagnostics are correctness diagnostics first
Purity
No impure render-time work
Immutability
Do not mutate React snapshots/shared inputs
Stable structure
Hooks/components stay recognisable and ordered
Refs
Do not use mutable ref values as normal render state
Effects/state
Avoid render loops and avoidable sync effect updates
Compiler compatibility
Supported syntax/config/library contracts

Lint first, compile second​

A safe Compiler migration path is:

1Enable current lint rules
2Understand violations
3Fix incrementally
4Increase Compiler coverage
5Measure behavior + performance

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:

Dependencies describe the reactive configuration an Effect reads
Reactive values used
Dependency list
Effect lifecycle

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​

Where should this Hook/API call live?
Normal Hook in component/custom HookTop level before conditional returns
Need conditional resource read with use()Follow the special use API rules
Need a Hook in an event handlerMove Hook to component/custom Hook; event uses returned value/callback
Lint warns about Effect dependenciesFix synchronization model instead of hiding dependencies

Common mistakes​

  • calling useState or useEffect conditionally;
  • calling Hooks after an early return;
  • calling Hooks from events/callbacks;
  • assuming the special use exception applies to all Hooks;
  • disabling rules-of-hooks or exhaustive-deps broadly;
  • suppressing Compiler diagnostics instead of understanding them;
  • manually calling component functions;
  • treating lint rules as style-only warnings.

Team policy​

For production codebases:

  1. run current eslint-plugin-react-hooks in CI;
  2. fail builds on new Rules-of-React violations;
  3. document narrow exceptions;
  4. avoid repo-wide disables;
  5. fix the architecture that creates dependency fights;
  6. 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.

Summary​

Stable calls let React preserve identity across renders
Stable component execution
Stable Hook slots
Correct predictable React

References​