DEV Community

React useMemo Guides

useMemo returns a memoized value so that it does not need to be recalculated.

This is a collection of top and trending guides written by the community on subjects related to React useMemo concepts. For all things React, check out the React tag! Please contribute more posts like this to help your fellow developer in need.

Introduction to React.memo, useMemo and useCallback

When I start to write this blog, I ask myself whether I should only talk about the differences between useMemo and useCallback because they are React Hooks while React.memo is not. Eventually, I decided to include React.memo as well since on the one hand the word memo in both terminologies might sound a bit confusing for people. On the other hand, it's all about React optimization 😁


When to useCallback and useMemo in our React projects?

Today we will talk about when to useCallback and useMemo React hooks in our projects.


useCallback and useMemo hooks. What are they and how do you use them?

In the above section, we looked at what useCallback hook is and how to use it. It is now time to look at another hook closely related to useCallback, useMemo. The function signature of useMemo is similar to that of useCallback. The difference is that useMemo returns a memoized value while useCallback returns a memoized callback function. Both take a function as first argument and an array of dependencies as second argument.

useMemo hook is used for performing operations which are computationally expensive (Takes a long time to perform or a lot of computer memory). If the dependencies do not change, memoized value will always be returned. If you want the computation to be performed once then pass an empty array.


React useMemo and useCallback

In this article we will look at two react hooks useMemo and useCallback, which will bring us to look at when React renders components.

We will look only at function-based components and not class-based components. (I am not familiar with such class-based components).


You can't create a constant using useMemo in React

There are 3 possible options to create a constant in React: useState, useMemo and useRef. We're going to see which one is the best way to create real constants (not constant-like values).


Understanding useMemo in react

The useMemo hook is a hook that kinda has a memory. It memoizes the value returned by a function. So to understand this useMemo better we'll take a look at a JavaScript technique called memoization.


React - Combining useMemo and Switch

Here, Form gets executed only when selectedUser changes, so the expression need not be evaluated every time. This method allows us to optimise the expression evaluation as switch needs to execute it every time but useMemo doesn't.


How to Optimise React with useMemo and React.memo

There comes a time when we have to worry about more than just making sure our applications work, but that they work optimally. When using react, we have certain tools at our disposal to make sure our applications are optimised. In this article, I will demonstrate how to achieve this using React.memo and the useMemo hook.


The React useMemo Hook Made Simple

Performance is important, especially in large-scale application. The React useMemo hook is one tool you can use to improve the performance of your React apps. This tutorial will help you understand what useMemo hook is and how it works. It will also show you how to use it.


Demystifying useRef and useMemo in React

As we can see, the WelcomeCard is rendered again everytime we click on the counter, in fact it is rendered more times than the counter. This is happening even though there is nothing that is dynamic inside the card itself, thus the re-rendering is a waste. Now imagine a website with lots of static data components, which re-render everytime states or states of parents change. To avoid this we can use useMemo as follows:-


React performance optimization with useMemo & memo

In the example above, we used useMemo inside ItemComponent1. Thus anything returns the component will be initialized only once. It won't be re-rendered at the time of parent re-rendering.


What is useMemo hook in React..

Hooks are relatively new feature of react, they were introduced in React 16.8, they help us make use of state and react features from a function based component, for example useState, useEffect, useHistory and many others. Hooks help us to avoid the complexities of classes and make our code simpler to read and understand.

In this article, we will go about the useMemo hook in React. In addition to understanding the functionality of this hook, it is also important to note that this hook has its importance from react interview perspective since many interviewers like to ask questions related to useMemo hook.


How to useMemo and useCallback: you can remove most of them

If you’re not completely new to React, you’re probably already at least familiar with useMemo and useCallback hooks. And if you work on a medium to large-scale application, chances are you can describe some parts of your app as an “incomprehensible chain of useMemo and useCallbacks that is impossible to read and debug". Those hooks somehow have the ability to just spread around the code uncontrollably, until they just completely take over and you find yourself writing them just because they are everywhere and everyone around you is writing them.


React.memo and useMemo - What's the Difference?

Tip 2: Because of the use of shallow comparison, be careful about passing in non-primitive props like an object, array or function. Do not pass these in directly as props, but instead, instantiate and assign these to variables which are then passed in. For functions, the useCallback hook is handy for ensuring the same instance of the function is passed in as props, thus allowing the shallow prop comparison to result in true. For objects and arrays, the useMemo hook might be helpful, which I'll go through in the next section.


[ASK] Use useMemo for conditional render

Hi, I quite often use this syntax to render my components conditionally and I've got a lot of review about this to not use useMemo to render markups. Is it wrong doing it this way?


When to use useMemo and useCallback in React?

These two React hooks, useMemo and useCallback are useful when you’re dealing with expensive operations (that is, operations that are very complex and take a lot of time and resources, like CPU.)


Happy React useMemo coding!