Skip to content

How to Fix useEffect Dependency Array Warnings

DodaTech Updated 2026-06-26 1 min read

The Problem

In this quick-fix, you'll learn how to specify correct dependency array to avoid ESLint warnings. This matters because incorrect deps cause stale closures infinite loops missing updates. In real-world applications, this pattern appears in data fetching based on props effects using state values.

The Wrong Way

function UserProfile({ userId }) {
  const [user, setUser] = useState(null);
  useEffect(() => { fetch('/api/users/'+userId).then(r=>r.json()).then(setUser); }, []);
  return <div>{user?.name}</div>;
}
Fetches only once with initial userId -- stale if userId changes

The Right Way

function UserProfile({ userId }) {
  const [user, setUser] = useState(null);
  useEffect(() => {
    const c = new AbortController();
    fetch('/api/users/'+userId, {signal:c.signal}).then(r=>r.json()).then(setUser);
    return () => c.abort();
  }, [userId]);
  return <div>{user?.name}</div>;
}
Fetches when userId changes -- aborts previous request

Prevention

  • Include all reactive values in deps array
  • Use exhaustive-deps ESLint rule
  • Use functional updates to reduce deps
  • Use refs for values that shouldnt trigger re-runs

FAQ

### What does exhaustive-deps rule check??

Every reactive value used inside useEffect must be in deps array. Missing value means effect uses stale reference.

Why does adding suggested deps break my code??

If adding dep causes infinite loops your effect has side effects that shouldnt be there. Restructure code dont suppress the rule.

Can I disable exhaustive-deps??

Not recommended. The rule prevents real bugs. If must suppress use eslint-disable comment with clear explanation.

This guide is brought to you by DodaTech -- built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro