alexa

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

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')
  }
}

226 0
7

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online

Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry