paint-brush
The Difference Between useMemo and useCallback — An In-Depth Lookby@bluebirdyourdestiny
388 reads
388 reads

The Difference Between useMemo and useCallback — An In-Depth Look

by bluebirdyourdestinySeptember 24th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A useMemo is called using React source code, while a useCallback is called by the user. UseMemo can be used to memoize expensive functions to avoid having to call them on every render. A variable of usememo contains only the result of the return, which means everything in the body of the argument function is ignored. Using useMemO is a straightforward React hook that takes no parameters and does not require a re-render count. Using a hook that manages the side-effects in functional components is a gamble, but using it is very likely a mistake.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - The Difference Between useMemo and useCallback — An In-Depth Look
bluebirdyourdestiny HackerNoon profile picture


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.


As a standard construction of hooks, those two solutions are not so different. Like with useEffect, a hook that manages the side-effects in functional components, argument callback comes first, and then an array of dependencies.


Here are a few important questions:


  • If the purpose of useEffect is to update the component, why not use this hook to facilitate data storage and not only for memoization?
  • Why use two methods for one purpose?
  • What not return data or JSX from useCallback and then use it?
  • What is the difference between these methods?


An In-Depth Example


A useMemo is called using React source code, while a useCallback is called by the user. Here is an in-depth example:


function memoUsed() {
  const _  = useMemo(() => {
    return ‘insert JSX here’
  })

  return _
}

function callbackUsed() {
  const _  = useCallback(() => {
    return ‘insert JSX here’
  })

  return _()
}



As you can see, useMemo is easily imitated using a string returned in two slightly different ways. A variable of useMemo contains only the result of the return, which means everything in the body of the argument function is ignored. A variable of useCallback contains a function without execution. It simply uses a straight string, also known by the user as a return statement. This means that these two approaches actually have the same re-render count and deliver the same results.


useMemo is Not Recommended to Call Other Hooks


Calling side hooks in useMemo can cause state changes and force component updates. In comparison, React is already updating. In addition, React spots changes and runs updates immediately. It’s also important to note that useCallback is similar to React. Here is another in-depth example:


function memoUsed() {
  const [state, setState] = useState(null)

  const _  = useMemo(() => {
    // cause infinite re-render
    setState(‘value’)
    return ‘insert JSX here’
  })

  return _
}

function callbackUsed() {
  const [state, setState] = useState(null)

  const _  = useCallback(() => {
    // regular thing
    setState(‘value’)
    return ‘insert JSX here’
  })

  return _()
}



The Function Takes No Parameters in useMemo


useMemo is a straightforward React hook. In comparison, useCallback needs to process event data or custom arguments indicated by the user. Here is another in-depth example:


function memoUsed() {
  const _  = useMemo((arg1) => {
    // React ignores arguments
    return ‘insert JSX here’
  }, [])

  return _
}

function callbackUsed() {
  const _  = useCallback((what, where) => {
    // can be used inside functions
    return ‘insert ${what} ${where}’
  })

  return _(‘JSX’, ‘here’)
}


Dependency Arrays With useMemo


With useMemo, dependency arrays can be undefined, but doing so is a gamble. Without defined dependency arrays in both hooks, there is no memorization, and a re-render will be required. In TypeScript definitions, you can use the line below for useMemo. However, it does have some bugs, so don’t make it optional since that will likely lead to mistakes:


// allow undefined, but don't make it optional as that is very likely a mistake


The Big Picture


In most instances, useMemo can be replaced by useCallback, especially for data and methods memorization. Maybe the next version of React will eliminate the need for three separate hooks (yep, useDispatch can also store more complex state objects). We’ll have to wait and see.


Also Published At: https://procoders.tech/blog/difference-between-usememo-and-usecallback/