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
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