1

In my React Native app I have a PureComponent where I log whether props or state have changed each time shouldComponentUpdate() is called, like this:

shouldComponentUpdate(nextProps, nextState) {
    console.log(JSON.stringify(this.props) === JSON.stringify(nextProps))
    console.log(JSON.stringify(this.state) === JSON.stringify(nextState))
    return true
}

In one instance, this outputs true and true, meaning neither props nor state have changed. I thought that in order for shouldComponentUpdate() to get called, one of these has to change. What could cause it to get called when neither has changed?

gkeenley
  • 6,088
  • 8
  • 54
  • 129
  • How could `shouldComponentUpdate()` do its job (checking whether the compenent should be re-rendered or not) if it is not called every single time? The component will be re-rendered only if a prop or the state changed but, in order to detect the change, `shouldComponentUpdate()` needs to be called and evaluate them. – secan Oct 14 '22 at 12:12

0 Answers0