useCallback Purpose
Question
What does useCallback do and when to use it?
Hint
useCallback prevents new function identity each render.
Explanation
useCallback(fn, deps) returns a memoized version of fn that only changes when deps change. This keeps the function reference stable across renders. Use it when passing callbacks to memoized child components (React.memo) or as dependencies to hooks (useEffect, useMemo). Without useCallback, a new function is created every render, breaking memoization of children. Do not wrap every function — only those causing performance issues or used as dependencies.
Related Resources
Related Lessons
- Memoization
Learn memoization.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.