Sub topic 3
Why Suppressing the Linter Is a Trap
This chapter assumes you know JavaScript closures, React props and state, useEffect, cleanup functions, and dependency arrays.
Why Suppressing the Linter Is a Trap
When useEffect reads a reactive value, the exhaustive-deps lint rule usually requires that value in the dependency array. Removing it can create a stale closure: the Effect keeps an old value after the component renders with a new one.
useEffect(() => {
connection.on('message', () => {
showNotification(message, theme);
});
return () => connection.disconnect();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [roomId]);This code hides a real dependency. The notification may use an old theme or message. If the value should be current but should not restart the connection, use an Effect Event instead.
How the Dependency Linter Treats Effect Events
An Effect Event created by useEffectEvent is intentionally non-reactive. The linter knows that it should not appear in the dependency array.
const onConnected = useEffectEvent(() => {
showNotification('Connected!', theme);
});
useEffect(() => {
const connection = createConnection(roomId);
connection.on('connected', onConnected);
connection.connect();
return () => connection.disconnect();
}, [roomId]);The Effect reacts to roomId. The Effect Event reads the latest theme when it runs.
Effect Events in Timers, Subscriptions, and Async Code
Effect Events work well for callbacks that run later, such as timer ticks, subscription messages, and completion handlers. They avoid resetting the external resource merely because a value used by the callback changed.
Call the Effect Event from the Effect-managed callback, not during rendering.
Wrapping Callbacks in Custom Hooks
A custom Hook can hide this pattern:
import { useEffect, useEffectEvent } from 'react';
export function useInterval(callback, delay) {
const onTick = useEffectEvent(callback);
useEffect(() => {
const id = setInterval(onTick, delay);
return () => clearInterval(id);
}, [delay]);
}The interval restarts when delay changes, but the callback sees current state.
When `useEffectEvent` Is the Wrong Tool
Use an event handler for a click or submit. Derive values during rendering when no external system is involved. Restructure an Effect when its dependencies reveal that the synchronization boundary is wrong. Do not use useEffectEvent to silence warnings or to pass callbacks between components.
From Experimental API to Stable API
Older React versions exposed this feature as experimental_useEffectEvent. The stable API is useEffectEvent, available in modern React releases. Check your installed React version and matching lint-plugin version before using it. Do not rename the experimental import without confirming package support.
Exercise
A subscription must reconnect when roomId changes, but its notification should use the latest theme. What belongs in the dependency array?
Show answer
Put roomId in the dependency array. Move the notification callback into useEffectEvent, and do not list the Effect Event or theme there.