useEffect runs side effects after the component renders. Effects are scheduled to run after browser paint, without blocking the UI.
Signature
Parameters
() => void | (() => void)
required
A function containing the imperative, effectful code. Can optionally return a cleanup function that will be called before the effect runs again or when the component unmounts.
ReadonlyArray<unknown>
An array of dependencies. The effect will only re-run if one of the values in this array has changed (compared using
===). If omitted, the effect runs after every render.Returns
void
Basic Usage
Effect with Cleanup
Fetching Data
Event Listeners
Run Once on Mount
Multiple Effects
Effects run after the browser has painted, making them suitable for most side effects. For effects that must run synchronously before paint (like measuring DOM layout), use
useLayoutEffect instead.Always include all values from the component scope that are used inside the effect in the dependency array. This ensures your effect always has the latest values.
If your effect returns a cleanup function, it will be called before running the effect again (when dependencies change) and when the component unmounts.