In this video we're going to move on to our second big rule around reducers.
So with this rule we say that producers are supposed to produce or return state or data that is going
to be used inside of our application and the producer has to do so using only the previous state from
the last time that it was called and the action object that has been dispatched and is being passed
into a given reducer.
Now understanding this text right here is a little bit challenging.
So we're going to look at a diagram or to help you really understand what's going on here.
All right.
This is a diagram of what happens to your reducer.
The very first time that it gets called inside of your application.
Now as I mentioned in the last video when you first start up the redux application each reducer is going
to be automatically called exactly one time.
So it is a automatic invocation that essentially allows your reducers to specify some default state
value the first time your seducer gets called during that initialization process.
It's going to receive two arguments.
The first argument is going to have a value of undefined.
And then the second argument will be some action object that we might refer to as action number one
because it is the first action that the reducers going to see.
So it is then up to the reducer to take these two arguments right here and return some initial state
value.
Now we really already went through this process right here.
Back when we put together the song's application in the last section I'm going to pull on some code
from that application onto the screen really quickly OK.
So here's our selected song reducer.
So when our application first started up selected song reducer was invoked automatically.
One time let's imagine that the code for invoking at one time was placed right here.
So redux internally did something like selected song reducer and it passed in a first argument with
the value of undefined.
And then the second object is going to be some initialization action object.
We don't really know what type that this action is going to have.
We just know that it's going to be in there it's going to be an action object with some mystery type
property that we don't really know.
So the first time that our reduced or gets called we're going to essentially look at that first argument
right there and very quickly realize that it is undefined.
That's why we put a default value for selected song right here.
Remember what this syntax right here really does.
It's a little bit of yes 20:15 syntax that is essentially the equivalent of saying if selected song
is equal to undefined then change the value of selected song to be no instead.
So again the very first time we call the reduce our first argument is undefined.
We don't really want to deal with that value of undefined.
So we are going to default it's value to instead be null and that little option right there that defaulting
is only going to take place the very first time that a reduced or gets in vote member all the equals
nil right there does is check to see if selected song is equal to undefined.
If it does it's going to update the value of that argument to be NULL instead.
We're not going to always default a argument like this to be equal to nil.
In many cases we will instead defaulted to be an empty array or it might be an empty string or some
number like the number 10 or something like that.
It really just comes down to the purpose of the reducer that we are working on to decide on the default
value.
But in the case of selected song reducer a default value of Noal made a lot of sense because that meant
no there is no currently selected value inside of our application.
OK.
So I can clean that up really quick and we're going to flip back over to this diagram I guess that's
what happens the first time the reducer is cult now where things start to get really interesting is
the second or third or fourth or fifth time that the reducer gets caught the next time.
Like the second time that this producer gets called The first argument is no longer going to be equal
to the value undefined.
Instead it's going to be whatever the reduce or returns the last time that it was ran.
You'll notice that the first one to reduce ran it returned what we call state view one right here.
So the next time the second time that the reducer gets called The first argument will then be equal
to state v 1.
So essentially that first argument into your reducer is always going to be what ever it was it returned
the last time it was ran.
So you can kind of imagine that we've got this kind of acyclic action where our reducer just keeps on
getting called with its own value that is turning over and over again.
And the only real change inside there is the action object.
The only thing that changes each under a or gets called is that action object and the type in Paillard
property inside of it I get so that's what the second rule right here is talking about.
Our producer must return some data or some state using only the previous state value that it returned
and the action object.
So now that we have a better idea of rule number two right here let's take a quick pause.
Ill move on to rule number three.