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 trialRachel Heneault
12,401 PointsI'm confused why you'd use the callback when updating state? Seems like an extra step when it gave you the same results
I'm confused why you'd use the callback when updating state? Seems like an extra step when it gave you the same results without adding the extra callback.
3 Answers
Dylan Thunn
22,658 PointsYou don't technically have to it, it is just a more stable way of doing it for react, with a small app though it is probably overkill. Once you get to using hooks to update state it won't really matter.
Dan Sutherland
1,463 PointsprevState is the value of the state BEFORE the callback is fired, whereas this.state.stateName holds the value of state to be altered at the time that callback is being fired. Most of the time this won't be an issue but as React can batch render updates, sometimes if a state is being updated by a variety of methods you can get get unexpected results if your updates are iterative.
Norman Lew
Full Stack JavaScript Techdegree Graduate 14,784 PointsHi Dan, if you are still around, I still don't quite understand it. Can you give some examples as to where there would be a difference?
Rachel Heneault
12,401 Pointsthank you :)