eslint-plugin-exhaustive-deps-exclude
v2.2.0
Published
ESLint plugin that excludes single dependencies from React's exhaustive-deps rule via inline comments
Maintainers
Readme
exhaustive-deps-exclude
Based on React's own
exhaustive-depsrule, copied from[email protected]at React commit019019be403c3269e15b8d7ebefb57d30f84086b.
The Problem
React's useEffect has a simple mental model: the code inside executes whenever one of the dependencies changes. The dependency array is supposed to give developers control over when effects run.
React's ESLint rule react-hooks/exhaustive-deps however enforces that you include every value used inside the effect in the dependency array. But here's the catch: if you're forced to include every dependency, why does the dependency array even exist? React could just auto-track everything. Developers should have control over what triggers re-execution.
In reality, there are legitimate cases where you know a dependency shouldn't trigger a re-run. You understand your code better than the linter does.
React's "Solution" is Overcomplicated
Consider this chat room example where you want to play a sound on new messages, but only if not muted: (Example taken from Removing Effect Dependencies)
function ChatRoom({ roomId }) {
const [messages, setMessages] = useState([]);
const [isMuted, setIsMuted] = useState(false);
useEffect(() => {
const connection = createConnection();
connection.connect();
connection.on("message", (receivedMessage) => {
setMessages((msgs) => [...msgs, receivedMessage]);
if (!isMuted) {
playSound();
}
});
return () => connection.disconnect();
}, [roomId, isMuted]); // eslint forces you to include isMuted in the dependency array
}The bug: when you toggle isMuted, the connection disconnects and reconnects unnecessarily.
React introduced useEffectEvent as their answer to this problem:
import { useState, useEffect, useEffectEvent } from "react";
function ChatRoom({ roomId }) {
const [messages, setMessages] = useState([]);
const [isMuted, setIsMuted] = useState(false);
const onMessage = useEffectEvent((receivedMessage) => { // ╭─────────────────────────────────╮
setMessages((msgs) => [...msgs, receivedMessage]); // │ Whole new function with a whole │
if (!isMuted) { // │ new hook just to exclude one │
playSound(); // │ dependency... │
} // ╰─────────────────────────────────╯
});
useEffect(() => {
const connection = createConnection();
connection.connect();
connection.on("message", (receivedMessage) => {
onMessage(receivedMessage);
});
return () => connection.disconnect();
}, [roomId]);
}With useEffectEvent, you need to (1) extract logic, (2) import another hook, and (3) restructure your code. 😮💨
With this plugin, you just add one comment explaining your intent:
function ChatRoom({ roomId }) {
const [messages, setMessages] = useState([]);
const [isMuted, setIsMuted] = useState(false);
useEffect(() => {
const connection = createConnection();
connection.connect();
connection.on("message", (receivedMessage) => {
setMessages((msgs) => [...msgs, receivedMessage]);
if (!isMuted) {
playSound();
}
});
return () => connection.disconnect();
// exhaustive-deps-exclude [isMuted]
}, [roomId]);
}That's it! 🥳 Clean, explicit, and you keep your code structure intact.
When to Exclude Dependencies
1. WebSocket/Connection Managers
useEffect(() => {
websocket.on("message", (data) => {
handleMessage(data, currentUser);
});
// exhaustive-deps-exclude [websocket, currentUser]
}, []);You want to set up the listener once. Re-creating it when currentUser changes would cause duplicate listeners or connection issues.
2. Stable Callback References
function DataTable({ onRowClick, filters }) {
useEffect(() => {
const data = fetchData(filters);
data.forEach((row) => {
row.onClick = () => onRowClick(row);
});
// exhaustive-deps-exclude [onRowClick]
}, [filters]);
}onRowClick changes reference on every parent render, but its behavior is stable. You only care about re-fetching when filters change.
3. Refs
useEffect(() => {
const element = elementRef.current;
element.addEventListener("scroll", handleScroll);
return () => element.removeEventListener("scroll", handleScroll);
// exhaustive-deps-exclude [elementRef]
}, [handleScroll]);Refs don't cause re-renders, so including them in deps is pointless.
4. setState Functions
const [count, setCount] = useState(0);
useEffect(() => {
const interval = setInterval(() => {
setCount((c) => c + 1);
}, 1000);
return () => clearInterval(interval);
// exhaustive-deps-exclude [setCount]
}, []);setCount is stable across renders, but ESLint doesn't know that.
5. Event Handlers That Read Latest State
function SearchBox({ onSearch, debounceMs }) {
const [query, setQuery] = useState("");
useEffect(() => {
const handler = setTimeout(() => {
onSearch(query);
}, debounceMs);
return () => clearTimeout(handler);
// exhaustive-deps-exclude [onSearch]
}, [query, debounceMs]);
}You want to debounce based on query changes, not onSearch reference changes.
Installation
Install it as a dev dependency, under the package manager the project already uses:
npm install --save-dev eslint-plugin-exhaustive-deps-exclude
# or
pnpm add --save-dev eslint-plugin-exhaustive-deps-exclude
# or
yarn add --dev eslint-plugin-exhaustive-deps-excludeIt runs on ESLint 8.57, 9 and 10, Node 20.19 and newer, under flat config.
ESLint 8 reads a flat config once ESLINT_USE_FLAT_CONFIG=true sits in the environment.
Configuration
configs.recommended turns React's rule off and this one on:
// eslint.config.js
import exhaustiveDepsExclude from "eslint-plugin-exhaustive-deps-exclude";
export default [
{ files: ["**/*.{js,jsx,ts,tsx}"], ...exhaustiveDepsExclude.configs.recommended },
];Beside React's own recommended rules, this plugin's rules come last, and a severity of your own comes after both:
// eslint.config.js
import reactHooks from "eslint-plugin-react-hooks";
import exhaustiveDepsExclude from "eslint-plugin-exhaustive-deps-exclude";
export default [
{
files: ["src/**/*.{js,jsx,ts,tsx}"],
plugins: {
"react-hooks": reactHooks,
"exhaustive-deps-exclude": exhaustiveDepsExclude,
},
rules: {
...reactHooks.configs.recommended.rules,
...exhaustiveDepsExclude.configs.recommended.rules,
"exhaustive-deps-exclude/exhaustive-deps": "warn",
},
},
];Both rules report the same missing dependencies, and leaving React's on reports each one twice.
React's recommended rules switch react-hooks/exhaustive-deps on, and this plugin's rules switch it back off.
The reverse order would leave React's rule on.
Usage
Add an inline comment before the closing bracket of your dependency array (useEffect, useLayoutEffect, useCallback, useMemo, useImperativeHandle, or a hook additionalHooks names):
useEffect(() => {
// your effect code
// exhaustive-deps-exclude [dep1, dep2, dep3]
}, [includedDep]);A name in the comment spells the dependency the way the rule reports it, so a property read is excluded by its path:
useEffect(() => {
connect(config.url);
// exhaustive-deps-exclude [config.url]
}, []);The plugin will:
- ✅ Check that all non-excluded dependencies are in the array
- ✅ Ignore dependencies listed in the exclude comment
- ✅ Warn about unnecessary exclusions (if you exclude something not used in the effect)
- ✅ Detect conflicting inclusions/exclusions
- ✅ Offer the exclusion comment as an editor fix, next to the fix that adds the dependency
(Bonus:) Why not use '// eslint-ignore-next-line react-hooks/exhaustive-deps'?
Because completely disabling the rule means zero linting help. You could accidentally forget to include a dependency that should be there, and ESLint won't catch it.
With eslint-disable-next-line:
useEffect(() => {
doSomething(a, b, c);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [a]); // Missing b and c - no warning!With exhaustive-deps-exclude:
useEffect(() => {
doSomething(a, b, c);
// exhaustive-deps-exclude [c]
}, [a]); // ⚠️ Plugin warns: "b is used but not in deps or excluded"This plugin gives you selective exclusion while still catching genuine mistakes. You explicitly declare which dependencies you're intentionally excluding and the plugin verifies everything else is correct.
