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,000
How can we make this component reusable?
2
00:00:05,000 --> 00:00:09,020
Well, the good news is we can already reuse it.
3
00:00:09,020 --> 00:00:11,400
In App.js, where I'm using it,
4
00:00:11,400 --> 00:00:14,240
we can simply copy that line and paste it in.
5
00:00:14,240 --> 00:00:15,440
And if we save everything,
6
00:00:15,440 --> 00:00:18,140
we got the two ExpenseItems already.
7
00:00:18,140 --> 00:00:21,860
So that was easy. It's super easy to reuse a component.
8
00:00:21,860 --> 00:00:23,760
You can use it as often as you want.
9
00:00:23,760 --> 00:00:26,210
You can add it two more times
10
00:00:26,210 --> 00:00:28,700
and have four ExpenseItems overall.
11
00:00:28,700 --> 00:00:30,770
That will work.
12
00:00:30,770 --> 00:00:34,630
But, of course, at the moment it's not really reusable.
13
00:00:34,630 --> 00:00:36,740
Yes, it's technically more than once,
14
00:00:36,740 --> 00:00:39,930
but it's always displaying the same data.
15
00:00:39,930 --> 00:00:40,770
And the reason for that
16
00:00:40,770 --> 00:00:44,760
is that the data is baked into this component.
17
00:00:44,760 --> 00:00:48,080
Now, think about regular JavaScript
18
00:00:48,080 --> 00:00:50,840
without React for a second.
19
00:00:50,840 --> 00:00:55,380
There, I mentioned early already, we also use functions
20
00:00:55,380 --> 00:01:00,380
to split functionality across multiple smaller codebases
21
00:01:00,450 --> 00:01:03,540
and also to have reusable functions
22
00:01:03,540 --> 00:01:06,160
which we can call multiple times.
23
00:01:06,160 --> 00:01:09,720
Now, when we write functions in JavaScript,
24
00:01:09,720 --> 00:01:12,580
then we make these functions reusable
25
00:01:12,580 --> 00:01:15,140
by accepting parameters.
26
00:01:15,140 --> 00:01:19,040
And that allows us to call one of the same function
27
00:01:19,040 --> 00:01:21,900
with different parameter values.
28
00:01:21,900 --> 00:01:25,830
And, therefore, the function may and typically will produce
29
00:01:25,830 --> 00:01:29,020
different results for different input values,
30
00:01:29,020 --> 00:01:32,910
but it's still always the same function being called.
31
00:01:32,910 --> 00:01:36,370
And React basically has the same concept built in.
32
00:01:36,370 --> 00:01:39,200
We can make our components reusable
33
00:01:39,200 --> 00:01:44,200
by using parameters and a concept called props in React.
34
00:01:45,650 --> 00:01:48,680
Let's say in your App component,
35
00:01:48,680 --> 00:01:53,400
you have a goalItem variable or constant defined
36
00:01:53,400 --> 00:01:56,480
which holds a string of Finish!
37
00:01:56,480 --> 00:01:59,010
And then let's say you have a custom component,
38
00:01:59,010 --> 00:02:01,410
the CourseGoalItem component,
39
00:02:01,410 --> 00:02:03,810
which has a list item inside of it
40
00:02:03,810 --> 00:02:06,850
where this goalItem should be output.
41
00:02:06,850 --> 00:02:08,300
That's just another example,
42
00:02:08,300 --> 00:02:09,830
not the code we've been working on,
43
00:02:09,830 --> 00:02:11,480
but it's another example.
44
00:02:11,480 --> 00:02:12,980
We can have a custom component
45
00:02:12,980 --> 00:02:14,850
which should output a list item
46
00:02:14,850 --> 00:02:18,650
where the goalItem should be output dynamically.
47
00:02:18,650 --> 00:02:22,710
The problem is that the goalItem variable lives
48
00:02:22,710 --> 00:02:26,700
in the App component, not in the CourseGoalItem component.
49
00:02:26,700 --> 00:02:28,690
And to a certain extent, that is good
50
00:02:28,690 --> 00:02:31,860
because it makes the CourseGoalItem independent
51
00:02:31,860 --> 00:02:35,450
if it doesn't store the concrete value internally.
52
00:02:35,450 --> 00:02:36,860
But, of course, we wanna define
53
00:02:36,860 --> 00:02:39,260
what's being output by CourseGoalItem
54
00:02:39,260 --> 00:02:42,230
with help of the variable managed in App.
55
00:02:42,230 --> 00:02:46,570
And we don't have direct access to the HTML code output
56
00:02:46,570 --> 00:02:49,780
by some component in other components.
57
00:02:49,780 --> 00:02:53,000
We can't just send our stored data there,
58
00:02:53,000 --> 00:02:56,730
but, instead, we can utilize a concept called props.
59
00:02:56,730 --> 00:03:01,010
We can pass data to the custom component
60
00:03:01,010 --> 00:03:03,420
by adding a attribute.
61
00:03:03,420 --> 00:03:05,470
And inside of that component,
62
00:03:05,470 --> 00:03:08,650
we can then get access to all these attributes
63
00:03:08,650 --> 00:03:12,170
which might have been set on our custom component.
64
00:03:12,170 --> 00:03:13,890
Again, we're basically building
65
00:03:13,890 --> 00:03:16,550
our own custom HTML elements.
66
00:03:16,550 --> 00:03:21,320
And just as HTML elements can have attributes,
67
00:03:21,320 --> 00:03:23,800
it turns out that with React,
68
00:03:23,800 --> 00:03:27,420
our own custom components can also have attributes.
69
00:03:27,420 --> 00:03:30,710
There, this concept is just called props
70
00:03:30,710 --> 00:03:32,660
instead of attributes.
71
00:03:32,660 --> 00:03:36,070
And props simply stands for properties.
72
00:03:36,070 --> 00:03:40,223
We can set properties of our own custom components.
73
00:03:41,130 --> 00:03:44,610
Now, how does this props concept work though?
74
00:03:44,610 --> 00:03:47,990
In ExpenseItem, we have the date, the title, and the amount,
75
00:03:47,990 --> 00:03:50,080
and we'd like to output this here,
76
00:03:50,080 --> 00:03:55,080
but it shouldn't be stored in here but instead in App.js.
77
00:03:55,410 --> 00:03:58,690
There, indeed, in this App component,
78
00:03:58,690 --> 00:04:01,860
we could have another constant or variable,
79
00:04:01,860 --> 00:04:06,860
expenses, which is an array, so multiple expense items.
80
00:04:07,090 --> 00:04:08,560
And let's say every expense here
81
00:04:08,560 --> 00:04:10,850
is a simple JavaScript object
82
00:04:10,850 --> 00:04:14,760
which now has a title like Car Insurance,
83
00:04:14,760 --> 00:04:18,406
which also has an amount like the 294.67,
84
00:04:21,470 --> 00:04:23,920
and where we have a date
85
00:04:23,920 --> 00:04:28,920
like this new Date we created here in ExpenseItem before.
86
00:04:29,200 --> 00:04:30,680
Now, again, we don't just have one,
87
00:04:30,680 --> 00:04:35,090
but we have, let's say, three such expenses here,
88
00:04:35,090 --> 00:04:36,900
or maybe four.
89
00:04:36,900 --> 00:04:39,820
Now, to make sure we don't have to type these all by hand,
90
00:04:39,820 --> 00:04:42,110
attached you find a simple text file,
91
00:04:42,110 --> 00:04:45,800
which contains four dummy expense items,
92
00:04:45,800 --> 00:04:49,610
which you can just wrap and copy into your expenses array.
93
00:04:49,610 --> 00:04:53,120
These objects also have an extra id field
94
00:04:53,120 --> 00:04:55,170
which we don't need at the moment though.
95
00:04:56,500 --> 00:04:59,750
Nonetheless, now you will have four objects
96
00:04:59,750 --> 00:05:01,760
inside of this expenses array.
97
00:05:01,760 --> 00:05:06,230
And now we want to pass the data of these different objects
98
00:05:06,230 --> 00:05:09,350
to these different ExpenseItems.
99
00:05:09,350 --> 00:05:13,470
That means we wanna make these ExpenseItems configurable
100
00:05:13,470 --> 00:05:16,100
from outside, you could say.
101
00:05:16,100 --> 00:05:18,640
The data should not be stored inside of them
102
00:05:18,640 --> 00:05:21,050
but instead received from outside.
103
00:05:21,050 --> 00:05:24,693
And that works with this props concept I just mentioned.
104
00:05:25,890 --> 00:05:29,900
In App.js, we can simply add attributes
105
00:05:29,900 --> 00:05:34,320
to these custom HTML elements, so to these components.
106
00:05:34,320 --> 00:05:37,050
I'm using these terms interchangeably here.
107
00:05:37,050 --> 00:05:40,540
And, for example, we could add a title attribute
108
00:05:40,540 --> 00:05:42,170
here to ExpenseItem
109
00:05:43,050 --> 00:05:46,933
and set this to a value of Toilet Paper.
110
00:05:48,800 --> 00:05:52,480
So we can just type Toilet Paper here.
111
00:05:52,480 --> 00:05:55,670
But now I wouldn't yet again be hard-coding some data
112
00:05:55,670 --> 00:05:57,690
in this JSX code.
113
00:05:57,690 --> 00:06:00,300
Since we already have this expenses array here,
114
00:06:00,300 --> 00:06:02,650
I also can dynamically retrieve
115
00:06:02,650 --> 00:06:05,640
the title stored in this first ExpenseItem
116
00:06:05,640 --> 00:06:08,200
for this first ExpenseItem here.
117
00:06:08,200 --> 00:06:09,033
And we can do this
118
00:06:09,033 --> 00:06:12,730
by, again, using this single-curly-brace syntax.
119
00:06:12,730 --> 00:06:14,130
We cannot just use this
120
00:06:14,130 --> 00:06:17,050
between opening and closing tags of elements,
121
00:06:17,050 --> 00:06:21,060
we can also use it for assigning values to attributes.
122
00:06:21,060 --> 00:06:24,780
And here I can then access expenses, which is this array,
123
00:06:24,780 --> 00:06:28,280
there the first item with index zero,
124
00:06:28,280 --> 00:06:32,460
and there, this first item, like all items, is an object
125
00:06:32,460 --> 00:06:35,340
which has a title property, for example.
126
00:06:35,340 --> 00:06:39,563
So we can access .title here.
127
00:06:40,800 --> 00:06:45,714
And now this title is being passed to this ExpenseItem.
128
00:06:45,714 --> 00:06:47,920
Now, we can do the same for the amount.
129
00:06:47,920 --> 00:06:51,070
We set, let's say, an amount attribute.
130
00:06:51,070 --> 00:06:53,040
And these attribute names, by the way,
131
00:06:53,040 --> 00:06:54,530
are totally up to you.
132
00:06:54,530 --> 00:06:56,660
I'm going with title and amount here,
133
00:06:56,660 --> 00:06:58,990
but you could pick different things.
134
00:06:58,990 --> 00:07:00,600
And as a value for amount,
135
00:07:00,600 --> 00:07:03,750
I, again, access expenses and the first expense there
136
00:07:03,750 --> 00:07:05,263
and then the amount field.
137
00:07:06,960 --> 00:07:10,200
And last but not least, we can add a date attribute
138
00:07:10,200 --> 00:07:14,540
and access expenses and there the first expense .date.
139
00:07:14,540 --> 00:07:17,400
You just have to make sure that the part after the dot
140
00:07:17,400 --> 00:07:19,580
matches the property names here
141
00:07:19,580 --> 00:07:22,563
because we are accessing these objects.
142
00:07:23,510 --> 00:07:25,340
Now, I'll hit the auto-format code
143
00:07:25,340 --> 00:07:26,860
to make this easier to read
144
00:07:26,860 --> 00:07:29,400
by splitting these across multiple lines.
145
00:07:29,400 --> 00:07:33,810
We can now do the same for the other ExpenseItems.
146
00:07:33,810 --> 00:07:35,670
So I'll just repeat this
147
00:07:35,670 --> 00:07:40,323
but then access the, oops, the second expense here
148
00:07:41,950 --> 00:07:45,440
and the third expense with index two
149
00:07:45,440 --> 00:07:49,543
and the fourth expense with index three.
150
00:07:51,630 --> 00:07:55,040
Now, if we save that, we see no change, though,
151
00:07:55,040 --> 00:07:58,380
because we only did the first half
152
00:07:58,380 --> 00:08:00,240
of the work we need to do.
153
00:08:00,240 --> 00:08:04,260
We added these attributes with any names of our choice here
154
00:08:04,260 --> 00:08:07,230
because these are our custom elements,
155
00:08:07,230 --> 00:08:09,513
hence these attribute names are up to us.
156
00:08:10,470 --> 00:08:12,830
But inside of that component code,
157
00:08:12,830 --> 00:08:15,210
so inside of ExpenseItem.js,
158
00:08:15,210 --> 00:08:17,260
we also now need to do something
159
00:08:17,260 --> 00:08:20,050
with these received attribute values,
160
00:08:20,050 --> 00:08:22,610
and that's the half which is missing.
161
00:08:22,610 --> 00:08:26,030
Now, the question is how do we get access to the values
162
00:08:26,030 --> 00:08:29,893
defined in the place where we use our custom components?
163
00:08:30,880 --> 00:08:33,630
And the answer is parameters.
164
00:08:33,630 --> 00:08:36,360
I mentioned that in regular JavaScript,
165
00:08:36,360 --> 00:08:39,570
we use parameters for passing data into functions,
166
00:08:39,570 --> 00:08:42,860
and it's kind of similar for React.
167
00:08:42,860 --> 00:08:45,610
However, we're now not getting a title
168
00:08:45,610 --> 00:08:49,360
and an amount and a date parameter, as you might think,
169
00:08:49,360 --> 00:08:51,840
but, instead, we get one parameter.
170
00:08:51,840 --> 00:08:55,340
React will ensure that we get one parameter
171
00:08:55,340 --> 00:08:58,540
in every component which we use as a component,
172
00:08:58,540 --> 00:09:01,270
like we're doing it here for ExpenseItem.
173
00:09:01,270 --> 00:09:04,420
And that one parameter will be an object
174
00:09:04,420 --> 00:09:08,830
which holds all the received attributes as properties,
175
00:09:08,830 --> 00:09:12,083
hence the name props for the overall concept.
176
00:09:12,950 --> 00:09:14,970
Therefore, we get one parameter,
177
00:09:14,970 --> 00:09:17,600
and you can name this parameter whatever you want.
178
00:09:17,600 --> 00:09:20,910
It's your function. You could name it data.
179
00:09:20,910 --> 00:09:23,030
But, typically, it's named props
180
00:09:23,030 --> 00:09:25,590
to make it clear that this is this object
181
00:09:25,590 --> 00:09:29,180
which holds all the values we get
182
00:09:29,180 --> 00:09:32,720
for the attributes on our custom element.
183
00:09:32,720 --> 00:09:33,750
And to be precise,
184
00:09:33,750 --> 00:09:38,230
we get key-value pairs in this props object here
185
00:09:38,230 --> 00:09:40,773
which is passed in by React automatically.
186
00:09:41,680 --> 00:09:45,250
The keys will be the attribute names defined here,
187
00:09:45,250 --> 00:09:47,940
so title, amount, and date in my case.
188
00:09:47,940 --> 00:09:51,713
And the values, of course, will be the values set here.
189
00:09:52,630 --> 00:09:56,360
So if I now wanna output the title received
190
00:09:56,360 --> 00:09:58,540
here in this h2 tag,
191
00:09:58,540 --> 00:10:03,063
I can now access props.title like this,
192
00:10:04,660 --> 00:10:06,920
getting access to this props parameter
193
00:10:06,920 --> 00:10:09,520
and then there to the title property
194
00:10:09,520 --> 00:10:14,453
which will exist because we set a title attribute.
195
00:10:15,440 --> 00:10:19,040
Now, if you chose a different name than title here,
196
00:10:19,040 --> 00:10:21,170
like, for example, name,
197
00:10:21,170 --> 00:10:23,840
if you chose that as a name, for example,
198
00:10:23,840 --> 00:10:27,810
then you have to access props.name here.
199
00:10:27,810 --> 00:10:31,970
So the key which you access on your props object
200
00:10:31,970 --> 00:10:35,313
has to be the name you picked for your attribute.
201
00:10:36,148 --> 00:10:37,710
Now, here I'll go back to title
202
00:10:37,710 --> 00:10:40,300
and therefore adjust it here in both places again,
203
00:10:40,300 --> 00:10:43,510
but you have to make sure that this matches.
204
00:10:43,510 --> 00:10:46,210
Otherwise, it will not work.
205
00:10:46,210 --> 00:10:47,910
Now, which names you choose,
206
00:10:47,910 --> 00:10:51,930
if that's title or name or text or whatever,
207
00:10:51,930 --> 00:10:54,430
that's up to you, it's your component.
208
00:10:54,430 --> 00:10:57,960
But, of course, you typically wanna choose prop names
209
00:10:57,960 --> 00:11:00,180
and therefore attribute names
210
00:11:00,180 --> 00:11:04,210
which are self-explanatory and which makes sense.
211
00:11:04,210 --> 00:11:06,600
So since we're passing in the amount here,
212
00:11:06,600 --> 00:11:08,330
amount makes sense.
213
00:11:08,330 --> 00:11:12,290
Something like someNumber would be valid
214
00:11:12,290 --> 00:11:14,653
but wouldn't make a lot of sense, I guess,
215
00:11:15,610 --> 00:11:17,033
so I'll revert that.
216
00:11:17,960 --> 00:11:20,300
Now, therefore, in ExpenseItem,
217
00:11:20,300 --> 00:11:24,900
we can not just output props.title but also props.amount,
218
00:11:24,900 --> 00:11:29,620
and here for the date, props.date.toISOString.
219
00:11:29,620 --> 00:11:31,670
And that means we can now get rid
220
00:11:31,670 --> 00:11:33,370
of these three constants here
221
00:11:33,370 --> 00:11:35,830
since we now get all the data we need
222
00:11:35,830 --> 00:11:39,530
from outside this component.
223
00:11:39,530 --> 00:11:43,340
We're not defining it inside of the ExpenseItem component,
224
00:11:43,340 --> 00:11:44,750
inside of this function,
225
00:11:44,750 --> 00:11:47,330
but, instead, it's in the end defined in App.js
226
00:11:47,330 --> 00:11:49,760
and passed into ExpenseItem
227
00:11:49,760 --> 00:11:54,760
for the different usages of ExpenseItem through attributes.
228
00:11:55,310 --> 00:11:58,640
And that's how you share data between React components.
229
00:11:58,640 --> 00:12:03,640
You can make your components truly reusable and configurable
230
00:12:03,670 --> 00:12:06,290
by using this props concept,
231
00:12:06,290 --> 00:12:10,283
which is one of the key concepts React has to offer.
232
00:12:11,270 --> 00:12:15,170
You'll use props all the time, so keep it in mind.
233
00:12:15,170 --> 00:12:16,400
It's super important.
234
00:12:16,400 --> 00:12:18,660
Props is a super important concept
235
00:12:18,660 --> 00:12:21,500
because it allows you to make your components reusable,
236
00:12:21,500 --> 00:12:23,580
and it allows you to pass data
237
00:12:23,580 --> 00:12:26,700
from another component to this component.
238
00:12:26,700 --> 00:12:28,270
And hence if we now have a look,
239
00:12:28,270 --> 00:12:31,750
we see these different expenses are rendered
240
00:12:31,750 --> 00:12:34,650
with different titles, different dates,
241
00:12:34,650 --> 00:12:36,183
and different amounts.
19163
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.