简体   繁体   中英

Converting shouldcomponentupdate to memo in React

What will be the equivalent code using React memo?

 shouldComponentUpdate(nextProps) {

    if (this.props.addCourse !== nextProps.addCourse || this.props.level !== nextProps.level ) {
      return true;
    } else {
      return false;
    }
  }

The memo Higher Order Component consumes an additional areEqual function that should return the inverse of shouldComponentUpdate .

memo

Note

Unlike the shouldComponentUpdate() method on class components, the areEqual function returns true if the props are equal and false if the props are not equal. This is the inverse from shouldComponentUpdate .

const areEqual = (prevProps, nextProps) => {
  if (
    prevProps.addCourse !== nextProps.addCourse ||
    prevProps.level !== nextProps.level
  ) {
    return false;
  }
  return true;
};

memo(MyComponent, areEqual);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM