rules-of-hooks
Configuration
rslint.config.ts
Enforce React's Rules of Hooks: hooks must only be called at the top level of React function components or custom Hooks, never inside loops, conditions, nested functions, class components, or async functions.
Rule Details
This rule reports React Hook calls that violate the Rules of Hooks:
- A hook (a function whose name is
useor starts withuse[A-Z0-9], or a member access likeNamespace.useFoo) must be called at the top level of a function component or custom hook — never insideif/else, ternary expressions,&&/||/??chains,try/catchblocks, loops, or callbacks. - A hook must not be called after an early
returnorthrow. - A hook must not be called inside a class component (method, accessor, or
class-field arrow), inside an
asyncfunction, or at the top level of a module. - The React
use(...)hook is exempt from the loop, conditional, and early-return checks (it may be called conditionally), but is still rejected insidetry/catchblocks and async functions. - Functions created with
useEffectEvent(...)may only be called from inside a React effect hook (useEffect,useLayoutEffect,useInsertionEffect, or any hook matching theadditionalEffectHookssetting) or anotheruseEffectEventcallback in the same component.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Settings
additionalEffectHooks
: A regular expression (matched against the bare hook name) that names extra
hooks for which useEffectEvent-bound identifiers may be referenced as
callbacks. Defaults to none.