Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,190 --> 00:00:05,890
Now to work towards this expense tracker,
2
00:00:05,890 --> 00:00:10,180
let's again think about the kind of components we got there.
3
00:00:10,180 --> 00:00:12,300
And you might remember that in the end,
4
00:00:12,300 --> 00:00:16,379
we were able to split up this simple looking application
5
00:00:16,379 --> 00:00:18,460
into a bunch of Components.
6
00:00:18,460 --> 00:00:21,550
In the chart, we had the individual chart bars,
7
00:00:21,550 --> 00:00:23,880
we had the individual expense items,
8
00:00:23,880 --> 00:00:26,670
we had the form for adding a new expense
9
00:00:26,670 --> 00:00:29,020
and therefore we get a bunch of Components
10
00:00:29,020 --> 00:00:32,290
and a bunch of Components we could get started with.
11
00:00:32,290 --> 00:00:35,570
Now, I don't we get started with the expense item though.
12
00:00:35,570 --> 00:00:38,610
So this row where we have the title and the amount
13
00:00:38,610 --> 00:00:41,200
and the date of an expense.
14
00:00:41,200 --> 00:00:44,990
And therefore we now wanna build our first own Component.
15
00:00:44,990 --> 00:00:47,410
Now we could add it here in App JS,
16
00:00:47,410 --> 00:00:50,710
but it is considered a good and best practice
17
00:00:50,710 --> 00:00:54,493
to actually put new Components into new files,
18
00:00:54,493 --> 00:00:57,630
so that you have one file per Component.
19
00:00:57,630 --> 00:01:00,270
And that will mean that in a React project
20
00:01:00,270 --> 00:01:03,330
you will end up with dozens or hundreds of files
21
00:01:03,330 --> 00:01:05,550
because on React project you'll have dozens
22
00:01:05,550 --> 00:01:07,460
or hundreds of Components in the end
23
00:01:07,460 --> 00:01:09,750
and that's absolutely normal.
24
00:01:09,750 --> 00:01:11,970
Now to organize our source code a little bit
25
00:01:11,970 --> 00:01:16,240
here in the source folder, I'll add a Components folder,
26
00:01:16,240 --> 00:01:19,550
which will hold all these Components source files.
27
00:01:19,550 --> 00:01:22,260
Now I will not move App JS in there
28
00:01:22,260 --> 00:01:25,220
because this app Component will be a special
29
00:01:25,220 --> 00:01:26,560
kind of Component.
30
00:01:26,560 --> 00:01:29,880
Not regarding its code, that will be the same code
31
00:01:29,880 --> 00:01:32,130
as you see it in all the other Components,
32
00:01:32,130 --> 00:01:35,390
but regarding its role in the application.
33
00:01:35,390 --> 00:01:38,570
It will be our so-called, root Component,
34
00:01:38,570 --> 00:01:41,400
which means it's the main Component being rendered here
35
00:01:41,400 --> 00:01:44,260
in our starting file in index JS.
36
00:01:44,260 --> 00:01:47,830
And all our Components will be either nested
37
00:01:47,830 --> 00:01:51,520
inside of App JS or nested inside of our Components
38
00:01:51,520 --> 00:01:54,980
which then in turn again, are nested somewhere else.
39
00:01:54,980 --> 00:01:57,860
Because ultimately with React,
40
00:01:57,860 --> 00:02:00,810
we build a Component Tree, you could say.
41
00:02:00,810 --> 00:02:03,690
You have two main app Component at the top,
42
00:02:03,690 --> 00:02:05,750
and then below that, you could have any other
43
00:02:05,750 --> 00:02:08,570
kinds of custom HTML elements.
44
00:02:08,570 --> 00:02:11,510
So, other kinds of Components, which in the end
45
00:02:11,510 --> 00:02:14,270
hold other pieces of the user interface.
46
00:02:14,270 --> 00:02:16,850
And big application stare for, of course
47
00:02:16,850 --> 00:02:19,860
can result in quite big Component Trees
48
00:02:19,860 --> 00:02:23,730
where only the top most Component is rendered directly
49
00:02:23,730 --> 00:02:25,490
into the HTML page,
50
00:02:25,490 --> 00:02:29,623
with help of that React dom render instruction.
51
00:02:30,560 --> 00:02:32,850
All the other Components will not be rendered
52
00:02:32,850 --> 00:02:35,890
with this instruction but instead will be used
53
00:02:35,890 --> 00:02:40,540
as regular HTML elements inside of our HTML code,
54
00:02:40,540 --> 00:02:42,410
inside of our Components.
55
00:02:42,410 --> 00:02:44,783
But you'll see that all in action in a second.
56
00:02:45,680 --> 00:02:49,130
So instead of the Components folder, I'll add a new file,
57
00:02:49,130 --> 00:02:50,510
and I'm using shortcuts here,
58
00:02:50,510 --> 00:02:52,330
you can also use these buttons, of course,
59
00:02:52,330 --> 00:02:54,530
for adding folders and files.
60
00:02:54,530 --> 00:02:57,750
And I'll add a new file here, which I will name,
61
00:02:57,750 --> 00:03:01,500
expense item dot JS, because this will hold
62
00:03:01,500 --> 00:03:03,333
my expense item Component.
63
00:03:04,220 --> 00:03:06,560
Now, one word about the fall name.
64
00:03:06,560 --> 00:03:09,460
Here again, you are generally free
65
00:03:09,460 --> 00:03:12,880
to name this however you want, but it's a common convention
66
00:03:12,880 --> 00:03:16,140
in React applications to name it like this.
67
00:03:16,140 --> 00:03:19,840
Starting with a capital character, one word,
68
00:03:19,840 --> 00:03:23,220
where if you combine multiple words in one word,
69
00:03:23,220 --> 00:03:25,930
every sub word, so to say,
70
00:03:25,930 --> 00:03:30,590
starts with a new capital character like the I here in item.
71
00:03:30,590 --> 00:03:33,170
And then inside of this Component,
72
00:03:33,170 --> 00:03:34,840
if the file is named like this,
73
00:03:34,840 --> 00:03:37,270
you'll of course should have a Component
74
00:03:37,270 --> 00:03:41,010
that is dealing with rendering expense item data.
75
00:03:41,010 --> 00:03:45,010
So that the file name tells us which kind of logic
76
00:03:45,010 --> 00:03:49,313
and HTML code will live inside of that file.
77
00:03:50,270 --> 00:03:53,030
So now let's write the concrete Component code
78
00:03:53,030 --> 00:03:54,690
in that new file.
79
00:03:54,690 --> 00:03:57,390
And of course that means that we now finally
80
00:03:57,390 --> 00:04:01,500
have to understand what Component code is.
81
00:04:01,500 --> 00:04:04,340
How is a Component written in React?
82
00:04:04,340 --> 00:04:06,500
Well, we already saw it in App JS.
83
00:04:06,500 --> 00:04:09,020
It's just a function.
84
00:04:09,020 --> 00:04:13,170
And that is a key takeaway, that is super important,
85
00:04:13,170 --> 00:04:14,980
definitely keep that in mind.
86
00:04:14,980 --> 00:04:19,980
A Component in React is just a JavaScript function.
87
00:04:21,350 --> 00:04:25,870
A special kind of function special regarding what it returns
88
00:04:25,870 --> 00:04:30,400
that it does return this JSX code, but other than that,
89
00:04:30,400 --> 00:04:33,503
it's just a Java script function.
90
00:04:34,990 --> 00:04:36,440
Therefore, if we wanna create
91
00:04:36,440 --> 00:04:40,500
an expense item Component here, we add the function keyword,
92
00:04:40,500 --> 00:04:43,040
give it any name of our choice, though again,
93
00:04:43,040 --> 00:04:45,820
the convention is to repeat the file name here
94
00:04:45,820 --> 00:04:49,320
just without the extension, so expense item.
95
00:04:49,320 --> 00:04:54,320
And then in here we return the HTML code, the JSX code,
96
00:04:55,800 --> 00:04:59,683
which should be brought onto the screen by this Component.
97
00:05:00,540 --> 00:05:03,570
So for the moment I'll add an h2 tag here
98
00:05:03,570 --> 00:05:06,793
and say expense item in there.
99
00:05:07,780 --> 00:05:12,780
Now that's our first very basic custom made Component.
100
00:05:12,950 --> 00:05:15,750
And of course it's quite similar to the app Component
101
00:05:15,750 --> 00:05:18,970
which I gave to you, because in the end,
102
00:05:18,970 --> 00:05:20,830
all Components are similar.
103
00:05:20,830 --> 00:05:25,830
Components are just functions which then return HTML code.
104
00:05:25,830 --> 00:05:29,460
And of course the HTML code do you do return in a Component
105
00:05:29,460 --> 00:05:32,880
depends on what the Components should render on the screen.
106
00:05:32,880 --> 00:05:36,320
Of course, that's not our final HTML code
107
00:05:36,320 --> 00:05:38,740
for this kind of Component.
108
00:05:38,740 --> 00:05:42,280
Now to use this Component, we need to export it.
109
00:05:42,280 --> 00:05:44,830
Otherwise it's only usable inside of that file
110
00:05:44,830 --> 00:05:46,980
and that's not helpful to us.
111
00:05:46,980 --> 00:05:50,390
So, we add an export statement which in JavaScript
112
00:05:50,390 --> 00:05:51,960
simply looks like this.
113
00:05:51,960 --> 00:05:56,363
We export this function as the default for this file.
114
00:05:58,150 --> 00:06:00,910
Now we can import it in another file.
115
00:06:00,910 --> 00:06:03,540
And as I mentioned, we're not going to import it
116
00:06:03,540 --> 00:06:06,820
in index JS, and we're not going to render it like this,
117
00:06:06,820 --> 00:06:10,600
because we only do this once for our root Component.
118
00:06:10,600 --> 00:06:13,910
Instead, I now wanna use my custom Component here
119
00:06:13,910 --> 00:06:18,510
like a regular HTML element, so like h2 element,
120
00:06:18,510 --> 00:06:22,593
inside of the HTML code written in the app Component here.
121
00:06:23,470 --> 00:06:26,500
Hence at the top of the app JS file,
122
00:06:26,500 --> 00:06:29,740
we should now import our accustomed Component.
123
00:06:29,740 --> 00:06:34,740
So we simply import expense item from dot slash Components,
124
00:06:37,000 --> 00:06:39,080
we need to go into this Components folder,
125
00:06:39,080 --> 00:06:43,290
because that's where the file lives, slash expense item,
126
00:06:43,290 --> 00:06:45,093
and then without the extension.
127
00:06:46,400 --> 00:06:49,770
This path in the end tells JavaScript
128
00:06:49,770 --> 00:06:52,200
that we're looking for expense item file
129
00:06:52,200 --> 00:06:56,860
in a Components folder which sits next to this app JS file
130
00:06:56,860 --> 00:06:58,893
in which we have does import statement.
131
00:07:00,590 --> 00:07:03,470
Now that this is imported, the cool thing is
132
00:07:03,470 --> 00:07:07,070
that we can just use this like an HTML element.
133
00:07:07,070 --> 00:07:10,150
So we can, for example, replace this paragraph
134
00:07:10,150 --> 00:07:14,380
with expense item like this opening and closing
135
00:07:14,380 --> 00:07:15,493
written like this.
136
00:07:16,410 --> 00:07:20,070
The key difference to the built in HTML elements
137
00:07:20,070 --> 00:07:23,610
just is that it's not starting with a lowercase character,
138
00:07:23,610 --> 00:07:25,850
but with an uppercase character.
139
00:07:25,850 --> 00:07:28,530
And indeed your own custom Components
140
00:07:28,530 --> 00:07:31,840
must start with an uppercase character
141
00:07:31,840 --> 00:07:34,280
when you're using DOM JSX code like this,
142
00:07:34,280 --> 00:07:36,510
so that React is able to detect
143
00:07:36,510 --> 00:07:38,860
that this is a custom Component.
144
00:07:38,860 --> 00:07:41,680
Because the simple rule which React applies
145
00:07:41,680 --> 00:07:46,680
is that lowercase elements are built in HTML elements.
146
00:07:47,750 --> 00:07:51,050
So it will look for them as built in elements.
147
00:07:51,050 --> 00:07:54,820
Whereas elements starting with an uppercase character
148
00:07:54,820 --> 00:07:58,143
are elements defined by you or some other developer.
149
00:07:59,000 --> 00:08:01,310
So this is a custom made Component
150
00:08:01,310 --> 00:08:04,770
and we have to use the name which we use here in the import
151
00:08:04,770 --> 00:08:07,890
as a name here, because that's how this connection
152
00:08:07,890 --> 00:08:08,980
is established.
153
00:08:08,980 --> 00:08:11,700
This tells React that we want to use this Component
154
00:08:11,700 --> 00:08:13,900
which is imported under this name
155
00:08:13,900 --> 00:08:15,853
from the expense item file.
156
00:08:16,770 --> 00:08:19,470
If we now save everything, all the files,
157
00:08:19,470 --> 00:08:21,710
and we go back to the browser,
158
00:08:21,710 --> 00:08:25,590
we see expense items showing up here,
159
00:08:25,590 --> 00:08:27,940
which of course is the H two tag rendered
160
00:08:27,940 --> 00:08:30,670
by our own custom Component.
161
00:08:30,670 --> 00:08:33,912
Because we now created and imported
162
00:08:33,912 --> 00:08:36,059
used this custom Component.
163
00:08:36,059 --> 00:08:38,440
And it's always these steps.
164
00:08:38,440 --> 00:08:41,659
You create a Component, which is in the end, just a function
165
00:08:41,659 --> 00:08:45,560
of returning some HTML code, you then export it.
166
00:08:45,560 --> 00:08:48,500
And then ultimately you import it in the file
167
00:08:48,500 --> 00:08:51,980
where you wanna use it so that they're in the JSX code,
168
00:08:51,980 --> 00:08:55,480
you can use it like an HTML element,
169
00:08:55,480 --> 00:08:58,663
just starting with this uppercase character.
13838
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.