Why shouldComponentUpdate?

As long as the component inherits from react.component.ponent will exist when the parent component rerenders, the child component will also rerender, even if the child component has not changed. The child component simply calls setState and reexecutes render, even if the setState argument is an empty object.

The use of the shouldComponentUpdate

In the child component:

shouldComponentUpdate(nextProps,nextState) {
    if (nextProps.m1 === this.props.m1 && nextState.m2 === this.state.m2) {
        return false;
    } else {
        return true; }}Copy the code

When shouldComponentUpdate returns true, the current component render, if false, no render.

CodeSandBox online demo