I understand that nextProps and nextState are used in shouldComponentUpdate to determine if the component should rerender or not based on the result of comparing this.state.someProperty to nextState.someProperty. If they are different, the component should rerender.
That is clear.
However, this does not seem to be what is happening. View this code.
class Box extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
}
this.counter =
this.counter.bind(this)
}
counter() {
setInterval(()=>{
this.setState({count: this.state.count+=1})
}, 10000);
}
componentDidMount() {
this.counter();
}
shouldComponentUpdate(nextProps, nextState) {
console.log(this.state.count + " " + nextState.count)
return true;
}
render() {
return (
<div>
<h1>This App Counts by the Second </h1>
<h1>{this.state.count}</h1>
</div>
);
}
};
In shouldComponentUpdate, I am logging both the state.count and nextState.count values, and they are equivalent each time. Should they not be different? If not, what is the purpose of checking to see if they are equivalent if changing the state with setState ensures that they are the same?