React優(yōu)化子組件render的使用
在react中,父組件的重新render會引發(fā)子組件的重新render,但是一些情況下我們會覺得這樣做有些多余,比如:
- 父組件并未傳遞props給子組件
- 新傳遞的props渲染結(jié)果不變
class A extends React.Component { render() { console.log('render') return <div>這是A組件</div> } } class Main extends React.Component { render() { return ( <div> // 點擊button會讓A不斷調(diào)用render <button onClick={() => this.setState({ a: 1 })}>Main</button> <A /> </div> ) } }
為了解決這個問題,需要分為ES6類組件和函數(shù)式組件兩種:
類組件
使用shouldComponentUpdate
來對props和state進行判斷以此決定是否進行render
class A extends React.Component { shouldComponentUpdate(nextProps, nextState) { //兩次props對比 return nextProps.a === this.props.a ? false : true } render() { console.log('render') return <div>這是A組件</div> } } class Main extends React.Component { // ... render() { return ( <div> <button onClick={() => this.setState({ a: 1 })}>Main</button> <A a={this.state.a} /> </div> ) } }
通過返回false來跳過這次更新
使用React.PureComponent
,它與React.Component
區(qū)別在于它已經(jīng)內(nèi)置了shouldComponentUpdate
來對props和state進行淺對比,并跳過更新
//PureComponent class A extends React.PureComponent { render() { console.log('render') return <div>這是A組件</div> } } class Main extends React.Component { state = { a: 1 } render() { return ( <div> <button onClick={() => this.setState({ a: 1 })}>Main</button> <A a={this.state.a} /> </div> ) } }
函數(shù)組件
使用高階組件React.memo
來包裹函數(shù)式組件,它和類組件的PureComponent類似,也是對對props進行淺比較決定是否更新
const A = props => { console.log('render A') return <div>這是A組件</div> } // React.memo包裹A const B = React.memo(A) const Main = props => { const [a, setA] = useState(1) console.log('render Main') return ( <div> // 通過setA(a + 1)讓父組件重新render <button onClick={() => setA(a + 1)}>Main</button> // 一直傳入相同的props不會讓子組件重新render <B a={1} /> </div> ) }
它的第二個參數(shù)接受一個兩次props作為參數(shù)的函數(shù),返回true則禁止子組件更新
其他
上面提到的淺比較就是根據(jù)內(nèi)存地址判斷是否相同:
// extends React.Component class A extends React.Component { render() { console.log('render A') console.log(this.props) return <div>這是組件A</div> } } class Main extends React.Component { test = [1, 2, 3] render() { console.log('render Main') return ( <div> <button onClick={() => { // 父組件render this.setState({}) this.test.push(4) }} > Main </button> <A test={this.test} /> </div> ) } }
結(jié)果是:
使用React.component:
使用React.PureComponent:
使用React.component,點擊之后子組件重新render。改為React.PureComponent之后,點擊button子組件并不會render。也因此,PureComponent根據(jù)前后內(nèi)存地址判斷是否相等,所以向子組件傳遞函數(shù)作為props時,使用內(nèi)聯(lián)箭頭函數(shù)的形式將會導致子組件的重新render;所以可以用箭頭函數(shù)作為成員變量的形式再將函數(shù)引用作為props傳遞。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
react如何同步獲取useState的最新狀態(tài)值
這篇文章主要介紹了react如何同步獲取useState的最新狀態(tài)值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01React?Hook中的useState函數(shù)的詳細解析
Hook 就是 JavaScript 函數(shù),這個函數(shù)可以幫助你鉤入(hook into) React State以及生命周期等特性,這篇文章主要介紹了React?Hook?useState函數(shù)的詳細解析的相關(guān)資料,需要的朋友可以參考下2022-10-10詳解如何用webpack4從零開始構(gòu)建react開發(fā)環(huán)境
這篇文章主要介紹了詳解如何用webpack4從零開始構(gòu)建react開發(fā)環(huán)境,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01react中的forwardRef 和memo的區(qū)別解析
forwardRef和memo是React中用于性能優(yōu)化和組件復用的兩個高階函數(shù),本文給大家介紹react中的forwardRef 和memo的區(qū)別及適用場景,感興趣的朋友跟隨小編一起看看吧2023-10-10