Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,250 --> 00:00:05,210
So by now we got components everywhere.
2
00:00:05,210 --> 00:00:06,800
We got the Expenses component,
3
00:00:06,800 --> 00:00:10,200
ExpenseItem, ExpenseDate, app, yeah.
4
00:00:10,200 --> 00:00:11,800
A lot of components.
5
00:00:11,800 --> 00:00:15,100
But that really is what React is all about.
6
00:00:15,100 --> 00:00:16,720
Now, obviously throughout this course
7
00:00:16,720 --> 00:00:18,760
we're going to learn about more features,
8
00:00:18,760 --> 00:00:20,490
which we can add to these components
9
00:00:20,490 --> 00:00:24,490
to make them more interactive, to improve their performance,
10
00:00:24,490 --> 00:00:27,930
to send Http requests and much more,
11
00:00:27,930 --> 00:00:31,190
but in a nutshell, and that's really important,
12
00:00:31,190 --> 00:00:33,200
it's all about components
13
00:00:33,200 --> 00:00:36,700
and it's about props for configuring these components
14
00:00:36,700 --> 00:00:38,440
for passing data into them.
15
00:00:38,440 --> 00:00:43,410
And components really just are these custom HTML elements
16
00:00:43,410 --> 00:00:46,710
where you combine HTML code,
17
00:00:46,710 --> 00:00:49,820
JSX code to be precise and styling
18
00:00:49,820 --> 00:00:54,130
and if you want to, also extra JavaScript logic,
19
00:00:54,130 --> 00:00:55,933
as we're doing it in ExpenseDate.
20
00:00:56,810 --> 00:01:01,180
That is really what components are about.
21
00:01:01,180 --> 00:01:03,970
Now I did mention it multiple times already.
22
00:01:03,970 --> 00:01:05,740
We build all these components
23
00:01:05,740 --> 00:01:07,990
to then build a user interface
24
00:01:07,990 --> 00:01:10,090
and we are already doing that.
25
00:01:10,090 --> 00:01:12,510
In App.js we're using Expenses,
26
00:01:12,510 --> 00:01:14,838
in there we are using ExpenseItem,
27
00:01:14,838 --> 00:01:17,720
in there we're using ExpenseDate.
28
00:01:17,720 --> 00:01:21,220
Generally, this approach of building a user interface
29
00:01:21,220 --> 00:01:25,390
from smaller building blocks is called composition.
30
00:01:25,390 --> 00:01:28,230
Now there's one specifically interesting aspect
31
00:01:28,230 --> 00:01:32,123
of composition, which we haven't seen thus far though.
32
00:01:33,120 --> 00:01:36,450
What if we wanted to create a component
33
00:01:36,450 --> 00:01:39,810
which actually just serves as a shell
34
00:01:39,810 --> 00:01:42,860
around any kind of other content.
35
00:01:42,860 --> 00:01:46,040
At the moment, we have highly specific components.
36
00:01:46,040 --> 00:01:49,410
The ExpenseDate component is about outputting a date
37
00:01:49,410 --> 00:01:54,130
in exactly that format and in exactly that look.
38
00:01:54,130 --> 00:01:56,090
The ExpenseItem component is just
39
00:01:56,090 --> 00:01:58,410
about outputting an expense item.
40
00:01:58,410 --> 00:02:00,870
And in your React application,
41
00:02:00,870 --> 00:02:02,990
you will always have a lot of these
42
00:02:02,990 --> 00:02:04,923
very specific components.
43
00:02:05,780 --> 00:02:10,173
All these components are also just configured through props,
44
00:02:10,173 --> 00:02:13,560
through the date prop, the title prop, the amount prop
45
00:02:13,560 --> 00:02:16,260
in the case of ExpenseItem, for example.
46
00:02:16,260 --> 00:02:18,360
And again, that is fairly standard.
47
00:02:18,360 --> 00:02:20,410
You'll have a lot of these components
48
00:02:20,410 --> 00:02:22,333
in your React applications.
49
00:02:23,360 --> 00:02:26,030
Sometimes however, you wanna have a component
50
00:02:26,030 --> 00:02:29,960
where you don't configure everything through props
51
00:02:29,960 --> 00:02:33,040
but where instead you're able to pass content
52
00:02:33,040 --> 00:02:37,660
between the opening and closing tags of that component.
53
00:02:37,660 --> 00:02:40,513
And here's an example where this would make sense.
54
00:02:42,330 --> 00:02:45,020
If we have a look at our current output,
55
00:02:45,020 --> 00:02:46,470
then we see
56
00:02:46,470 --> 00:02:48,100
two kind of
57
00:02:48,100 --> 00:02:50,930
boxes or containers.
58
00:02:50,930 --> 00:02:54,420
We got a container around all the expense items.
59
00:02:54,420 --> 00:02:58,360
It has a light gray background, and rounded corners,
60
00:02:58,360 --> 00:03:02,570
and a slight drop shadow, which might be hard to see.
61
00:03:02,570 --> 00:03:06,540
And we have a container around the overall expenses list,
62
00:03:06,540 --> 00:03:08,960
with a darker gray background
63
00:03:08,960 --> 00:03:13,143
but also rounded corners and a slight drop shadow.
64
00:03:14,140 --> 00:03:16,150
Now, the idea behind all of these components
65
00:03:16,150 --> 00:03:18,670
is to have reusable building blocks
66
00:03:18,670 --> 00:03:22,480
also to avoid code duplication.
67
00:03:22,480 --> 00:03:23,640
And here at the moment
68
00:03:23,640 --> 00:03:27,200
we at least have some style duplication,
69
00:03:27,200 --> 00:03:31,750
also maybe some HTML structure duplication.
70
00:03:31,750 --> 00:03:35,500
Expenses has a div which surrounds our expenses
71
00:03:35,500 --> 00:03:38,620
which applies certain styles, thanks to this class.
72
00:03:38,620 --> 00:03:41,930
And the same is true for ExpenseItem.
73
00:03:41,930 --> 00:03:44,930
Now, of course not all divs have the same look though.
74
00:03:44,930 --> 00:03:48,870
Inside of our expense items we also got divs like this one
75
00:03:48,870 --> 00:03:51,140
which have a totally different look.
76
00:03:51,140 --> 00:03:55,900
Nonetheless, we could extract this surrounding container div
77
00:03:55,900 --> 00:03:59,500
which we have both in ExpenseItem and Expenses.js
78
00:03:59,500 --> 00:04:02,350
and extract the styles they have in common,
79
00:04:02,350 --> 00:04:04,940
like rounded corners and a drop shadow,
80
00:04:04,940 --> 00:04:06,653
into a separate component.
81
00:04:07,999 --> 00:04:10,690
And we could name such a component card
82
00:04:10,690 --> 00:04:13,650
because this is a specific card look.
83
00:04:13,650 --> 00:04:15,780
And this is not a term I came up with.
84
00:04:15,780 --> 00:04:18,709
Instead in general, in web development,
85
00:04:18,709 --> 00:04:20,510
when you hear the term card,
86
00:04:20,510 --> 00:04:23,540
it's typically means some kind of container look
87
00:04:23,540 --> 00:04:27,020
with rounded corners, drop shadows, and elements like these.
88
00:04:27,020 --> 00:04:29,513
So that's why I'm picking card as a name here.
89
00:04:30,850 --> 00:04:33,130
Now, such a card component
90
00:04:33,130 --> 00:04:35,580
is still a regular component here.
91
00:04:35,580 --> 00:04:38,070
So let's create it as such
92
00:04:38,070 --> 00:04:41,980
but such a card component could now do one main thing.
93
00:04:41,980 --> 00:04:43,550
It could return a div
94
00:04:43,550 --> 00:04:47,810
or any other kind of container HTML element
95
00:04:47,810 --> 00:04:49,678
with a class name
96
00:04:49,678 --> 00:04:51,610
of let's say,
97
00:04:51,610 --> 00:04:53,130
Card.
98
00:04:53,130 --> 00:04:56,010
And then we could add a Card.css file
99
00:04:56,010 --> 00:04:59,130
and import that into that card component.
100
00:04:59,130 --> 00:05:02,313
So import Card.css,
101
00:05:03,680 --> 00:05:08,020
and now in the Card.css file we could have, let's say,
102
00:05:08,020 --> 00:05:13,020
the box-shadow and the border-radius from Expenses.css.
103
00:05:14,520 --> 00:05:17,210
Maybe we would be able to extract more styles
104
00:05:17,210 --> 00:05:19,220
but I'll go with these two.
105
00:05:19,220 --> 00:05:24,220
I'll cut them from Expenses.css and add them to Card.css
106
00:05:24,580 --> 00:05:29,063
inside of a card CSS class selector, which I added there.
107
00:05:31,450 --> 00:05:35,000
I will also go to ExpenseItem.css
108
00:05:35,000 --> 00:05:37,640
and here in ExpenseItem in this class selector,
109
00:05:37,640 --> 00:05:41,140
we all defined a box-shadow and a border-radius.
110
00:05:41,140 --> 00:05:44,283
So I will remove these styles from there as well.
111
00:05:45,520 --> 00:05:47,170
Now, why am I doing that?
112
00:05:47,170 --> 00:05:50,730
Just to show this kind of component.
113
00:05:50,730 --> 00:05:55,730
Now we got this card container component you could say,
114
00:05:55,860 --> 00:05:59,130
which should act as a shell around
115
00:05:59,130 --> 00:06:04,130
either our ExpenseItem content or our Expenses content.
116
00:06:05,030 --> 00:06:06,360
And that's the key.
117
00:06:06,360 --> 00:06:09,700
This card component will now not be configured
118
00:06:09,700 --> 00:06:11,850
through some attributes.
119
00:06:11,850 --> 00:06:14,853
Instead, my idea would be that,
120
00:06:16,020 --> 00:06:18,260
let's say in ExpenseItem,
121
00:06:18,260 --> 00:06:21,220
we can simply replace this built-in div
122
00:06:21,220 --> 00:06:24,290
with our custom card component.
123
00:06:24,290 --> 00:06:26,790
And then we get these predefined styles
124
00:06:26,790 --> 00:06:28,793
for the card automatically.
125
00:06:29,760 --> 00:06:31,970
Maybe we then just want to make them extensible
126
00:06:31,970 --> 00:06:35,900
but we get these predefined styles automatically.
127
00:06:35,900 --> 00:06:36,920
And for that of course,
128
00:06:36,920 --> 00:06:41,920
we wanna import Card from ./Card inside of ExpenseItem.
129
00:06:43,870 --> 00:06:46,720
The thing just is if I save that,
130
00:06:46,720 --> 00:06:49,713
you will see that all the expense items are lost.
131
00:06:50,690 --> 00:06:53,870
And the reason for that is that out of the box,
132
00:06:53,870 --> 00:06:55,080
just like that,
133
00:06:55,080 --> 00:06:59,060
you can't use your custom components as wrappers
134
00:06:59,060 --> 00:07:01,570
around other kind of content.
135
00:07:01,570 --> 00:07:04,720
Having content between opening and closing tags
136
00:07:04,720 --> 00:07:07,260
doesn't work just like that.
137
00:07:07,260 --> 00:07:10,430
But of course it does work for built-in HTML elements,
138
00:07:10,430 --> 00:07:12,480
like the div or the h2 tag.
139
00:07:12,480 --> 00:07:15,130
So it would be nice if we could also make it work
140
00:07:15,130 --> 00:07:17,020
for our custom components
141
00:07:17,020 --> 00:07:20,270
to build such reusable wrapper components
142
00:07:20,270 --> 00:07:21,763
like this, for example.
143
00:07:22,710 --> 00:07:25,630
And of course, React has a solution here.
144
00:07:25,630 --> 00:07:29,390
We can build such wrapper components.
145
00:07:29,390 --> 00:07:33,030
In Card.js we again, when I accept props.
146
00:07:33,030 --> 00:07:36,990
But now, as I mentioned we'll not work with some attributes
147
00:07:36,990 --> 00:07:40,330
but instead we will use one special prop
148
00:07:40,330 --> 00:07:44,240
which is built into React, which every component receives,
149
00:07:44,240 --> 00:07:46,623
even if you're never setting it explicitly.
150
00:07:47,500 --> 00:07:50,400
And that's a prop which value I wanna output
151
00:07:50,400 --> 00:07:53,820
between the opening and closing tag of this div,
152
00:07:53,820 --> 00:07:56,200
inside of the card component function.
153
00:07:56,200 --> 00:07:57,540
It's the props
154
00:07:58,470 --> 00:08:00,063
children prop.
155
00:08:01,370 --> 00:08:04,520
Children is a reserved name.
156
00:08:04,520 --> 00:08:08,100
We don't set a children prop on this card.
157
00:08:08,100 --> 00:08:09,680
I'm setting a class named prop
158
00:08:09,680 --> 00:08:12,750
and actually at the moment, this won't do anything.
159
00:08:12,750 --> 00:08:15,320
But I'm not setting a children prop.
160
00:08:15,320 --> 00:08:17,840
Children, as I said, is a reserved name
161
00:08:17,840 --> 00:08:21,440
and the value of this special children prop
162
00:08:21,440 --> 00:08:23,740
will always be the content
163
00:08:23,740 --> 00:08:26,300
between the opening and closing tags
164
00:08:26,300 --> 00:08:28,010
of your custom component.
165
00:08:28,010 --> 00:08:29,380
So in this case,
166
00:08:29,380 --> 00:08:34,059
this content between the opening and closing card tag,
167
00:08:34,059 --> 00:08:38,380
that is what will be available on props children
168
00:08:38,380 --> 00:08:39,893
inside of that card.
169
00:08:41,059 --> 00:08:45,450
And therefore, if I now save this, we got some content back.
170
00:08:45,450 --> 00:08:47,563
However it's also a bit broken.
171
00:08:48,620 --> 00:08:52,990
And the reason for that is that I extracted some styles
172
00:08:52,990 --> 00:08:56,770
which ExpenseItem and Expenses had in common,
173
00:08:56,770 --> 00:08:59,780
but I had more styles to find for expense items
174
00:08:59,780 --> 00:09:02,840
and these styles are important.
175
00:09:02,840 --> 00:09:04,730
But they're missing now.
176
00:09:04,730 --> 00:09:08,230
I am setting a class name prop on card here
177
00:09:08,230 --> 00:09:10,920
but you must not forget that card
178
00:09:10,920 --> 00:09:14,430
is now a custom component defined by you.
179
00:09:14,430 --> 00:09:16,870
All the default HTML components
180
00:09:16,870 --> 00:09:21,150
out of the box supports class name for adding CSS classes
181
00:09:21,150 --> 00:09:23,640
to the rendered HTML elements.
182
00:09:23,640 --> 00:09:27,300
But your custom components only support what you tell them
183
00:09:27,300 --> 00:09:28,940
to support.
184
00:09:28,940 --> 00:09:33,370
So if you wanna make sure that a class name can be set
185
00:09:33,370 --> 00:09:37,190
on your card component and then has an effect,
186
00:09:37,190 --> 00:09:40,170
we have to tweak the code in the card component.
187
00:09:40,170 --> 00:09:42,390
And here we would probably wanna tweak it
188
00:09:42,390 --> 00:09:47,390
such that we add whatever is set as a class name on card
189
00:09:47,560 --> 00:09:51,170
to this class name string, we're setting as a class name
190
00:09:51,170 --> 00:09:52,830
on that div.
191
00:09:52,830 --> 00:09:56,010
So here we could add a classes constant
192
00:09:56,010 --> 00:09:57,340
which is
193
00:09:57,340 --> 00:09:59,720
card as a default class
194
00:09:59,720 --> 00:10:01,800
which is always applied,
195
00:10:01,800 --> 00:10:03,180
white space
196
00:10:03,180 --> 00:10:04,690
plus
197
00:10:04,690 --> 00:10:06,407
props.className.
198
00:10:07,450 --> 00:10:11,370
So now anything we receive as a class name from outside
199
00:10:11,370 --> 00:10:13,930
is added to that string.
200
00:10:13,930 --> 00:10:16,250
And here we can then dynamically point
201
00:10:16,250 --> 00:10:18,243
at this class, this constant.
202
00:10:20,750 --> 00:10:22,640
So with that, we're now making sure
203
00:10:22,640 --> 00:10:25,840
that any value set on the class named prop
204
00:10:25,840 --> 00:10:28,840
is added to this long string of class names
205
00:10:28,840 --> 00:10:33,220
which is then finally set on the div inside of the card.
206
00:10:33,220 --> 00:10:35,470
And with that all, if we saved it,
207
00:10:35,470 --> 00:10:38,170
we get the same look as before,
208
00:10:38,170 --> 00:10:41,440
not yet for expenses, but for the expense items
209
00:10:41,440 --> 00:10:45,823
but now we have this reusable wrapper component.
210
00:10:46,770 --> 00:10:49,570
And we can also use that in Expenses now.
211
00:10:49,570 --> 00:10:52,300
Here, instead of using a div,
212
00:10:52,300 --> 00:10:54,630
we can also use our card component.
213
00:10:54,630 --> 00:10:58,270
We can import Card from ./Card
214
00:10:58,270 --> 00:11:01,790
and replace this div here with that card.
215
00:11:01,790 --> 00:11:05,350
Still setting a extra class name here as well.
216
00:11:05,350 --> 00:11:07,583
And also replace the closing tag.
217
00:11:08,910 --> 00:11:12,190
And if we do that, now we get the rounded corners back
218
00:11:12,190 --> 00:11:14,393
on that expenses container as well.
219
00:11:16,150 --> 00:11:17,740
Now, why would we do that?
220
00:11:17,740 --> 00:11:20,400
What did we gain by doing that?
221
00:11:20,400 --> 00:11:23,340
Well, in this case, of course, not a whole lot
222
00:11:23,340 --> 00:11:27,560
but we were able to extract some code duplication
223
00:11:27,560 --> 00:11:29,870
from inside our CSS files
224
00:11:29,870 --> 00:11:32,940
into this separate wrapper component.
225
00:11:32,940 --> 00:11:35,630
And it's not just a duplicate CSS code.
226
00:11:35,630 --> 00:11:38,800
We also were able to extract this HTML code,
227
00:11:38,800 --> 00:11:41,320
this JSX code, this div here.
228
00:11:41,320 --> 00:11:43,430
In this case, it's just one div
229
00:11:43,430 --> 00:11:44,500
but throughout the course
230
00:11:44,500 --> 00:11:47,980
you will also see more complex wrapper components
231
00:11:47,980 --> 00:11:51,540
which might have a more complex JSX structure.
232
00:11:51,540 --> 00:11:54,520
Things like modals and alerts.
233
00:11:54,520 --> 00:11:58,110
And in such cases, being able to extract that
234
00:11:58,110 --> 00:12:01,820
often allows you to save a lot of code duplication
235
00:12:01,820 --> 00:12:06,040
and it often allows you to keep your other components clean.
236
00:12:06,040 --> 00:12:09,430
And this is another aspect of composition.
237
00:12:09,430 --> 00:12:12,340
We compose our ExpenseItem component
238
00:12:12,340 --> 00:12:14,360
by using card as a wrapper,
239
00:12:14,360 --> 00:12:17,140
by using some built-in HTML elements,
240
00:12:17,140 --> 00:12:20,400
and by then all the putting in the ExpenseDate.
241
00:12:20,400 --> 00:12:24,180
And all these components and elements are composed together
242
00:12:24,180 --> 00:12:27,280
to form the overall ExpenseItem component
243
00:12:27,280 --> 00:12:30,460
which then again, is used in other components
244
00:12:30,460 --> 00:12:34,840
to in the end overall, build the overall user interface.
245
00:12:34,840 --> 00:12:36,960
So composition is important.
246
00:12:36,960 --> 00:12:40,020
You use it all the time when working with React.
247
00:12:40,020 --> 00:12:44,420
Whenever you combine components, you are using composition.
248
00:12:44,420 --> 00:12:47,480
And an especially important part of composition
249
00:12:47,480 --> 00:12:49,710
is this props children feature
250
00:12:49,710 --> 00:12:53,010
which allows you to also create wrapper components
251
00:12:53,010 --> 00:12:56,220
which is a special type of component, you could say,
252
00:12:56,220 --> 00:12:58,760
which you also sometimes need.
253
00:12:58,760 --> 00:13:01,310
Now again, as with all these things,
254
00:13:01,310 --> 00:13:03,490
you are, of course, going to see many more examples
255
00:13:03,490 --> 00:13:05,173
throughout the course.
19952
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.