Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,330 --> 00:00:03,780
So we just learned how to take an array.
2
00:00:04,800 --> 00:00:14,520
On our state and turn it into elements that we can then see right now in a real world scenario, we're
3
00:00:14,520 --> 00:00:19,770
probably not going to hard code the information that we're going to have.
4
00:00:19,890 --> 00:00:20,280
Right.
5
00:00:20,670 --> 00:00:26,580
Normally, we would probably get it from a back end of some kind, some kind of server, some kind of
6
00:00:26,580 --> 00:00:27,210
database.
7
00:00:28,140 --> 00:00:31,540
So I'm going to show you how that would actually work.
8
00:00:31,590 --> 00:00:39,900
Now, luckily, I have a pretty good resource where it will give us a sample of 10 users, right, without
9
00:00:39,900 --> 00:00:43,320
us having to actually build a backend server of some kind.
10
00:00:43,690 --> 00:00:45,000
And it's at this link.
11
00:00:46,550 --> 00:00:54,050
It will give us back is this array of these objects, right, that have these properties like ID and
12
00:00:54,050 --> 00:00:57,750
name and email on it that we will use for our application.
13
00:00:58,490 --> 00:01:05,870
So what we need to figure out is how do we get this app component to make a call to the back end and
14
00:01:05,870 --> 00:01:15,290
then put that data into our state so that our render function knows to use it right now can have access
15
00:01:15,290 --> 00:01:15,590
to it.
16
00:01:15,710 --> 00:01:16,710
So how do we do that?
17
00:01:17,030 --> 00:01:21,370
You see you remember how we got the render method by extending into component.
18
00:01:22,160 --> 00:01:28,160
We also get a bunch of other methods that are called lifecycle mith.
19
00:01:28,970 --> 00:01:30,510
What are lifecycle methods?
20
00:01:30,890 --> 00:01:37,100
Well, if we look at this here, we see a list of all these methods and it's an overwhelming list.
21
00:01:37,400 --> 00:01:47,090
But what lifecycle methods are is they're essentially methods that get called at different stages of
22
00:01:47,090 --> 00:01:51,470
when this component gets rendered.
23
00:01:52,310 --> 00:01:58,790
Now, I know that's kind of jargon right now, but I'm going to explain it to you throughout this course.
24
00:01:58,910 --> 00:02:02,930
But the main one I want us to focus on is component did mount.
25
00:02:03,560 --> 00:02:09,440
Now, what component did Mount does is it's kind of like the name, right?
26
00:02:09,740 --> 00:02:18,380
When this component mounts, mounting is essentially one react, puts our component on the page.
27
00:02:18,380 --> 00:02:18,740
Right.
28
00:02:18,860 --> 00:02:21,170
It renders it onto the DOM for the first time.
29
00:02:21,890 --> 00:02:26,990
When it does that, it it calls whatever block of code we write inside of here.
30
00:02:27,200 --> 00:02:28,160
That's all it does.
31
00:02:29,530 --> 00:02:37,450
So what we want to do is we want to use JavaScript, Native Fach, right to fetch from that you URL
32
00:02:38,050 --> 00:02:42,160
make an API request to that your URL.
33
00:02:43,310 --> 00:02:49,070
And what this fat returns us is a promise, right, but that promise.
34
00:02:51,270 --> 00:02:57,980
Gives us a response of the actual body of our response, but we don't know what that looks like at first,
35
00:02:57,990 --> 00:02:58,170
right.
36
00:02:58,180 --> 00:02:59,670
So let's just log it out and see.
37
00:03:01,180 --> 00:03:03,040
So we see this response object, right?
38
00:03:03,050 --> 00:03:08,590
It's got the body and it's got these headers, it's got all these properties that we don't really care
39
00:03:08,590 --> 00:03:08,820
about.
40
00:03:08,830 --> 00:03:09,880
We don't really need them.
41
00:03:09,880 --> 00:03:10,240
Right.
42
00:03:10,570 --> 00:03:15,850
The main thing we want is we want the body, but we want it in a format that our JavaScript understands
43
00:03:15,910 --> 00:03:17,110
in the format of JSON.
44
00:03:17,470 --> 00:03:17,800
Right.
45
00:03:18,190 --> 00:03:23,620
So let's from here return response JSON, which is a method.
46
00:03:23,770 --> 00:03:27,640
Right, on this response object that will give us back.
47
00:03:28,820 --> 00:03:32,060
That response in the JSON format, which we want.
48
00:03:33,270 --> 00:03:40,440
And then that will get returned to us as a new promise, which we will now have as the body, which
49
00:03:40,440 --> 00:03:42,300
will be this array.
50
00:03:44,000 --> 00:03:48,650
So when we get that array, let's just log it again so that we know what we're getting.
51
00:03:51,760 --> 00:03:55,330
We see Aurora right in the format, in the objects that we know.
52
00:03:56,250 --> 00:04:06,330
And we can use so what we want to do is now we want to call set state, as we did before, but now we
53
00:04:06,330 --> 00:04:11,720
want to set our monsters to this array of these users.
54
00:04:11,760 --> 00:04:12,120
Right.
55
00:04:13,530 --> 00:04:20,890
And we want our initial state to be an empty array because we don't need to hardcoded anymore, right?
56
00:04:20,910 --> 00:04:26,640
We're just going to we're going to wait for our component to mount and then we're going to fetch all
57
00:04:26,640 --> 00:04:34,360
of our users and then we're going to update our state's monster property with this new array of users.
58
00:04:34,740 --> 00:04:36,570
And now when we check our application.
59
00:04:37,780 --> 00:04:44,710
We see that we have all of our monsters or users, right, and we're displaying all their name.
60
00:04:45,600 --> 00:04:49,560
Now we see this area here, right, it's just a warning and it's telling us that we're not using the
61
00:04:49,560 --> 00:04:51,390
logo anymore, so let's just get rid of it.
62
00:04:51,930 --> 00:04:55,560
That's the great thing about create racked up as well, is it comes with LINTANG.
63
00:04:55,950 --> 00:04:59,100
We can be aware of writing and making our code clean.
64
00:05:00,570 --> 00:05:07,980
So now, just to go over this again, we're using this component did Mount Life-cycle method that we
65
00:05:07,980 --> 00:05:12,420
have access to because of our class component.
66
00:05:13,340 --> 00:05:21,050
And we are fetching from this you erl, we are taking the response and converting it into the JSON format
67
00:05:21,050 --> 00:05:29,030
that are JavaScript can understand and use, and then we're going to take the users that we got back
68
00:05:29,030 --> 00:05:32,720
from it and set our monsters to that array of user.
69
00:05:33,710 --> 00:05:36,980
And that's how we end up with this list of.
70
00:05:38,030 --> 00:05:38,510
Users.
71
00:05:39,600 --> 00:05:47,100
In the next section, we are going to now convert this to look a little bit closer to this.
6788
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.