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:
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.
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 _()
}
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’)
}
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
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/