Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript React Basics Understanding State Update State Based on Previous State

Why 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?

prevState 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.

travis halarewich
travis halarewich
9,165 Points

I 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
Waylan Sands
Courses Plus Student 5,824 Points

prevState 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.