在 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...