1 00:00:00,150 --> 00:00:06,660 When it comes to use reducer we really need to understand one thing we cannot update state directly. 2 00:00:06,780 --> 00:00:12,000 Again let me repeat we are not allowed to update our state directly. 3 00:00:12,000 --> 00:00:14,970 You see so far in our app we followed this pattern. 4 00:00:15,060 --> 00:00:16,420 First we set up state. 5 00:00:16,470 --> 00:00:22,470 Then we add some state values using you state create some functions in the functions we use the set 6 00:00:22,470 --> 00:00:27,660 function that is returned return from your state and eventually will pass its functions down to components 7 00:00:28,020 --> 00:00:32,130 where users interact with our state by clicking the buttons for example. 8 00:00:32,130 --> 00:00:39,220 Now when it comes to user reducer in our functions we're not going to update our state using set functions. 9 00:00:39,240 --> 00:00:46,890 The second parameter from you state in fact use reducer returns a state and a dispatch function that 10 00:00:46,890 --> 00:00:51,570 is responsible for updating our state in a dispatch function. 11 00:00:51,600 --> 00:00:57,130 We are going to pass our actions and you can think of actions as what we would want to do. 12 00:00:57,300 --> 00:01:02,370 You want to show the model you're going to pass open modal action you want to remove the item from the 13 00:01:02,370 --> 00:01:06,920 card you're going to pass a remove action and hopefully you get the gist. 14 00:01:06,930 --> 00:01:13,440 Once we dispatch the action we're going to handle it in the reducer and register is nothing but a function 15 00:01:13,440 --> 00:01:14,730 that takes two arguments. 16 00:01:14,730 --> 00:01:19,510 State or you can also think of it as an old state and a action. 17 00:01:19,530 --> 00:01:23,260 Notice how we pass two arguments when setting up a user juicer. 18 00:01:23,330 --> 00:01:27,270 First is our register function and the second is initial state. 19 00:01:27,720 --> 00:01:34,350 So within the reducer we're going to check what type of action is this budget and based on that we're 20 00:01:34,350 --> 00:01:36,870 going to update our state accordingly. 21 00:01:36,870 --> 00:01:40,120 We always always return a new state from reducer. 22 00:01:40,200 --> 00:01:45,870 So essentially we cannot let me repeat we cannot mutate the old state. 23 00:01:45,870 --> 00:01:47,460 We need to return a new one. 24 00:01:47,790 --> 00:01:54,540 And once we do that becomes our new state even though updating our state this way requires a bit more 25 00:01:54,540 --> 00:01:55,260 setup. 26 00:01:55,260 --> 00:02:00,030 It also gives us more structure and provides more predictable state management.