Using React.memo for Controlling Component Rendering

16 August 2022

Updated: 03 September 2023

React Top Level API

The React library contains some functions at it’s top level scope. Amongst these are the built-in hooks (like useState, useCallback, etc.) as well as some other functions for manipulating React Elements directly - which I’ve covered in a previous post on The React Top Level API

Component Rendering

By default, React will trigger a component render whenever there is a change to its state or props. React.memo allows us to take control of the props triggered render by giving us a way to look into the prop-change process

React.memo is a higher order component (HOC) that allows us to wrap a component and control whether or not it is updated/re-rendered by defining a function that tells react whether or not it’s props are different - and effectively whether this should trigger a new render

Doing the above is useful for complex components that don’t necessarily need to be rendered every time their props are changed

API Definition

The React Docs give us the following example for the React.memo HOC:

1
const MyComponent = (props) => {
2
/* render using props */
3
}
4
5
const areEqual = (prevProps, nextProps) => {
6
/*
7
return true if passing nextProps to render would return
8
the same result as passing prevProps to render,
9
otherwise return false
10
*/
11
}
12
13
const MyComponentMemo = React.memo(MyComponent, areEqual)

The component MyComponent will be rendered whenever props are changed, however, using React.memo lets us define a function called areEqual that we can can use to tell React.memo whether or not the new props would render a different result to the old props

We can then use MyComponentMemo in place of MyComponent to take control of when the component is rendered

Rendering On a Specific Type of Change

Say we have the specific component TimeDisplay which shows the time that’s being passed into it from App:

1
import './App.css'
2
import React, { useState, useEffect } from 'react'
3
4
interface TimeDisplayProps {
5
time: number
6
}
7
8
const TimeDisplay: React.FC<TimeDisplayProps> = ({ time }) => {
9
const display = new Date(time).toString()
10
11
return <h1>{display}</h1>
12
}
13
14
export default function App() {
15
const [time, setTime] = useState(Date.now())
16
17
useEffect(() => {
18
const handle = setInterval(() => {
19
setTime(Date.now())
20
}, 100)
21
22
return () => {
23
clearInterval(handle)
24
}
25
}, [])
26
27
return (
28
<main>
29
<TimeDisplay time={time} />
30
</main>
31
)
32
}

The TimeDisplay component in our case only displays time to the second, so any millisecond-level changes don’t matter to the component and so we can save on those renders by checking if the difference in time is similar to the previous render’s time

Let’s assume for our purpose that it’s acceptable for the time to be delayed by about 5 seconds, we then can define a function called areTimesWithinOneSecond which compares the next render’s props with the previous and returns if they are within 5 seconds of each other:

1
const areTimesWithinFiveSeconds = (
2
prev: TimeDisplayProps,
3
next: TimeDisplayProps
4
): boolean => {
5
const diff = next.time - prev.time
6
7
return diff < 5000
8
}

We can use the above function in a React.memo to define a version of the TimeDisplay component which will prevent unnecessary renders:

1
const TimeDisplayMemo = React.memo(TimeDisplay, areTimesWithinFiveSeconds)

And it can then be used as a drop-in replacement for the TimeDisplay component:

1
export default function App() {
2
const [time, setTime] = useState(Date.now())
3
4
useEffect(() => {
5
const handle = setInterval(() => {
6
setTime(Date.now())
7
}, 100)
8
9
return () => {
10
clearInterval(handle)
11
}
12
}, [])
13
14
return (
15
<main>
16
<TimeDisplayMemo time={time} />
17
</main>
18
)
19
}

Conclusion

From the above implementation we can see that it’s possible to delay rendering of a component using React.memo if the component doesn’t need to be re-rendered, hence improving performance by decreasing the number of renders react needs to carry out

REPL

The REPL with the example above can seen below:

References