React

Using React.memo() in React 16.6

Advertisements

React 16.6 was released a couple of days and and it brings a couple of new features.

One of these is React.lazy() which is something that requires a separate post in itself along with React Suspense.

The other one, which we will talk about in this post is React.memo().

What is React.memo()?

It is similar in functionality to PureComponent which helps us have a finer control over the rendering of our component. The only difference is that React.memo() brings that to functional components whereas PureComponent was applicable to classes only.

The default behavior of a component declared using React.memo is that it renders only if the props have changed. It does a shallow comparison of the props to check that but there is an option to override that as well.

Why is it called memo?

Memoization is a technique in which we cache the result of a function call if we know that it is going to be excessively used. This is generally applied to expensive functional calls but since we are doing something similar with respect to memoization when deciding whether to render the component or not, it makes sense to call it React.memo().  Here’s Dan Abramov giving a similar logic on twitter:

How do I use it?

const reactMemoComponent = React.memo((props) => { 
/* renders only if props changed */ 
});
JavaScript

This helps boost the performance by avoiding re-rendering if not needed.

Since React.memo is a higher order component, you can wrap existing functional components using it as well.

const existingComponent= (props) => { /* component definition */ };
const memoizedComponent = React.memo(existingComponent);
JavaScript

If you want to use your own comparison function instead of the default shallow comparison  function, React.memo provides that functionality as well. You can pass in your own comparison function as a second argument to the method to do so. This method needs to return a boolean value to conditionally evaluate if the component should re-render or not.

const customReactMemoComponent = React.memo((props) => { /* definition */ }, (prevProps, newProps) => {
// custom comparison logic
});
JavaScript

Demo

I have created a very small demo for this using create-react-app. There is a greeting component which displays a welcome text message. The App component’s state is being changed every one second, forcing it to re-render every second. As a result the Greeting component also gets re-rendered every second since it is present in the App component.

On creating a memoized component using React.memo() from the Greeting component, it gets rendered only once even though the App component is still being rendered every second.

This can be verified from the console logs, which show the log only once for the Memo component and multiple ones for the Greeting component:

And that is all you need to know about the new React.memo. Go ahead and start creating functional pure components! No need for classes anymore

Let us know your thoughts in the comments section and if you think this is a valuable addition to the react API.

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria

Recent Posts

Remapping keyboard keys to avoid Carpal Tunnel

I am terrible at optimizing my keyboard layout for anything. But off lately, my little…

1 month ago

Fixing cookies are blocked for a website with shields down on Brave

I recently switched completely to the Brave browser and have set ad blocking to aggressive…

5 months ago

Generating a QR code using Node.js

I was preparing a slide deck for a hackathon and decided to put in a…

6 months ago

How to clear the global npx cache

I have been using npx a lot lately, especially whenever I want to use a…

6 months ago

Copy/Pasting output from the terminal

Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…

7 months ago

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

8 months ago
Advertisements