In the last section we verified that whenever we called our action creator from our list item component 2
we saw the action appear inside of our selection reducer. 3
So now it's just up to us to make sure that whenever this thing sees an action of type select library 4
it will return the actions payload which is libraries ID instead of all day. 5
So that's all we have to do. 6
I'm going to remove the two lines I have in here right now and instead I'm going to replace this with 7
some boilerplate that we're going to see for just about every single producer that we ever create. 8
OK. 9
By convention every REDUCE-IT that we create is going to have a switch statement inside of it that is 10
going to switch over the actions type. 11
So the reason that we do this is to have one very nice very easy to read statement to figure out what 12
this user does and more importantly what actions it responds to. 13
In our case our action to reduce her wrist needs to respond to an action of Tide select library. 14
So I will add in the case of select library like so I also wanted to respond to the default case. 15
So if I ever get an action coming through here that is not of type select library I need to handle that 16
case as well. 17
So if a action comes through with type like I dunno melt motherboard or something. 18
You know I don't know something. 19
Something crazy like that. 20
I need to make sure that I still return something from this reducer because remember all of my reducers 21
can run many times throughout my applications lifecycle and I need to make sure that they always return 22
some non undefined value. 23
If I ever return undefined from Ray douceur redux is going to throw an error. 24
So now by default if this action creator is ever seen at this reducers ever called with an action that 25
it does not care about it will return whatever it returned the last time that it ran. 26
So if a mysterious action comes through here with the type we don't care about just return whatever 27
state we returned last time and remember the state argument right here is the state that was returned 28
from the last time that the redux are from the producer Ron. 29
Now how would the case in which the action is of type select library. 30
Well in that case we want to return the actions payload property because inside of our action creator 31
if you recall and flip over to our action Krater file we gave our action of types like the library a 32
payload of the library's ID that was selected because we called the action cratered self at that library 33
ID. 34
So now inside my producer whenever the action is of type slides library I want to return that library's 35
ID. 36
The last thing you have to do in here is make sure that I handle the very default or like first initialization 37
case to reduce or so the very first time the producer runs. 38
And remember as we saw in the console just a second ago when redux first boots up it runs all of my 39
reducers I need to make sure that I return some non undefined value from my reducer the first sign that 40
runs state will be called with a value of undefined. 41
So if I just let this thing run by itself I will end up returning undefined which is going to result 42
in error. 43
So to get around that I will always provide an initial state default argument. 44
So this is going to be a little piece of ESX right here what this says right here is if this argument 45
or if this argument right here is undefined we should instead default it to the value of NULL. 46
So instead of returning undefined as our initial state we will return NULL which means essentially in 47
our case I do not have a currently selected library. 48
I just continue on. 49
I don't have a selected library right now. 50
OK so I know it's reducers stuff is a little bit crazy. 51
Well probably a lot crazy. 52
I mean I'll be really honest you it's super crazy. 53
Fortunately we are going to get so much practice with reducers because they're the absolute core of 54
every redux application. 55
So again just like earlier when we went through our jazz playground and we were just kind of you know 56
angling around with a redux a little bit. 57
Even at this point in time I do not expect you to be a master of redux. 58
I do not expect you to be able to rewrite all this code right here from scratch without any type of 59
reference in the next app that we build. 60
We will definitely go through reducers action creators actions again in great great great detail. 61
So if it's still confusing do not sweat at all. 62
We're going to get a lot of practice with it. 63
OK. 64
So at this point in time if I refreshed my simulator and then start clicking on these different things 65
I don't get any feedback right now but in theory I have a piece of steak called selective library ID 66
that is being changed every time I click on one of these lines right here. 67
So the last thing we have to do is make sure that whenever a particular library ID is equal to the currently 68
selected library ID we expand a given row. 69
So let's take care of that in the next section.