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