Sub topic 8
Rules of Effect Events
This chapter assumes you know how to declare an Effect Event with useEffectEvent, and that you understand why an Effect Event always reads the latest props and state. Effect Events have a few strict rules. Most of them follow from one fact: an Effect Event is not reactive. It is a piece of non-reactive logic that only an Effect is meant to fire.
The Rules at a Glance
| Rule | Why it exists |
|---|---|
| Call Effect Events only from inside Effects | They exist to run non-reactive logic that an Effect triggers |
| Never call them during rendering | Rendering must stay pure, and the latest-value guarantee applies only after commit |
| Never pass them to other components or Hooks | Other code can't tell that the function is non-reactive and might call it at the wrong time |
| Never list them in a dependency array | They are not reactive, so they must not re-synchronize an Effect |
| Declare them next to the Effect that uses them | This keeps it clear which Effect owns the logic |
Call Them Only from Inside Effects
You can call an Effect Event inside an Effect body, or inside callbacks that the Effect sets up, such as subscription handlers, timers, and .then() callbacks. Never call one while the component renders:
import { useEffectEvent } from 'react';
function Timer({ onDone }) {
const onTick = useEffectEvent(() => {
onDone();
});
// Wrong: Effect Events must not be called during render
// onTick();
// Wrong: Effect Events must not be passed to other components or Hooks
// return <Child onTick={onTick} />;
return null;
}When a button click should run code, write a normal event handler. Handlers are already non-reactive because they run in response to an interaction, so wrapping one in useEffectEvent gains you nothing and breaks the rule.
Keep Them Local
An Effect Event belongs to the Effect it was written for. Do not pass it as a prop or as an argument to another Hook. If a child receives onTick, the child might call it during render or list it in its own dependencies, and React can't protect you from either mistake. If a child needs a callback, pass it a regular function.
The rule covers Hooks as well. To accept a callback inside a custom Hook, wrap the callback in an Effect Event inside that Hook. The caller passes a plain function, and the Effect Event stays private to the Hook.
Never Make Them Dependencies
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]); // onConnected is deliberately left out
}React does not promise that onConnected keeps the same identity between renders. If you listed it as a dependency, the Effect could reconnect on every render, which is the exact bug Effect Events exist to fix.
Common Errors
- Calling an Effect Event from a click handler. Use a normal handler instead.
- Declaring an Effect Event in a parent and passing it to a child's Effect. Declare it in the component that owns the Effect.
- Moving
roomIdinto an Effect Event because it was "annoying." The chat would then stay connected to the old room.
Exercise
A component declares const onSave = useEffectEvent(() => save(draft)); and renders <button onClick={onSave}>Save</button>. Which rule does this break, and how should you fix it?
Show answer
It calls an Effect Event outside an Effect, from a click handler that is also passed to another component. Clicks are already non-reactive, so use a plain handler: <button onClick={() => save(draft)}>Save</button>.
Exercise
Why must an Effect Event never appear in a dependency array?
Show answer
It is non-reactive, and React does not guarantee it a stable identity. Listing it could re-run the Effect on every render. Its job is to read the latest values without triggering re-synchronization.