Current section: State Reducer 30 exercises
solution

State Reducer

Transcript

00:00 So we've got our quick too much that is being managed for us So we don't need to worry about that so much. We just need to have a reducer function that will handle Updating the state appropriately when we've clicked too much. So say a reducer and we've got our state and our action and

00:16 We'll switch on the action type. And yeah, if it's toggling then we'll return that if it's reset then we'll return the initial state But we want to handle the case if we've cooked too much then let's not bother with that stuff so if clicked Too much then return in the state Perhaps we want to only do this when we're toggling though, right?

00:36 Because if we reset we need to be able to actually reset so let's put that down in here And there we go. Great now typescripts not super jazzed about this because we haven't actually implemented this yet so let's dive into the use toggle and we're gonna say our

00:51 Reducer defaults to toggle reducer and now instead of toggle reducer. We'll just use reducer. That's it There it's actually a lot. It's more straightforward certainly more straightforward than it used to be. Believe me. It was awful before But yeah, you just accept a reducer and use that instead of your built-in reducer

01:12 So now as I click no, I won't click anymore until I reset and then I can reset at any time. I like and It's working super great So that is the state reducer. The idea is when an event is dispatched then instead of Calling our own reducer. We're gonna call your reducer and you're in charge now and

01:32 That allows that inverts control now something to keep in mind Is that this means that you're exposing your states and your actions to? The external world and so this now becomes part of your API Which does mean like breaking changes are more likely if you want to refactor and stuff So it is not something you want to just do everywhere

01:54 But it is an extremely powerful API and it drastically reduced the number of Requests for special one-off features that I would get in downshift. And so definitely a super fan of this particular pattern But yeah, it does kind of open up your internals as being part of your external API. So you want to keep that in mind?

02:16 But there's a couple other things that I'm not a super fan of so let's keep going