Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

The exhaustive-deps warning on useEffect, and how to clear it properly

ErrorReact Hook useEffect has a missing dependency: 'roomId'. Either include it or remove the dependency array

Short answer

The linter found a prop, state value or function the effect reads that is not in its dependency array. Do not delete the check. Change the code: move the value or function inside the effect, move constants outside the component, pass primitives, or read live values through an Effect Event.

  • Applies to React 19, eslint-plugin-react-hooks 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read which name the warning mentions, then find where it is declared. Props, state and anything defined in the component body count as reactive.

    npx eslint src --rule '{"react-hooks/exhaustive-deps": "warn"}'
  2. 2Match what you see, then jump to its fix

    1. 1React Hook useEffect has a missing dependency: 'fetchData'A function defined in the component is called inside the effectFix
    2. 2The warning names an object such as 'options', or the effect re-runs on every renderAn object or array created during render is read by the effectFix
    3. 3Adding the dependency would reconnect, refetch or restart something on every changeThe effect should read the latest value but not re-run when it changesFix
    4. 4An effect watches a flag like 'submitted' and then sends a requestThe code belongs in an event handler, not an effectFix

What does the error look like?

  • React Hook useEffect has a missing dependency: 'roomId'. Either include it or remove the dependency array
  • React Hook useEffect has a missing dependency: 'fetchData'
  • react-hooks/exhaustive-deps

Cause 1 of 4

A function defined in the component is called inside the effect

1Check

A function declared in the component body is a new value on every render, so the linter wants it in the array. Putting it there would re-run the effect on every render.

grep -n "useEffect" -A8 src/components/*.jsx

2Fix

If only the effect uses the function, declare it inside the effect. React's guide shows this as the first way to remove a dependency.

useEffect(() => {
  function load() {
    return fetch(`/api/items?room=${roomId}`).then(r => r.json());
  }
  load().then(setItems);
}, [roomId]);

3Verify

Run the linter again; the warning for that effect should be gone.

npx eslint src

Confirmed by react.dev

Did this fix it?

Cause 2 of 4

An object or array created during render is read by the effect

1Check

Objects and functions are new on every render, so they re-trigger effects even when their contents are the same.

grep -n "const options" -B2 -A6 src/components/*.jsx

2Fix

If the object never depends on props or state, move it above the component. If it does, read its primitive fields (strings, numbers) and depend on those.

const options = { serverUrl: "https://localhost:1234", roomId: "music" };

function ChatRoom() {
  useEffect(() => {
    const connection = createConnection(options);
    connection.connect();
    return () => connection.disconnect();
  }, []);
}

3Verify

The linter stops warning, and the effect runs once on mount in the browser.

npx eslint src

Confirmed by react.dev

Did this fix it?

Cause 3 of 4

The effect should read the latest value but not re-run when it changes

1Check

Typical cases are an onMessage callback prop or a flag such as isMuted that is only read when an event arrives.

grep -rn "useEffect" src/ | head

2Fix

Wrap the reading code in an Effect Event with useEffectEvent, call it from the effect, and leave it out of the array. Effect Events are not reactive.

const onMessage = useEffectEvent(msg => {
  setMessages(m => [...m, msg]);
  if (!isMuted) playSound();
});

useEffect(() => {
  const connection = createConnection();
  connection.connect();
  connection.on("message", msg => onMessage(msg));
  return () => connection.disconnect();
}, [roomId]);

3Verify

Toggle isMuted while connected. The connection stays open and the new value is used.

npx eslint src

Confirmed by react.dev

Did this fix it?

Cause 4 of 4

The code belongs in an event handler, not an effect

1Check

If the work happens because the user clicked something, it does not need an effect at all.

grep -rn "useEffect" -A6 src/ | grep -E "submitted|clicked"

2Fix

Move the request into the handler itself, and delete the effect and the flag.

function handleSubmit(e) {
  e.preventDefault();
  post("/api/register", { firstName, lastName });
}

3Verify

Submit the form once and check the network tab shows exactly one request.

npm start

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Treat the dependency warning like a compile error, as React's own guide advises.
  • Never silence it with eslint-disable-next-line react-hooks/exhaustive-deps; the guide shows it freezes an old value inside the effect.
  • Keep helper functions inside the effect or above the component when they do not use props or state.
  • Fetch with a cleanup flag so a late response from an older request is ignored.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.