Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In React componentDidUpdate, can props and state change at the same time?

In a react component, normally you should not mutate props inside it. Also, the parent can only change the props, not the state directly. Based on these two facts, is it correct to assume that in any call of componentDidUpdate e.g.,

componentDidUpdate(prevProps: Readonly>, prevState: Readonly)

this.props could be different from prevProps, OR this.state could be different from prevState, but both situation cannot occur at the same time?

like image 879
prmph Avatar asked Apr 15 '18 08:04

prmph


1 Answers

From react docs:

React may batch multiple setState() calls into a single update for performance.

So my best guess based on that is that for performance reasons React may group together props and state updates to perform only one update instead of two. So to answer your question:

this.props could be different from prevProps, OR this.state could be different from prevState, but both situations cannot occur at the same time?

I think it may actually happen like previously said - for performance reasons.

like image 183
Tomasz Mularczyk Avatar answered Oct 04 '22 09:10

Tomasz Mularczyk