React Newbies Guide: How to Use useMemo for Expensive Calculations and Complex State Management

Atanas Dimitrov
Bits and Pieces
Published in
3 min readJan 18, 2023

--

Photo by Kelly Sikkema on Unsplash

The useMemo React hook is a powerful tool that allows developers to optimize the performance of their applications by only re-rendering components when certain values have changed. This can greatly improve the overall performance and user experience of your application. In this article, we will explore some of the best use cases for useMemo and provide examples of how to implement it in your code.

Working with Expensive Calculations

One of the most common use cases for useMemo is when working with expensive calculations or operations that do not need to be performed on every render. For example, imagine you have a component that displays a large dataset and needs to calculate the average value of all the items. Instead of recalculating the average on every render, you can use useMemo to only recalculate and re-render the component when the dataset changes. Here is an example of how you might implement this:

const MyComponent = ({ data }) => {
const average = useMemo(() => {
let sum = 0;
for (let i = 0; i < data.length; i++) {
sum += data[i];
}
return sum / data.length;
}, [data]);

return <div>The average value is {average}</div>;
};

Working with Complex State or Props

Another great use case for useMemo is when working with complex state or props that are used by multiple components. By wrapping these values in useMemo, you can ensure that they are only recalculated when the relevant data changes, rather than on every render. This can be particularly useful when working with deeply nested components that pass data down through multiple levels. Here is an example of how you might use useMemo in this scenario:

const MyParentComponent = () => {
const [data, setData] = useState([]);
const filteredData = useMemo(() => {
return data.filter(item => item.status === 'active');
}, [data]);

return (
<div>
<MyChildComponent data={filteredData} />
<button onClick={() => setData([...data, { status: 'active' }])}>Add Data</button>
</div>
);
};

const MyChildComponent = ({ data }) => {
// This component will only re-render when the filteredData changes
return <div>{data.length} active items</div>;
};

In addition to these use cases, useMemo can also be used to optimize the performance of other hooks, such as useEffect and useCallback. For example, you can use useMemo to ensure that a callback passed to useEffect is only recreated when the relevant data changes, rather than on every render.

In conclusion, useMemo is a powerful tool that can help you optimize the performance of your React applications. By using it in the right situations, you can improve the overall user experience and make your code more efficient. Remember that useMemo is not a magic solution, it should be used when it is appropriate and necessary.

Before you’re gone

Please, consider showing some ❤️ and give this article a 👏🏻! Your support means a lot to me as a new writer.

Thank you! 🙇🏻

Build apps with reusable components like Lego

Bit’s open-source tool help 250,000+ devs to build apps with components.

Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.

Learn more

Split apps into components to make app development easier, and enjoy the best experience for the workflows you want:

Micro-Frontends

Design System

Code-Sharing and reuse

Monorepo

--

--

💻 Passionate software developer always looking to improve and learn. Follow me on my blogging journey! 🚀