Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,450 --> 00:00:05,350
So how does React work?
2
00:00:05,350 --> 00:00:08,940
Lets not forget that React is a JavaScript library
3
00:00:08,940 --> 00:00:11,390
for building user interfaces.
4
00:00:11,390 --> 00:00:13,760
That's what I taught you at the very beginning
5
00:00:13,760 --> 00:00:17,310
of this course and of course it hasn't changed.
6
00:00:17,310 --> 00:00:19,160
We also learned and we saw,
7
00:00:19,160 --> 00:00:21,880
that React is all about components.
8
00:00:21,880 --> 00:00:25,040
We use components to build those user interfaces,
9
00:00:25,040 --> 00:00:28,500
and React embraces this component concept.
10
00:00:28,500 --> 00:00:32,250
It uses components to effectively compose user interfaces
11
00:00:32,250 --> 00:00:33,500
and it uses components
12
00:00:33,500 --> 00:00:36,840
to effectively update user interfaces.
13
00:00:36,840 --> 00:00:38,740
There's one important note.
14
00:00:38,740 --> 00:00:42,510
We also saw this ReactDOM thing
15
00:00:42,510 --> 00:00:46,600
and in the end ReactDOM is your interface to the web.
16
00:00:46,600 --> 00:00:51,600
React itself, React.js does not know the web.
17
00:00:51,830 --> 00:00:54,420
It does know nothing about the browser in the end.
18
00:00:54,420 --> 00:00:57,590
React knows how to work with components
19
00:00:57,590 --> 00:00:59,130
but it doesn't care
20
00:00:59,130 --> 00:01:03,300
whether those components contain HTML elements
21
00:01:03,300 --> 00:01:07,220
or if they contain totally fictional elements,
22
00:01:07,220 --> 00:01:09,430
that does not matter to React.
23
00:01:09,430 --> 00:01:12,280
It's ReactDOM to which that matters in the end
24
00:01:12,280 --> 00:01:16,570
and which ultimately needs to bring real HTML elements
25
00:01:16,570 --> 00:01:18,090
to the screen.
26
00:01:18,090 --> 00:01:21,630
But React is just a library that manages components,
27
00:01:21,630 --> 00:01:23,250
that manages state
28
00:01:23,250 --> 00:01:26,320
and that manages different components states
29
00:01:26,320 --> 00:01:30,440
and that finds out how components might need to change
30
00:01:30,440 --> 00:01:33,600
and which differences you might have from a previous state
31
00:01:33,600 --> 00:01:36,610
of a component compared to the current state.
32
00:01:36,610 --> 00:01:41,060
And React hands all that information regarding what changed
33
00:01:41,060 --> 00:01:43,880
and what should be visible on the screen
34
00:01:43,880 --> 00:01:46,080
no matter what screen that is,
35
00:01:46,080 --> 00:01:48,040
off to the interface it's working with,
36
00:01:48,040 --> 00:01:50,620
for example, it hands it off to ReactDOM
37
00:01:50,620 --> 00:01:53,240
because ReactDOM is then responsible
38
00:01:53,240 --> 00:01:56,740
for working with the real DOM, which is part of the browser.
39
00:01:56,740 --> 00:01:59,140
And therefore ReactDOM is responsible
40
00:01:59,140 --> 00:02:01,190
for bringing something onto the screen,
41
00:02:01,190 --> 00:02:03,840
which the user is then able to see.
42
00:02:03,840 --> 00:02:06,230
React only cares about components.
43
00:02:06,230 --> 00:02:09,449
It cares about props, which is basically data you pass
44
00:02:09,449 --> 00:02:12,570
to components to make components configurable
45
00:02:12,570 --> 00:02:16,410
and to enable parent-child component communication.
46
00:02:16,410 --> 00:02:17,930
React cares about state
47
00:02:17,930 --> 00:02:21,020
which is internal data inside of a component.
48
00:02:21,020 --> 00:02:23,150
And React cares about context
49
00:02:23,150 --> 00:02:25,160
which is component-wide data.
50
00:02:25,160 --> 00:02:26,300
Now of course, React has a couple
51
00:02:26,300 --> 00:02:27,930
of other features built in as well.
52
00:02:27,930 --> 00:02:29,880
But these are the core features.
53
00:02:29,880 --> 00:02:33,720
And whenever props, state or context changes,
54
00:02:33,720 --> 00:02:36,900
components that use these concepts are updated
55
00:02:36,900 --> 00:02:40,830
by React and React checks whether this component
56
00:02:40,830 --> 00:02:43,820
now wants to draw something new onto the screen.
57
00:02:43,820 --> 00:02:45,710
And if that should be the case,
58
00:02:45,710 --> 00:02:49,000
React will let ReactDOM know about that
59
00:02:49,000 --> 00:02:52,440
so that ReactDOM is able to bring that new screen,
60
00:02:52,440 --> 00:02:56,180
that new component, that new output to the screen.
61
00:02:56,180 --> 00:02:59,880
So therefore, if we have a close look at that components
62
00:02:59,880 --> 00:03:02,170
real DOM communication here,
63
00:03:02,170 --> 00:03:05,710
the question of course is, how exactly does this work?
64
00:03:05,710 --> 00:03:09,080
As I mentioned, React is concerned about components
65
00:03:09,080 --> 00:03:11,180
and what React in the end does is,
66
00:03:11,180 --> 00:03:14,420
it uses a concept called the virtual DOM.
67
00:03:14,420 --> 00:03:17,340
It determines how the component tree,
68
00:03:17,340 --> 00:03:19,130
which your app is building in the end,
69
00:03:19,130 --> 00:03:22,380
and every component itself, of course, has a sub tree,
70
00:03:22,380 --> 00:03:24,980
that JSX code returned by that component.
71
00:03:24,980 --> 00:03:28,260
It determines how that component tree currently looks like
72
00:03:28,260 --> 00:03:29,670
and what it should look like,
73
00:03:29,670 --> 00:03:32,000
for example, after a state update.
74
00:03:32,000 --> 00:03:35,160
And that information is then handed off to ReactDOM
75
00:03:35,160 --> 00:03:36,890
which now knows about the differences
76
00:03:36,890 --> 00:03:40,630
and which then knows how it should manipulate the real DOM
77
00:03:40,630 --> 00:03:42,870
to match that virtual DOM,
78
00:03:42,870 --> 00:03:46,370
that virtual snapshot React derived
79
00:03:46,370 --> 00:03:48,133
for your component trees.
80
00:03:49,280 --> 00:03:52,070
And related to that, there's one important thing to note.
81
00:03:52,070 --> 00:03:53,790
Throughout this course I often said
82
00:03:53,790 --> 00:03:57,060
that React would rerun a component function,
83
00:03:57,060 --> 00:03:59,830
that it would reevaluate a component.
84
00:03:59,830 --> 00:04:02,510
And indeed, as I mentioned before,
85
00:04:02,510 --> 00:04:07,510
whenever state props, or a context of a component changes,
86
00:04:07,580 --> 00:04:10,450
that component function is re-executed.
87
00:04:10,450 --> 00:04:14,400
That component is re-evaluated by React.
88
00:04:14,400 --> 00:04:15,760
But it is worth noting
89
00:04:15,760 --> 00:04:18,579
that reevaluating a component is not the same
90
00:04:18,579 --> 00:04:20,300
as re rendering the DOM.
91
00:04:20,300 --> 00:04:24,520
So just because a component function is re-executed by React
92
00:04:24,520 --> 00:04:27,510
does not mean that the respective part
93
00:04:27,510 --> 00:04:29,640
of the actual real DOM
94
00:04:29,640 --> 00:04:32,790
is re-rendered or re-evaluated.
95
00:04:32,790 --> 00:04:36,810
Instead we have to differentiate between our component part,
96
00:04:36,810 --> 00:04:39,300
our React part and the real DOM.
97
00:04:39,300 --> 00:04:41,430
Our components, as I just said,
98
00:04:41,430 --> 00:04:46,150
are re-evaluated whenever props, state or context changes.
99
00:04:46,150 --> 00:04:49,680
So React then executes that component function again.
100
00:04:49,680 --> 00:04:53,790
Now the real DOM on the other hand is only updated
101
00:04:53,790 --> 00:04:56,420
in the places where it needs to be changed
102
00:04:56,420 --> 00:04:59,980
based on that difference React derived
103
00:04:59,980 --> 00:05:03,280
between the previous state of a component and its tree
104
00:05:03,280 --> 00:05:05,880
and the current state after the state prop
105
00:05:05,880 --> 00:05:07,650
or context change.
106
00:05:07,650 --> 00:05:10,610
So the real DOM is not changed all the time.
107
00:05:10,610 --> 00:05:13,460
It's changed rarely and only when needed.
108
00:05:13,460 --> 00:05:15,580
And that's important for performance,
109
00:05:15,580 --> 00:05:18,080
because making a virtual comparison
110
00:05:18,080 --> 00:05:20,710
between the previous state and the current state,
111
00:05:20,710 --> 00:05:23,000
that's fairly cheap and easy to do.
112
00:05:23,000 --> 00:05:25,320
That happens only in memory.
113
00:05:25,320 --> 00:05:28,820
Reaching out to the real DOM, that's rendered in the browser
114
00:05:28,820 --> 00:05:31,960
is pretty expensive from a performance perspective,
115
00:05:31,960 --> 00:05:34,360
because working with the real DOM just turns out
116
00:05:34,360 --> 00:05:36,480
to be a performance intensive task.
117
00:05:36,480 --> 00:05:38,990
Of course not a tiny change in one place,
118
00:05:38,990 --> 00:05:40,640
but if you do that tiny change
119
00:05:40,640 --> 00:05:42,720
in a lot of places all the time,
120
00:05:42,720 --> 00:05:45,280
then your page might become slow
121
00:05:45,280 --> 00:05:48,810
because you're working with the real DOM too much.
122
00:05:48,810 --> 00:05:51,000
And that's my React has this structure
123
00:05:51,000 --> 00:05:54,860
of doing virtual comparisons with that virtual DOM
124
00:05:54,860 --> 00:05:59,180
and then only passing the changes between your last snapshot
125
00:05:59,180 --> 00:06:01,510
and the current snapshot to the real DOM.
126
00:06:01,510 --> 00:06:03,750
That's how React works here.
127
00:06:03,750 --> 00:06:06,310
It does this virtual DOM diffing,
128
00:06:06,310 --> 00:06:09,750
finding out the difference between two snapshots.
129
00:06:09,750 --> 00:06:11,330
And to see a real example,
130
00:06:11,330 --> 00:06:12,960
it could look like this.
131
00:06:12,960 --> 00:06:15,200
Let's say all the given component
132
00:06:15,200 --> 00:06:18,050
our previous evaluation result.
133
00:06:18,050 --> 00:06:21,730
So when the component function ran the last time is this.
134
00:06:21,730 --> 00:06:23,430
And now some state changes
135
00:06:23,430 --> 00:06:25,980
and all of a sudden we wanna show a new paragraph.
136
00:06:25,980 --> 00:06:28,800
So that's our current evaluation result.
137
00:06:28,800 --> 00:06:32,290
In this case, React would determine that the difference
138
00:06:32,290 --> 00:06:35,200
between both snapshots is this paragraph
139
00:06:35,200 --> 00:06:38,140
and it would report does change to ReactDOM
140
00:06:38,140 --> 00:06:40,500
so that ReactDOM can update the real DOM
141
00:06:40,500 --> 00:06:42,888
and insert this paragraph.
142
00:06:42,888 --> 00:06:46,040
ReactDOM would not rerender the entire DOM.
143
00:06:46,040 --> 00:06:49,720
It would not touch this existing h1 or div element.
144
00:06:49,720 --> 00:06:52,470
It would only insert the paragraph
145
00:06:52,470 --> 00:06:55,630
after the h1 element inside of the div.
146
00:06:55,630 --> 00:06:59,560
That's how React works behind the scenes in a nutshell.
147
00:06:59,560 --> 00:07:03,100
Now let's see this in action with some real code
148
00:07:03,100 --> 00:07:04,860
and let's play around with that a bit
149
00:07:04,860 --> 00:07:06,700
so that we get a feeling for this
150
00:07:06,700 --> 00:07:10,260
and so that we can all find out how this all behaves
151
00:07:10,260 --> 00:07:13,930
and possibly changes for props and state changes
152
00:07:13,930 --> 00:07:17,810
for context, and for other components being involved
153
00:07:17,810 --> 00:07:20,863
because it is really crucial that you understand this.
12265
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.