1 00:00:00,730 --> 00:00:05,300 In this video in the next couple we're going to start to go over these rules of reducers and we're going 2 00:00:05,300 --> 00:00:10,300 to eventually cumulate with a really interesting dive into some of the source code of Redus. 3 00:00:10,310 --> 00:00:14,710 And you're going to really get a behind the scenes look at how producers really work. 4 00:00:15,050 --> 00:00:15,300 All right. 5 00:00:15,320 --> 00:00:16,040 Let's start off with. 6 00:00:16,040 --> 00:00:17,310 Rule number one up here. 7 00:00:17,360 --> 00:00:22,880 So the first rule of reducing or is that it must return any value besides undefined. 8 00:00:23,120 --> 00:00:27,890 So you might have noticed that when we put together that dummy reducer or the replace me one inside 9 00:00:27,890 --> 00:00:32,620 that index dot J as file we had to return some fixed number or fixed string. 10 00:00:32,660 --> 00:00:37,580 The reason for that was that if we ever return undefined from the reducer going to very quickly see 11 00:00:37,580 --> 00:00:38,890 an error message. 12 00:00:38,930 --> 00:00:43,670 So to demonstrate that very quickly I'm going to flip back over to my post reducer and I'm going to 13 00:00:43,670 --> 00:00:46,470 remove that return statement entirely. 14 00:00:46,490 --> 00:00:51,980 So now any time that this function gets called it's going to return a value of undefined. 15 00:00:52,250 --> 00:00:54,970 That is not a redux rule or a re-act role. 16 00:00:54,980 --> 00:00:57,100 That is how javascript functions work. 17 00:00:57,230 --> 00:01:03,320 If you do not have an actual return statement with some value to the right of that like a number or 18 00:01:03,350 --> 00:01:10,460 a string or an array you don't have any of that then the function is going to return the value undefined 19 00:01:10,520 --> 00:01:12,410 by default when it gets called. 20 00:01:12,730 --> 00:01:15,380 So I get to empty out the function and I'll save it. 21 00:01:15,440 --> 00:01:19,910 Now we can flip back over to our application and we'll very quickly see a nasty error message that says 22 00:01:20,100 --> 00:01:23,540 Post returned undefined during initialization. 23 00:01:23,540 --> 00:01:26,770 So again we are not allowed to return undefined. 24 00:01:26,810 --> 00:01:31,610 That means that every single time that you reduce or gets called it should always have a return statement 25 00:01:31,670 --> 00:01:36,240 at some point in time and that return statement should always return some type of value. 26 00:01:36,560 --> 00:01:38,130 So we can return a number. 27 00:01:38,300 --> 00:01:43,430 Let's try this out really quickly I'll save this and when I flip back over my apple refreshed and the 28 00:01:43,550 --> 00:01:47,990 error message goes away I can return a string. 29 00:01:48,110 --> 00:01:49,910 I can save that and flip back over. 30 00:01:49,910 --> 00:01:52,020 We'll see that the application still works. 31 00:01:52,070 --> 00:01:55,920 We can return a empty array. 32 00:01:56,060 --> 00:01:57,280 No problem here. 33 00:01:57,380 --> 00:02:00,090 We can do an object no problem here. 34 00:02:00,200 --> 00:02:02,520 We can even do the value no. 35 00:02:02,780 --> 00:02:06,060 So when I save that and flip back over no problem there as well. 36 00:02:06,060 --> 00:02:12,350 It's when I failed to place a return statement or I specifically return a value of undefined that we're 37 00:02:12,350 --> 00:02:14,660 going to run into that error message. 38 00:02:14,660 --> 00:02:20,260 Now if you look at this message very closely it says returned undefined during initialization. 39 00:02:20,540 --> 00:02:25,370 So when redux first boots up it's going to run you of your reducers one time and we'll dive into that 40 00:02:25,370 --> 00:02:27,760 process a bit more closely in just a moment. 41 00:02:27,800 --> 00:02:32,190 But essentially when redux first boots up it's going to call your reducers one time. 42 00:02:32,330 --> 00:02:35,670 And so right you're this during initialization we're right there. 43 00:02:35,750 --> 00:02:40,700 That means that the first time you're douceur was ran it returned the value undefined. 44 00:02:40,700 --> 00:02:46,430 Now the rule around not returning undefined is not only about initialization your reducer can never 45 00:02:46,520 --> 00:02:52,940 ever return undefined whether it is an initialization or at some point in time the future when an action 46 00:02:52,940 --> 00:02:54,280 has been dispatched. 47 00:02:55,160 --> 00:03:01,600 OK so that's rule number one around reducers so we must always return any value besides undefined. 48 00:03:01,860 --> 00:03:03,280 So let's take a quick pause right here. 49 00:03:03,300 --> 00:03:07,140 When we come back the next section we're going to move on to our next big rule around reducers.