Sub topic 9
Why Suppressing the Linter Is a Trap
This chapter assumes you know how closures capture variables, how useEffect dependency arrays work, and the basic useEffectEvent syntax from earlier chapters.
The react-hooks/exhaustive-deps lint rule complains when an Effect reads a reactive value that is not in its dependency array. If adding that value would cause unwanted re-synchronization, it is tempting to silence the warning with a comment. The code then compiles and looks fine, but it usually contains a stale-value bug.
A Suppressed Effect That Goes Stale
This component should record key presses only while logging is enabled:
import { useState, useEffect } from 'react';
export default function KeyLogger() {
const [enabled, setEnabled] = useState(false);
const [keys, setKeys] = useState([]);
useEffect(() => {
function handleKeyDown(e) {
if (enabled) {
setKeys((k) => [...k, e.key]);
}
}
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<>
<button onClick={() => setEnabled((v) => !v)}>
Logging: {enabled ? 'on' : 'off'}
</button>
<p>{keys.join(' ')}</p>
</>
);
}The author wanted to subscribe once, so they wrote [] and turned off the warning. Clicking the button changes the label, but no keys are ever logged.
Why the Value Is Stale
Each render creates a new handleKeyDown function that closes over that render's enabled. Because the dependency array is empty, the Effect runs only after the first render. The listener registered on window is the first render's function, and in that function enabled is permanently false. Later renders create new functions with the right value, but nothing ever registers them.
The lint rule exists to catch exactly this. When you suppress it, the bug stays and only the warning goes away.
Two Honest Fixes
The first fix is to tell the truth about dependencies. Add enabled to the array. The listener is removed and re-added on each toggle. That is cheap here, so it is a valid answer.
The second fix applies when the value should not trigger re-synchronization. In this component, the subscription logic is reactive, but reading enabled is a decision made at the moment of the event. Move that read into an Effect Event:
import { useState, useEffect, useEffectEvent } from 'react';
export default function KeyLogger() {
const [enabled, setEnabled] = useState(false);
const [keys, setKeys] = useState([]);
const onKey = useEffectEvent((key) => {
if (enabled) {
setKeys((k) => [...k, key]);
}
});
useEffect(() => {
function handleKeyDown(e) {
onKey(e.key);
}
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, []);
return (
<>
<button onClick={() => setEnabled((v) => !v)}>
Logging: {enabled ? 'on' : 'off'}
</button>
<p>{keys.join(' ')}</p>
</>
);
}Now the array really is empty: the Effect reads no reactive values directly, and the linter agrees without a suppression comment. onKey always sees the latest enabled.
Comparing the Options
| Approach | Subscribes | Sees latest `enabled` | Linter checks future changes |
|---|---|---|---|
| Suppress with `[]` | Once | No | No |
| Add `enabled` to deps | On every toggle | Yes | Yes |
| Effect Event | Once | Yes | Yes |
When you feel the urge to write a suppression comment, ask which value you want to read without reacting to it. Usually that value belongs in an Effect Event, or the Effect needs restructuring.
Exercise
A teammate's Effect connects to roomId and calls log(userName) on each message. They wrote [roomId] with an eslint-disable comment because userName is missing. After the user renames themselves, messages are still logged with the old name until the room changes. Explain why, and give a fix that keeps reconnection tied to roomId only.
Show answer
The message handler was created during the render when the Effect last ran, so it closed over that render's userName. Renaming triggers a re-render but not a new Effect run, so the old handler keeps the old name. Wrap the logging in const onMessage = useEffectEvent((msg) => log(userName, msg)), call onMessage(msg) from the handler, and remove the suppression comment. The dependency array [roomId] is then complete, and the logs use the current name.