Skip to content

use-hook-at-top-level

Requires hooks to run at the top level of components and custom hooks.

The 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.

Incorrect
function UserProfile({ userId }) {
if (userId) {
useEffect(() => {}, [userId]);
}
}
Correct
function UserProfile({ userId }) {
useEffect(() => {
if (userId) loadUser(userId);
}, [userId]);
}
OptionTypeDefaultNotes
ignoreHooksstring[][]Exact hook names to skip.
importSourcesRecord<string, boolean>{}true checks a source, false skips it.
onlyHooksstring[][]Whitelist mode. When present, the other filters are ignored.