Full answer
A closure is a function that remembers the variables around it when it was created. In React each render is a separate call of your component, so every function created in it remembers that render's values. Usually this is exactly what you want. It becomes a bug when an old function lives longer than its render: an interval, a timer, a subscription, or an event listener registered once.
Consider an effect with [] dependencies that starts an interval and reads count inside. The effect ran during the first render, so it saw count as 0. Every tick still sees 0, even after the state has moved on. This is a stale closure.
There are three standard fixes. The first is to list the value in the dependency array so the effect restarts with fresh values. That is right when the effect truly depends on the value, but it reconnects a chat or restarts a timer each time. The second is to avoid reading state: use an updater function, setCount((c) => c + 1), which does not need the current value. The third applies to code that should react to one value but only read another. For this, React 19.2 provides useEffectEvent.
const onConnected = useEffectEvent(() => { ... }) creates a function that always sees the latest props and state, yet is not a reactive value, so it is left out of the dependency array. In the example, the connection depends on roomId, but the message should use the current theme. Without useEffectEvent, you must either put theme in the dependencies (reconnecting whenever the theme changes) or read a stale theme.
Rules: call an Effect Event only from inside effects, not during render and not from child components, and do not use it to hide a dependency that should trigger the effect. The lint rule reminds you.
This is the stale version, shown on purpose. It connects once, but reports the first theme forever, and the linter warns about the missing dependency:
import { useEffect } from 'react';
function ChatRoom({ roomId, theme }) {
useEffect(() => {
const timer = setTimeout(() => console.log('Connected to ' + roomId + ' with ' + theme), 30);
return () => clearTimeout(timer);
}, [roomId]); // theme is read but not listed
return <h2>{roomId}</h2>;
}import { useEffect, useEffectEvent, useState } from 'react';
function createConnection(roomId, onConnected) {
const timer = setTimeout(onConnected, 200);
return { disconnect: () => clearTimeout(timer), roomId };
}
function ChatRoom({ roomId, theme }) {
const onConnected = useEffectEvent(() => {
console.log('Connected to ' + roomId + ' with the ' + theme + ' theme');
});
useEffect(() => {
const connection = createConnection(roomId, onConnected);
return () => connection.disconnect();
}, [roomId]);
return <h2>Room: {roomId}</h2>;
}
export default function App() {
const [theme, setTheme] = useState('light');
return (
<div>
<button onClick={() => setTheme('dark')}>Switch theme</button>
<ChatRoom roomId="general" theme={theme} />
</div>
);
}The connection depends only on roomId. The check switched the theme to dark before the connection finished, and the message used the latest theme without reconnecting. Output of the run: Connected to general with the dark theme
Key points
- Each render creates functions that capture that render's values.
- Long-lived callbacks (timers, listeners, subscriptions) can hold stale values.
- Fix by adding dependencies, using updater functions, or using useEffectEvent for read-only logic.
- useEffectEvent (React 19.2) always sees the latest values and is not a dependency.
- Call Effect Events only inside effects. Never use them to silence the dependency lint.
Common mistakes
- Using an empty dependency array while reading state inside an interval, and getting a frozen value.
- Disabling the exhaustive-deps lint rule to hide the problem.
- Calling an Effect Event during render or passing it to a child component.
- Moving a value that should restart the effect into an Effect Event.
Follow-ups
- How would you write a working interval counter without useEffectEvent?
- What is the difference between a ref that holds the latest value and an Effect Event?
- Why is it wrong to put an Effect Event in the dependency array?
Tips
- Start with a one-line definition of closure, then link it to renders. This avoids a vague answer.
- Offer the three fixes in order of simplicity. It shows range.
- Note that useEffectEvent is recent. Say which version added it.