What is the main difference between useCallback
and useMemo
?
And when to use useCallback
of React Hooks?
And useMemo gives you referential equality between renders for values. useCallback and useMemo both expect a function and an array of dependencies. The difference is that useCallback returns its function when the dependencies change while useMemo calls its function and returns the result.
You want to use useMemo to save yourself from rerunning an expensive calculation to generate a new value, and you want to use useCallback to store an existing value.
Performance. The useMemo Hook can be used to keep expensive, resource intensive functions from needlessly running. In this example, we have an expensive function that runs on every render. When changing the count or adding a todo, you will notice a delay in execution.
An In-Depth Look at the Difference Between useMemo and useCallback. UseCallback is used to optimize the rendering behavior of your React function components, while useMemo is used to memoize expensive functions to avoid having to call them on every render.
useMemo(() => (bar) => foo + bar, [foo]);
Equivalent code with useCallback:
useCallback((bar) => foo + bar, [foo]);
Use callback is just a shorthand variation of useMemo
to use with functions.
Here is why it exists:
When you use useMemo
,the value usually changes when one of its dependencies change. For example:
const fullName = useMemo(() => firstName + lastName, [firstName, lastName]);
If firstName
or lastName
change, fullName
will also change. On the other hand, functions themselves usually don't need to be recomputed, and their dependencies are mostly closure values that may change.
const getFullName = useCallback(() => firstName + lastName, [firstName, lastName]);
Here, when firstName
or lastName
change, we don't need to have a completely different function with a different body, but we do need to have a new instance of the same function so that it has up to date values in closure (dependencies). So React team just added it for convenience as it is a common use case, and the () => () =>
syntax is somewhat ugly.
useMemo
and useCallback
both use something called memoization which you can think of it like the hooks are remembering something.
The differences:
useMemo
will memoize/remember the value that is returned from the function you pass into it until the dependancies change.
const num = 10
const result = useMemo(() => num + num, [num])
// result is now equal to 20
useCallback
will memoize/remember the actual function you pass into it until the dependancies change which gives you something called referential equality.
const num = 10
const result = useCallback(() => num + num, [num])
// result is now equal to () => num + num.
// result === result between renders.
Referential equality:
() => {} === () => {} // This is false
const a = () => {}
a === a // This is true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With