20

What is the main difference between useCallback and useMemo? And when to use useCallback of React Hooks?

Reza Ghorbani
  • 2,396
  • 2
  • 28
  • 33
  • Related - [What is the intention of using React's useCallback hook in place of useEffect?](https://stackoverflow.com/q/54371244/104380) – vsync Mar 30 '21 at 18:12

3 Answers3

9
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.

Kit Isaev
  • 680
  • 8
  • 19
4

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
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Atomicts
  • 2,146
  • 2
  • 16
  • 13
-1

useMemo helps prevent re-rendering unless dependencies to a function have changed while useCallback helps prevent rerendering unless a function has changed. I.e when a function is to be passed as an argument to another function, useCallback will only allow re-rendering after a different function is passed. Here is a link to a resource that might help explain further

https://btholt.github.io/complete-intro-to-react-v5/hooks-in-depth

matthias_h
  • 11,356
  • 9
  • 22
  • 40
  • 1
    This is not quite correct. `useCallback` doesn't check if function has changed (because it will be declared on each render and will always be different object), and it also accepts a dependency array.just like `useMemo`. – Kit Isaev Apr 20 '20 at 21:40