Skip to content

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

### Why does useCallback need deps??

Memoizes function reference. When deps change function recreated. Missing deps causes stale closure.

How to call API with latest props without recreating callback??

Use ref to store latest props or include deps.

What if function is used in multiple places??

Create separate callbacks for each usage or use ref pattern.

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