React Reference ยท Concepts and Pitfalls
Effects vs event handlers
Decide whether code belongs in a React event handler or an effect: reacting to what the user did versus synchronizing with something outside React.
React gives you two places to run code that is not rendering: event handlers and effects. Choosing the wrong one is a leading source of confusing bugs. The test is to ask why the code runs.
An event handler runs because the user did something specific: clicked, typed, submitted. It is the right place for code that answers that action: sending a message, showing a notification, navigating, calling an API to save data. An effect runs because the component is on screen with certain props and state and needs to stay synchronized with something outside React: a server connection, a subscription, a timer, a non-React widget, the document title. An effect says "whenever these values are what they are, make the outside world match".
// Because of an action: handler <button onClick={() => sendMessage(text)}>Send</button> // Because of what is on screen: effect useEffect(() => { const connection = connect(roomId); return () => connection.disconnect(); }, [roomId]);
Try it
Here is the same requirement, "show a message when an item is added to the cart", done wrongly with an effect and rightly with a handler. The effect version also fires when the page loads with an item already in the cart, and each time cart changes for any reason, because the effect reacts to state, not to the click.
The effect version has already announced "tea" on load, before anyone clicked, and it will announce it again after adding a cup. The handler version speaks only when the button is pressed.
Effects are the right tool for synchronization. In the next example, the room name decides which (fake) connection is open. Changing it closes the old connection and opens a new one, while sending a message is a plain handler.
Syntax
A short decision table:
| Question | Event handler | Effect |
|---|---|---|
| Why does it run? | A specific user action or event | The component appeared or its values changed |
| Does it need cleanup? | No | Yes, to undo the setup |
| Typical content | Submit a form, buy, navigate, show a toast, log a click | Connect, subscribe, start a timer, fetch for display, sync the DOM |
| When does React run it? | Exactly once per event | After every commit where dependencies changed (and twice on mount in development StrictMode) |
| Sees latest state? | The snapshot from its render | The snapshot from its render; use useEffectEvent for non-reactive reads |
Usage notes
- "Because the user clicked" means a handler. If the code would be wrong to run after a page reload, it does not belong in an effect.
- Effects are for synchronization, not for chains of state. An effect that sets state to trigger another effect is a smell; do the computation in the handler. See You might not need an effect.
- Fetching on display is a legitimate effect (the data is needed because the component is shown), but a fetch that saves data belongs in a handler or a form action.
- StrictMode's double run is a useful test: if running setup, cleanup, setup again breaks something, the code probably is an event in disguise.
- A handler can read the latest state without declaring dependencies, which is why putting logic there is simpler than an effect with a long dependency array.
- Mixing both: the handler changes state ("the user picked a room"), and the effect reacts to the new state by synchronizing the connection.
- Analytics such as "page viewed" is usually an effect (the page is shown), while "button clicked" is a handler.