
shyarkhalil
Front End Web Development Techdegree Graduate 17,762 PointsCan someone explain for me why do we use this.setState(this.state)?
I don't understand why do we use "this" and why do we use this.setState(this.state)?
3 Answers

Giovanni P.
4,803 PointsWhile the OP has probably already figured this out, it might be good for there to be an actual answer in case others are looking for it.
The reason we call this.setState( this.state )
is so that React actually rerenders the page with the new state. If we just set this.state
and didn't call this.setState( this.state )
, the state variable would change, but React wouldn't know it is supposed to rerender the page.
I hope that helps!

I Dilate
3,982 PointsHi shyarkhalil,
Do you have any experience with JavaScript yet?
There's a great lesson that will shed some light on how "this" works in JavaScript, inside the Beginning Javascript track. If you haven't covered that yet, then I urge you take it before moving onto attempting anything with React.

I Dilate
3,982 PointsHere's a good summary of how "this" works, from the Full Stack Javascript track...

shyarkhalil
Front End Web Development Techdegree Graduate 17,762 PointsHi I Dilate! But you didn't answered me why do we use this.setState(this.state)?