Sub topic 9
Why Suppressing the Linter Is a Trap
This chapter assumes you know how closures capture variables in JavaScript, how useEffect and its dependency array work, and how the react-hooks/exhaustive-deps lint rule reports missing dependencies. Earlier chapters showed that an Effect re-synchronizes whenever a reactive value it reads changes, and that useEffectEvent lets you read a value without reacting to it. Before Effect Events existed, many developers got the same "don't re-run on this value" behavior with a single comment:
// eslint-disable-next-line react-hooks/exhaustive-depsThe comment stops the warning. It also causes a class of bugs that are hard to see in testing and hard to trace later. This chapter explains how those bugs happen, why they are intermittent, and how to replace each suppression with code that says what you mean.
What the Suppression Actually Does
The exhaustive-deps rule reads the body of your Effect, collects every reactive value it uses (props, state, and anything computed from them during render), and compares that set to the dependency array. When a value is missing, it warns you.
Adding eslint-disable-next-line above the dependency array does not tell React anything. React never sees lint comments. The comment only switches off the check for that line. The Effect still closes over the values from the render in which it was created, and React still re-runs it only when a listed dependency changes.
So the suppression amounts to an untrue statement: "this Effect does not depend on these values." The code inside the Effect still reads them, and it reads whatever copies existed when the Effect last ran.
The Stale Closure Bug, Step by Step
Here is a timer that counts up by a configurable step. The author wants the interval set up once, so they leave the array empty and silence the linter.
import { useState, useEffect } from 'react';
export default function Timer() {
const [count, setCount] = useState(0);
const [increment, setIncrement] = useState(1);
useEffect(() => {
const id = setInterval(() => {
setCount((c) => c + increment);
}, 1000);
return () => clearInterval(id);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<>
<h1>{count}</h1>
<button onClick={() => setIncrement((i) => i + 1)}>
Increment by {increment}
</button>
</>
);
}Run it and click the button a few times. The label changes to "Increment by 4", but the counter still goes up by 1 each second.
The cause is the closure. On the first render, increment is 1. The Effect function created during that render captures that increment binding. The interval callback, created inside the Effect, captures it too. Clicking the button triggers a new render with increment equal to 2, and that render creates a new Effect function that sees 2. React compares the dependency arrays, finds [] equal to [], and throws the new Effect away. The interval from the first render keeps running with its own copy of increment.
Nothing crashes and no error is logged. The component just behaves as if part of its state had stopped updating.
Why These Bugs Are Intermittent
The timer bug shows up on the first click. Many suppression bugs only show up in particular orderings of events, which makes them easy to miss.
Consider the chat room from earlier chapters with theme left out of the array on purpose:
import { useEffect } from 'react';
import { createConnection } from './chat.js';
import { showNotification } from './notifications.js';
function ChatRoom({ roomId, theme }) {
useEffect(() => {
const connection = createConnection(roomId);
connection.on('connected', () => {
showNotification('Connected!', theme);
});
connection.connect();
return () => connection.disconnect();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [roomId]);
}This looks right in a quick test. Switch rooms and the notification appears with the current theme, because switching rooms re-runs the Effect, and the new Effect captures the theme from that render.
Now try a different order. Switch the theme to dark, then lose the network briefly so the connection fires connected again, still in the same room. The handler shows a light notification, because it captured theme the last time roomId changed. Whether you see the bug depends on whether a reconnect happens between a theme change and a room change. Your manual test may never hit that order, while your users eventually will.
The table summarizes how the outcome depends on what happened before the callback ran.
| Sequence of events | Value the callback reads | Correct? |
|---|---|---|
| Change room, then `connected` fires | Theme at the room change | Yes |
| Change theme, then change room, then `connected` | Theme at the room change (new) | Yes |
| Change room, change theme, reconnect in same room | Theme at the room change (old) | No |
| Change theme only, interval or listener fires later | Theme from an earlier render | No |
The Bug You Haven't Written Yet
The worst cost of a suppression is in future edits. Suppose a teammate later adds a serverUrl prop:
import { useEffect } from 'react';
import { createConnection } from './chat.js';
import { showNotification } from './notifications.js';
function ChatRoom({ roomId, serverUrl, theme }) {
useEffect(() => {
const connection = createConnection(serverUrl, roomId);
connection.on('connected', () => {
showNotification('Connected!', theme);
});
connection.connect();
return () => connection.disconnect();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [roomId]);
}Without the comment, the linter would flag serverUrl at once. With it, the linter stays silent. Changing serverUrl now leaves the user connected to the old server until they happen to switch rooms. This is no longer a stale notification color. The Effect is synchronized with the wrong external system.
The original author may have meant to opt out of theme only. The comment cannot express "only theme". It opts out of everything.
Replacing the Suppression
In each case the fix is to state the intent in code rather than in a lint comment. The logic that should not cause re-synchronization moves into an Effect Event, and the dependency array goes back to being complete.
The timer becomes:
import { useState, useEffect, useEffectEvent } from 'react';
export default function Timer() {
const [count, setCount] = useState(0);
const [increment, setIncrement] = useState(1);
const onTick = useEffectEvent(() => {
setCount((c) => c + increment);
});
useEffect(() => {
const id = setInterval(() => {
onTick();
}, 1000);
return () => clearInterval(id);
}, []);
return (
<>
<h1>{count}</h1>
<button onClick={() => setIncrement((i) => i + 1)}>
Increment by {increment}
</button>
</>
);
}The interval is still set up once. Each tick calls onTick, which always reads increment from the latest render. The empty array is now honest: the Effect itself reads no reactive values, and the linter agrees without any comment.
The chat room follows the same pattern:
import { useEffect, useEffectEvent } from 'react';
import { createConnection } from './chat.js';
import { showNotification } from './notifications.js';
function ChatRoom({ roomId, serverUrl, theme }) {
const onConnected = useEffectEvent(() => {
showNotification('Connected!', theme);
});
useEffect(() => {
const connection = createConnection(serverUrl, roomId);
connection.on('connected', () => {
onConnected();
});
connection.connect();
return () => connection.disconnect();
}, [roomId, serverUrl]);
}The theme-only reconnect now shows the current theme. A future edit that reads a new prop inside the Effect gets flagged by the linter, because the check is active again.
When the Suppression Hides a Different Problem
Some suppressions are not about reading a value without reacting to it. They hide a dependency that changes on every render, such as an object or function created inside the component body. Wrapping that in an Effect Event would hide the real issue. The usual fixes are:
- Move the object or function inside the Effect, so it is not a dependency at all.
- Move it outside the component if it does not use props or state.
- Depend on the primitive values you actually need (
options.roomId) rather than the whole object.
A later chapter on when not to use useEffectEvent covers these cases in more detail. The point here is that the suppression comment was never the right fix in any of them. It turned a visible warning into an invisible bug.
Exercises
Exercise
In the suppressed Timer, the user clicks the button three times quickly, then waits five seconds. By how much does the count increase during those five seconds, and why?
Show answer
It increases by 5, one per tick. The interval callback was created during the first render and captured increment = 1. The clicks cause new renders, but the empty dependency array means React never re-runs the Effect, so the old interval keeps adding the stale value 1.
Exercise
A component has useEffect(() => { logVisit(url, cart.length); }, [url]) with a suppression comment above the array. The intent is to log once per URL and include the current cart size. Is there a stale-value bug, and how would you rewrite it without the comment?
Show answer
In this specific Effect, cart.length is read immediately when the Effect runs, so it is current at that moment and the stale-value bug does not appear. The suppression is still harmful because it hides any future missing dependency. Rewrite it as const onVisit = useEffectEvent((visitedUrl) => { logVisit(visitedUrl, cart.length); }); and useEffect(() => { onVisit(url); }, [url]);, which keeps url reactive and needs no suppression.
Exercise
Why can't you write a suppression that ignores only theme but still checks every other value in the Effect?
Show answer
The eslint-disable-next-line comment disables the exhaustive-deps rule for the whole dependency array line. ESLint has no syntax for excluding one variable from that rule, so every other missing dependency, including ones added later, is also hidden.