useCallback Cached Callback

Last updated: 2026-11-02

useCallback Cached Callback

useCallback caches a function reference: while the dependencies stay the same it returns the same function. When you hand such a callback to a memoized child component it prevents the parent from re-rendering the child for nothing, because a freshly created function reference always breaks the shallow comparison inside memo. Outside such a consumer the cache buys nothing at all, so it is worth reaching for only when a stable reference is what actually matters.

GroupEffects & Performance
KindHook
SinceReact 16.8

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

const handleClick = useCallback((id) => {
  removeItem(id)
}, [removeItem])   // list the dependencies you use

// equivalent to useMemo(() => handleClick, deps)

⚙️ Parameters

fnThe function to cache (an event handler, a callback passed to a child component, and so on).
depsThe dependency array: any change returns a new function, otherwise the previous reference is returned.

Returns:Returns the cached function reference: while the dependencies do not change, every render receives the same function.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

While you type in the input the console no longer prints "Rendered: Count +1" — the onClick reference is stable so the memoized child skips the re-render; clicking the button still increments the count.

💡 Key Points

💡 Tip useCallback(fn, deps) is just sugar for useMemo(() => fn, deps), so both cache a reference.
⚠️ Warning Without a memoized child component or a consumer that depends on the array, useCallback is pure overhead.
ℹ️ Info A missing dependency makes the callback close over stale data — the same dependency rules as useEffect; prefer functional state updates to shrink the list.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q When do I really need useCallback?
A When the callback is passed to a child wrapped in memo, or used as a dependency of useEffect or useMemo — a stable reference is only meaningful there.
Q What if useCallback(fn, []) reads stale state?
A Switch to a functional update such as setCount(c => c + 1) that depends on no external value; when you really need the old value, list it as a dependency.
Q How is it related to useMemo?
A useCallback is the function-shaped special case of useMemo: one caches a value, the other caches a function reference, and the mechanism underneath is the same.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏