Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,066 --> 00:00:05,666
-: As you work on your React application
2
00:00:05,666 --> 00:00:08,600
and on your React components,
3
00:00:08,600 --> 00:00:11,466
you will notice in any project
4
00:00:11,466 --> 00:00:15,800
that your components eventually become bigger and bigger.
5
00:00:15,800 --> 00:00:20,233
As you have more and more logic and JSX code in them.
6
00:00:20,233 --> 00:00:23,766
This is something which will naturally happen.
7
00:00:23,766 --> 00:00:24,200
This is something which will naturally happen.
8
00:00:24,200 --> 00:00:28,266
That is why React has this component concept.
9
00:00:28,266 --> 00:00:30,733
This allows you to split your application
10
00:00:30,733 --> 00:00:33,466
into smaller building blocks,
11
00:00:33,466 --> 00:00:36,533
where every building block, every component
12
00:00:36,533 --> 00:00:40,033
is focused on one core task, you could say.
13
00:00:40,033 --> 00:00:40,266
is focused on one core task, you could say.
14
00:00:40,266 --> 00:00:43,366
And then you build your overall user interface,
15
00:00:43,366 --> 00:00:45,900
by combining these building blocks.
16
00:00:45,900 --> 00:00:49,066
By doing that, you keep every component on its own
17
00:00:49,066 --> 00:00:51,766
relatively small and manageable,
18
00:00:51,766 --> 00:00:55,266
and you keep its code base small and manageable
19
00:00:55,266 --> 00:00:59,900
and you still can build a complex user interface.
20
00:00:59,900 --> 00:01:04,900
Now, there is no hard rule when to build a new component,
21
00:01:04,900 --> 00:01:08,333
whereas as adding more to an existing component.
22
00:01:08,333 --> 00:01:08,766
whereas as adding more to an existing component.
23
00:01:08,766 --> 00:01:12,500
But in this component here, in this ExpenseItem component,
24
00:01:12,500 --> 00:01:14,033
we could already argue
25
00:01:14,033 --> 00:01:16,633
that it's getting a little bit too big,
26
00:01:16,633 --> 00:01:20,133
though, I will say that this is definitely still all right.
27
00:01:20,133 --> 00:01:25,933
Nonetheless, we could argue that this date object here,
28
00:01:25,933 --> 00:01:30,466
this calendar item which we wanna show on the user interface
29
00:01:30,466 --> 00:01:34,200
could be treated as a separate component.
30
00:01:34,200 --> 00:01:34,433
could be treated as a separate component.
31
00:01:34,433 --> 00:01:37,600
Of course, the same case could be made for the title
32
00:01:37,600 --> 00:01:38,800
or the amount, or a title and amount combined.
33
00:01:38,800 --> 00:01:40,866
or the amount, or a title and amount combined.
34
00:01:40,866 --> 00:01:43,933
As I said, there is no hard rule.
35
00:01:43,933 --> 00:01:45,733
But since for this calendar,
36
00:01:45,733 --> 00:01:48,433
we'll have a couple of helper constants
37
00:01:48,433 --> 00:01:51,166
and we'll have extra styles.
38
00:01:51,166 --> 00:01:51,233
and we'll have extra styles.
39
00:01:51,233 --> 00:01:55,566
I personally like to split this ExpenseItem component here
40
00:01:55,566 --> 00:01:57,500
into two components, the ExpenseItem component
41
00:01:57,500 --> 00:01:59,233
into two components, the ExpenseItem component
42
00:01:59,233 --> 00:02:00,633
which we already have,
43
00:02:00,633 --> 00:02:05,233
and then a new component for rendering this date
44
00:02:05,233 --> 00:02:08,433
in this calendar item look.
45
00:02:08,433 --> 00:02:10,733
And therefore, we're now going to create
46
00:02:10,733 --> 00:02:13,733
our second custom component.
47
00:02:13,733 --> 00:02:14,066
our second custom component.
48
00:02:14,066 --> 00:02:16,400
And for that, I'll create a new file
49
00:02:16,400 --> 00:02:22,133
in the components folder and I'll name it, ExpenseDate.js.
50
00:02:22,133 --> 00:02:22,666
in the components folder and I'll name it, ExpenseDate.js.
51
00:02:22,666 --> 00:02:25,033
Now as always, the file name is up to you
52
00:02:25,033 --> 00:02:28,033
but I recommend that you follow this convention
53
00:02:28,033 --> 00:02:29,400
of naming it like this,
54
00:02:29,400 --> 00:02:32,600
starting with a capital character, one word,
55
00:02:32,600 --> 00:02:34,233
with every extra word
56
00:02:34,233 --> 00:02:36,433
also starting with a capital character.
57
00:02:36,433 --> 00:02:38,900
And again, it should also express
58
00:02:38,900 --> 00:02:41,066
what's happening inside of that file.
59
00:02:41,066 --> 00:02:44,000
What that component will be about.
60
00:02:44,000 --> 00:02:45,666
And this component will be
61
00:02:45,666 --> 00:02:48,700
about rendering the date of an expense.
62
00:02:48,700 --> 00:02:52,333
Hence this sounds like a fitting name to me.
63
00:02:52,333 --> 00:02:54,466
Now in there, we create a new component
64
00:02:54,466 --> 00:02:57,366
and that still is a function.
65
00:02:57,366 --> 00:02:57,466
and that still is a function.
66
00:02:57,466 --> 00:03:01,066
Components and React are just regular functions
67
00:03:01,066 --> 00:03:06,233
with the extra twist of returning this JSX code.
68
00:03:06,233 --> 00:03:08,466
Now I'll name it ExpenseDate, again,
69
00:03:08,466 --> 00:03:12,700
following this convention that we repeat the file name here.
70
00:03:12,700 --> 00:03:12,866
following this convention that we repeat the file name here.
71
00:03:12,866 --> 00:03:16,433
And I'll then export it to make it reusable
72
00:03:16,433 --> 00:03:20,000
outside of this file, like this.
73
00:03:20,000 --> 00:03:24,233
Now in here, I wanna calculate month, day and year.
74
00:03:24,233 --> 00:03:25,800
Now in here, I wanna calculate month, day and year.
75
00:03:25,800 --> 00:03:28,833
So I'll cut this logic from ExpenseItem
76
00:03:28,833 --> 00:03:31,766
and move it to ExpenseDate.
77
00:03:31,766 --> 00:03:32,000
and move it to ExpenseDate.
78
00:03:32,000 --> 00:03:33,266
And therefore, of course,
79
00:03:33,266 --> 00:03:37,300
I need to accept props as a parameter here.
80
00:03:37,300 --> 00:03:40,433
And I do now expect that this component will be used
81
00:03:40,433 --> 00:03:44,566
such that a date prop is set.
82
00:03:44,566 --> 00:03:48,733
Now in ExpenseItem, I wanna use this new component.
83
00:03:48,733 --> 00:03:48,966
Now in ExpenseItem, I wanna use this new component.
84
00:03:48,966 --> 00:03:50,333
But before we do that,
85
00:03:50,333 --> 00:03:53,300
I will actually grab this div with the month,
86
00:03:53,300 --> 00:03:54,433
I will actually grab this div with the month,
87
00:03:54,433 --> 00:03:57,200
year and day inside of it and cut it as well,
88
00:03:57,200 --> 00:04:00,533
and return that as JSX code inside of ExpenseDate.js.
89
00:04:00,533 --> 00:04:03,733
and return that as JSX code inside of ExpenseDate.js.
90
00:04:03,733 --> 00:04:04,466
and return that as JSX code inside of ExpenseDate.js.
91
00:04:04,466 --> 00:04:09,866
And now in ExpenseItem, at the top we can add a import.
92
00:04:09,866 --> 00:04:16,132
And import ExpenseDate from ./ExpenseDate, like this.
93
00:04:16,132 --> 00:04:19,766
./ExpenseDate because we're looking in the same folder
94
00:04:19,766 --> 00:04:23,200
as the ExpenseItem.js file lives in.
95
00:04:23,200 --> 00:04:24,666
And now here,
96
00:04:24,666 --> 00:04:28,933
where we previously had our JSX code for the date,
97
00:04:28,933 --> 00:04:34,600
we instead output this ExpenseDate component.
98
00:04:34,600 --> 00:04:35,933
And as a side note,
99
00:04:35,933 --> 00:04:38,433
if you have a component that has no content
100
00:04:38,433 --> 00:04:40,500
between the opening and closing tags,
101
00:04:40,500 --> 00:04:40,833
between the opening and closing tags,
102
00:04:40,833 --> 00:04:43,366
you can also write it a little bit differently.
103
00:04:43,366 --> 00:04:47,200
You can write it as a self-closing element like this.
104
00:04:47,200 --> 00:04:49,600
This is then also, okay.
105
00:04:49,600 --> 00:04:49,900
This is then also, okay.
106
00:04:49,900 --> 00:04:53,100
And we could do the same here in App.js
107
00:04:53,100 --> 00:04:57,100
with the ExpenseItem, since this also has no content
108
00:04:57,100 --> 00:04:59,866
between opening and closing tag.
109
00:04:59,866 --> 00:05:00,166
between opening and closing tag.
110
00:05:00,166 --> 00:05:03,966
This is totally optional but it is quite common to do that,
111
00:05:03,966 --> 00:05:09,000
if there is no content between the opening and closing tag.
112
00:05:09,000 --> 00:05:14,833
So now we're using ExpenseDate in the ExpenseItem component.
113
00:05:14,833 --> 00:05:16,166
And we're doing this
114
00:05:16,166 --> 00:05:20,133
to move some of the JSX code and JavaScript logic
115
00:05:20,133 --> 00:05:24,333
out of the ExpenseItem into a separate component.
116
00:05:24,333 --> 00:05:24,633
out of the ExpenseItem into a separate component.
117
00:05:24,633 --> 00:05:27,900
This component is now again, also reusable
118
00:05:27,900 --> 00:05:31,533
and we can use it anywhere in this React application,
119
00:05:31,533 --> 00:05:31,733
and we can use it anywhere in this React application,
120
00:05:31,733 --> 00:05:33,633
not just at ExpenseItem.
121
00:05:33,633 --> 00:05:36,733
We could use it elsewhere as well, if we wanted to.
122
00:05:36,733 --> 00:05:41,133
And it now also helps us with keeping ExpenseItem lean.
123
00:05:41,133 --> 00:05:42,800
This is now again, shorter
124
00:05:42,800 --> 00:05:46,566
and therefore maybe a bit easier to maintain and manage.
125
00:05:46,566 --> 00:05:46,733
and therefore maybe a bit easier to maintain and manage.
126
00:05:46,733 --> 00:05:49,333
And the bigger your application gets
127
00:05:49,333 --> 00:05:51,233
and your components become,
128
00:05:51,233 --> 00:05:54,366
the more you wanna look into splitting them up.
129
00:05:54,366 --> 00:05:54,633
the more you wanna look into splitting them up.
130
00:05:54,633 --> 00:05:58,066
Though, again, just to really emphasize this,
131
00:05:58,066 --> 00:06:01,300
there is no hard rule on when to split.
132
00:06:01,300 --> 00:06:04,066
This is something which will come with experience
133
00:06:04,066 --> 00:06:06,333
and you'll also see plenty of examples
134
00:06:06,333 --> 00:06:08,500
throughout this course.
135
00:06:08,500 --> 00:06:10,266
Now we must not forget though,
136
00:06:10,266 --> 00:06:14,566
that our new ExpenseDate component needs a prop.
137
00:06:14,566 --> 00:06:14,966
that our new ExpenseDate component needs a prop.
138
00:06:14,966 --> 00:06:16,566
It needs the date prop,
139
00:06:16,566 --> 00:06:18,966
at least that's the name I'm referring to here
140
00:06:18,966 --> 00:06:22,933
inside of ExpenseDate, to well, extract that date
141
00:06:22,933 --> 00:06:26,966
and format it and extract the month, day and year.
142
00:06:26,966 --> 00:06:30,200
Hence on ExpenseItem or in ExpenseItem
143
00:06:30,200 --> 00:06:32,233
when we use ExpenseDate, we should set the date prop.
144
00:06:32,233 --> 00:06:34,666
when we use ExpenseDate, we should set the date prop.
145
00:06:34,666 --> 00:06:35,033
when we use ExpenseDate, we should set the date prop.
146
00:06:35,033 --> 00:06:38,100
And if you use the different name here in ExpenseDate,
147
00:06:38,100 --> 00:06:38,133
And if you use the different name here in ExpenseDate,
148
00:06:38,133 --> 00:06:40,766
you, of course, wanna use a different name here
149
00:06:40,766 --> 00:06:43,466
instead of date as well.
150
00:06:43,466 --> 00:06:48,766
Now, the value which I pass here will just be props.date.
151
00:06:48,766 --> 00:06:48,866
Now, the value which I pass here will just be props.date.
152
00:06:48,866 --> 00:06:51,433
And I know that this is now the point
153
00:06:51,433 --> 00:06:54,266
where it can get confusing,
154
00:06:54,266 --> 00:06:54,433
where it can get confusing,
155
00:06:54,433 --> 00:06:58,400
because now we're basically funneling some data
156
00:06:58,400 --> 00:07:02,133
through multiple levels of components.
157
00:07:02,133 --> 00:07:06,533
In the end, we can see this component tree come to life now,
158
00:07:06,533 --> 00:07:06,666
In the end, we can see this component tree come to life now,
159
00:07:06,666 --> 00:07:09,533
because now we don't just have the App component
160
00:07:09,533 --> 00:07:12,100
and then one other custom component,
161
00:07:12,100 --> 00:07:14,300
but we have multiple custom components
162
00:07:14,300 --> 00:07:17,233
and they are nested inside of each other.
163
00:07:17,233 --> 00:07:17,866
and they are nested inside of each other.
164
00:07:17,866 --> 00:07:19,733
Just to make this really clear again,
165
00:07:19,733 --> 00:07:23,500
in the App component, we're using the ExpenseItem component
166
00:07:23,500 --> 00:07:23,933
in the App component, we're using the ExpenseItem component
167
00:07:23,933 --> 00:07:24,166
in the App component, we're using the ExpenseItem component
168
00:07:24,166 --> 00:07:26,633
and inside of the ExpenseItem component,
169
00:07:26,633 --> 00:07:26,666
and inside of the ExpenseItem component,
170
00:07:26,666 --> 00:07:29,033
we're using the ExpenseDate component.
171
00:07:29,033 --> 00:07:29,266
we're using the ExpenseDate component.
172
00:07:29,266 --> 00:07:33,333
And not just that, we're also forwarding our data
173
00:07:33,333 --> 00:07:37,433
with the help of props through multiple components.
174
00:07:37,433 --> 00:07:37,733
with the help of props through multiple components.
175
00:07:37,733 --> 00:07:42,600
We're passing data from the App component into ExpenseItem
176
00:07:42,600 --> 00:07:42,933
We're passing data from the App component into ExpenseItem
177
00:07:42,933 --> 00:07:46,366
and in ExpenseItem, were outputting some of the data.
178
00:07:46,366 --> 00:07:50,166
But other parts of the data to be precise, the date,
179
00:07:50,166 --> 00:07:50,233
But other parts of the data to be precise, the date,
180
00:07:50,233 --> 00:07:53,500
which we here, also already received through props,
181
00:07:53,500 --> 00:07:53,600
which we here, also already received through props,
182
00:07:53,600 --> 00:07:56,366
it's then forwarded even further
183
00:07:56,366 --> 00:08:00,700
into the ExpenseDate component, again, by using props.
184
00:08:00,700 --> 00:08:04,266
Because props are our way of passing data
185
00:08:04,266 --> 00:08:06,066
from component A to B.
186
00:08:06,066 --> 00:08:09,733
And it's also totally fine to have a component
187
00:08:09,733 --> 00:08:12,833
which just passes data on.
188
00:08:12,833 --> 00:08:12,933
which just passes data on.
189
00:08:12,933 --> 00:08:14,633
So in the end you could say,
190
00:08:14,633 --> 00:08:17,500
we pass data from App to ExpenseDate,
191
00:08:17,500 --> 00:08:19,433
the date, to be precise, is passed on.
192
00:08:19,433 --> 00:08:23,300
And we passed it on through ExpenseItem,
193
00:08:23,300 --> 00:08:23,500
And we passed it on through ExpenseItem,
194
00:08:23,500 --> 00:08:25,800
because that's how props work.
195
00:08:25,800 --> 00:08:31,300
We pass data from a component to a direct child component.
196
00:08:31,300 --> 00:08:31,366
We pass data from a component to a direct child component.
197
00:08:31,366 --> 00:08:33,066
So to a component which is used
198
00:08:33,066 --> 00:08:35,765
in that other components JSX code
199
00:08:35,765 --> 00:08:38,732
and we can't skip such a component.
200
00:08:38,732 --> 00:08:38,933
and we can't skip such a component.
201
00:08:38,933 --> 00:08:42,166
So if the date should be passed to ExpenseDate in the end.
202
00:08:42,166 --> 00:08:45,366
Here, in this case, since we use ExpenseDate
203
00:08:45,366 --> 00:08:46,933
inside of ExpenseItem,
204
00:08:46,933 --> 00:08:50,400
we first of all have to pass that data to ExpenseItem
205
00:08:50,400 --> 00:08:53,933
and then forward it to ExpenseDate.
206
00:08:53,933 --> 00:08:57,333
I hope this is clear, if it's not entirely clear yet,
207
00:08:57,333 --> 00:09:00,833
that's also something you're going to see in great detail
208
00:09:00,833 --> 00:09:04,933
and in many examples throughout the course.
209
00:09:04,933 --> 00:09:07,300
But with that, we are forwarding the date
210
00:09:07,300 --> 00:09:09,600
and we get this separate component now.
211
00:09:09,600 --> 00:09:11,466
If we now saved that all,
212
00:09:11,466 --> 00:09:15,166
we again see our date data being output here.
213
00:09:15,166 --> 00:09:15,600
we again see our date data being output here.
214
00:09:15,600 --> 00:09:17,566
Now we also wanna have some styling
215
00:09:17,566 --> 00:09:22,900
and therefore I'll add a new file, the ExpenseDate.css file.
216
00:09:22,900 --> 00:09:23,066
and therefore I'll add a new file, the ExpenseDate.css file.
217
00:09:23,066 --> 00:09:26,100
And you, again, find that file attached.
218
00:09:26,100 --> 00:09:28,566
You can just replace yours with it
219
00:09:28,566 --> 00:09:32,300
or open it and copy everything you find in the attached file
220
00:09:32,300 --> 00:09:36,100
into your ExpenseDate.css file.
221
00:09:36,100 --> 00:09:36,366
into your ExpenseDate.css file.
222
00:09:36,366 --> 00:09:38,066
And once you did that,
223
00:09:38,066 --> 00:09:41,400
you can go to the ExpenseDate.js file.
224
00:09:41,400 --> 00:09:43,400
And at the very top of that file,
225
00:09:43,400 --> 00:09:48,533
import ./ExpenseDate.css
226
00:09:48,533 --> 00:09:48,666
import ./ExpenseDate.css
227
00:09:48,666 --> 00:09:52,933
to make the entire project set up aware of that CSS file.
228
00:09:52,933 --> 00:09:56,033
And then add a couple of CSS classes here.
229
00:09:56,033 --> 00:09:56,400
And then add a couple of CSS classes here.
230
00:09:56,400 --> 00:09:59,900
On the surrounding div, we add a class name
231
00:09:59,900 --> 00:10:03,200
of expense-date, for example.
232
00:10:03,200 --> 00:10:11,000
On that month div, we add expense-date__month.
233
00:10:11,000 --> 00:10:11,566
On that month div, we add expense-date__month.
234
00:10:11,566 --> 00:10:18,200
For the year div, we add expense-date__year.
235
00:10:18,200 --> 00:10:18,566
For the year div, we add expense-date__year.
236
00:10:18,566 --> 00:10:24,633
And for the day, we add expense-date__day.
237
00:10:24,633 --> 00:10:27,066
And here I imported the wrong file.
238
00:10:27,066 --> 00:10:30,700
This should of course be ExpenseDate.css.
239
00:10:30,700 --> 00:10:31,300
This should of course be ExpenseDate.css.
240
00:10:31,300 --> 00:10:33,300
And with all of that done, if we saved this.
241
00:10:33,300 --> 00:10:38,433
Now, we got this nice look here for our dates.
242
00:10:38,433 --> 00:10:38,566
Now, we got this nice look here for our dates.
243
00:10:38,566 --> 00:10:40,300
And this looks much nicer.
244
00:10:40,300 --> 00:10:44,333
And we also practiced how we can add yet another component.
245
00:10:44,333 --> 00:10:46,866
And adding this component was optional,
246
00:10:46,866 --> 00:10:49,633
we could have put everything into ExpenseItem.
247
00:10:49,633 --> 00:10:49,666
we could have put everything into ExpenseItem.
248
00:10:49,666 --> 00:10:51,133
The entire JSX code
249
00:10:51,133 --> 00:10:53,166
which we have in a separate component now,
250
00:10:53,166 --> 00:10:54,666
the helper constants,
251
00:10:54,666 --> 00:10:57,266
we could have added this all into one component.
252
00:10:57,266 --> 00:10:57,500
we could have added this all into one component.
253
00:10:57,500 --> 00:10:59,800
We could have also added the CSS code here
254
00:10:59,800 --> 00:11:02,500
in the ExpenseItem.css code.
255
00:11:02,500 --> 00:11:04,500
But then this ExpenseItem component
256
00:11:04,500 --> 00:11:06,333
would become bigger and bigger.
257
00:11:06,333 --> 00:11:06,633
would become bigger and bigger.
258
00:11:06,633 --> 00:11:09,533
And whilst in this case, it would have still been fine.
259
00:11:09,533 --> 00:11:11,566
It is generally a good practice
260
00:11:11,566 --> 00:11:14,300
to keep your components small and focused,
261
00:11:14,300 --> 00:11:17,833
and that is why we did extract this extra component here.
21306
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.