useCallback
useCallback is a React Hook that returns a memoized version of a function, keeping the same function reference between renders unless its dependencies change. It is mainly used to prevent unnecessary re-renders of child components.
React
In React, a new function is created on every render. Normally that is fine, but if you pass a function as a prop to a child that is optimized to skip re-renders, a brand-new function each time defeats that optimization. useCallback keeps the function stable.
It is closely related to useMemo: useCallback(fn, deps) is essentially useMemo that returns the function itself rather than the result of calling it. Reach for it only when function identity actually matters for performance.
Syntax
const memoized = useCallback(() => doSomething(a), [a]);Examples
JSX — handleClick keeps the same identity across renders because its dependency array is empty, while still incrementing the count.
import { useState, useCallback } from "react";
function Parent() {
const [count, setCount] = useState(0);
const handleClick = useCallback(() => {
setCount(c => c + 1);
}, []);
return <button onClick={handleClick}>{count}</button>;
}