Transcript
00:00 Let's start out by updating the usage and that will help guide us to the direction of how we implement our reducer. So first of all this change count is now set state and this is called state
00:14 and we get the count from the state and then our set state is now going to have a count property which is count plus the step and here it will be an object whoops with a count property where
00:30 it's count minus the step and that is the API that we're looking for. Oh and of course we also need our count set in here so we're getting a bunch of type errors because we haven't implemented our reducer to support this API yet. To make things a little bit easier we're going to have
00:49 our state be typed here with count as number and then we'll have our action will just be equal to the state. So that's the API that we're exposing for right now just for a moment here. So you just
01:04 pass us the same API for your action as you do as we have for our state. So then our count this is actually now going to be our state object and our action will be an action object and to make all
01:20 this work we can just return the action and now if I increment and decrement all that is working just fine but we've got a problem here with one of the requirements that we had. So if I add some
01:34 other state and then we console log that state you'll notice we have our some other state here but as soon as we make a state change that some other state is gone and that's because we're
01:47 updating this or calling set state with that new count. So if we wanted to we could say some other state is state dot some other state yeah that would work TypeScript doesn't like it but who cares so there we go we're maintaining that state we want to simplify it further you could actually
02:07 just say dot dot dot state and in fact let's not override it we want to do it on the correct side so we'll say dot dot dot state combined with that count and so now that's going to carry our state with us but we actually have that state when we are reducing things so we could actually do this
02:26 take the action take the state whatever the action is will override that state and so now we don't have to bother doing this here whoops we can get rid of it right there and we're going to maintain that other state so if you had something that had a lot of elements of state and you're
02:43 like I just want to update this one thing then this is how you might put together an API that supported that. So there you go that is getting us actually a little bit closer to some aspect of what it's like to work with reducers typically this actually resembles the class component syntax
03:02 for set state that we did for it feels like many many years ago at this point but yeah set state from classes operated in a similar manner where you just provide an object with the properties that you want to update and it will merge those together and that is the API we provided pretty cool