Skip to content
the poor coder

react usememo vs usecallback

Poor Man
Published
Reading time
2 min read
On this page

React useMemo vs useCallback#

As a React developer, I have come across the terms useMemo and useCallback quite often. Both of these hooks are used to optimize the performance of our React components by avoiding unnecessary re-renders. However, they have different use cases and should be used in different scenarios.

useMemo#

useMemo is a hook that allows us to memoize the result of a function call and cache it until one of its dependencies changes. This means that the function will only be re-executed when its dependencies change, otherwise, the cached value will be returned. This can improve the performance of our components as we can avoid re-executing heavy computations every time our component re-renders.

Let's look at an example. Say we have a function that calculates the factorial of a given number:

Text
const factorial = (n) => {
  if (n === 0 || n === 1) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}

If we were to use this function in our component without memoizing it, it would be re-executed every time our component re-renders, even if the input value hasn't changed. We can use useMemo to cache the result of this function and avoid unnecessary re-executions:

Text
const result = useMemo(() => {
  return factorial(inputValue);
}, [inputValue]);

Here, the useMemo hook will cache the result of the factorial function until the inputValue changes. When the inputValue changes, the factorial function will be re-executed and the new result will be cached.

useCallback#

useCallback, on the other hand, is a hook that memoizes a function and returns a memoized version of it. This can be useful when passing functions down to child components as props, as it ensures that the child component doesn't re-render unnecessarily when the function hasn't changed.

Let's say we have a component that renders a list of items and we pass down a function that handles the click event of each item:

Text
const handleItemClick = (item) => {
  console.log(item);
}

return (
  <ItemList items={items} onItemClick={handleItemClick} />
);

If we were to use the handleItemClick function directly, it would be recreated every time our component re-renders. This means that if we pass it down to our child component as a prop, the child component will re-render every time our parent component re-renders, even if the handleItemClick function hasn't changed. We can use useCallback to memoize the function and avoid this unnecessary re-rendering:

Text
const handleItemClick = useCallback((item) => {
  console.log(item);
}, []);

return (
  <ItemList items={items} onItemClick={handleItemClick} />
);

Here, we use useCallback to memoize the handleItemClick function and pass it down to our child component as a prop. The empty dependency array [] tells React that this function doesn't depend on any external variables and should only be created once.

Conclusion#

In summary, useMemo and useCallback are two useful hooks that can help us optimize the performance of our React components. useMemo is used to memoize the result of a function call and cache it until one of its dependencies changes, while useCallback is used to memoize functions and avoid unnecessary re-renders when passed down as props to child components. Understanding the differences between these two hooks and their use cases can help us write more efficient and performant React components.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC