Skip to content

How to Fix Event Handler with useCallback

DodaTech Updated 2026-06-26 1 min read

The Problem

In this quick-fix, you'll learn how to optimize event handlers with useCallback. This matters because unstable event handler references break child component memoization. In real-world applications, this pattern appears in click handlers form submit handlers drag handlers.

The Wrong Way

function App() {
  const [items, setItems] = useState([]);
  const handleDelete = (id) => setItems(p => p.filter(i => i.id !== id));
  return items.map(item => <ExpensiveItem key={item.id} onDelete={handleDelete} />);
}
handleDelete re-created every render ExpensiveItem never benefits from memo

The Right Way

function App() {
  const [items, setItems] = useState([]);
  const handleDelete = useCallback((id) => setItems(p => p.filter(i => i.id !== id)), []);
  return items.map(item => <ExpensiveItem key={item.id} onDelete={handleDelete} />);
}
handleDelete stable across renders ExpensiveItem memo works

Prevention

  • Wrap event handlers passed to memo children with useCallback
  • Functional state updates allow empty deps
  • Include state in deps when handler uses state

FAQ

### Should I wrap every event handler with useCallback??

No. Only when passed to memoized children. Otherwise adds overhead. Profile first.

Does useCallback prevent re-render for inline handlers??

No only stabilizes reference. Does not prevent parent re-render.

How to handle event with parameter??

Wrap in useCallback with parameter and pass directly to child.

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