FrontendDeveloper.in

React question detail

How is useMemo different from useCallback?

The following table compares both useMemo and useCallback:

FeatureuseMemouseCallback
PurposeMemoizes the result of a computationMemoizes a function reference
ReturnsA value (e.g., result of a function)A function
UsageuseMemo(() => computeValue(), [deps])useCallback(() => doSomething(), [deps])
Primary Use CaseAvoid expensive recalculationsPrevent unnecessary re-creations of functions
Common ScenarioFiltering, sorting, calculating derived dataPassing callbacks to child components
When It's UsefulWhen the value is expensive to computeWhen referential equality matters (e.g., props)
Recomputed WhenDependencies changeDependencies change
Returned Value TypeAny (number, object, array, etc.)Always a function
OverheadSlight (evaluates a function and caches result)Slight (caches a function reference)
Back to all React questions
Get LinkedIn Premium at Rs 399