如何解决在 React 中将 shouldcomponentupdate 转换为备忘录
使用 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。