DEV Community

Azrul Aziz
Azrul Aziz

Posted on

React: useCallback hooks simple explanation

useCallback(function, [dependecies])
Enter fullscreen mode Exit fullscreen mode

useCallback is one of the new features introduced in the react hooks API. Personally the name is quite confusing because callback is usually referred to asynchronous functions, the function that we invoke whenever a certain operation has finished. useCallback however is used for a different purpose.

So what does it do? According to React official docs, it Returns a memoized callback. In simpler words, it returns a cached version of a function. Basically this hook is mainly use for performance reason (memory-wise).
How does it do that? lets see an example:


const [height, setHeight] = useState(100)
const [age, setAge] = useState(3)

const handleSetHeight = () => setHeight(height + 10)
const handleSetAge = () => setAge(age + 1)
Enter fullscreen mode Exit fullscreen mode

We setup two useState hooks & declare two functions to handle state changes. This seems normal. The issue here is that everytime we invoke a function and re-render happens, a new instance of both of these functions will be created. Even if we invoke only one function, the instance of the other function will also be created. Imagine if there are more functions, how many instance have to be created during each re-render. Its redundant & causes performance issues.

useCallback helps in solving this issue. It will cache/memoized function that we pass to it. For example, lets rewrite both function above like this:

const handleSetHeight = useCallback(() => setHeight(height + 10), [height])
const handleSetAge = useCallback(() => setAge(age + 1), [age])
Enter fullscreen mode Exit fullscreen mode

By doing this, whenever we invoke a function and re-render happens, a new function instance will only be created for that particular function that is being invoked. No new instance is created for the other function. The second argument passed to useCallback, the dependecies array plays a major part. A new function instance will only be generated if any value of the variable inside that array changes between re-rendering. If nothing changes, useCallback will just return the cached version of the function instance.

Basically this is what useCallback hook is used for. To prevent unnecessary operation and improve performance.

Top comments (9)

Collapse
 
cathalmacdonnacha profile image
Cathal Mac Donnacha ๐Ÿš€

I think it's worth pointing out that using useCallback can actually make your app even slower and may damage performance: dmitripavlutin.com/dont-overuse-re...

So the function is still created on a re-render, it just returns the same function reference as before. Therefore I think this article may be a bit misleading to readers thinking they should always implement useCallback().

Collapse
 
rohimchou profile image
RohimChou

typo: it Returns a memoized memorized callback.

Collapse
 
nasht profile image
Nathan Hazout

No, memoized is the correct word: en.wikipedia.org/wiki/Memoization

Collapse
 
rohimchou profile image
RohimChou
  1. I am a dumb-dumb
  2. Thank you for this !!
Thread Thread
 
herman_pervin profile image
Pudina

Neither of you was correct. Memo is not a real word. It's short for memorandum, so it should be memorandumization. If you're going to spell it with memo, then at least include a hyphen: memo-ization.

Collapse
 
herman_pervin profile image
Pudina

Neither of you was correct. Memo is not a real word. It's short for memorandum, so it should be memorandumization. If you're going to spell it with memo, then at least include a hyphen: memo-ization.

Collapse
 
minbelapps profile image
minbelapps • Edited

Nice article but I missed the answer to this comment from React docs:

The array of dependencies is not passed as arguments to the function. Conceptually, though, thatโ€™s what they represent: every value referenced inside the function should also appear in the dependencies array.

So it's not clear is it a dependency array or is it an array for cache'ing control.

The thing is, that all those useCallbacks work with empty array `[]` and from this point, it's not the dependency array.

Collapse
 
julescsv profile image
Jules Njik

Clean and simple.
Good Job!

Collapse
 
hekkeynwa profile image
Henrik Juhl

Excellent explanation thanks