When it comes to redux essentially we need to remember understand only few following building blocks
in redux.
Store is a single piece of truth and you can think of it as a global state in the Ori act that in fact
terms store and state are used interchangeably when covering redux.
When we think of it it's nothing new to us.
In a previous project we used context API in a very similar way.
The major difference between redux in context API is the way we can update our state you see when it
comes to redux.
We cannot update store directly and that is not allowed.
The way we update our store and redux is by dispatching an action and you can think of actions as what
we would want to do.
For example you want to add a product to the car.
You will dispatch an ad to court action.
You want to open up the model you will dispatch open model action and hopefully you get the gist.
Once you dispatch the action you can handle it.
In reducer and reduce there is nothing more than a function that takes two arguments state and action
when it comes to state argument you can think of it as an old state or state before action was dispatched
an action argument provides the info.
What type of action was dispatched.
The key about reducer is that it does not mutate state directly.
In fact you always always should return a new state from reducer in reducer we're going to handle.
What action is dispatched and update our new state appropriately.
Once we return a state from reducer that becomes our new state and we can start all over to give you
an example imagine we have a shopping cart and we would want to add an item to the cart.
So our shopping cart is going to be our store.
That could be an object that could be an array.
Doesn't really matter so we're going to access our store and we're going to display in our application
what items are already in the car.
Now the thing is if I would want to add an item to the cart I'm not just going to click on a button
and directly access the store and then update the store.
And of course display all the items that have been added to the cart.
That is not allowed.
In fact once we click on Add To Cart button then we're going to dispatch an action we can name our actions.
However we would want but most likely adding to the cart is going to be add to cart action and then
once we dispatch our add to cart action then we're going to handle it in reducing and reducing is going
to take the old state so the state before action was dispatched and then it's just going to check okay.
What we would want to do when we're handling the Add to Cart action and of course since in our case
we would want to add item to the cart.
We are going to copy the old state values update the cart.
Make sure that we added the item and then return our new state.
So once we return our new state that becomes our state.
If we would want to do it all over again we can definitely do it.
If we would want to let's say dispatch a different action remove items and cart.
We're gonna do the same thing only in this time we're dispatching remove item from the cart and then
of course in reducer we're also handling a bit differently.
However it doesn't change the main idea where we cannot update the store directly from the yard.
We need to dispatch an action design that needs to go through reducer and only the state that is returned
from reducer is our new state.