Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

useCallback vs. useMemo and when to use them

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

like image 794
Reza Ghorbani Avatar asked May 02 '19 12:05

Reza Ghorbani


People also ask

When should we use useCallback and useMemo?

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.

When should I use useMemo?

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.

In which situation would you use useMemo () in React?

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.

Is useCallback FN same as useMemo (( => FN )?

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.


2 Answers

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.

like image 50
Kit Isaev Avatar answered Oct 26 '22 19:10

Kit Isaev


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
like image 43
Atomicts Avatar answered Oct 26 '22 19:10

Atomicts