3 回答

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

TA貢獻1906條經驗 獲得超3個贊
簡單的說就是purecomponents自己實現了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條經驗 獲得超8個贊
import React from 'react';
class A extends React.Component {
//當參數為復合數據組件時,比如對象、數組、Set、Map等, 以及它們的組件
}
class B extends React.PureComponent {
//當參數為基本數據時使用,比如String, Number, Boolean等。
}
添加回答
舉報