You've now finished up our reducer and this video I want to mention one quick thing that you need to
be aware of as we went through our reducer.
Remember we referred to this action object right here essentially as how to change state.
It's an object that you and I are going to essentially define when we pass an argument into our dispatch
function down here.
So you and I have total control over what that action object looks like.
We can put any properties into it that we want.
However usually there are some community conventions that we follow.
And let me be clear you do not have to follow these community conventions.
You don't have to follow them at all.
Totally optional but if you want some other engineer to look at your code and instantly understand what's
going on you might decide to follow these conventions.
So let me show you what they are.
So our action object had properties of color to change of like red and then an amount.
But usually by convention our action objects are going to have two different properties.
The first one will be type.
That's gonna be a history a string that describes the exact change operation and we want to run inside
of our reducer.
And then secondly we'll have a payload property.
There'll be some data that's absolutely critical to initiating or actually executing that change operation.
So if we wanted to follow community convention for our action object we would use a type of something
like change red because we're describing the type of operation we want to run.
We want to make a change to the property red and then payload in our case would be 15.
That's how much we want to change red by now if we start to use community convention right here.
Are reducer technically is gonna get a little bit more complicated.
So I want to refactor our action object to follow community convention when we do you're going to notice
that yes the reducer gets a little bit more complicated and so I will leave it up to you to decide whether
or not you want to make use of action objects.
That just makes sense to you and are convenient to you.
Or if you want to follow community convention again it's 100 percent in your court you can decide what
you want to do.
OK so let's give this a shot.
I'm going to flip back over to a square screen.
I'm going to change my action comment right here.
I'm gonna say that action rather than saying simply color to change color to change instead is going
to be a type property.
So the type is going to describe the exact change that we want to make to our state.
Now there's not necessarily any meaning to this string.
It's only meaningful to you and I as developers so I'm going to decide to use a type of change underscore
read change underscore green and change underscore blue like so and then rather than having an amount
we want that to be payload instead at this point I just change the comment that doesn't actually change
anything.
We don't have to actually go round our reducer and our component and actually initiate these changes
or make me effective.
So let's first start inside of our component I'll go down to square screen.
We're going to have to go around to all of our different dispatch functions that we call inside of here
and change these two type and payload it's going to first take care of amounts because that one's a
bit easier it's going to change amount six times because we call dispatch six times inside of our ASX
block here.
So there's one two three four five and six.
Next up we're going to go around to all of our different colored to change statements in our properties
inside of our JSA X block once again.
There are six of them.
We're going to change color to change to be simply type instead.
So there's one two
three four five and six.
There we go.
And then finally we need to change the actual type property values.
We don't have red blue or green anymore.
If we're following communing convention who would instead be change red or change blue.
So I will change that type of red to change underscore red change underscore red change underscore blue
change underscore blue and then down here on the last one change underscore green and change underscore
green.
OK.
So there's all of our different dispatch statements changed it's now to go and update our reducer as
well.
Here's my reducer.
I'm gonna find my switch statement.
The first thing we have to do is say rather than doing a switch on color to change we're going to decide
what state update we want to run based upon the type property.
So rather than switching over action not color change we're going to instead switch over action type
and we can update the text inside of our three case statements.
So all do change.
Red change green and change blue and here's where life gets really painful.
We no longer have that like nice variable to refer to inside of here.
So while we've got state DOT red that's going to work but we have to go all around and we have to update
action dot amount all over the place.
So now we've got to update that in like nine places inside of here between the entire switch statement.
I've got one two three inside of each case statement.
It's all do action not payload to three or five six seven eight and nine
so let's say this once again flip back over and yet still turning red still going blue and still going
green so as usual if you now see an error message or if the application is not working.
Guarantee you you just made a typo so inside of here.
So do a little hunting you're going want to find where you made that typo.
So now I got a question for you is our code any better at this point.
Maybe not.
Yeah I would say no probably not.
I don't think it's necessarily any better.
I wouldn't say it's better easier to read.
I don't know.
So like I said if you want to use your own action objects and just throw in the properties you want
yourself just go for it.
Absolutely just go for it.
Otherwise by convention we're going to use the type property with payload inside this course.
We are going to follow convention because that is convention and in general I want to always teach you
commune a convention site of a course but just between you and me if you decide not to follow that convention
none the world is not going to end.
Let's put it that way.
All right.
So this looks pretty good.
I think that we've got to reduce or put together so quick pause right here.
Continue next video.