Reputation: 37328
I have a component, and its componentDidUpdate
seems to trigger for no reason. It's a child component, has no state. Even though the prevProps
and this.props
are the exact same its triggering, is there any reason for this? I thought update should only trigger when there is a change in the props
/state
?
Upvotes: 1
Views: 3498
Reputation: 14101
componentDidUpdate()
is fired every time the parent component re-renders (and passes in new props). And in stateful components also whenever setState()
is fired.
Even if old prevprops
and this.props
are exactly the same, componentDidUpdate()
is still fired if the parent component re-renders.
If you want to prevent react to enter a render cycle, you should implement the shouldComponentUpdate()
method, where you can compare new and old props.
Upvotes: 7
Reputation: 3873
update should only trigger when there is a change in the props/state
No, React renders to VirtualDOM everything each time. But then it updates only changed properties in real DOM.
To avoid that you can use PureRenderMixin or your custom comparation code in shouldComponentUpdate
.
Upvotes: 3