Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Effect and Ref Hooks

React useEffect hook

Reference for React useEffect: synchronize a component with an external system, when effects run, cleanup, dependencies, StrictMode behavior and pitfalls.

useEffect runs code after React has rendered and updated the screen. It is the hook for synchronizing a component with something outside React: a timer, a network connection, a subscription, a non-React widget, or the browser's document title.

An effect is not an event handler and not a place to compute values. If something happens because the user clicked, put it in the click handler. If a value can be computed from props and state, compute it during render. Effects are for staying in sync with an external system while the component is on screen.

JSX
useEffect(() => {
  // setup: connect, subscribe, start
  return () => {
    // cleanup: disconnect, unsubscribe, stop
  };
}, [dependencies]);

Try it

Syncing the document title
Setup and cleanup around a timer

Press the buttons and watch the console: changing step stops the old timer before starting a new one, and hiding the component runs the cleanup one last time.

Syntax

JSX
useEffect(setup, dependencies?)
TypeScript
function useEffect(
  setup: () => void | (() => void),
  dependencies?: ReadonlyArray<unknown>
): void;
Name Description
setup The effect function. May return a cleanup function. It runs after the render is committed.
dependencies Optional array of reactive values the setup reads (props, state, values derived from them). Compared with Object.is. See the dependency array.

Returns: nothing (undefined).

Edge cases: the setup function must not be async (an async function returns a promise, which React would treat as a cleanup). Define an async function inside and call it. An effect never runs during server rendering.

Usage notes

  • Timing: React runs effects after the browser has had a chance to paint, so they do not block the screen. Use useLayoutEffect only when you must measure or change layout before paint.
  • Order: on an update, React runs the previous effect's cleanup first, then the new setup. On unmount, only cleanup runs. Child effects run before parent effects.
  • StrictMode: in development, React mounts, unmounts and mounts a component once more to check that cleanup mirrors setup. Seeing "start, stop, start" in the console is expected and does not happen in production.
  • Every reactive value used inside must be listed in the dependency array. Do not silence the linter; restructure instead (move values inside, or use useEffectEvent for values that should not retrigger).
  • You might not need an effect: transforming data for display, resetting state when a prop changes (use a key), and responding to a click do not belong here.
  • Fetching data: an effect can fetch, but it needs a cleanup flag to ignore stale responses. Frameworks and libraries handle caching and races better.
  • State updates inside effects cause an extra render. That is fine for syncing, but a sign of a missing derived value when you only copy props into state.