The Startup
Published in

The Startup

React useMemo Hook to Optimize Your Code

Photo by Michael Dziedzic on Unsplash

Memoization is the technique of storing the results of computationally expensive operations. When the operation is invoked again with similar inputs, the stored/cached results are returned, rather than executing the function anew.

In React, memoization can be utilized to optimize calculations every time a component is updated and re-rendered.

UseMemo is a React hook that was made exactly for that.

const memoizedValue = React.useMemo(() => computeSomething(a,b),[a,b]);

useMemo receives two arguments.

The first is the expensive function to be executed, and the second is an array of dependencies. Whenever at least one of the dependencies updates between renders, useMemo is bound to call the function, and return a new updated version of memoizedValue.

On every new render, useMemo will compare its dependencies with the previous render. If no changes between any dependencies is observed, useMemo will return the previous, memoized value, and no expensive calculation will need to take place.

If an empty dependencies array [] is provided, useMemo will only execute the function once on the very first render of the component, while referring to the memoized value for the remainder of the component’s lifecycle.

Good Use Case For useMemo

useMemo is especially useful when you are dealing with potentially large lists of items.

For example, upon receiving a large list of items as a response from a third-party API, you could map the items to react components to display as some information cards.

The mapping function would be defined inline within a parent component.

const Grid = () => {
const [items, setItems] = React.useState(initialState);
const cards = items.map(item => {
return <Card key={item.id}/>
})
return (
<div>
{cards}
</div>
)
}

Every time the Grid component updates, the mapping functions is executed, calculating and returning an array of Card elements. If we are dealing with a large number of items this could prove inefficient.

We need to map the items to the card elements only on the Grids initial render, or whenever the list of items changes. Any other updates to the Grid component should not trigger the mapping operation.

With the useMemo hook we can ensure that we “remember” the value of the cards variable, and only execute the mapping function upon any changes to items.

const cards = React.useMemo(() => {items.map(item => {
return <Card key={item.id}/>})
}, [items]);

The only dependency is the state that is the array of items.

Don’t Overuse useMemo

It is best to first write your code without useMemo and only implement it upon revisiting the code. Implementing useMemo too often can harm the application since more memory needs to be allocated. And React is fast. It can usually cope with the execution of inline functions on every render.

The benefits of useMemomay be negligible and your time could be spent improving other areas of your code.

So what’s the point then you might ask?

Whenever you choose to optimize your code, there is a cost, but there is no guarantee of a benefit.

With useMemo there is a benefit when you are dealing with a large list of items, and the mapping operation is an expensive one you don’t want your code to invoke on every re-render.

Thank you for reading! And please let me know if I made any mistakes, I am always learning!

--

--

--

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +756K followers.

Recommended from Medium

D3.js the Mighty Setup

Get more done with value-added services from buzinessware

Agenda of What is RxJS First Order Observable Section

How to deploy a React app on AWS using the AWS CDK

My Favorite JavaScript Array Methods

Frequently Asked JavaScript Interview Questions and Answers

100 Days to Learning JavaScript

Why you shouldn’t use React / React Native

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sanjar Kairosh

Sanjar Kairosh

Full Stack Engineer. Enjoys reading. Writes about a mixture of topics to satisfy curiosity.

More from Medium

Do we still need Class-based components in React?

React Animations that Change Behavior when Props Change

How To Prepare For React.js Interviews in 2022 (Tips and Resources) Included!

Do You Need To Master JavaScript Before Learning React?