The useCallback Hook in React
Caching callback function definitions with useCallback: preventing unnecessary re-renders of memoized child components and custom hook dependencies.
The useCallback Hook in React
useCallback is a React hook that caches a function definition between renders instead of recreating a new function instance on every render cycle.
1. Why Functions Recreate on Render
Every time a component re-renders, all function declarations inside it are recreated in memory. While creating small functions is fast, passing newly instantiated functions to memoized child components (React.memo) triggers unnecessary child re-renders.
import { useState, useCallback, memo } from "react";
// Memoized child component only re-renders if props change:
const ExpensiveList = memo(function ExpensiveList({ onItemClick }: { onItemClick: (id: string) => void }) {
console.log("Rendering ExpensiveList...");
return <div>List Content</div>;
});
export function Dashboard() {
const [count, setCount] = useState(0);
// β
Stable function reference across re-renders:
const handleClick = useCallback((id: string) => {
console.log("Clicked item:", id);
}, []); // Empty deps: reference never changes
return (
<div>
<button onClick={() => setCount(c => c + 1)}>Count: {count}</button>
<ExpensiveList onItemClick={handleClick} />
</div>
);
}2. useCallback vs. useMemo
useMemo(() => fn, deps)returns the value produced by calling the function.useCallback(fn, deps)returns the function definition itself.useCallback(fn, deps)is equivalent touseMemo(() => fn, deps).
Related Topics
The useMemo Hook in React
Optimizing expensive calculations with useMemo: caching computed values, referential equality, and when not to over-optimize.
Custom Hooks in React
Building custom React hooks: extracting reusable stateful logic, naming conventions (use*), returning values/tuples, and real-world examples (useDebounce, useLocalStorage).