Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,800 --> 00:00:07,040
Now to get a first feeling for how React keeps the user interface in sync with state.
2
00:00:07,070 --> 00:00:14,270
Let's quickly compare the advice app that we built in the first section with a vanilla JavaScript implementation
3
00:00:14,270 --> 00:00:16,340
of the same application.
4
00:00:17,300 --> 00:00:19,730
So here I have them open side by side.
5
00:00:19,730 --> 00:00:27,500
And if you want to follow along, I actually added this vanilla JavaScript dot HTML file right into
6
00:00:27,710 --> 00:00:32,810
the same code sandbox that we used to build this first advice app.
7
00:00:33,450 --> 00:00:41,010
So right here in the source folder there is now this vanilla dot JS or actually vanilla dot HTML file.
8
00:00:41,010 --> 00:00:46,560
And again I will post the link to this code sandbox into this lecture.
9
00:00:48,250 --> 00:00:48,880
All right.
10
00:00:48,880 --> 00:00:54,940
So in case you don't really remember what we did back here, you can always pause the video and just
11
00:00:54,940 --> 00:00:58,090
analyze this code here for a second for yourself.
12
00:00:58,180 --> 00:01:04,960
So what I want to do now, again, is to just quickly compare the React code that we wrote before with
13
00:01:04,960 --> 00:01:06,970
this vanilla JavaScript.
14
00:01:07,780 --> 00:01:15,100
Now, ironically, this vanilla JavaScript implementation is actually in an HTML file, so I placed
15
00:01:15,310 --> 00:01:22,270
all the HTML file and then also the JavaScript right into one HTML file just to keep it all in the same
16
00:01:22,270 --> 00:01:22,960
place.
17
00:01:23,230 --> 00:01:28,360
But already this shows us the very first difference between the two philosophies.
18
00:01:28,360 --> 00:01:33,430
So here everything is basically done in JavaScript.
19
00:01:33,430 --> 00:01:35,560
So even the JS.
20
00:01:36,160 --> 00:01:41,920
So basically this kind of HTML here is written inside of JavaScript.
21
00:01:41,920 --> 00:01:49,900
So JavaScript is taking care of everything while here in the vanilla JavaScript HTML is still in charge.
22
00:01:49,900 --> 00:01:56,500
So we have an HTML file and that HTML file is then including the JavaScript into it.
23
00:01:56,500 --> 00:01:59,710
So it's basically the other way around, right?
24
00:01:59,740 --> 00:02:05,770
So right inside this HTML, we still have our same H1 here for the advice.
25
00:02:05,800 --> 00:02:07,850
The only difference is that it's empty.
26
00:02:07,850 --> 00:02:11,960
While here we include that advice state.
27
00:02:11,990 --> 00:02:18,770
Then we have the button and the paragraph for the message then inside of the script.
28
00:02:18,770 --> 00:02:25,310
So inside of the JavaScript part, we need to of course then manually select all of these three Dom
29
00:02:25,310 --> 00:02:26,090
elements.
30
00:02:26,240 --> 00:02:33,110
So I selected the advice element, the button, and also this one here for the count.
31
00:02:33,110 --> 00:02:38,320
And so for that, all of them needed to have this class while here.
32
00:02:38,330 --> 00:02:39,980
Well, we don't need any of that.
33
00:02:39,980 --> 00:02:43,250
There is no class here in any of these elements.
34
00:02:43,250 --> 00:02:46,250
And also we are nowhere manually selecting them.
35
00:02:46,250 --> 00:02:47,030
Right.
36
00:02:48,830 --> 00:02:54,040
So then next we have this get advice function, which we will get to in a minute.
37
00:02:54,050 --> 00:02:59,860
And then we still basically have two pieces which we can call like State.
38
00:02:59,870 --> 00:03:04,730
So we have a count value which starts at zero and an advice.
39
00:03:04,850 --> 00:03:08,450
So these are exactly the same as here, these states.
40
00:03:09,610 --> 00:03:13,810
Then finally, in the end, we also attach an event listener.
41
00:03:14,630 --> 00:03:17,720
So this get advice function right here.
42
00:03:17,720 --> 00:03:24,290
So we attach that to the button that we selected here manually before while here in the JSX, we do
43
00:03:24,290 --> 00:03:27,320
it using this onClick attribute right here.
44
00:03:27,620 --> 00:03:34,640
So right off the bat, you see immediately that we need to do a lot of things manually over here that
45
00:03:34,640 --> 00:03:36,770
here happen more automatically.
46
00:03:36,770 --> 00:03:41,330
And again, that here it is really the JavaScript that is in charge of everything.
47
00:03:41,330 --> 00:03:43,610
While here it is still the HTML.
48
00:03:43,760 --> 00:03:50,690
But anyway, let's now get here to the heart of the application which is really this get advice function.
49
00:03:50,690 --> 00:03:58,100
So just like here in our React implementation, we start by getting the data and then we update the
50
00:03:58,100 --> 00:04:05,540
service or actually the advice and the count values that we defined down here, right?
51
00:04:05,540 --> 00:04:09,560
So we're updating the count here and the advice right here.
52
00:04:09,860 --> 00:04:14,550
However, simply updating these two values will of course do nothing.
53
00:04:14,550 --> 00:04:20,670
So the user interface will not change by us updating these JavaScript values.
54
00:04:20,670 --> 00:04:28,590
While here in this React example, as soon as we update the advice and the count state automatically
55
00:04:28,590 --> 00:04:33,090
react kept the user interface in sync with these two values.
56
00:04:33,090 --> 00:04:38,160
And so that's basically the big message that we got from the previous lecture, right?
57
00:04:38,160 --> 00:04:44,820
So that React and all these other front end frameworks are really good at keeping the data automatically
58
00:04:44,820 --> 00:04:47,130
in sync with the user interface.
59
00:04:47,130 --> 00:04:49,690
And again, that's exactly what's happening here.
60
00:04:49,710 --> 00:04:55,230
All we care about here is to set the new data and that's all we do.
61
00:04:55,260 --> 00:05:01,650
React is the one who automatically takes care of updating the user interface without us having to do
62
00:05:01,650 --> 00:05:02,400
anything.
63
00:05:02,900 --> 00:05:09,580
But here, of course in the vanilla JavaScript implementation, we need to manually do that.
64
00:05:09,590 --> 00:05:13,310
So we need to manually keep the data in sync with the UI.
65
00:05:13,640 --> 00:05:19,310
So we have the data here and then we use that data to update the Dom manually.
66
00:05:19,640 --> 00:05:25,940
So we come here to our count element and the advice element that we also manually selected previously
67
00:05:25,940 --> 00:05:31,430
and we change their text content property, which is just normal Dom manipulation.
68
00:05:31,550 --> 00:05:36,890
So then here we set this one to the count variable and this one to the advice variable.
69
00:05:37,190 --> 00:05:44,570
And this really is a fundamental difference and a fundamental shift in how we build frontend applications.
70
00:05:44,840 --> 00:05:52,070
Now you might argue that in this very small example, doing this is not a lot of work and maybe it might
71
00:05:52,070 --> 00:05:54,740
not even be necessary to learn, react.
72
00:05:54,740 --> 00:06:01,280
And I would agree with you that we would of course not need react to build something really small and
73
00:06:01,280 --> 00:06:03,240
simple, such as this.
74
00:06:03,250 --> 00:06:09,120
But as soon as we get just a little bit bigger than this, it starts kind of getting out of control.
75
00:06:09,270 --> 00:06:17,430
So we would have to select tons of elements and we would really need to create a lot of extra code that
76
00:06:17,430 --> 00:06:24,030
with React, we don't have to again, because it automatically takes care of keeping the data in sync
77
00:06:24,030 --> 00:06:25,490
with the user interface.
78
00:06:25,500 --> 00:06:30,660
That's really the main thing that I want you to keep in mind from this lecture and from the previous
79
00:06:30,660 --> 00:06:31,170
one.
80
00:06:31,640 --> 00:06:38,480
So, of course, feel free to analyze all the differences that exist here in this code, because just
81
00:06:38,480 --> 00:06:42,620
with this, you can really see the two different philosophies at work.
82
00:06:43,010 --> 00:06:48,770
I'm not going to go even deeper into this at this point, but it would be pretty interesting if you
83
00:06:48,770 --> 00:06:55,310
take a few minutes and really compare what's going on on this side here and on the React implementation.
84
00:06:55,310 --> 00:07:01,760
And so once you're done doing that, it's now time to finally learn what React actually is in a bit
85
00:07:01,760 --> 00:07:02,720
more detail.
86
00:07:02,720 --> 00:07:05,900
So hopefully I see you right in the next lecture soon.
9168
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.