3 回答

TA貢獻1810條經(jīng)驗 獲得超4個贊
PureComponent的本質(zhì)是幫你寫了一個shouldComponentUpdate,做一層淺比較,實現(xiàn)渲染時優(yōu)化。
如果是簡單類型的比較,就不用自己寫shouldComponentUpdate了。
需要注意的是:PureComponent和shouldComponentUpdate不能共存

TA貢獻1906條經(jīng)驗 獲得超3個贊
簡單的說就是purecomponents自己實現(xiàn)了shouldComponentUpdate 類似下面
function shouldComponentUpdate(nextProps, nextState){
const cProps = this.props, cState = this.state;
for(let key in nextProps){
if(cProps[key] !== nextProps[key]) return true
}
for(let key in nextState){
if(cState[key] !== nextState[key]) return true
}
return false;
}

TA貢獻1858條經(jīng)驗 獲得超8個贊
import React from 'react';
class A extends React.Component {
//當參數(shù)為復(fù)合數(shù)據(jù)組件時,比如對象、數(shù)組、Set、Map等, 以及它們的組件
}
class B extends React.PureComponent {
//當參數(shù)為基本數(shù)據(jù)時使用,比如String, Number, Boolean等。
}
添加回答
舉報