Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,547 --> 00:00:03,750
This first code here
2
00:00:03,750 --> 00:00:05,560
for our Todo element,
3
00:00:05,560 --> 00:00:07,860
exists in this App component.
4
00:00:07,860 --> 00:00:09,380
There's nothing wrong with it.
5
00:00:09,380 --> 00:00:12,510
But not only do we have some static text here,
6
00:00:12,510 --> 00:00:15,607
we would of course want the actual Todo title here.
7
00:00:15,607 --> 00:00:17,191
Not only is that the case,
8
00:00:17,191 --> 00:00:19,680
if we would want to add a second Todo,
9
00:00:19,680 --> 00:00:23,210
we would have to replicate this entire code block.
10
00:00:23,210 --> 00:00:26,260
And if we then ever want to change anything about that,
11
00:00:26,260 --> 00:00:28,130
let's say we wanna add extra span
12
00:00:28,130 --> 00:00:29,690
to that button, whatever.
13
00:00:29,690 --> 00:00:33,030
Then we have to do this in all those places
14
00:00:33,030 --> 00:00:35,090
where we use the same code.
15
00:00:35,090 --> 00:00:37,260
That's not really what we wanna do.
16
00:00:37,260 --> 00:00:41,040
That's why React embraces this concept of components
17
00:00:41,040 --> 00:00:43,570
of building our own building blocks,
18
00:00:43,570 --> 00:00:45,473
which we then compose together.
19
00:00:46,810 --> 00:00:49,520
For this, I'll remove this duplicate code
20
00:00:49,520 --> 00:00:52,770
and instead create a new file.
21
00:00:52,770 --> 00:00:55,550
A new file in that src folder,
22
00:00:55,550 --> 00:00:58,250
which I'll name Todo.js.
23
00:00:58,250 --> 00:00:59,410
The name is up to you,
24
00:00:59,410 --> 00:01:01,710
the extension should be dot js though.
25
00:01:01,710 --> 00:01:05,080
And that will simply hold another component,
26
00:01:05,080 --> 00:01:08,163
we're going to build our second component here.
27
00:01:09,070 --> 00:01:11,530
Now typically, those component files,
28
00:01:11,530 --> 00:01:14,280
except for that so called root component,
29
00:01:14,280 --> 00:01:15,770
this App component,
30
00:01:15,770 --> 00:01:18,780
are stored in a separate components folder,
31
00:01:18,780 --> 00:01:21,380
which I also added in the source folder.
32
00:01:21,380 --> 00:01:24,340
So, I'll move Todo.js into that folder.
33
00:01:24,340 --> 00:01:25,800
That's not a must do,
34
00:01:25,800 --> 00:01:28,883
but it is a convention to keep your project organized.
35
00:01:29,870 --> 00:01:31,450
Now in this Todo.js file,
36
00:01:31,450 --> 00:01:33,880
I wanna create my second component,
37
00:01:33,880 --> 00:01:36,080
that Todo component.
38
00:01:36,080 --> 00:01:38,810
And we did learn that a component in React
39
00:01:38,810 --> 00:01:40,490
is just a function.
40
00:01:40,490 --> 00:01:41,323
So in the end here,
41
00:01:41,323 --> 00:01:42,690
we define a function Todo
42
00:01:43,970 --> 00:01:46,270
and then we export that.
43
00:01:46,270 --> 00:01:50,030
We need to export it like this with this syntax,
44
00:01:50,030 --> 00:01:54,440
to make this function available outside of this file
45
00:01:54,440 --> 00:01:56,710
and usable in other files.
46
00:01:56,710 --> 00:02:00,670
Now, one important to note about this function name though,
47
00:02:00,670 --> 00:02:04,200
you can name your component functions however you want.
48
00:02:04,200 --> 00:02:07,770
But, there is one important restriction,
49
00:02:07,770 --> 00:02:09,500
one important thing to keep in mind.
50
00:02:09,500 --> 00:02:11,440
The name of the function
51
00:02:11,440 --> 00:02:14,550
should start with a capital character.
52
00:02:14,550 --> 00:02:17,930
That matters because when we later use this function
53
00:02:17,930 --> 00:02:21,270
as a component in our HTML code,
54
00:02:21,270 --> 00:02:24,710
so if we use it as a custom HTML element,
55
00:02:24,710 --> 00:02:27,980
then it will have to start with a capital character
56
00:02:27,980 --> 00:02:29,180
there as well,
57
00:02:29,180 --> 00:02:33,580
to differentiate it from the built-in HTML elements.
58
00:02:33,580 --> 00:02:36,190
React wants custom components
59
00:02:36,190 --> 00:02:38,683
to start with a capital character.
60
00:02:39,590 --> 00:02:41,740
So now we have our function here.
61
00:02:41,740 --> 00:02:43,460
And then here in this function,
62
00:02:43,460 --> 00:02:47,450
we must return something that can be rendered
63
00:02:47,450 --> 00:02:50,030
to turn this function into a component.
64
00:02:50,030 --> 00:02:53,100
And that something is this div here,
65
00:02:53,100 --> 00:02:55,610
this div with the class name card.
66
00:02:55,610 --> 00:02:58,350
I'll cut it from App.js
67
00:02:58,350 --> 00:03:00,143
and then return it here in Todo.js.
68
00:03:01,150 --> 00:03:04,293
And press that auto format shortcut again.
69
00:03:05,410 --> 00:03:07,760
So now that's our Todo function
70
00:03:07,760 --> 00:03:09,593
returning this JS Xcode.
71
00:03:10,780 --> 00:03:11,870
Back in App.js,
72
00:03:11,870 --> 00:03:14,710
we can now use this Todo component.
73
00:03:14,710 --> 00:03:19,240
And we don't do that by executing Todo like a function
74
00:03:19,240 --> 00:03:21,940
as we would normally do that.
75
00:03:21,940 --> 00:03:25,800
But instead, we use it as a custom HTML element,
76
00:03:25,800 --> 00:03:29,973
just as we did it with App in the index.js file.
77
00:03:31,040 --> 00:03:34,250
So here, where I cut the code from,
78
00:03:34,250 --> 00:03:39,250
I'll add Todo like this as an HTML element.
79
00:03:39,290 --> 00:03:40,710
The only special thing here
80
00:03:40,710 --> 00:03:42,890
is that it's self closing.
81
00:03:42,890 --> 00:03:46,170
Alternatively, we write an opening and closing tag,
82
00:03:46,170 --> 00:03:48,000
this would work as well.
83
00:03:48,000 --> 00:03:51,110
But since there will be no content between the tags,
84
00:03:51,110 --> 00:03:53,670
we can also write a self closing tag.
85
00:03:53,670 --> 00:03:56,890
But then it has to have this forward slash,
86
00:03:56,890 --> 00:03:58,983
it would not be allowed like this.
87
00:04:00,140 --> 00:04:01,640
Now, as noted before,
88
00:04:01,640 --> 00:04:03,680
it is worth pointing out here,
89
00:04:03,680 --> 00:04:05,420
that this component,
90
00:04:05,420 --> 00:04:06,860
this custom element,
91
00:04:06,860 --> 00:04:09,652
when we're using it here in this JS Xcode,
92
00:04:09,652 --> 00:04:13,700
that there it has to start with a capital character.
93
00:04:13,700 --> 00:04:17,149
So, todo with a lowercase t at the beginning
94
00:04:17,149 --> 00:04:19,050
would not be valid.
95
00:04:19,050 --> 00:04:21,279
Because react internally
96
00:04:21,279 --> 00:04:26,000
differentiates between built-in HTML elements like divs,
97
00:04:26,000 --> 00:04:30,350
and h1 tags and custom elements, by casing.
98
00:04:30,350 --> 00:04:31,920
All these built-in elements
99
00:04:31,920 --> 00:04:34,380
start with a lowercase character,
100
00:04:34,380 --> 00:04:37,360
custom elements built by you or others
101
00:04:37,360 --> 00:04:40,310
have to start with the uppercase character.
102
00:04:40,310 --> 00:04:42,963
And that is just something to be aware of.
103
00:04:43,810 --> 00:04:46,090
Now to use Todo in App.js though,
104
00:04:46,090 --> 00:04:49,730
we need to import it from the Todo.js file.
105
00:04:49,730 --> 00:04:51,500
And for this at the top of this file,
106
00:04:51,500 --> 00:04:56,500
we import Todo from dot slash components
107
00:04:56,670 --> 00:04:58,860
to construct a relative path
108
00:04:58,860 --> 00:05:01,723
to that components folder, slash Todo.
109
00:05:02,870 --> 00:05:07,780
Again, the file extension of dot js can be omitted here.
110
00:05:07,780 --> 00:05:12,150
That's now this Todo imported and rendered here.
111
00:05:12,150 --> 00:05:14,933
If we now save all files,
112
00:05:16,100 --> 00:05:18,330
we see the same output as before,
113
00:05:18,330 --> 00:05:19,390
which is good.
114
00:05:19,390 --> 00:05:21,700
That means that everything still works.
115
00:05:21,700 --> 00:05:23,908
But, we're now getting to the output
116
00:05:23,908 --> 00:05:27,200
with help of our separate component.
117
00:05:27,200 --> 00:05:29,480
Now, if we wanna have multiple Todos,
118
00:05:29,480 --> 00:05:33,730
we just got to replicate this Todo component here
119
00:05:33,730 --> 00:05:35,470
in the App component.
120
00:05:35,470 --> 00:05:38,750
And we now have a couple of Todos.
121
00:05:38,750 --> 00:05:40,650
And if we now ever want to change anything
122
00:05:40,650 --> 00:05:42,190
about that Todo component,
123
00:05:42,190 --> 00:05:44,530
about this HTML markup
124
00:05:44,530 --> 00:05:45,960
that makes up our Todo,
125
00:05:45,960 --> 00:05:49,400
we just need to do it in the Todo.js file,
126
00:05:49,400 --> 00:05:50,830
like adding this span
127
00:05:50,830 --> 00:05:52,160
which has no real purpose,
128
00:05:52,160 --> 00:05:53,720
but acts as a demo.
129
00:05:53,720 --> 00:05:55,270
And if we did save that file,
130
00:05:55,270 --> 00:05:59,120
we see that extra span in all our Todos.
131
00:05:59,120 --> 00:06:00,610
Because we defined it once
132
00:06:00,610 --> 00:06:03,320
and then reuse it multiple times.
133
00:06:03,320 --> 00:06:05,700
Just as in vanilla JavaScript,
134
00:06:05,700 --> 00:06:07,980
we would define a function once,
135
00:06:07,980 --> 00:06:10,570
and then possibly call it multiple times
136
00:06:10,570 --> 00:06:13,640
from different places in our application.
137
00:06:13,640 --> 00:06:17,780
It's the same concept here with React components.
138
00:06:17,780 --> 00:06:19,450
Though we don't need to span here,
139
00:06:19,450 --> 00:06:21,760
so I will get rid of that now.
140
00:06:21,760 --> 00:06:23,600
What we do need here though,
141
00:06:23,600 --> 00:06:26,590
is some configurability.
142
00:06:26,590 --> 00:06:27,423
At the moment,
143
00:06:27,423 --> 00:06:30,220
all Todo is just say TITLE.
144
00:06:30,220 --> 00:06:32,660
And that's not too useful.
145
00:06:32,660 --> 00:06:35,190
These should be different Todo titles
146
00:06:35,190 --> 00:06:36,683
for our different Todos.
147
00:06:37,620 --> 00:06:38,453
And of course,
148
00:06:38,453 --> 00:06:41,200
I can change the title here to Learn React.
149
00:06:41,200 --> 00:06:44,330
But since I do this in this Todo.js file,
150
00:06:44,330 --> 00:06:47,460
this will now affect all Todo usages again.
151
00:06:47,460 --> 00:06:49,543
So, all Todos which were outputting.
152
00:06:50,470 --> 00:06:54,000
So, that's not how we make this more configurable.
153
00:06:54,000 --> 00:06:56,580
Instead, we want to let the place
154
00:06:56,580 --> 00:06:58,100
where we used the Todo.
155
00:06:58,100 --> 00:06:59,620
So the App component,
156
00:06:59,620 --> 00:07:04,600
pass different data into these Todo components,
157
00:07:04,600 --> 00:07:07,580
just as we pass arguments into functions
158
00:07:07,580 --> 00:07:08,870
when we call them,
159
00:07:08,870 --> 00:07:12,690
to then output different data in each Todo instance,
160
00:07:12,690 --> 00:07:15,420
depending on which data we're passing in.
161
00:07:15,420 --> 00:07:17,373
And that's what we're going to do now.
11942
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.