When it comes to use reducer we really need to understand one thing we cannot update state directly.
Again let me repeat we are not allowed to update our state directly.
You see so far in our app we followed this pattern.
First we set up state.
Then we add some state values using you state create some functions in the functions we use the set
function that is returned return from your state and eventually will pass its functions down to components
where users interact with our state by clicking the buttons for example.
Now when it comes to user reducer in our functions we're not going to update our state using set functions.
The second parameter from you state in fact use reducer returns a state and a dispatch function that
is responsible for updating our state in a dispatch function.
We are going to pass our actions and you can think of actions as what we would want to do.
You want to show the model you're going to pass open modal action you want to remove the item from the
card you're going to pass a remove action and hopefully you get the gist.
Once we dispatch the action we're going to handle it in the reducer and register is nothing but a function
that takes two arguments.
State or you can also think of it as an old state and a action.
Notice how we pass two arguments when setting up a user juicer.
First is our register function and the second is initial state.
So within the reducer we're going to check what type of action is this budget and based on that we're
going to update our state accordingly.
We always always return a new state from reducer.
So essentially we cannot let me repeat we cannot mutate the old state.
We need to return a new one.
And once we do that becomes our new state even though updating our state this way requires a bit more
setup.
It also gives us more structure and provides more predictable state management.