lifecycle of class component in react code example

Example 1: componentDidUpdate

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

Example 2: lifecycle methods react

Every component in React goes through a lifecycle of events. I like to think of them as going through a cycle of birth, growth, and death.

MountingBirth of your component
UpdateGrowth of your component
UnmountDeath of your component

Example 3: component did mmount

componentDidUpdate(prevProps, prevState, snapshot)

Example 4: react lifecycle

constructor(props) {
  super(props);
  // Don't call this.setState() here!
  this.state = { counter: 0 };
  this.handleClick = this.handleClick.bind(this);
}