src/content/docs/linter/rules/use-hook-at-top-level.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JSX and TSX" icon="seti:javascript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/correctness/useHookAtTopLevel`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - This rule belongs to the following domains: - [`react`](/linter/domains#react) - [`next`](/linter/domains#next) - Sources: - Same as [`react-hooks/rules-of-hooks`](https://github.com/facebook/react/blob/main/packages/eslint-plugin-react-hooks/README.md){
"linter": {
"rules": {
"correctness": {
"useHookAtTopLevel": "error"
}
}
}
}
Enforce that all React hooks are being called from the Top Level component functions.
This rule should be used only in React projects.
To understand why this required see https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level
function Component1({ a }) {
if (a == 1) {
useEffect();
}
}
function Component1({ a }) {
if (a != 1) {
return;
}
useEffect();
}
function notAHook() {
useEffect();
}
function Component1() {
useEffect();
}
test("the hook", () => {
renderHook(() => useHook());
});