Skip to main content

7.17 PureComponents Instead shouldComponentUpdate

If you implemented shouldComponentUpdate with a check like this:

Persons.js
...

shouldComponentUpdate(nextProps, nextState) {
console.log("[Persons.js] shouldComponentUpdate");
if (
nextProps.persons !== this.props.persons ||
nextProps.changed !== this.props.changed ||
nextProps.clicked !== this.props.clicked
) {
return true;
} else {
return false;
}
}
...

You can do instead this:

Persons.js
import React, { PureComponent } from "react";
import Person from "./Person/Person";

class Persons extends PureComponent {

...

// shouldComponentUpdate(nextProps, nextState) {
// console.log("[Persons.js] shouldComponentUpdate");
// if (
// nextProps.persons !== this.props.persons ||
// nextProps.changed !== this.props.changed ||
// nextProps.clicked !== this.props.clicked
// ) {
// return true;
// } else {
// return false;
// }
// // return true;
// }

...

The result will be the same.

Because PureComponent in the end is just a normal component that already implements shouldComponentUpdate with a complete props check, so that checks for any changes in any prop of that component.