Transcript
00:00 You know how I said we were getting pretty close to the way that setState worked in class components? Yeah, we're going to take that even further and make it so that when you call setState, you can actually call it with the previous state and use that, or I guess the current state, and use that to calculate
00:18 or compute the next state. The reason this is actually really valuable is because if we were to put this setState call inside of a promise then handler, or after an await, or something like that, then what we had previously,
00:34 which is just this, this could fall prey to the count being stale. So it's old version of count that we had when this setState, or when this increment function was established. By doing things this way, we avoid that problem,
00:51 and also if you call setState a couple of times, then you avoid not having access to the latest version of the state. So the function form, or the action function that we're going to provide here, actually does solve some interesting issues, and the typical rule is
01:10 if your previous state, or if your state change depends on the current state, then you should use the function form like this, which we have in our useState hook also supports a callback like this. So typically you want to do it that way if you're going to depend on the previous version of the state. So we're going to support that API,
01:31 this is going to get us even closer to the way that this.setState worked back in the class component days. So, should be fun, and we're just taking things a little further and exploring a little bit more what we can do with this useReducer API.