Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialammarkhan
Front End Web Development Techdegree Student 21,661 PointsWhy use prevState?
I don't understand why we use prevState because basically, prevState shld be something like
let preState = this.setState
before using it, (I m using my own imagination because we never assigned prevState to any value so what is prevState?
travis halarewich
9,165 PointsI do not believe prevState is set by React like Dan Sutherland has mentioned. Iām pretty sure it is just a random name made up like all parameters for functions are. Hopefully someone who knows 100% can chime in
1 Answer
Waylan Sands
Courses Plus Student 5,824 PointsprevState was just a random name he gave for the argument added to the setState function. When you use it this way it will assume the value of the current or "previous" state and then the function will execute.
https://reactjs.org/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous
Dan Sutherland
1,463 PointsDan Sutherland
1,463 PointsprevState is the state value BEFORE that callback is applied. It isn't set here because prevState is set by React, as we extend React.Component with our class based component we can access the methods set by React such as prevState.