React is a free and open-source front-end JavaScript library that can be used to build user interfaces at lightning speed. React is maintained by its community, and many developers are self-taught. Maybe you are one of them…trying to figure out whether you should use useMemo vs useCallback!
In this article, we’ll explore React useMemo vs useCallback and when to use useCallback and useMemo.
Understanding the Terms: useMemo vs useCallback vs useEffect
useCallback, useMemo, and useEffect are used to optimize the performance of React-based applications as components are rerendered.
useCallback
useCallback is a react hook that returns a memorized callback when passed a function and a list of dependencies that set the parameters. It’s useful when a component is passing a callback to its child component in order to prevent rendering. It only changes the callback when one of its dependencies is changed.
useMemo
useMemo is very similar to useCallback. It accepts a function and a list of dependencies, but the difference between useMemo and useCallback is that useMemo returns the memo-ized value returned by the passed function. It only recalculates the value when one of the dependencies changes. It’s very useful if you want to avoid expensive calculations on every render when the returned value isn’t changing.
useEffect
We won’t talk about useEffect in detail, but it’s worth noting. The useEffect accepts a function as a first parameter and a list of dependencies as a second parameter. When the dependencies change, it executes the passed function. The hook helps developers perform mutations, subscriptions, logging, and other side effects once all the components have been rendered.
Using useCallback vs useMemo in Practice
Creating efficient React code is vitally important as HTML pages grow more and more complex over time. Re-rendering large components are expensive and put a strain on the browser through a single-page application, which increases processing time and negatively impacts users. useMemo and useCallback can help developers circumvent non-code-related performance issues.
When you are wrapping a component with React.Memo()
, it signals the intent to resume code but doesn’t extend to functions passed as parameters. When a component is wrapped with useCallback, React saves a reference to the function. Passing this reference as a property to new components will shorten your rendering time.
useMemo and useCallback Difference
Despite seeming very similar, there are different use cases for each. You should wrap functions with useCallback when passing a function as a dependency to other hooks or wrapping a functional component in React.Memo()
that accepts your method as a property. You can use useMemo when you are working on functions where the inputs gradually change, where data values aren’t large enough to cause memory issues, or if the parameters are large enough so that the cost of comparison doesn’t outweigh the use of the wrapper.
useCallback works well in instances where the code would otherwise be recompiled with every call. Memorizing the results can decrease the cost of calling functions over and over again when the inputs change gradually over time.
When NOT to use useMemo and useCallback
As useful as they are, you shouldn’t use useCallback or useMemo for every function. Each call requires additional work to unravel your function call and make a decision about how to proceed. When you decide whether or not to use these functions, consider your performance levels first. You might want to start by improving your code first. Look for weak spots and try to refactor your JavaScript. (A frontend application performance monitoring tool might be handy at this point!). Think of useCallback and useMemo as a means of fine-tuning React. It can’t fix a badly written codebase, but if everything is already in place, it can take you from good to great.
Conclusion
Now that we’ve explored useMemo vs useCallback, you can see how these hooks can be extremely useful. Of course, if you need additional support and guidance, you can hire React developers from anywhere in the world with the right skills and experience to deliver your project using React!