Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,020 --> 00:00:04,610
To make our component a bit more configurable
2
00:00:04,610 --> 00:00:07,040
and accept different data,
3
00:00:07,040 --> 00:00:09,720
we in the end wanna utilize a concept,
4
00:00:09,720 --> 00:00:11,620
which we know from the function world
5
00:00:11,620 --> 00:00:14,740
and from standard HTML elements.
6
00:00:14,740 --> 00:00:18,370
In the place where we use Todo, so in this app component
7
00:00:18,370 --> 00:00:21,050
it would be nice if we could add an attribute
8
00:00:21,050 --> 00:00:24,910
like text or title, totally up to you.
9
00:00:24,910 --> 00:00:28,480
Let's go with text and then set this to some value,
10
00:00:28,480 --> 00:00:31,293
like Learn React on the first Todo,
11
00:00:33,360 --> 00:00:36,100
then Master React on the second Todo,
12
00:00:36,100 --> 00:00:38,070
and let's say,
13
00:00:38,070 --> 00:00:42,543
Explore the full React course, in the third Todo.
14
00:00:42,543 --> 00:00:45,260
Now, it would be nice if you could do that.
15
00:00:45,260 --> 00:00:48,910
But if I add this here, in App.js and save it,
16
00:00:48,910 --> 00:00:52,450
this has no effect on what we see on the screen.
17
00:00:52,450 --> 00:00:53,450
The reason for this
18
00:00:53,450 --> 00:00:57,530
is that we're setting these custom attributes
19
00:00:57,530 --> 00:00:59,390
but then the Todo.js file,
20
00:00:59,390 --> 00:01:03,610
we have no code that would pick up those values.
21
00:01:03,610 --> 00:01:08,050
Nonetheless, this is how you do pass data into a component.
22
00:01:08,050 --> 00:01:11,170
We just also need to write some code in the component
23
00:01:11,170 --> 00:01:15,460
to then accept and use those received values.
24
00:01:15,460 --> 00:01:19,870
And we do that with a concept called props.
25
00:01:19,870 --> 00:01:23,690
We accept the parameter in our function component
26
00:01:23,690 --> 00:01:26,150
and in every function component,
27
00:01:26,150 --> 00:01:28,490
we can accept that parameter,
28
00:01:28,490 --> 00:01:31,560
and that parameter is typically called props.
29
00:01:31,560 --> 00:01:33,180
You can name it whatever you want,
30
00:01:33,180 --> 00:01:35,550
since it's your code, but it's called props
31
00:01:35,550 --> 00:01:39,180
because this React concept is called props.
32
00:01:39,180 --> 00:01:40,700
This will be an object.
33
00:01:40,700 --> 00:01:42,980
Props will be a JavaScript object
34
00:01:42,980 --> 00:01:46,840
where all the attributes added on the element
35
00:01:46,840 --> 00:01:49,860
are in the end available as key value pairs
36
00:01:49,860 --> 00:01:52,010
in this props object.
37
00:01:52,010 --> 00:01:55,800
So here, these attribute names will be the keys
38
00:01:55,800 --> 00:01:59,270
and the values will be the values.
39
00:01:59,270 --> 00:02:02,160
So props inside of the Todo component
40
00:02:02,160 --> 00:02:05,600
will be an object now, that has a text property
41
00:02:05,600 --> 00:02:09,639
and then different values for this text property.
42
00:02:09,639 --> 00:02:12,210
And if you named this attribute differently,
43
00:02:12,210 --> 00:02:13,230
that's also okay,
44
00:02:13,230 --> 00:02:16,310
you will then have a property of that name
45
00:02:16,310 --> 00:02:18,640
in your props object.
46
00:02:18,640 --> 00:02:21,320
So therefore now here, in this h2 tag,
47
00:02:21,320 --> 00:02:22,690
we wanna output
48
00:02:22,690 --> 00:02:26,750
this text we're getting on this props object.
49
00:02:26,750 --> 00:02:28,340
Now in standard JavaScript,
50
00:02:28,340 --> 00:02:32,690
we access object properties with the dot notation.
51
00:02:32,690 --> 00:02:35,420
So we could output props.text here
52
00:02:35,420 --> 00:02:38,630
to access the text property in the props object
53
00:02:38,630 --> 00:02:42,490
and therefore the values stored in this property.
54
00:02:42,490 --> 00:02:43,890
But if we do this here,
55
00:02:43,890 --> 00:02:47,300
we just see props.text as our output.
56
00:02:47,300 --> 00:02:49,840
The reason for this is that everything here
57
00:02:49,840 --> 00:02:54,840
in this JSX block is treated as HTML elements or plain text.
58
00:02:56,630 --> 00:02:58,320
The good news, however is
59
00:02:58,320 --> 00:03:02,420
that we can also output dynamic expressions
60
00:03:02,420 --> 00:03:04,380
as part of the JSX code.
61
00:03:04,380 --> 00:03:06,470
We can tell React that
62
00:03:06,470 --> 00:03:09,810
something should not be treated as plain text,
63
00:03:09,810 --> 00:03:12,340
but instead as a JavaScript expression
64
00:03:12,340 --> 00:03:14,350
that should be evaluated.
65
00:03:14,350 --> 00:03:16,920
And we do this with a special syntax
66
00:03:16,920 --> 00:03:20,780
inside of this JSX code, which looks like this.
67
00:03:20,780 --> 00:03:25,780
Opening and closing curly braces, single curly braces.
68
00:03:27,060 --> 00:03:28,520
And between those braces
69
00:03:28,520 --> 00:03:31,470
you can have any JavaScript expression you want
70
00:03:31,470 --> 00:03:34,450
like for example, two plus two.
71
00:03:34,450 --> 00:03:37,210
Now this will not be treated as plain text
72
00:03:37,210 --> 00:03:40,570
but instead it will be executed as JavaScript.
73
00:03:40,570 --> 00:03:44,403
So if I save, we see four, the result of this expression.
74
00:03:45,760 --> 00:03:49,760
Now you can have any single line expression in here
75
00:03:49,760 --> 00:03:52,270
but you can't have any block statements here.
76
00:03:52,270 --> 00:03:55,810
So adding a if check here would not work.
77
00:03:55,810 --> 00:04:00,210
But what does work is referring to props.text here.
78
00:04:00,210 --> 00:04:02,310
So simply outputting the result
79
00:04:02,310 --> 00:04:06,263
of accessing this text property on the props object.
80
00:04:07,430 --> 00:04:10,230
If we add this code and save this,
81
00:04:10,230 --> 00:04:14,193
we see our different texts here for the different Todos.
82
00:04:15,230 --> 00:04:18,089
And now we built our own truly
83
00:04:18,089 --> 00:04:22,600
reusable React component here, the Todo component.
84
00:04:22,600 --> 00:04:25,120
It is truly reusable
85
00:04:25,120 --> 00:04:28,720
because now it does not just have some hard coded structure
86
00:04:28,720 --> 00:04:30,847
and text inside of it,
87
00:04:30,847 --> 00:04:35,430
but also some dynamic code using this core concept
88
00:04:35,430 --> 00:04:38,300
built into React, the props concept
89
00:04:38,300 --> 00:04:41,160
for accepting data from outside.
90
00:04:41,160 --> 00:04:44,193
So from the place where it is being rendered.
91
00:04:45,040 --> 00:04:47,880
That is a key concept, this props concept
92
00:04:47,880 --> 00:04:51,920
and that is how you build re-usable custom elements,
93
00:04:51,920 --> 00:04:54,320
re-usable components.
94
00:04:54,320 --> 00:04:58,420
And that's there for a major first step here.
95
00:04:58,420 --> 00:05:01,370
Now let's make sure that we handle events,
96
00:05:01,370 --> 00:05:03,700
that we handle our click on the delete button
97
00:05:03,700 --> 00:05:07,120
and that we then also change something on the screen
98
00:05:07,120 --> 00:05:08,960
when the button is clicked.
99
00:05:08,960 --> 00:05:13,430
To be precise that we present this nice overlay,
100
00:05:13,430 --> 00:05:15,423
when that delete button is clicked.
7835
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.