Sub topic 10
How the Dependency Linter Treats Effect Events
This chapter assumes you know the useEffect dependency array and have already seen how to declare an Effect Event with useEffectEvent. It also assumes your project runs ESLint with eslint-plugin-react-hooks. The plugin has two rules that matter here. react-hooks/exhaustive-deps checks dependency arrays, and react-hooks/rules-of-hooks checks where Hooks and their results are used.
Turning the rules on
Recent versions of eslint-plugin-react-hooks recognize useEffectEvent. A minimal flat config that enables both rules looks like this:
// 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',
},
},
];If the linter still asks you to add an Effect Event to a dependency array, your plugin version is probably older than your React version. Upgrade the plugin before you work around the warning.
What the linter knows about an Effect Event
When the linter sees const onConnected = useEffectEvent(...) inside a component, it marks onConnected as an Effect Event. That one fact changes how both rules treat the variable:
- Inside an Effect, calling
onConnected()does not makeonConnecteda dependency. The linter does not ask you to list it. - Props and state read inside the Effect Event body are not counted as dependencies of the Effect. Here
themeis read byonConnected, not by the Effect, so the linter doesn't ask for it. - Values the Effect reads directly, such as
roomId, are still required. The Effect Event does not exempt the rest of the Effect.
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]); // lint-clean: roomId is required, onConnected and theme are not
}What the linter reports
The linter also catches uses that break the rules of Effect Events.
| What you wrote | Rule | Result |
|---|---|---|
| Called `onConnected()` inside a `useEffect` in the same component | none | No warning |
| Added `onConnected` to the dependency array | `exhaustive-deps` | Reported: Effect Events must not be listed as dependencies |
| Called `onConnected()` during render | `rules-of-hooks` | Reported: Effect Events can only be called from Effects |
| Passed `onConnected` as a prop or to another Hook | `rules-of-hooks` | Reported for the same reason |
| Removed `roomId` from the array | `exhaustive-deps` | Reported: missing dependency `roomId` |
Listing an Effect Event as a dependency is flagged because it would mislead readers. React does not promise that the function keeps the same identity between renders, and the whole point of an Effect Event is that it never triggers re-synchronization.
Where the linter's knowledge ends
The linter analyzes one file at a time and tracks Effect Events by their declaration. It recognizes an Effect Event only when useEffectEvent is called in the same component as the Effect. If a custom Hook returns an Effect Event and a component calls it from its own Effect, the linter sees an ordinary function value and asks you to list it. That warning is a sign of a design problem, because Effect Events must not be passed out of the component that declares them. Keep the useEffectEvent call and the Effect that uses it together, as the custom Hooks chapter shows.
Exercise
A component declares const onTick = useEffectEvent(() => setCount((c) => c + step)); and uses it in useEffect(() => { const id = setInterval(() => onTick(), delay); return () => clearInterval(id); }, [delay, onTick, step]);. Which entries does the linter accept, and what should the array be?
Show answer
delay is read directly by the Effect, so it is required. onTick is an Effect Event, and listing it is reported by exhaustive-deps. step is read only inside the Effect Event, so the Effect does not need it, and listing it would reset the interval every time step changes. The array should be [delay].
Exercise
Why does the linter warn when a component calls, inside its own Effect, an Effect Event that a custom Hook returned?
Show answer
The linter only recognizes Effect Events declared with useEffectEvent in the same component, so it treats the returned function as an ordinary reactive value and asks for it as a dependency. Passing an Effect Event out of a Hook also breaks the rules, so the fix is to keep the useEffectEvent call and its Effect inside the same Hook or component.