0

React's usetState has an eagerState optimization, duplicate rendering is not triggered when I set the same value repeatedly

but is there a similar optimization for this.setState of class components? even if I do this.setState({count: 1}) in the class component every time, it still renders repeatedly

here is the online code: https://codesandbox.io/s/zealous-panka-wonxfy?file=/src/App.js

lingxi
  • 3
  • 3

1 Answers1

0

I am afraid that with "classical" React-components, you have to take care of preventing unnecessary re-renders yourself, as stated in this answer to a similar question.

Basically, you need to write your custom shouldComponentUpdate() method and check whether or not the counter has changed.

Steffen Frank
  • 1,220
  • 7
  • 14