November 2018
Beginner
502 pages
10h 22m
English
getDerivedStateFromProps is invoked every time a component is rendered. It can be used to change state when certain props change. This is a static method in a component class that returns the changed state, or null if there are no changes to the state.
Let's have a look at this life cycle method in our app. Add the following at the top of the App class:
public static getDerivedStateFromProps(props: {}, state: IState) { console.log("getDerivedStateFromProps", props, state); return null;}
If we look in the console when the app is running, we see that our method is called each time the countdown decrements:

Read now
Unlock full access