微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 React 中将 shouldcomponentupdate 转换为备忘录

如何解决在 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 的倒数。

memo

注意

与类组件上的 areEqual 方法不同, shouldComponentUpdate 函数返回 shouldComponentUpdate() 如果 props 相等并且 areEqual 如果道具不相等。这是来自的逆 true

false

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?