Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

React Memo

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:

4. Notes

5. Comparison with useMemo and useCallback and

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

<

p

Doubled 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.