问题描述
使用 React memo 的等效代码是什么?
shouldComponentUpdate(nextProps) {
if (this.props.addCourse !== nextProps.addCourse || this.props.level !== nextProps.level ) {
return true;
} else {
return false;
}
}
解决方法
function isNotOwnPropertyOfBoundObject(key) {
return !this.hasOwnProperty(key);
}
function getOmittedPropertiesFromBoundKeyList(item) {
return this.filter(isNotOwnPropertyOfBoundObject,item);
}
function getFirstMissingPropertyFromBoundKeyList(item) {
return this.find(isNotOwnPropertyOfBoundObject,item) || null;
}
const blockList = [
{ name: "name",formula: "f",id: 1 },{ formula: "f",{ name: "name",formula: "f" },{ name: "name" },];
const keyList = ["name","formula","id"];
console.log(
'omitted property names for each block item ...',blockList.map(getOmittedPropertiesFromBoundKeyList,keyList)
);
console.log(
'first missing property for each block item ...',blockList.map(getFirstMissingPropertyFromBoundKeyList,keyList)
);
高阶组件使用一个额外的 .as-console-wrapper { min-height: 100%!important; top: 0; }
函数,该函数应该返回 memo
的倒数。
注意
与类组件上的 areEqual
方法不同,
shouldComponentUpdate
函数返回 shouldComponentUpdate()
如果 props 相等并且 areEqual
如果道具不相等。这是来自的逆
true
。
false