How to use ComponentDidUpdate?

There are many methods in React for different purposes. ComponentDidMount(), componentDidUpdate() etc. ComponentDidUpdate is a method of component lifecycle. It is called after any props or stats are updated. It can be used to update anything after a state update. ComponentDidUpdate() is called after ComponentDidMount(). It takes two arguments: the first argument will be the previous props, second :previous state. Inside the method you can specify the condition to be checked. Here is an example.

componentDidUpdate(prevProps, prevState) {
  if (prevState.pokemons !== this.state.pokemons) {
    console.log('pokemons state has changed.')
  }
}

To see where to use componentDidUpdate() lets see a case in which it is used. When you are using a third party API on a condition where the previous state and current state have changed. The API will be called if the state is changed. The API call will be stated inside the conditional statement.