Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Performance and Concurrency Hooks

React useCallback hook

Reference for React useCallback: keep the same function identity between renders so memoized children and dependency arrays stay stable, with examples.

Every time a component renders, the functions declared inside it are created again. Usually that is harmless. It matters when the function is passed to a child wrapped in memo, or listed in the dependency array of an effect or another hook: a "new" function counts as a change.

useCallback returns the same function object across renders until one of its dependencies changes. It does not make the function faster, and it does not run it; it only keeps its identity stable.

JSX
const handleSave = useCallback(() => save(draft), [draft]);

Try it

Memoized child with and without useCallback

Re-render the parent: only "fresh handler" logs again, because its function prop changes every time.

A functional update keeps dependencies empty

Because the update uses setItems((current) => ...), the callback does not read items, so it needs no dependencies and AddButton renders only once, however much you type.

Syntax

JSX
const fn = useCallback(callback, dependencies);
// TypeScript: useCallback<T extends Function>(callback: T, deps: DependencyList): T
Name Description
callback The function to keep. It may take any arguments and return any value. React does not call it.
dependencies Array of every reactive value the function reads. Compared with Object.is.

Returns: the same callback object from the first render if the dependencies are unchanged; the new one from the current render otherwise.

Edge cases: during the first render you get the function you passed. Without a dependency array you get a new function every render.

Usage notes

  • It is useMemo for functions: useCallback(fn, deps) is the same as useMemo(() => fn, deps).
  • Useless without a consumer. If the receiver is not wrapped in memo and the function is not a dependency anywhere, caching changes nothing.
  • Prefer functional updates (setX(prev => ...)) to remove state from the dependency list.
  • Stale closures. A cached function captures the values of the render it was created in. Missing dependencies cause it to read old state; the lint rule reports them.
  • Need the latest value without new identity? Use a ref to hold it, or useEffectEvent inside an effect.
  • Custom hooks: if a hook returns functions, wrapping them in useCallback lets callers safely use them in dependency arrays.
  • Measure first. Wrapping every function adds noise; add it when a profiler shows needless child renders. React Compiler can do this automatically.