In this video in the next couple we're going to start to go over these rules of reducers and we're going
to eventually cumulate with a really interesting dive into some of the source code of Redus.
And you're going to really get a behind the scenes look at how producers really work.
All right.
Let's start off with.
Rule number one up here.
So the first rule of reducing or is that it must return any value besides undefined.
So you might have noticed that when we put together that dummy reducer or the replace me one inside
that index dot J as file we had to return some fixed number or fixed string.
The reason for that was that if we ever return undefined from the reducer going to very quickly see
an error message.
So to demonstrate that very quickly I'm going to flip back over to my post reducer and I'm going to
remove that return statement entirely.
So now any time that this function gets called it's going to return a value of undefined.
That is not a redux rule or a re-act role.
That is how javascript functions work.
If you do not have an actual return statement with some value to the right of that like a number or
a string or an array you don't have any of that then the function is going to return the value undefined
by default when it gets called.
So I get to empty out the function and I'll save it.
Now we can flip back over to our application and we'll very quickly see a nasty error message that says
Post returned undefined during initialization.
So again we are not allowed to return undefined.
That means that every single time that you reduce or gets called it should always have a return statement
at some point in time and that return statement should always return some type of value.
So we can return a number.
Let's try this out really quickly I'll save this and when I flip back over my apple refreshed and the
error message goes away I can return a string.
I can save that and flip back over.
We'll see that the application still works.
We can return a empty array.
No problem here.
We can do an object no problem here.
We can even do the value no.
So when I save that and flip back over no problem there as well.
It's when I failed to place a return statement or I specifically return a value of undefined that we're
going to run into that error message.
Now if you look at this message very closely it says returned undefined during initialization.
So when redux first boots up it's going to run you of your reducers one time and we'll dive into that
process a bit more closely in just a moment.
But essentially when redux first boots up it's going to call your reducers one time.
And so right you're this during initialization we're right there.
That means that the first time you're douceur was ran it returned the value undefined.
Now the rule around not returning undefined is not only about initialization your reducer can never
ever return undefined whether it is an initialization or at some point in time the future when an action
has been dispatched.
OK so that's rule number one around reducers so we must always return any value besides undefined.
So let's take a quick pause right here.
When we come back the next section we're going to move on to our next big rule around reducers.