Sub topic 13
When useEffectEvent Is the Wrong Tool
This chapter assumes you already know how to declare an Effect Event, how the dependency linter treats it, and how to pass reactive values to it as arguments. useEffectEvent fixes one specific problem: an Effect re-synchronizes because it reads a value it should not react to. If your problem is different, an Effect Event usually hides the real fix. The sections below cover the most common misuses and what to use in their place.
Logic Caused by a User Action Belongs in an Event Handler
If code runs because the user clicked, typed, or submitted something, put it in an event handler. You don't need an Effect, and you don't need an Effect Event.
import { useState } from 'react';
import { post } from './api.js';
function Form() {
const [text, setText] = useState('');
function handleSubmit(e) {
e.preventDefault();
post('/messages', { text }); // runs because the user submitted
setText('');
}
return (
<form onSubmit={handleSubmit}>
<input value={text} onChange={(e) => setText(e.target.value)} />
<button type="submit">Send</button>
</form>
);
}A common anti-pattern sets a submitted flag in state, watches it in an Effect, and then calls an Effect Event to read text. All three steps can go. Event handlers already see the latest props and state, so they need no special help.
Derived Values Belong in Render
If you can calculate a value from props and state, calculate it while rendering. Don't store it in state through an Effect, and don't read it through an Effect Event.
function Cart({ items }) {
const total = items.reduce((sum, item) => sum + item.price, 0);
return <p>Total: {total}</p>;
}Values That Should Re-synchronize Must Stay Dependencies
Ask whether the Effect should run again when the value changes. If a new roomId means you need a new connection, roomId has to stay a dependency. Wrapping it in an Effect Event to stop the reconnect brings back the stale-value bug the linter warned you about: the UI shows the new room while the connection stays in the old one.
Restructure the Effect First
Many unwanted dependencies disappear when you restructure the code:
| Symptom | Better fix |
|---|---|
| Effect reads state only to compute the next state | Use an updater: `setCount(c => c + 1)` |
| Object or function created during render is a dependency | Create it inside the Effect |
| One Effect does two unrelated jobs | Split it into two Effects |
| Effect copies props into state | Derive the value during render |
import { useState, useEffect } from 'react';
function Counter() {
const [count, setCount] = useState(0);
useEffect(() => {
const id = setInterval(() => setCount((c) => c + 1), 1000);
return () => clearInterval(id);
}, []); // no Effect Event needed: the updater reads the latest count
return <h1>{count}</h1>;
}Effect Events Are Not General-Purpose Stable Callbacks
You can't pass an Effect Event to a child component, call it during render, or use it as an onClick handler. If you want a stable function to pass down, use a normal function, or useCallback when memoization matters. Refs still make sense for mutable values that never affect rendering, such as a timeout ID. To read the latest props from inside an Effect, prefer an Effect Event over a hand-written "latest ref" pattern.
Exercise
An Effect fetches search results for query and has [query] as dependencies. A teammate wraps the fetch in an Effect Event and changes the dependencies to [] "to avoid extra requests." What goes wrong, and what is the right fix?
Show answer
The fetch then runs only on mount. Later changes to query never fetch new results, so the screen shows stale data. query is reactive, and the Effect should re-synchronize when it changes, so it must stay a dependency. To cut extra requests, debounce the input or ignore outdated responses with a cleanup flag. Don't hide the dependency.
Exercise
An Effect sets fullName state from firstName and lastName props, and reads a locale prop through an Effect Event. What should replace this code?
Show answer
Remove the Effect, the state, and the Effect Event. Compute fullName during render from firstName, lastName, and locale.