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