Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,150 --> 00:00:04,059
For that in the components folder
2
00:00:04,059 --> 00:00:06,050
I'll add a new sub folder.
3
00:00:06,050 --> 00:00:07,820
I'll name it Demo.
4
00:00:07,820 --> 00:00:09,570
And in there I'll add a new file.
5
00:00:09,570 --> 00:00:14,570
And that's my DemoOutput.js file
6
00:00:15,120 --> 00:00:18,360
where I import React from react.
7
00:00:18,360 --> 00:00:22,820
And then here I have the DemoOutput where I get props
8
00:00:24,780 --> 00:00:27,140
and I export this as a default, of course.
9
00:00:27,140 --> 00:00:29,000
So that's a standard component.
10
00:00:29,000 --> 00:00:31,620
At least it is as soon as we return js
11
00:00:31,620 --> 00:00:34,790
and now I'll take my paragraph here.
12
00:00:34,790 --> 00:00:36,500
This is new.
13
00:00:36,500 --> 00:00:38,790
Remove this line here from the app component
14
00:00:38,790 --> 00:00:41,820
and add it instead here as the return value.
15
00:00:41,820 --> 00:00:45,710
To be precise, I'll do something slightly different now,
16
00:00:45,710 --> 00:00:48,910
I will not conditionally render my own component or not
17
00:00:48,910 --> 00:00:51,328
because it should be obvious that this will behave
18
00:00:51,328 --> 00:00:54,880
like rendering the built-in paragraph component or not.
19
00:00:54,880 --> 00:00:57,270
Instead, inside of DemoOutput
20
00:00:57,270 --> 00:01:00,410
I'll change the HTML structure a tiny bit.
21
00:01:00,410 --> 00:01:05,349
It'll not just be this paragraph like this.
22
00:01:05,349 --> 00:01:08,090
Instead, I will render this text
23
00:01:08,090 --> 00:01:09,760
in the paragraph conditionally
24
00:01:09,760 --> 00:01:11,650
and I will render it conditionally
25
00:01:11,650 --> 00:01:14,630
based on some data I get through props
26
00:01:14,630 --> 00:01:18,340
so that I can check if props show for example.
27
00:01:18,340 --> 00:01:21,050
So if the show prop is truthy
28
00:01:21,050 --> 00:01:22,620
let's say we expect a Boolean here.
29
00:01:22,620 --> 00:01:26,340
So if it is true, and if that is the case
30
00:01:26,340 --> 00:01:28,350
then I want to render "This is new!"
31
00:01:28,350 --> 00:01:30,850
otherwise I will render an empty string.
32
00:01:30,850 --> 00:01:33,880
So the paragraph element is always rendered
33
00:01:33,880 --> 00:01:35,620
just the text changes now
34
00:01:35,620 --> 00:01:37,143
based on the show prop.
35
00:01:38,360 --> 00:01:40,300
So now back in app component 'Hi there!'
36
00:01:40,300 --> 00:01:43,160
for one to include my DemoOutput here.
37
00:01:43,160 --> 00:01:46,193
So make sure you import DemoOutput of course,
38
00:01:47,080 --> 00:01:49,830
and there, I want to set the show prop
39
00:01:49,830 --> 00:01:53,050
because we use that show prop in DemoOutput.
40
00:01:53,050 --> 00:01:54,650
And the value we pass here
41
00:01:54,650 --> 00:01:56,947
should of course be ShowParagraph
42
00:01:58,190 --> 00:01:59,560
because that is a Boolean.
43
00:01:59,560 --> 00:02:00,920
It is false initially,
44
00:02:00,920 --> 00:02:03,560
and it's changed to true if we click the button
45
00:02:03,560 --> 00:02:05,930
and then back to false ends on.
46
00:02:05,930 --> 00:02:08,360
So now it is component here is always rendered
47
00:02:08,360 --> 00:02:10,889
and in the component, the paragraph is always rendered
48
00:02:10,889 --> 00:02:12,540
but the text that's shown
49
00:02:12,540 --> 00:02:15,830
that will change based on the show prop.
50
00:02:15,830 --> 00:02:18,110
So now if I saved that,
51
00:02:18,110 --> 00:02:18,943
let's go back.
52
00:02:18,943 --> 00:02:21,260
First of all, if it clicked this you see
53
00:02:21,260 --> 00:02:22,910
we got basically the same behavior
54
00:02:22,910 --> 00:02:25,970
as before there is no great difference.
55
00:02:25,970 --> 00:02:28,810
Now, if we expand our Dom here a little bit
56
00:02:28,810 --> 00:02:32,050
we see of course that the paragraph is always there
57
00:02:32,050 --> 00:02:34,060
but again, if it clicked a button,
58
00:02:34,060 --> 00:02:37,910
we see that the paragraph flashes when I click it
59
00:02:37,910 --> 00:02:40,540
and it flashes again if I click it again,
60
00:02:40,540 --> 00:02:42,400
the reason for the paragraph flashing
61
00:02:42,400 --> 00:02:44,500
is that we now only work with that element
62
00:02:44,500 --> 00:02:46,810
and we add or remove the text.
63
00:02:46,810 --> 00:02:49,380
And this is treated as a change for entire element
64
00:02:49,380 --> 00:02:52,470
which is why the paragraph flashes and not just the text
65
00:02:52,470 --> 00:02:56,250
because the text is basically like a prop to the paragraph.
66
00:02:56,250 --> 00:02:58,360
You could say it's it's content,
67
00:02:58,360 --> 00:02:59,780
but again it's just a paragraph.
68
00:02:59,780 --> 00:03:02,020
It's not the h1 or the button element.
69
00:03:02,020 --> 00:03:04,500
So the fact that we're using a custom element,
70
00:03:04,500 --> 00:03:07,100
a custom component does not change this.
71
00:03:07,100 --> 00:03:08,600
And of course it shouldn't
72
00:03:08,600 --> 00:03:11,010
the updating mechanism should always work
73
00:03:11,010 --> 00:03:13,310
by comparing differences.
74
00:03:13,310 --> 00:03:15,290
It is of course, interesting to see
75
00:03:16,300 --> 00:03:18,970
that we are changing state in the app component.
76
00:03:18,970 --> 00:03:20,780
And yet only the paragraph
77
00:03:20,780 --> 00:03:23,440
which is part of another component flashes.
78
00:03:23,440 --> 00:03:26,950
So this of course, proofs that the react updating
79
00:03:26,950 --> 00:03:31,410
and diffing mechanism works and finds out changes correctly.
80
00:03:31,410 --> 00:03:33,550
Now, another thing is also interesting though,
81
00:03:33,550 --> 00:03:35,560
if we go to the Console
82
00:03:35,560 --> 00:03:37,350
and a reload here, you see app running
83
00:03:37,350 --> 00:03:38,210
and you see that,
84
00:03:38,210 --> 00:03:41,300
of course runs whenever I click this button.
85
00:03:41,300 --> 00:03:45,060
Now keep in mind the actual change happens in DemoOutput
86
00:03:45,060 --> 00:03:48,350
but of course the app component runs again as well
87
00:03:48,350 --> 00:03:50,460
because that's where we managed the state.
88
00:03:50,460 --> 00:03:54,327
And I mentioned the component where you manage state
89
00:03:54,327 --> 00:03:57,001
or where you have props or context
90
00:03:57,001 --> 00:04:00,230
and that state props or context changes
91
00:04:00,230 --> 00:04:05,230
that component will be re-evaluated and re-executed.
92
00:04:05,670 --> 00:04:08,260
And we are managing the state in app.
93
00:04:08,260 --> 00:04:09,830
So just because the change
94
00:04:09,830 --> 00:04:13,460
visually only affects this paragraph in another component
95
00:04:13,460 --> 00:04:16,390
does not mean that the app component is not re-evaluated.
96
00:04:16,390 --> 00:04:19,560
Of course it is because there we manage our state
97
00:04:19,560 --> 00:04:20,540
and that's, by the way
98
00:04:20,540 --> 00:04:23,560
also what I meant earlier with props and context
99
00:04:23,560 --> 00:04:27,110
in the end always coming down to state changes.
100
00:04:27,110 --> 00:04:29,340
The props we got in DemoOutput
101
00:04:29,340 --> 00:04:33,470
are of course responsible for why DemoOutput is re-rendered.
102
00:04:33,470 --> 00:04:36,480
But ultimately of course, the value we get here
103
00:04:36,480 --> 00:04:37,860
on the show prop
104
00:04:37,860 --> 00:04:40,850
is the value we manage in the ShowParagraph state,
105
00:04:40,850 --> 00:04:43,830
ultimately it all comes down to state
106
00:04:43,830 --> 00:04:45,653
and state changes in react.
107
00:04:46,760 --> 00:04:48,850
Now it, of course should be obvious
108
00:04:48,850 --> 00:04:52,237
that if I now add a console log here in DemoOutput
109
00:04:59,250 --> 00:05:00,870
that we'll see this being printed
110
00:05:00,870 --> 00:05:02,510
for every button click as well.
111
00:05:02,510 --> 00:05:03,680
We see it initially
112
00:05:03,680 --> 00:05:06,120
because that component is rendered initially.
113
00:05:06,120 --> 00:05:08,270
And then if I click Toggle Paragraph,
114
00:05:08,270 --> 00:05:11,090
well, we again see DemoOutput running for every click.
115
00:05:11,090 --> 00:05:12,910
And again, that makes a lot of sense
116
00:05:12,910 --> 00:05:15,303
because the props changed with every click.
117
00:05:16,630 --> 00:05:20,400
So thus far, this hopefully all is pretty clear
118
00:05:20,400 --> 00:05:22,010
and pretty straightforward.
119
00:05:22,010 --> 00:05:25,200
Now I'll show you something that might be confusing.
120
00:05:25,200 --> 00:05:28,170
Keep in mind the DemoOput running is printed
121
00:05:28,170 --> 00:05:29,003
for every click.
122
00:05:29,003 --> 00:05:31,750
And again, that makes sense because we do change the state
123
00:05:31,750 --> 00:05:34,470
and therefore the props here with every click.
124
00:05:34,470 --> 00:05:37,290
But now let me tweak this in App.js
125
00:05:37,290 --> 00:05:40,710
I will now hide the code to value I pass to show
126
00:05:40,710 --> 00:05:43,950
and it will set this to false.
127
00:05:43,950 --> 00:05:46,870
This means that we'll never show the output
128
00:05:46,870 --> 00:05:49,900
because it's now detached from the ShowParagraph state.
129
00:05:49,900 --> 00:05:53,050
Indeed, I'm not using the ShowParagraph state at the moment.
130
00:05:53,050 --> 00:05:56,203
I'm still changing it, but I'm not using the state value.
131
00:05:57,350 --> 00:05:59,940
Now, what would you expect as a result?
132
00:05:59,940 --> 00:06:02,370
Let's see if the app reload
133
00:06:02,370 --> 00:06:05,400
we see app running and DemoOutput running makes sense.
134
00:06:05,400 --> 00:06:07,730
The app just loaded, both was rendered,
135
00:06:07,730 --> 00:06:09,310
but something interesting happens
136
00:06:09,310 --> 00:06:10,870
when I click Toggle Paragraph.
137
00:06:10,870 --> 00:06:13,470
If I do that, you see app running,
138
00:06:13,470 --> 00:06:15,300
okay, we changed the state there
139
00:06:15,300 --> 00:06:17,420
but you also see DemoOutput running.
140
00:06:17,420 --> 00:06:18,880
And that's interesting.
141
00:06:18,880 --> 00:06:21,150
The props didn't change, right?
142
00:06:21,150 --> 00:06:22,750
We have to show prop
143
00:06:22,750 --> 00:06:26,030
and the value I pass in there is still false.
144
00:06:26,030 --> 00:06:28,800
It never changes, it's always false.
145
00:06:28,800 --> 00:06:31,433
So why was DemoOutput re-executed?
146
00:06:32,400 --> 00:06:34,766
Well, let's take it step by step.
147
00:06:34,766 --> 00:06:39,370
The app function is re-executed because state changed here.
148
00:06:39,370 --> 00:06:41,670
Now, what is part of that app function?
149
00:06:41,670 --> 00:06:43,270
Of course, this return statement
150
00:06:43,270 --> 00:06:45,483
and there we return some js code.
151
00:06:46,320 --> 00:06:49,040
Now, all those js elements here
152
00:06:49,040 --> 00:06:51,440
in the end are like function calls
153
00:06:51,440 --> 00:06:53,760
to the respect of component functions.
154
00:06:53,760 --> 00:06:56,440
So we call the function for the DemoOutput component.
155
00:06:56,440 --> 00:06:59,280
We call the function for the button component.
156
00:06:59,280 --> 00:07:03,280
That's why those child components are also re-executed
157
00:07:03,280 --> 00:07:06,870
and re-evaluated just because the parent component changed
158
00:07:06,870 --> 00:07:08,250
because they are part
159
00:07:08,250 --> 00:07:10,980
of the parent components, function body.
160
00:07:10,980 --> 00:07:13,440
And if the parent component function runs again
161
00:07:13,440 --> 00:07:16,470
of course the child component functions also run again.
162
00:07:16,470 --> 00:07:19,550
So in the end, the prop value doesn't even matter here
163
00:07:19,550 --> 00:07:21,800
for this component to be executed again,
164
00:07:21,800 --> 00:07:24,683
it's just a fact that the parent changed.
165
00:07:26,410 --> 00:07:27,640
Changes in props
166
00:07:27,640 --> 00:07:30,620
might lead to actual changes on the real Dom
167
00:07:30,620 --> 00:07:33,170
but for the function to be re-evaluated
168
00:07:33,170 --> 00:07:37,110
it's enough that the parent component was re-evaluated.
169
00:07:37,110 --> 00:07:40,540
Of course, the fact that DemoOutput runs again
170
00:07:40,540 --> 00:07:42,770
does not mean that the real Dom is touched.
171
00:07:42,770 --> 00:07:44,660
We can verify this with elements
172
00:07:44,660 --> 00:07:47,180
there you'll see that nothing flashes.
173
00:07:47,180 --> 00:07:49,950
I can hammer that button and nothing changes there
174
00:07:51,650 --> 00:07:54,850
because react just re-evaluates the components.
175
00:07:54,850 --> 00:07:57,560
And as you learned, re-evaluating a component
176
00:07:57,560 --> 00:07:59,380
and re-running its component function
177
00:07:59,380 --> 00:08:01,870
is not the same as re-rendering the real Dom
178
00:08:01,870 --> 00:08:03,753
or manipulating the real Dom.
179
00:08:04,640 --> 00:08:06,800
But it is really important to understand
180
00:08:06,800 --> 00:08:09,650
that if a component is re-executed
181
00:08:09,650 --> 00:08:11,380
all its child components
182
00:08:11,380 --> 00:08:14,263
will also be re-executed and re-evaluated.
183
00:08:15,260 --> 00:08:18,070
So therefore of course not just a DemoOutput component
184
00:08:18,070 --> 00:08:20,980
is re-evaluated, the button is as well.
185
00:08:20,980 --> 00:08:24,830
If I console log button running here
186
00:08:25,990 --> 00:08:30,470
you will see that if I go to Console and reload
187
00:08:30,470 --> 00:08:32,990
we see all three outputs initially.
188
00:08:32,990 --> 00:08:35,460
And if I click this Toggle Paragraph button
189
00:08:35,460 --> 00:08:37,100
we also see all three.
190
00:08:37,100 --> 00:08:41,720
So the button component is also re-evaluated as well.
191
00:08:41,720 --> 00:08:44,560
This of course brings up one question though,
192
00:08:44,560 --> 00:08:46,880
here we have a very simple app
193
00:08:46,880 --> 00:08:51,220
just one route component with two child components
194
00:08:51,220 --> 00:08:54,810
which then each don't have any more child components
195
00:08:54,810 --> 00:08:56,960
if they would have child components though
196
00:08:56,960 --> 00:09:00,150
those child components would also be re-evaluated
197
00:09:00,150 --> 00:09:03,110
of course and I can show this to you.
198
00:09:03,110 --> 00:09:06,690
If I add a, MyParagraph.js file here
199
00:09:06,690 --> 00:09:09,106
where I essentially copied DemoOutput,
200
00:09:09,106 --> 00:09:10,880
but I then simplify this
201
00:09:10,880 --> 00:09:15,880
and here I have MyParagraph also as a component name.
202
00:09:16,470 --> 00:09:19,480
And then in there, I just output a paragraph tag
203
00:09:19,480 --> 00:09:23,003
with props children in between.
204
00:09:24,230 --> 00:09:27,330
I can use MyParagraph instead of the built in paragraph.
205
00:09:27,330 --> 00:09:29,470
And I'm really just doing this for demo purposes here
206
00:09:29,470 --> 00:09:31,790
to show you how react updating works.
207
00:09:31,790 --> 00:09:36,150
Not because this would be such a useful component, it isn't.
208
00:09:36,150 --> 00:09:38,720
But now with that, if I add MyParagraph
209
00:09:38,720 --> 00:09:42,950
which replaces to build-in paragraph, just to then reuse it
210
00:09:42,950 --> 00:09:46,010
if I add this you'll of course will see
211
00:09:46,010 --> 00:09:50,130
in the console that MyParagraph running also is re-executed
212
00:09:50,130 --> 00:09:51,530
for every button click.
213
00:09:51,530 --> 00:09:53,740
So this continuous down the component tree
214
00:09:53,740 --> 00:09:56,799
and this poses or this brings up one important question,
215
00:09:56,799 --> 00:09:59,100
isn't this bad?
216
00:09:59,100 --> 00:10:03,170
Because if all those component functions are re-executed
217
00:10:03,170 --> 00:10:06,120
that's a lot of ongoing function executions
218
00:10:06,120 --> 00:10:08,580
and virtual comparisons
219
00:10:08,580 --> 00:10:11,740
that certainly costs some performance, right?
220
00:10:11,740 --> 00:10:15,140
So isn't this bad if there actually is no reason for that
221
00:10:15,140 --> 00:10:16,950
because we know for example,
222
00:10:16,950 --> 00:10:20,350
that we never need to re-evaluate DemoOutput
223
00:10:20,350 --> 00:10:22,240
because it can't change.
224
00:10:22,240 --> 00:10:24,940
The only prop, which it has is hard coded.
225
00:10:24,940 --> 00:10:26,450
If it had no props at all
226
00:10:26,450 --> 00:10:30,240
we also would know the output in there will never change
227
00:10:30,240 --> 00:10:33,180
because of the state that's managed in app component.
228
00:10:33,180 --> 00:10:35,430
So re-executing DemoOutput
229
00:10:35,430 --> 00:10:38,883
and all its children is a total waste.
18138
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.