How to Fix useCallback Dependency List
DodaTech
Updated 2026-06-26
1 min read
The Problem
In this quick-fix, you'll learn how to correctly specify dependencies in useCallback. This matters because incorrect deps cause stale closures or unnecessary recreation. In real-world applications, this pattern appears in event handlers for memo children callbacks passed to context.
The Wrong Way
function ProductList({ category }) {
const fetchProducts = useCallback(() => {
return fetch("/api/products?cat=" + category);
}, []);
return <Search onSearch={fetchProducts} />;
}
fetchProducts always uses initial category -- stale
The Right Way
function ProductList({ category }) {
const fetchProducts = useCallback(() => {
return fetch("/api/products?cat=" + category);
}, [category]);
return <Search onSearch={fetchProducts} />;
}
fetchProducts updates when category changes
Prevention
- Include all reactive values in useCallback deps
- Use exhaustive-deps rule
- Use ref for values that shouldnt trigger recreation
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