Beautiful.
Once we have set up our store and reducer, now we can zero in on the specifics of the producer.
And when it comes to producer, again, like I previously already mentioned, producer function is used
to update our store and it takes two arguments state and action.
And you can think of state as an old state or state before the update and action of what happened in
your application or what kind of update you would want to make to your store.
Now, easy way of thinking of actions is thinking what is the user doing?
So, for example, we have the card.
So what if a user would like to click on the increase?
But well, we would set up the functionality where if that happens, then of course we increase the
amount of items.
We have an ID card.
We also set up to total and all that.
So the same would work if we decrease, if we remove and along those lines.
Now, of course, since there are arguments, you can technically name them however you like, but a
common practice is naming them state and action.
Now, once we pass in our parameters, then I would like to console them just so we can see what is
happening.
So I'm going to go with the object in this case because I think it's going to be more representative
and I'm going to go state and action.
So once we cancel, like both of them, we notice something really interesting.
So as far as state at the moment, it is undefined and then I'm not going to cover the action right
now.
This would be just the default action that is passed into initialized everything from the redox.
So later we were going to create our own actions.
But at this point in time, redacts just passes a default action which initializes everything.
So we our state, we have our action.
But of course, now we need to deal with the fact that our state at the moment is undefined.
So what's up with that?
When you say why don't we have to create store, we can pass in the second argument, and that would
be our initial store, of course, we would still need to set it up.
So, for example, above the register, I can say initial store and I'm going to create it as a variable,
of course, const initial store, initial store.
And for the time being there will be equal to an object.
And then within the object I'm going to have the current property and the count will be equal to zero.
Now, like I said, the second argument and a great store is where we pass in our initial store.
So I'm going to go with initial store and now in a console, we should still see our default action.
That would be that funny action with the type and then state now is count.
So in the beginning it was undefined because we didn't pass any kind of initial value.
But then, of course, once we set up our initial store, now this is our initial state of the application.
And then when it comes to our producer, we have one of the two options.
Either we are going to return updated state.
So state after the action happened, for example, again, user decided to remove the item.
So we're going to have functionality that checks for that action that actually happened.
Then, of course, we're going to do our calculations, remove that product from the state, and then
return a new state without that product.
But always, always has a default.
You should return the old state so state before the update.
So if, for example, some kind of action happen and you are not checking for that action, you should
always, always return to state before the update.
That's why in the bottom, I'm going to write return state, essentially what I'm doing, as I'm saying.
All right.
I'm going to be checking for the actions.
If none of the actions match to whatever action just happened in my application, then I'm just going
to return the state the way it was before the update.