1 00:00:01,350 --> 00:00:03,330 Instructor: In the last section, we wrapped up our tests 2 00:00:03,330 --> 00:00:05,490 around the comment box component. 3 00:00:05,490 --> 00:00:08,130 Now, rather than moving on to the comment list component 4 00:00:08,130 --> 00:00:10,260 we're going to instead do a little bit of a pivot 5 00:00:10,260 --> 00:00:13,350 and start setting up redux inside of our application. 6 00:00:13,350 --> 00:00:14,640 We'll start wiring up redux. 7 00:00:14,640 --> 00:00:17,130 We'll set up our reducer and our action creator 8 00:00:17,130 --> 00:00:19,890 and then we'll come back to the comment list. 9 00:00:19,890 --> 00:00:21,390 The reason that we're taking this detour 10 00:00:21,390 --> 00:00:24,240 is that the comment list is really dependent upon 11 00:00:24,240 --> 00:00:27,450 accessing our list of comments out of the redux state. 12 00:00:27,450 --> 00:00:28,740 So I think that we really need to work 13 00:00:28,740 --> 00:00:29,910 on that side of things 14 00:00:29,910 --> 00:00:32,130 before we put together the comment list. 15 00:00:32,130 --> 00:00:33,060 So with that in mind 16 00:00:33,060 --> 00:00:34,890 we're gonna flip back over to our code editor 17 00:00:34,890 --> 00:00:38,490 and start working on our redux side of things. 18 00:00:38,490 --> 00:00:41,520 I'll first get started inside my SRC directory 19 00:00:41,520 --> 00:00:42,900 by creating a new folder 20 00:00:42,900 --> 00:00:45,330 that's going to house all of our reducers. 21 00:00:45,330 --> 00:00:46,950 In this case, we just have one reducer 22 00:00:46,950 --> 00:00:49,410 but we're gonna make it anyways. 23 00:00:49,410 --> 00:00:50,910 So in the SRC directory 24 00:00:50,910 --> 00:00:53,673 I'll make a new folder called reducers. 25 00:00:54,510 --> 00:00:56,250 And then inside that folder 26 00:00:56,250 --> 00:00:58,890 I'm gonna create two separate files. 27 00:00:58,890 --> 00:01:02,520 One file is going to contain our comments reducer 28 00:01:02,520 --> 00:01:04,863 so I'll call it simply comments dot js. 29 00:01:05,700 --> 00:01:07,740 And then the other file that we're gonna create 30 00:01:07,740 --> 00:01:11,310 still inside the same directory will be index dot js 31 00:01:11,310 --> 00:01:14,790 and this is going to house our combined reducers call. 32 00:01:14,790 --> 00:01:17,760 Remember, combined reducers takes multiple reducers 33 00:01:17,760 --> 00:01:20,943 and combine some altogether into a single object. 34 00:01:22,950 --> 00:01:24,780 Okay, so let's first get started 35 00:01:24,780 --> 00:01:27,450 inside of our comments dot js file. 36 00:01:27,450 --> 00:01:29,913 So the idea behind this first reducer, 37 00:01:31,811 --> 00:01:33,990 oh, let's pull up a quick diagram here. 38 00:01:33,990 --> 00:01:34,823 Where is it? 39 00:01:34,823 --> 00:01:35,656 Here we go. 40 00:01:36,660 --> 00:01:39,300 So for this single reducer that we're going to have 41 00:01:39,300 --> 00:01:42,660 our intent is to return an array of strings. 42 00:01:42,660 --> 00:01:44,580 Each string is going to represent a comment 43 00:01:44,580 --> 00:01:47,010 that needs to be rendered out on the screen. 44 00:01:47,010 --> 00:01:47,843 For right now, 45 00:01:47,843 --> 00:01:50,850 we'll put together just kind of the skeleton of our reducer, 46 00:01:50,850 --> 00:01:53,610 and we'll come back later to it once we have a better idea 47 00:01:53,610 --> 00:01:56,130 of exactly how this action created down here 48 00:01:56,130 --> 00:01:57,420 is going to work as well. 49 00:01:57,420 --> 00:01:59,490 And we'll make some changes to the reducer. 50 00:01:59,490 --> 00:02:03,000 So for right now, just a little bit of boiler plate. 51 00:02:03,000 --> 00:02:06,210 So in my reducer comments dot js file 52 00:02:06,210 --> 00:02:08,940 I will create an export a function. 53 00:02:08,940 --> 00:02:10,833 So we'll say export default function. 54 00:02:12,660 --> 00:02:15,300 This function is going to be called with a first argument 55 00:02:15,300 --> 00:02:16,920 of our initial state, 56 00:02:16,920 --> 00:02:19,770 which be of our state produced by the reducer, 57 00:02:19,770 --> 00:02:22,083 and then a second argument of the action. 58 00:02:22,950 --> 00:02:26,490 And inside of here, we'll put down our switch statement. 59 00:02:26,490 --> 00:02:28,530 We're gonna look at the action type, 60 00:02:28,530 --> 00:02:29,820 and eventually we'll come back 61 00:02:29,820 --> 00:02:31,950 and add some different types to this reducer. 62 00:02:31,950 --> 00:02:35,730 But for right now, we'll have just one single case 63 00:02:35,730 --> 00:02:37,770 which is the default case. 64 00:02:37,770 --> 00:02:39,570 So in this case, we're just going to return 65 00:02:39,570 --> 00:02:41,283 whatever state we got passed. 66 00:02:42,300 --> 00:02:43,980 Now the last thing we need to take care of here 67 00:02:43,980 --> 00:02:46,560 is to make sure that we initialize our reducer 68 00:02:46,560 --> 00:02:49,830 with some initial state, which we can easily do 69 00:02:49,830 --> 00:02:51,510 by setting a default argument 70 00:02:51,510 --> 00:02:54,810 for our first argument right here or default value. 71 00:02:54,810 --> 00:02:58,950 So we'll default our state to be an empty array 72 00:02:58,950 --> 00:03:00,990 because eventually we're going to want to return 73 00:03:00,990 --> 00:03:02,283 an array of comments. 74 00:03:04,020 --> 00:03:04,950 Okay, so that's it. 75 00:03:04,950 --> 00:03:07,230 That's all we have to do inside this reducer for now. 76 00:03:07,230 --> 00:03:08,520 We'll come back in just a little bit 77 00:03:08,520 --> 00:03:10,203 and add an actual type to it. 78 00:03:11,190 --> 00:03:13,890 We'll now flip over to our index dot js file 79 00:03:13,890 --> 00:03:16,380 where we will import that reducer we just created 80 00:03:16,380 --> 00:03:18,903 and add it to a combined reducers call. 81 00:03:19,920 --> 00:03:24,920 So I'm going to import combined reducers from redux 82 00:03:25,230 --> 00:03:30,230 and comments reducer from reducers comments. 83 00:03:33,060 --> 00:03:34,710 On combined reducers right here, 84 00:03:34,710 --> 00:03:36,450 remember that the Redux Library 85 00:03:36,450 --> 00:03:38,550 exports many different functions. 86 00:03:38,550 --> 00:03:41,910 So we are using the curly braces to specifically get access 87 00:03:41,910 --> 00:03:44,433 to just the combined reducers function. 88 00:03:46,890 --> 00:03:48,630 Then underneath those imports 89 00:03:48,630 --> 00:03:50,640 we will combine all those reducers together 90 00:03:50,640 --> 00:03:52,620 or in this case, just the one I suppose 91 00:03:52,620 --> 00:03:54,420 and export it right away. 92 00:03:54,420 --> 00:03:58,113 So I'll say export default, combine reducers. 93 00:03:59,670 --> 00:04:01,290 We'll say that our state object 94 00:04:01,290 --> 00:04:03,630 will have a single key of comments 95 00:04:03,630 --> 00:04:06,663 and that's going to be where our list of comments exists. 96 00:04:07,530 --> 00:04:11,793 So I'll say comments is, comments reducer. 97 00:04:13,920 --> 00:04:15,030 All right, so that's it. 98 00:04:15,030 --> 00:04:17,940 That's all we need to do right now for our reducer setup. 99 00:04:17,940 --> 00:04:18,779 Now, in the next section, 100 00:04:18,779 --> 00:04:20,760 we'll come back and start setting up redux 101 00:04:20,760 --> 00:04:22,620 inside of our index dot js file 102 00:04:22,620 --> 00:04:24,690 and make sure that the redux side of things 103 00:04:24,690 --> 00:04:25,830 gets included over 104 00:04:25,830 --> 00:04:28,170 to the react side of our application as well. 105 00:04:28,170 --> 00:04:30,620 So quick break and I'll see you in just a minute.