Does componentWillReceiveProps always run even if the props didn't change?

Question

In the context of this exercise, does componentWillReceiveProps always run even if the props didn’t change?

Answer

Yes, the method will run anytime the component receives any props, whether or not the props are new or the same as before.

To prevent the method from running its code anytime props are passed in, and only when new props are passed in, you can override the method code, for instance by comparing this.props with nextProps and only running if they are different.

6 Likes

componentwillreceiveprops has been deprecated for over a year!

8 Likes

Why are we using setState here? The component is already updating, so why is this needed? Isn’t it unperformant?
Why don’t we simply use another prop value instead or even an instance variable, to save the highest number?

Check out the react docs to find alternatives. We probably shouldn’t bother learning to implement this thing that has been deprecated for 2 years unless it helps guide your understanding of the alternative!