useEffect
A React Hook that keeps a component in sync with something outside React, such as a browser event subscription or a server connection. React runs the effect after committing the render.
What belongs in an Effect?
Rendering answers “What should the UI look like?” An Effect answers “Now that this UI is on screen, what needs to stay in sync with it?” For example, a chat component can render the selected room name without opening a connection during render. Its Effect keeps the connection pointed at the selected room as the user navigates.
Common uses include subscribing to browser events, managing timers or connections, and controlling third-party widgets. If you can calculate a value from existing props or state, calculate it during render rather than copying it into state with an Effect. If an action should happen specifically because the user clicked a button, put it in the event handler.
Setup, dependencies, and cleanup
Call useEffect(setup, dependencies) at the top level of a component or custom Hook, not inside a loop or condition. React runs setup on the client after committing a render, meaning it has applied the render result. This is not a guarantee that the browser has already painted. Effects do not run during server rendering.
- No dependency argument: setup runs after every committed render of the component.
[]: setup runs on mount and does not repeat for ordinary updates. A remount starts it again.[roomId]: setup runs on mount and again whenroomIdchanges. React compares each dependency with its previous value usingObject.is.
Setup can return a cleanup function. When dependencies change, React runs the previous cleanup before running the new setup. It also runs cleanup when the component unmounts. In development, Strict Mode can run an extra setup–cleanup cycle before the normal setup to check that cleanup undoes what setup did.
Example: follow the selected chat room
import { useEffect } from 'react';
import { createConnection } from './chat.js';
function ChatRoom({ roomId }) {
useEffect(() => {
const connection = createConnection(roomId);
connection.connect();
return () => connection.disconnect();
}, [roomId]);
return <h2>Room {roomId}</h2>;
}
When roomId changes from A to B, the old cleanup still has access to the connection for A. React disconnects A before connecting to B. When the chat component is removed, React disconnects whichever room is active.
Common mistakes and interview questions
Treating dependencies as a schedule. The list describes the reactive values setup and cleanup read: props, state, and variables or functions declared in the component body. Leaving out roomId can leave the component connected to the wrong room. Conversely, an object created on every render has a new identity even if its fields match. If only the Effect needs that object, build it inside setup and list the values it uses instead.
Passing an async setup function. An async function returns a promise, but React expects setup to return either a cleanup function or nothing. Start asynchronous work inside setup. When fetching data, cancel obsolete requests or ignore their results so that a late response cannot overwrite newer data.
In interviews, expect to trace what happens on mount, a dependency change, and unmount. For an infinite loop, look for both halves of the feedback cycle: the Effect updates state, and the resulting render changes one of the Effect's dependencies.