`React.PureComponent` causes components not to be updated

when writing the code component, React.PureComponent inexplicably causes the state to be modified, but the component is not updated, so it is changed to Component to ok

.

is as follows: the timer is executed once per second, but the ui is not updated. The console shows that only after 4 times of regular execution (length is 4) will render once

   

Mar.22,2021

React.PureComponent actually overrides the SCU (shouldComponentUpdate) method.

The implementation of

React.Component shouldComponentUpdate returns true directly.

this is the difference between the two. The reason for this is that shouldComponentUpdate "mistakenly" changes the return value to false.

The SCU rewritten by

PureComponent is actually shallow equal's two state and nextState,prop and nextProps.

from your code, it is

let nextIndex=PPpreState.index; 
// 

let nextIndex = preState = preState.index + 1;

so not only did you modify nextIndex, but you also modified preState.index..

you can inherit React.Component, to write your own SCU, and then debug to read it.

I hope my answer will be helpful to you.


 let nextIndex=PPpreState.index; //  preState ,  index ,  PureComponent  shouldComponentUpdate  false.
Change

to

 let nextIndex= preState.index + 1;
Menu