The shouldComponentUpdate() life cycle method is used to determine whether or not the component will render when asked to. For example, if this method were implemented and returned false, the entire life cycle of the component would short-circuit, and no render would happen. This can be an important check to have in place if the component is rendering a lot of data and is re-rendered frequently. The trick is knowing whether or not the component state has changed.
Let's take a look at a simple list component:
import React, { Component } from "react";
function referenceEquality(arr1, arr2) {
return arr1 === arr2;
}
function valueEquality(arr1, arr2) {
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}
export default class MyList extends Component {
state = {
items: new Array(5000).fill(null).map((v, i) => i)
};
shouldComponentUpdate(props, state) {
if (!referenceEquality...