MediumConcept

How does useEffect work in React and what is the dependency array?

Technology:
React
Experience:
Junior,
Mid-level

Quick answer

useEffect runs a function after the component renders; the dependency array tells React when to re-run it: with no array it runs after every render, with an empty array only after the first, and with values only when those values change.

Detailed explanation

Effects are for synchronising your component with something outside React, such as subscriptions, timers, network requests or the document title. The function you return from an effect is the cleanup, and React runs it before the next run of the effect and when the component unmounts.

Every value from the component that the effect uses should be listed in the dependency array, otherwise the effect works with stale values. In development with Strict Mode, React deliberately mounts, unmounts and re-mounts components once to help you find missing cleanup. Do not use effects for things you can calculate during render or handle in an event handler.

Example

jsx
useEffect(() => {
  const controller = new AbortController();
  fetch("/api/users/" + userId, { signal: controller.signal })
    .then(r => r.json())
    .then(setUser)
    .catch(err => { if (err.name !== "AbortError") setError(err); });

  return () => controller.abort();   // cleanup when userId changes or unmounts
}, [userId]);

How to explain it in an interview

useEffect runs after React commits the render, so it is the place to synchronise with things outside React such as subscriptions, timers or network requests. The dependency array controls when it re-runs, and the function it returns is the cleanup that runs before the next effect and on unmount.

Key points

  • No array: every render. [] : once. [a, b]: when a or b change
  • Return a function to clean up
  • Do not use effects for derived data or user-triggered actions

Common mistakes

  • Leaving out dependencies to stop re-runs, which leads to stale values. Fix the cause instead of silencing the lint rule.
  • Using an effect to compute derived data that could be calculated during render.
  • Forgetting cleanup for subscriptions or timers, which causes leaks and duplicate listeners, especially under Strict Mode.

Follow-up questions

  • Why does my useEffect run twice in development?
  • What causes an infinite loop in useEffect?
  • ReactEasy

    What are React hooks and what are the rules of hooks?

    Hooks are functions such as useState and useEffect that let function components use state, lifecycle behaviour and other React features; they must be called at the top level of a component or custom hook, never inside loops, conditions or nested functions.

    Junior 路 Mid-level 路 React Hooks
  • ReactMedium

    What is the difference between useRef and useState in React?

    Updating state triggers a re-render, while changing a ref's .current value does not; use state for data shown on screen and refs for DOM access or values that must persist without causing renders.

    Mid-level 路 Senior 路 React Hooks
  • ReactEasy

    What is the virtual DOM in React and how does it work?

    The virtual DOM is a lightweight JavaScript copy of the UI that React compares with the previous version after each update, so it only changes the parts of the real DOM that actually differ.

    Intern 路 Junior 路 Performance
  • ReactEasy

    What is the difference between props and state in React?

    Props are read-only inputs passed from a parent component, while state is data a component owns and can change over time, which triggers a re-render when updated.

    Intern 路 Junior 路 State Management
  • ReactEasy

    Why do we need keys in React lists?

    Keys give each list item a stable identity so React can match items between renders and correctly add, remove or reorder them without recreating or mixing up their state.

    Junior 路 Mid-level 路 Performance