Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,990 --> 00:00:09,090
We know that we can write functions that return HTML, but we can also write classes that return HTML
2
00:00:09,910 --> 00:00:18,310
and the reason why we would want to use a class is because REACT has given us the ability to write classes
3
00:00:18,310 --> 00:00:23,830
that have a lot more functionality on them versus a function that returns some HTML.
4
00:00:24,130 --> 00:00:25,470
Now let me show you this.
5
00:00:26,140 --> 00:00:30,130
So in order to write a class, we've got a first import component.
6
00:00:34,750 --> 00:00:46,390
And instead of this function app, I'm going to convert this into a class, so class app extends this
7
00:00:46,390 --> 00:00:53,140
component that we get from react and as a result, we now have access to this render method inside of
8
00:00:53,140 --> 00:00:58,840
our app class and the render method returns any time I want.
9
00:00:58,870 --> 00:01:04,150
So let's return that HTML we had before inside of our function and we'll see.
10
00:01:05,190 --> 00:01:06,260
That it's actually the same.
11
00:01:08,140 --> 00:01:12,040
And to prove it, I'm going to change this to this text here.
12
00:01:13,330 --> 00:01:14,950
And we should see an update, right?
13
00:01:15,070 --> 00:01:15,850
Hello, Iowa.
14
00:01:17,280 --> 00:01:18,210
So this is great.
15
00:01:19,200 --> 00:01:22,980
So it's pretty much the exact same at this point as are functional.
16
00:01:23,860 --> 00:01:30,250
App that we had before, right, our function app that returned this block of HTML, but the reason
17
00:01:30,250 --> 00:01:37,570
why we would want to use this kind of thing is if we wanted, say, instead to display this static string
18
00:01:37,570 --> 00:01:38,490
of Hello U'wa.
19
00:01:39,010 --> 00:01:40,570
What if I wanted?
20
00:01:41,640 --> 00:01:46,710
Something like, let's say, instead of an attack dog, we had a button.
21
00:01:48,300 --> 00:01:50,190
And whenever you click this button.
22
00:01:52,020 --> 00:01:57,270
It would change the text to something else, right, we would change this Hellewell, to something else.
23
00:01:58,780 --> 00:01:59,170
Well.
24
00:02:00,680 --> 00:02:08,570
The thing about that is that by using a class component, we actually get access to this thing called
25
00:02:08,570 --> 00:02:09,139
state.
26
00:02:09,680 --> 00:02:19,670
What state is is it some object write a JavaScript object with properties that we can access at any
27
00:02:19,670 --> 00:02:22,040
point inside of our class.
28
00:02:22,460 --> 00:02:27,890
And the way we would do this is we would actually call the constructor keyword here.
29
00:02:30,020 --> 00:02:36,170
And inside of a constructor we want to call super right, and what super does is it calls the constructor
30
00:02:36,170 --> 00:02:38,220
method on the component class.
31
00:02:38,240 --> 00:02:41,390
And what that does is it gives us access to this Stotz state.
32
00:02:41,690 --> 00:02:42,020
Right.
33
00:02:42,470 --> 00:02:49,290
This state property now exists on our class app and we can set it to something.
34
00:02:49,310 --> 00:02:51,850
So this is the default value I want to set it to first.
35
00:02:51,860 --> 00:02:56,140
So let's say I want to set it to the string that we have right now.
36
00:02:56,200 --> 00:02:56,540
Right.
37
00:02:56,570 --> 00:03:01,850
I want to set some property on our state, object to Hello EOWA.
38
00:03:03,290 --> 00:03:09,800
And then I want to render that instead of hello, halloa, the static string, how would I do that?
39
00:03:10,250 --> 00:03:17,990
Well, I would first use these curly braces, which Jesse lets us do, and it tells the star that anything
40
00:03:17,990 --> 00:03:21,200
between these curly braces is going to be JavaScript.
41
00:03:21,230 --> 00:03:23,060
So I want you to render JavaScript, right.
42
00:03:23,540 --> 00:03:26,570
So we want to do this dot state, dot string.
43
00:03:26,870 --> 00:03:32,740
And now between these P tags, we actually don't see anything change because it's the same string.
44
00:03:33,320 --> 00:03:35,420
But just to prove it, I'm going to say hello.
45
00:03:35,450 --> 00:03:43,070
You're saying right there we see that it's looking at the state, looking at this property string and
46
00:03:43,070 --> 00:03:44,600
rendering that here instead.
47
00:03:46,450 --> 00:03:50,840
So that's awesome, because now I can do this in multiple places, right?
48
00:03:50,860 --> 00:03:54,750
I can use this as many times as I want and I'm going to see multiple versions of it.
49
00:03:54,760 --> 00:03:55,060
Right.
50
00:03:58,170 --> 00:04:06,180
What if I wanted to change this value while the way I would do it is this class app that extends component,
51
00:04:06,360 --> 00:04:10,140
this component also gives us this method called set state.
52
00:04:10,860 --> 00:04:18,899
And what sets it does is it lets us modify this state object right on every single HTML element.
53
00:04:19,320 --> 00:04:27,630
We now have access to this method, this property called UNCLICK, which takes a function that gets
54
00:04:27,630 --> 00:04:31,500
called when ever this element gets clicked.
55
00:04:32,280 --> 00:04:37,740
So lets pass in this function and in this function we're going to say this dot set state.
56
00:04:39,690 --> 00:04:46,650
And what sets day takes is an object with all of the properties that you want to change on your state
57
00:04:46,650 --> 00:04:48,420
as well as the new values that they have.
58
00:04:48,450 --> 00:04:53,730
So I want to change the property string to Hello, Andre.
59
00:04:56,230 --> 00:04:57,850
Now, when I click this button.
60
00:04:59,460 --> 00:05:00,680
We see that it changed, right?
61
00:05:01,980 --> 00:05:07,860
And that's really awesome, because this gives us a lot of control over what we want our components
62
00:05:07,860 --> 00:05:08,590
to display.
63
00:05:09,510 --> 00:05:16,110
So this will be really, really useful later when we don't know where we're going to get some of the
64
00:05:16,110 --> 00:05:20,310
information that we actually want to display inside of our HTML.
65
00:05:21,390 --> 00:05:25,590
But I'm going to show you in the next lesson why this is really useful.
6287
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.