React Memo
React.memo is a Higher Order Component (HOC) used to optimize the performance of function components. It memoizes the component's render output and skips re-rendering when the component's props have not changed, thereby improving performance. The following is React.memo detailed introduction and usage.
1. Basic usage
React.memo The basic usage of is to pass a function component as an argument to React.memo, and return a memoized component.
Example
import
React from
'react'
;
import
ReactDOM from
'react-dom'
;
const
MyComponent
=
React.
memo
(
(
props
)
=>
{
console.
log
(
'Rendering MyComponent'
)
;
return
<
div
{props.
text
}
div
;
}
)
;
const
App
=
(
)
=>
{
const
[
count
,
setCount
]
=
React.
useState
(
0
)
;
const
[
text
,
setText
]
=
React.
useState
(
'Hello, world!'
)
;
return
(
<
div
<button onClick
=
{
(
)
=>
setCount
(
count
1
)
}
Increment Count
button
<
MyComponent text
=
{
text
}
/>
div
)
;
}
;
const
root
=
ReactDOM.
createRoot
(
document.
getElementById
(
'root'
)
)
;
root.
render
(
<
App
/>
);
In this example, even if count state changes,MyComponent will not re-render, because its props has not changed.
2. Using a custom comparison function
By default,React.memo only compares the before and after props, and will not re-render if there is no change. You can control the re-rendering logic more precisely by passing a custom comparison function.
Signature of the custom comparison function:
function areEqual(prevProps, nextProps) {
// Returns true if equal, no need to re-render
// Returns false if not equal, requiring re-render
}
Example
import
React from
'react'
;
import
ReactDOM from
'react-dom'
;
const
MyComponent
=
React.
memo
(
(
props
)
=>
{
console.
log
(
'Rendering MyComponent'
)
;
return
<
div
{props.
text
}
div
;
}
,
(
prevProps
,
nextProps
)
=>
{
return
prevProps.
text
===
nextProps.
text
;
}
)
;
const
App
=
(
)
=>
{
const
[
count
,
setCount
]
=
React.
useState
(
0
)
;
const
[
text
,
setText
]
=
React.
useState
(
'Hello, world!'
)
;
return
(
<
div
<button onClick
=
{
(
)
=>
setCount
(
count
1
)
}
Increment Count
button
<
MyComponent text
=
{
text
}
/>
div
)
;
}
;
const
root
=
ReactDOM.
createRoot
(
document.
getElementById
(
'root'
)
)
;
root.
render
(
<
App
/>
);
In this example, the custom comparison function checks text whether has changed. If text has not changed,MyComponent will not re-render.
3. Practical Application Scenarios
React.memo is very suitable for the following scenarios:
- Static data display: the props received by the component
propschange rarely, but the component itself is complex and re-rendering is costly. - Performance optimization: in large lists or tables, each item is an independent component, using
React.memocan avoid unnecessary re-renders. - Avoiding deep equality checks: a custom comparison function can avoid deep equality checks, especially when
propscontains a large amount of data.
4. Notes
- Shallow comparison: by default,
React.memoperforms a shallow comparison, which means it only comparespropsfirst-level content; nested objects require a custom comparison function. - State and context:
React.memoonly focuses onpropschanges; changes in the component's internal state and context do not trigger re-rendering.
5. Comparison with useMemo and useCallback and
React.memo: used to memoize an entire component, optimizing component rendering.useMemo: used to memoize values or computation results inside a function component.useCallback: used to memoize callback functions inside a function component, avoiding unnecessary re-creation.
Example
import
React
,
{
useState
,
useMemo
,
useCallback
}
from
'react'
;
import
ReactDOM from
'react-dom'
;
const
ChildComponent
=
React.
memo
(
(
{
onClick
,
count
}
)
=>
{
console.
log
(
'Rendering ChildComponent'
)
;
return
<
button onClick
=
{
onClick
}
Count
:
{
count
}
button
;
}
)
;
const
App
=
(
)
=>
{
const
[
count
,
setCount
]
=
useState
(
0
)
;
const
increment
=
useCallback
(
(
)
=>
{
setCount
(
count
1
)
;
}
,
[
count
]
)
;
const
doubledCount
=
useMemo
(
(
)
=>
count
2
,
[
count
]
)
;
return
(
<
div
<
pDoubled Count
:
{
doubledCount
}
p
<
ChildComponent onClick
=
{
increment
}
count
=
{
count
}
/>
div
)
;
}
;
const
root
=
ReactDOM.
createRoot
(
document.
getElementById
(
'root'
)
)
;
root.
render
(
<
App
/>
);
In this example,useCallback and useMemo is used to optimize functions and computation results,React.memo is used to optimize the rendering of child components.
Summary
React.memo is a powerful tool that can effectively improve the performance of function components and avoid unnecessary re-renders. By properly using React.memo and custom comparison functions, you can significantly optimize application performance without affecting application logic.