Sub topic 10
How the Dependency Linter Treats Effect Events
The exhaustive-deps lint rule checks every Effect you write. This chapter shows how that rule, and its companion rules-of-hooks, handle functions created with useEffectEvent. You will learn which values the linter asks you to list and which it leaves out. You will also see the mistakes it catches and the ones it cannot catch.
This chapter assumes you know how a dependency array works and how to declare an Effect Event with useEffectEvent, both covered in earlier chapters. It also assumes you have run ESLint on a project before.
Two Rules Share the Work
The ESLint package eslint-plugin-react-hooks ships two rules that matter here.
| Rule | What it checks | Effect Event behavior |
|---|---|---|
| `react-hooks/exhaustive-deps` | Every reactive value an Effect reads appears in its dependency array | Treats an Effect Event as non-reactive and does not ask you to list it |
| `react-hooks/rules-of-hooks` | Hooks are called at the top level, and Hook results are used correctly | Reports Effect Events that are called during render or passed elsewhere |
The two rules split the job. exhaustive-deps looks inside each Effect and asks "what does this code read?" rules-of-hooks looks at the whole component and asks "where is this Effect Event used?" You need both rules turned on to get full coverage.
How exhaustive-deps Decides What Is Reactive
The rule walks the body of each Effect and collects every identifier that is declared in the component body. Props, state, and anything computed from them count as reactive. Then it filters out values that React guarantees are stable:
| Value | Must be listed? | Why |
|---|---|---|
| Props and state | Yes | They can change between renders |
| Variables computed during render | Yes | They are recalculated on each render |
| `setState` functions from `useState` | No | React keeps their identity stable |
| `dispatch` from `useReducer` | No | React keeps its identity stable |
| `ref` objects from `useRef` | No | The ref object never changes; only `.current` does |
| Functions returned by `useEffectEvent` | No | They are non-reactive by design |
| Module-level imports and constants | No | They are declared outside the component |
The linter recognizes these stable values by tracing each identifier back to the Hook call that created it. If onConnected was assigned from useEffectEvent(...) in the same component, the rule knows it is an Effect Event.
The values read inside the Effect Event's body are not analyzed as part of the Effect. The Effect only references the name onConnected. What onConnected reads internally (theme, for example) never enters the dependency calculation. That is the whole point: you moved theme out of the Effect's reactive reach.
Watching the Linter Change Its Answer
Start with the chat room from earlier chapters, written without an Effect Event:
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();
}, [roomId]);
return null;
}
export default ChatRoom;The linter warns that the Effect has a missing dependency: theme. It reads theme and finds that theme is a prop, so theme is reactive. It has no way to know you only meant to read it.
Now extract the notification into an Effect Event:
import { useEffect, useEffectEvent } from 'react';
import { createConnection } from './chat.js';
import { showNotification } from './notifications.js';
function ChatRoom({ roomId, theme }) {
const onConnected = useEffectEvent(() => {
showNotification('Connected!', theme);
});
useEffect(() => {
const connection = createConnection(roomId);
connection.on('connected', () => {
onConnected();
});
connection.connect();
return () => connection.disconnect();
}, [roomId]);
return null;
}
export default ChatRoom;The warning disappears. The Effect now reads roomId, which is listed, and onConnected, which the linter classifies as stable. theme is no longer read by the Effect at all.
The warning went away because you changed what the code says, not because you silenced the tool. If someone later adds a serverUrl prop and uses it inside the Effect, the linter will flag it as a missing dependency right away. A suppression comment would have hidden that new bug too.
Do Not List the Effect Event
You might be tempted to add onConnected to the array "just to be safe":
useEffect(() => {
const connection = createConnection(roomId);
connection.on('connected', () => {
onConnected();
});
connection.connect();
return () => connection.disconnect();
}, [roomId, onConnected]); // don't do thisRecent versions of the plugin report this. The message says that functions returned from useEffectEvent must not be included in the dependency array. An Effect Event is not part of the Effect's reactive inputs, so listing it gives readers a false picture of what triggers the Effect.
There is a runtime reason too. React does not promise that the Effect Event function keeps the same identity across renders. If its identity changed, listing it would make the Effect re-run on every render, which is exactly the behavior you were trying to avoid.
What rules-of-hooks Catches
exhaustive-deps only looks inside Effects. Misuse elsewhere in the component falls to rules-of-hooks, which checks where each Effect Event is referenced.
import { useEffect, useEffectEvent } from 'react';
function Child({ onTick }) {
useEffect(() => {
const id = setInterval(onTick, 1000);
return () => clearInterval(id);
}, [onTick]);
return null;
}
function Timer({ onDone }) {
const onTick = useEffectEvent(() => {
onDone();
});
onTick(); // reported: called during render
return <Child onTick={onTick} />; // reported: passed to another component
}
export default Timer;Both marked lines are reported. The messages explain that a function created with useEffectEvent can only be called from Effects and Effect Events in the same component, and cannot be passed down. The rule also reports assigning an Effect Event to another variable or putting it in an object, because those are ways to leak it out of the Effect.
The allowed uses are narrow:
- Calling it directly inside a
useEffect,useLayoutEffect, oruseInsertionEffectbody in the same component. - Calling it inside a callback created within such an Effect, such as a
setIntervalcallback or an event listener. - Calling it from another Effect Event in the same component.
What the Linter Cannot Check
The linter enforces where Effect Events are used. It cannot judge whether the logic inside them belongs there. Consider this mistake:
import { useEffect, useEffectEvent } from 'react';
import { createConnection } from './chat.js';
function ChatRoom({ roomId }) {
const onMount = useEffectEvent(() => {
const connection = createConnection(roomId);
connection.connect();
return connection;
});
useEffect(() => {
const connection = onMount();
return () => connection.disconnect();
}, []); // lint passes, but roomId changes are ignored
return null;
}
export default ChatRoom;The linter reports nothing. onMount is an Effect Event, the array is empty, and the Effect reads no other reactive values. Yet the component is broken. When roomId changes, it stays connected to the old room. Connecting to a room is the reactive part of this Effect, and it was hidden inside an Effect Event.
This is the same stale-value bug that a suppression comment creates. It has just been moved somewhere the linter does not look. Use this test: if a change to a value should restart the Effect's work, that value must be read by the Effect itself or passed in as an argument. Only logic that reacts to an event, such as showing a notification after a connection is made, belongs in the Effect Event.
Making Sure the Linter Knows About useEffectEvent
The linter can only classify Effect Events if your plugin version supports them. Support for the stable useEffectEvent import arrived in eslint-plugin-react-hooks 6.1, alongside React 19.2. Older plugin versions may not recognize it. They then report the Effect Event as a missing dependency, and that tempts people to add it to the array. If you see that warning, upgrade the plugin rather than following its advice.
A minimal flat config that turns on both rules:
// eslint.config.js
import reactHooks from 'eslint-plugin-react-hooks';
export default [
{
files: ['**/*.{js,jsx}'],
languageOptions: {
parserOptions: {
ecmaFeatures: { jsx: true },
},
},
plugins: {
'react-hooks': reactHooks,
},
rules: {
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn',
},
},
];Call the Hook by its own name. The linter tracks identifiers that come directly from a useEffectEvent(...) call. If you wrap it in a helper with a different name, the linter can no longer trace the result back to useEffectEvent, and it will treat the result as an ordinary reactive function. The final chapter covers the experimental import name used before the stable release.
Summary of Linter Behavior
| Situation | Rule | Result |
|---|---|---|
| Effect reads a prop not in the array | `exhaustive-deps` | Warning: missing dependency |
| Effect calls an Effect Event not in the array | `exhaustive-deps` | No warning |
| Effect Event listed in the array | `exhaustive-deps` | Warning: remove it |
| Effect Event called during render | `rules-of-hooks` | Error |
| Effect Event passed as a prop or to a Hook | `rules-of-hooks` | Error |
| Reactive logic hidden inside an Effect Event | None | No report; you must catch it in review |
Exercise
A teammate's Effect calls onVisit(url) with the array [url, onVisit], and their linter is silent about onVisit. Another teammate's linter flags onVisit as a missing dependency when it is left out. What is likely going on in each case, and what should the final array be?
Show answer
The second teammate is probably on an older eslint-plugin-react-hooks version that does not recognize useEffectEvent, so it treats onVisit as an ordinary reactive function. The first teammate's setup may be similarly outdated, since current versions report an Effect Event listed in the array. Both should upgrade to a plugin version that supports useEffectEvent (6.1 or later), and the array should be [url].
Exercise
Why does the linter accept an Effect Event that creates a chat connection from roomId with an empty dependency array, and why is that code still wrong?
Show answer
exhaustive-deps only inspects identifiers the Effect reads directly. The Effect reads only the Effect Event, which counts as stable, so the empty array passes. The code is wrong because connecting to a room should restart when roomId changes. Hiding that reactive logic inside an Effect Event recreates the stale-value bug the linter exists to prevent. The connection logic belongs in the Effect with roomId in the array.
Exercise
Which rule reports return <Child onTick={onTick} /> when onTick comes from useEffectEvent, and why can't exhaustive-deps catch it?
Show answer
rules-of-hooks reports it. exhaustive-deps only analyzes the bodies and dependency arrays of Effects. Passing an Effect Event as a prop happens in the render output, outside any Effect, so only the rule that tracks how Hook results are used across the component can see it.