use-hook-at-top-level
Requires hooks to run at the top level of components and custom hooks.
Rule details Problem
Section titled “Rule details ”ProblemThe rule looks for hook-like calls, names that start with use followed by a capital letter, inside component or custom
hook bodies.
It reports hooks called inside conditionals, loops, nested functions, try blocks, after an early return, and recursive
hook calls. Hooks inside finally blocks are allowed because that block always runs.
You can narrow or widen what gets checked with onlyHooks, ignoreHooks, and importSources. If onlyHooks is
present, it takes priority over the other two options.
Examples
Section titled “Examples” Incorrect
function UserProfile({ userId }) { if (userId) { useEffect(() => {}, [userId]); }} Correct
function UserProfile({ userId }) { useEffect(() => { if (userId) loadUser(userId); }, [userId]);}Options
Section titled “Options”| Option | Type | Default | Notes |
|---|---|---|---|
ignoreHooks | string[] | [] | Exact hook names to skip. |
importSources | Record<string, boolean> | {} | true checks a source, false skips it. |
onlyHooks | string[] | [] | Whitelist mode. When present, the other filters are ignored. |
Related rules
Section titled “Related rules” use-exhaustive-dependencies Checks the dependency array once the hook is in the right place
require-named-effect-functions Checks the callback shape passed to effect hooks