react-hooks/rules-of-hooks
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/rules-of-hooks": "error",
},
},
];if (isLoggedIn) {
const [user, setUser] = useState(null);
}if (!data) return <Loading />;
const [processed, setProcessed] = useState(data);<button onClick={() => {
const [clicked, setClicked] = useState(false);
}}/>if (shouldFetch) {
const data = use(fetchPromise);
}for (const promise of promises) {
results.push(use(promise));
}const [count, setCount] = useState(0);
const [name, setName] = useState('');
if (!isSpecial) {
return null;
}
if (shouldFetch) {Examples from the ESLint documentation.
react-hooks/rules-of-hooks
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/rules-of-hooks": "error",
},
},
];react/rules-of-hooks
react plugin · pedantic
{
"plugins": [
"react"
],
"rules": {
"react/rules-of-hooks": "error"
}
}correctness/useHookAtTopLevel
correctness · since 1.0.0
{
"linter": {
"rules": {
"correctness": {
"useHookAtTopLevel": "error"
}
}
}
}Verifies the list of dependencies for Hooks like useEffect and similar
Validates usage of error boundaries instead of try/catch for errors in child components
Validates against assignment/mutation of globals during render, part of ensuring that side effects must render outside of render
Validates against mutating props, state, and other values that are immutable
Validates against usage of libraries which are incompatible with memoization (manual or automatic)
Validates that existing manual memoized is preserved by the compiler. React Compiler will only compile components and hooks if its inference matches or exceeds the existing manual memoization