react-hooks/exhaustive-deps
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/exhaustive-deps": "error",
},
},
];Verifies the list of dependencies for Hooks like useEffect and similar
useEffect(() => {
console.log(count);
}, []); // Missing 'count'useEffect(() => {
fetchUser(userId);
}, []); // Missing 'userId'useMemo(() => {
return items.sort(sortOrder);
}, [items]); // Missing 'sortOrder'useEffect(() => {
console.log(count);
}, [count]);useEffect(() => {
fetchUser(userId);
}, [userId]);const logItems = () => {
console.log(items);
};
return <button onClick={logItems}>Log</button>;Examples from the ESLint documentation.
react-hooks/exhaustive-deps
eslint-plugin-react-hooks
import reactHooks from "eslint-plugin-react-hooks";
export default [
{
plugins: { "react-hooks": reactHooks },
rules: {
"react-hooks/exhaustive-deps": "error",
},
},
];react/exhaustive-deps
react plugin · correctness
{
"plugins": [
"react"
],
"rules": {
"react/exhaustive-deps": "error"
}
}correctness/useExhaustiveDependencies
correctness · since 1.0.0
{
"linter": {
"rules": {
"correctness": {
"useExhaustiveDependencies": "error"
}
}
}
}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