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等。
}
添加回答
举报
