2021年3月19日星期五

Call this.setState from Another Function [duplicate]

I'm a beginner and currently learning ReactJS on Codecademy platform. I need a little help to understand the example code shown below.

class Example extends React.Component {    constructor(props) {      super(props);      this.state = { weather: 'sunny' };      this.makeSomeFog = this.makeSomeFog.bind(this);    }       makeSomeFog() {      this.setState({        weather: 'foggy'      });    }  }  

I have noticed a weird line in there:

this.makeSomeFog = this.makeSomeFog.bind(this);  

In lesson section, There was explanation that says:

"This line is necessary because makeSomeFog()'s body contains the word this."

Can you give me a helping hand to understand what it means? Thank you!

https://stackoverflow.com/questions/66718415/call-this-setstate-from-another-function March 20, 2021 at 01:05PM

没有评论:

发表评论