Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Preact/React and setting state after fetch in react.js ?

By M.K. Verma · 2 May 2022

Preact/React and setting state after fetch in react.js ?

The right place to check for state change is either to pass callback as second argument to setState method

  this.setState({
      explanation: 'phil says so'
    },() => {
      console.log('Set the explanation state successfully')
    })

or using componentDidUpdate lifecycle method.

componentDidUpdate(prevProps, prevState){
  if (prevState.explanation !== this.state.explanation) {
    console.log('Set the explanation state successfully')
  }
}