﻿1
00:00:01,080 --> 00:00:03,750
‫Let's continue building our application

2
00:00:03,750 --> 00:00:06,180
‫by creating a brand new component

3
00:00:06,180 --> 00:00:09,513
‫and by taking a first look at reusability.

4
00:00:10,620 --> 00:00:14,640
‫But first, let's get the starter files for this project

5
00:00:14,640 --> 00:00:18,360
‫from the files that we downloaded from GitHub.

6
00:00:18,360 --> 00:00:20,760
‫So right here in the starter folder,

7
00:00:20,760 --> 00:00:25,760
‫take these three, copy and then go into your project folder

8
00:00:26,370 --> 00:00:30,303
‫and I will copy them for now here into the public folder.

9
00:00:31,620 --> 00:00:34,470
‫So we have this folder of pizza images,

10
00:00:34,470 --> 00:00:39,300
‫we have this starter data and this index.CSS

11
00:00:39,300 --> 00:00:44,300
‫which we should probably move into this source folder.

12
00:00:44,910 --> 00:00:47,220
‫So make sure that you have these two files here

13
00:00:47,220 --> 00:00:49,083
‫after including the starter files.

14
00:00:50,850 --> 00:00:52,950
‫And now let's come back here

15
00:00:52,950 --> 00:00:55,530
‫and let's create that brand new component

16
00:00:55,530 --> 00:00:57,093
‫that I was talking about.

17
00:00:58,350 --> 00:01:02,793
‫So in React we write new components using functions.

18
00:01:04,710 --> 00:01:08,550
‫So function, and I'm calling this one pizza

19
00:01:08,550 --> 00:01:12,090
‫because it will contain some data about a pizza

20
00:01:12,090 --> 00:01:13,110
‫and then here for now

21
00:01:13,110 --> 00:01:16,680
‫we have no parameters to this function

22
00:01:16,680 --> 00:01:19,290
‫and then the function body.

23
00:01:19,290 --> 00:01:21,930
‫Now in React, there are two important rules

24
00:01:21,930 --> 00:01:24,660
‫when we write components as functions.

25
00:01:24,660 --> 00:01:27,060
‫First, the function name needs to start

26
00:01:27,060 --> 00:01:29,490
‫with an uppercase letter like this

27
00:01:29,490 --> 00:01:33,600
‫and second, the function needs to return some markup.

28
00:01:33,600 --> 00:01:36,120
‫So usually in the form of JSX,

29
00:01:36,120 --> 00:01:40,950
‫but we can't even return nothing, like returning null.

30
00:01:40,950 --> 00:01:45,850
‫But here, let's just return some H2 element, for example

31
00:01:46,860 --> 00:01:49,020
‫and then let's just write pizza.

32
00:01:49,020 --> 00:01:52,890
‫So give it a save, but of course nothing will appear here

33
00:01:52,890 --> 00:01:54,750
‫in the user interface, and that's

34
00:01:54,750 --> 00:01:58,470
‫because we're not including this new component anywhere

35
00:01:58,470 --> 00:02:02,040
‫and even ESLint is warning us about that here.

36
00:02:02,040 --> 00:02:05,550
‫So this yellow line which says pizza is defined,

37
00:02:05,550 --> 00:02:06,870
‫but never used.

38
00:02:06,870 --> 00:02:10,200
‫So again, we're not including this pizza component

39
00:02:10,200 --> 00:02:13,470
‫into our app component which is the component

40
00:02:13,470 --> 00:02:15,820
‫that is currently being rendered on the screen.

41
00:02:16,890 --> 00:02:20,853
‫So we can use our component here just like this.

42
00:02:21,840 --> 00:02:23,130
‫So pizza

43
00:02:23,130 --> 00:02:27,840
‫and then we immediately close the element like this.

44
00:02:27,840 --> 00:02:30,243
‫This however will give us an error.

45
00:02:31,140 --> 00:02:33,900
‫So right here, which we already saw before

46
00:02:33,900 --> 00:02:35,520
‫and the reason for this error

47
00:02:35,520 --> 00:02:40,080
‫is that each component can only return exactly one element,

48
00:02:40,080 --> 00:02:42,363
‫not two elements as we have here.

49
00:02:44,280 --> 00:02:47,760
‫So let's wrap this here into a div

50
00:02:47,760 --> 00:02:50,260
‫and sometimes it can become a bit annoying

51
00:02:51,390 --> 00:02:54,903
‫that VS Code automatically closes these elements for us,

52
00:02:56,310 --> 00:03:00,780
‫but anyway, now we get our pizza in the UI.

53
00:03:00,780 --> 00:03:04,410
‫And so that's because we now nested this pizza component

54
00:03:04,410 --> 00:03:07,320
‫inside this app component.

55
00:03:07,320 --> 00:03:10,350
‫And with nesting, I mean that we basically used

56
00:03:10,350 --> 00:03:13,830
‫or caught this component here inside app.

57
00:03:13,830 --> 00:03:16,350
‫What's very, very important to notice

58
00:03:16,350 --> 00:03:18,930
‫is that by nesting, we do not mean

59
00:03:18,930 --> 00:03:22,563
‫that we write a function inside this other function.

60
00:03:23,580 --> 00:03:25,350
‫So what we should never do

61
00:03:25,350 --> 00:03:28,653
‫is to nest the component declaration itself.

62
00:03:29,760 --> 00:03:33,570
‫So like this, this still works actually,

63
00:03:33,570 --> 00:03:36,060
‫but it's a very, very bad idea

64
00:03:36,060 --> 00:03:38,880
‫for reasons that we will learn about later.

65
00:03:38,880 --> 00:03:41,760
‫So never nest the function declarations,

66
00:03:41,760 --> 00:03:45,960
‫but always declare all your components in the top level.

67
00:03:45,960 --> 00:03:47,400
‫So just like this.

68
00:03:47,400 --> 00:03:50,130
‫So again, when we say nesting components,

69
00:03:50,130 --> 00:03:52,470
‫what we mean is that we call,

70
00:03:52,470 --> 00:03:57,470
‫so we include one component into another like this.

71
00:03:57,810 --> 00:04:01,800
‫Now, okay, so let's now make this pizza component here

72
00:04:01,800 --> 00:04:04,560
‫just a little bit more interesting

73
00:04:04,560 --> 00:04:08,370
‫and for that we are going to use our starter data here.

74
00:04:08,370 --> 00:04:11,190
‫So let's open up data.js

75
00:04:11,190 --> 00:04:16,190
‫and all we need is for is to select everything, then copy it

76
00:04:17,040 --> 00:04:19,473
‫and then let's just paste it here.

77
00:04:20,700 --> 00:04:22,020
‫Okay.

78
00:04:22,020 --> 00:04:25,530
‫So as you see right now we are doing all the development

79
00:04:25,530 --> 00:04:28,260
‫of this application in one big file.

80
00:04:28,260 --> 00:04:31,920
‫While in the real world we divide our code into modules

81
00:04:31,920 --> 00:04:34,950
‫and then include these modules into one another,

82
00:04:34,950 --> 00:04:37,050
‫but here I just want to keep it simple

83
00:04:37,050 --> 00:04:38,823
‫so we're not going to do that yet.

84
00:04:39,930 --> 00:04:43,900
‫So actually we can then delete this data.js

85
00:04:45,030 --> 00:04:46,770
‫and close this down.

86
00:04:46,770 --> 00:04:50,490
‫So for now, we will simply use this here as an example.

87
00:04:50,490 --> 00:04:55,490
‫So I will now copy, paste, let's say this pizza here

88
00:04:57,450 --> 00:05:01,200
‫and use that here as the name

89
00:05:01,200 --> 00:05:03,453
‫and then let's also grab the ingredients.

90
00:05:04,500 --> 00:05:07,770
‫So let's just copy and paste all of this text

91
00:05:07,770 --> 00:05:10,860
‫and notice that I'm really only interested in the text.

92
00:05:10,860 --> 00:05:14,040
‫We don't need, of course, these quotes

93
00:05:14,040 --> 00:05:17,280
‫because, again, this is just like writing HTML

94
00:05:17,280 --> 00:05:20,190
‫and there you also don't need quotes, right?

95
00:05:20,190 --> 00:05:22,560
‫So here, let's create a paragraph

96
00:05:22,560 --> 00:05:27,480
‫and then immediately you see we get this red underline here

97
00:05:27,480 --> 00:05:29,370
‫which is because, again,

98
00:05:29,370 --> 00:05:31,950
‫we are trying to return two elements here

99
00:05:31,950 --> 00:05:33,483
‫which is not allowed.

100
00:05:34,320 --> 00:05:36,070
‫Okay, so here what I like to do

101
00:05:37,380 --> 00:05:42,380
‫is to then move up this line here using a shortcut.

102
00:05:42,450 --> 00:05:44,370
‫So let's see what that is for you.

103
00:05:44,370 --> 00:05:46,440
‫So it's this move line up.

104
00:05:46,440 --> 00:05:49,110
‫So you should probably get used to this shortcut

105
00:05:49,110 --> 00:05:52,800
‫and this one so where we move lines up and down,

106
00:05:52,800 --> 00:05:55,380
‫so like this for example,

107
00:05:55,380 --> 00:05:56,790
‫so that's very, very helpful

108
00:05:56,790 --> 00:05:58,740
‫and we're gonna do that all the time.

109
00:05:58,740 --> 00:06:03,720
‫We just need to get rid of this semicolon that we have here,

110
00:06:03,720 --> 00:06:05,943
‫which was right there.

111
00:06:07,530 --> 00:06:09,690
‫Okay, so we have some text,

112
00:06:09,690 --> 00:06:13,080
‫now let's also actually use this image

113
00:06:13,080 --> 00:06:15,570
‫that we have in the public folder.

114
00:06:15,570 --> 00:06:19,170
‫So yeah, this one right here.

115
00:06:19,170 --> 00:06:21,270
‫So remember how I mentioned in the beginning

116
00:06:21,270 --> 00:06:23,580
‫that all the assets of the app

117
00:06:23,580 --> 00:06:27,120
‫will go into this public folder because Webpack,

118
00:06:27,120 --> 00:06:29,760
‫so the module bundler will then basically

119
00:06:29,760 --> 00:06:31,863
‫automatically get them from there.

120
00:06:33,000 --> 00:06:35,520
‫And so we can now write an image here.

121
00:06:35,520 --> 00:06:37,353
‫So again, just like HTML,

122
00:06:38,850 --> 00:06:43,850
‫and then if we write a path to the image like this,

123
00:06:44,520 --> 00:06:47,703
‫spinaci.jpg,

124
00:06:49,320 --> 00:06:51,930
‫then Webpack will automatically get that here

125
00:06:51,930 --> 00:06:54,600
‫from this folder and let's see if that works.

126
00:06:54,600 --> 00:06:58,053
‫And indeed, there goes our image.

127
00:06:59,010 --> 00:07:02,130
‫Now we get these yellow underline here

128
00:07:02,130 --> 00:07:04,020
‫and that's because an ESLint rule

129
00:07:04,020 --> 00:07:07,950
‫which says that images always should have an alt prop.

130
00:07:07,950 --> 00:07:10,143
‫So let's just add that here.

131
00:07:11,190 --> 00:07:13,473
‫So to make it a little bit more accessible,

132
00:07:15,210 --> 00:07:18,660
‫let's just put the name of the pizza here

133
00:07:18,660 --> 00:07:20,133
‫and then ESLint is happy.

134
00:07:21,030 --> 00:07:23,100
‫So we have this component here

135
00:07:23,100 --> 00:07:27,210
‫and now let's talk about the idea of reusing this component.

136
00:07:27,210 --> 00:07:30,210
‫And for now, all we're going to do to reuse this component

137
00:07:30,210 --> 00:07:32,973
‫is to basically use it several times.

138
00:07:35,280 --> 00:07:38,610
‫So let's do three times just so you can see

139
00:07:38,610 --> 00:07:42,360
‫that now this piece of UI, which is the pizza component,

140
00:07:42,360 --> 00:07:45,180
‫will be included three times.

141
00:07:45,180 --> 00:07:49,023
‫So three times this delicious spinach pizza.

142
00:07:50,280 --> 00:07:53,760
‫Nice. Now of course the data here in all of them

143
00:07:53,760 --> 00:07:57,630
‫is now the same because we didn't customize that data yet,

144
00:07:57,630 --> 00:07:59,370
‫but we will do that later of course,

145
00:07:59,370 --> 00:08:01,470
‫when we talk about props.

146
00:08:01,470 --> 00:08:02,303
‫For now,

147
00:08:02,303 --> 00:08:04,800
‫I just want you to get this really important concept

148
00:08:04,800 --> 00:08:08,010
‫that we can call each piece of the UI

149
00:08:08,010 --> 00:08:11,160
‫or in other words, each component multiple times here

150
00:08:11,160 --> 00:08:12,960
‫in order to reuse it

151
00:08:12,960 --> 00:08:14,820
‫and that's a fundamental concept

152
00:08:14,820 --> 00:08:16,953
‫of writing React applications.

153
00:08:17,940 --> 00:08:20,130
‫Now, just one final thing here,

154
00:08:20,130 --> 00:08:22,020
‫and this has nothing to do with code,

155
00:08:22,020 --> 00:08:23,880
‫is that maybe you have noticed

156
00:08:23,880 --> 00:08:27,640
‫that we get all these colored lines here in the sidebar

157
00:08:28,950 --> 00:08:30,630
‫and even down here.

158
00:08:30,630 --> 00:08:34,500
‫Now the reason for this is that when we create a new project

159
00:08:34,500 --> 00:08:36,420
‫with Create-React-App,

160
00:08:36,420 --> 00:08:39,570
‫it will actually automatically set up this project

161
00:08:39,570 --> 00:08:41,730
‫as a GitHub repo.

162
00:08:41,730 --> 00:08:45,130
‫That's why you also get all these different colors here

163
00:08:46,500 --> 00:08:49,770
‫and also here you see all the changes that we have made

164
00:08:49,770 --> 00:08:51,420
‫to the different files.

165
00:08:51,420 --> 00:08:55,200
‫So here we deleted some stuff, we modified some stuff,

166
00:08:55,200 --> 00:08:58,113
‫and here these images are still untracked.

167
00:08:59,010 --> 00:09:01,230
‫Now, since we're not really going to use GitHub

168
00:09:01,230 --> 00:09:03,420
‫as we build these small projects,

169
00:09:03,420 --> 00:09:08,420
‫I actually just want to remove these colored lines here

170
00:09:08,550 --> 00:09:10,800
‫from the gutter, so from this place,

171
00:09:10,800 --> 00:09:14,340
‫and for that we can go to our settings

172
00:09:14,340 --> 00:09:18,403
‫and then just search for diff decorations.

173
00:09:21,780 --> 00:09:26,100
‫So here the default is all, so let's set it to none.

174
00:09:26,100 --> 00:09:28,590
‫And so then our editor will look

175
00:09:28,590 --> 00:09:31,020
‫just a little bit cleaner here,

176
00:09:31,020 --> 00:09:33,330
‫and we can close down the sidebar

177
00:09:33,330 --> 00:09:35,760
‫and with this, we finish this lecture

178
00:09:35,760 --> 00:09:37,203
‫and are ready to move on.

