React memo減少重復渲染詳解
1. 概述
此方法是一個 React 頂層 Api 方法,給函數(shù)組件來減少重復渲染,類似于 PureComponent 和 shouldComponentUpdate 方法的集合體。
React.memo頂層Api方法,它可以用來減少子組件的重復渲染次數(shù),從而提升組件渲染性能。
React.memo它是一個只能在函數(shù)組件中使用的頂層Api方法。
當父組件發(fā)生改變時,默認情況下它的子孫組件也會重新渲染,當某些子組件不需要更新時,也會被強制更新,為了避免這種情況,我們可以使用 React.memo。
2. 使用
在不使用 React.memo 方法的情況下,子組件即使和父組件沒有任何關聯(lián),只要父組件更新,子組件也會跟著更新:
import React, { useState, memo } from 'react'
const Child = () => {
console.log('child')
return (
<div>
<h3>child組件</h3>
</div>
)
}
const App = () => {
let [count, setCount] = useState(100)
let [name, setName] = useState('張三')
return (
<div>
<h3>App -- {count}</h3>
<button onClick={() => {
setCount(v => v + 1)
}}>
++++
</button>
<Child />
</div>
)
}
export default App
上面的方案對性能的消耗很大,于是我們使用 React.memo 方法來解決這個問題,我們可以這樣寫:
import React, { useState, memo } from 'react'
const Child = memo(() => {
console.log('child')
return (
<div>
<h3>child組件</h3>
</div>
)
})
const App = () => {
let [count, setCount] = useState(100)
let [name, setName] = useState('張三')
return (
<div>
<h3>App -- {count}</h3>
<button onClick={() => {
setCount(v => v + 1)
}}>
++++
</button>
<Child />
</div>
)
}
export default App
我們可以用一個更直觀的例子來展示 React.memo 的作用:
import React, { useState, memo } from 'react'
// React.memo頂層Api方法,它可以用來減少子組件的重復渲染次數(shù),從而提升組件渲染性能
// React.memo它是一個只能在函數(shù)組件中使用的頂層Api方法
const Child = memo(({count}) => {
console.log('child')
return (
<div>
{/* 此時子組件只依賴于父組件中的 count,所以父組件中的count改變,
子組件就會重新渲染,而input框中的值改變對子組件沒有影響 */}
<h3>child組件 -- {count}</h3>
</div>
)
})
const App = () => {
let [count, setCount] = useState(100)
let [name, setName] = useState('張三')
return (
<div>
<h3>App -- {count}</h3>
<input type="text" value={name} onChange={e => setName(e.target.value)} />
<button onClick={() => {
setCount(v => v + 1)
}}>
++++
</button>
<Child count={count} />
</div>
)
}
export default App
React.memo 中還可以寫回調函數(shù):
import React, { useState, memo } from 'react'
// shouldComponentUpdate它必須要有一個返回值,true則表示繼續(xù)渲染,false停止渲染
// React.memo參數(shù)2返回值如果為true則表示停止渲染,false繼續(xù)渲染
const Child = memo(
({ count }) => {
console.log('child')
return (
<div>
<h3>child組件 -- {count}</h3>
</div>
)
},
// prevProps 舊的props數(shù)據(jù) object
// nextProps 新的props數(shù)組 object
// 可以比較兩者的不同,來決定是否重新渲染
// 參數(shù)2寫的回調函數(shù),一般情況下都在props傳過來的數(shù)據(jù)為引用類型,才需要手動來判斷,如果是基本類型則不需要寫參數(shù)2,來手動判斷。
(prevProps, nextProps) => {
// true/false
// return false
// console.log(prevProps, nextProps)
return prevProps.count === nextProps.count
}
)
const App = () => {
let [count, setCount] = useState(100)
let [name, setName] = useState('張三')
return (
<div>
<h3>App -- {count}</h3>
<input type="text" value={name} onChange={e => setName(e.target.value)} />
<button onClick={() => {
setCount(v => v + 1)
}}>
++++
</button>
<Child count={count} />
</div>
)
}
export default App
上文說到 React.memo 中參數(shù)2寫的回調函數(shù),一般情況下都在 props 傳過來的數(shù)據(jù)為引用類型,才需要手動來判斷,如果是基本類型則不需要寫參數(shù)2,來手動判斷。所以我們下面來看一個傳值為引用類型的例子:
import React, { useState, memo } from 'react'
import _ from 'lodash'
// shouldComponentUpdate它必須要有一個返回值,true則表示繼續(xù)渲染,false停止渲染
// React.memo參數(shù)2返回值如果為true則表示停止渲染,false繼續(xù)渲染
const Child = memo(
({ count }) => {
console.log('child')
return (
<div>
<h3>child組件 -- {count.n}</h3>
</div>
)
},
// 使用lodash庫來完成對象的值的比較,從而用來完成減少組件的無用的重復渲染
(prevProps, nextProps) => _.isEqual(prevProps, nextProps)
)
const App = () => {
// let [count, setCount] = useState(100)
let [count, setCount] = useState({ n: 100 })
let [name, setName] = useState('張三')
return (
<div>
{/* <h3>App -- {count}</h3> */}
<h3>App -- {count.n}</h3>
<input type="text" value={name} onChange={e => setName(e.target.value)} />
<button
onClick={() => {
setCount({ n: Date.now() })
}}
>
++++
</button>
<Child count={count} />
</div>
)
}
export default App
注意:不使用參數(shù)2的時候,假設對象中屬性的值沒變,子組件在這種情況下也一定會重新渲染,這是因為對象的引用地址變了。
到此這篇關于React memo減少重復渲染詳解的文章就介紹到這了,更多相關React memo內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
React Hook - 自定義Hook的基本使用和案例講解
自定義Hook本質上只是一種函數(shù)代碼邏輯的抽取,嚴格意義上來說,它本身并不算React的特性,這篇文章主要介紹了React類組件和函數(shù)組件對比-Hooks的介紹及初體驗,需要的朋友可以參考下2022-11-11
React在Dva項目中創(chuàng)建并引用頁面局部組件的方式
這篇文章主要介紹了React在Dva項目中創(chuàng)建并引用頁面局部組件的方式,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
react?+?vite?+?ts項目中優(yōu)雅使用.svg文件
這篇文章主要為大家介紹了react?+?vite?+?ts項目中優(yōu)雅使用.svg文件,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
react?hooks?計數(shù)器實現(xiàn)代碼
這篇文章主要介紹了react?hooks計數(shù)器實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
Next.js搭建Monorepo組件庫文檔實現(xiàn)詳解
這篇文章主要為大家介紹了Next.js搭建Monorepo組件庫文檔,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11

