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
.
Note
Unlike the
shouldComponentUpdate()
method on class components, theareEqual
function returnstrue
if the props are equal andfalse
if the props are not equal. This is the inverse fromshouldComponentUpdate
.
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.