跟读练习: Learn useMemo In 10 Minutes - 通过视频学习英语口语
选择练习模式
每种模式都会打开全屏练习室。
139 句
如果句子过短或过长,请点击 Edit 进行调整。
1
Hello everyone, welcome back to my React Hooks series, where in this video we're going to take a look at use memo,
2
how it works, and when you should actually use it.
3
And if you're interested in diving into React even deeper, make sure to check out my full
4
React course, which I'm going to link down in the description below.
5
Have you wanted free web hosting that doesn't suck?
6
Well you're in luck because today's video sponsor, Atlantic .net, is giving you an entire year -long free trial of their hosting.
7
And if you use the code
8
Kyle when you check out, you're going to get an additional $50 of credit that you can use towards whatever you want on the hosting platform.
9
On top of that, this hosting platform has powerful servers with great data reliability
10
and redundancy so you know that your site is going to be there and running with the data you want it to.
11
So make sure you use the code
12
Kyle in the link down in the description below to get your free hosting.
13
Welcome back to Web Dev Simplified.
14
My name's Kyle and my job is to simplify the web for you so you can start building your dream project sooner.
15
So if that sounds interesting, make sure you subscribe to the channel for more videos just like this.
16
Now to get started, I just have some really basic boilerplate code set up for us to test our application.
17
I'm going to walk through it real quick so you know exactly what we're doing.
18
I just have a simple single component called app, which has two pieces of state.
19
One is a number, which we can control by this input over here on the right.
20
And the second one is a Boolean for dark or light, which is toggled by this change theme button on the right side of our screen.
21
Then what we do is we have a function which doubles our number, so just multiplies by two, but this function is very slow.
22
If I scroll down here, you can see I just have a really long for loop
23
that just loops doing nothing for a really long time to emulate what would happen in a really slow, long -running, complex function.
24
Obviously, since this function just multiplies by two, all this code is doing is making the function really slow.
25
And if I change this number, for example, by clicking this up arrow, you're going to see it takes about a second,
26
half a second for it to actually double the number.
27
So you can see this function is quite slow to execute.
28
Now continuing onward, we just have a variable here,
29
which is taking this dark boolean and changing if we have a dark or light theme down here.
30
And then lastly, over here, we just have the output code which changes our number
31
when we update here and also toggles our theme when we click this button.
32
So that's just the basics of our code
33
and you may think okay this is fine code it's working great
34
and the slowness is you know not that bad
35
but it only is going to be slow when we update our number but actually that's incorrect thinking.
36
If we click our change theme button you're going to notice we have the same delay.
37
I'm going to click
38
it now you can see it takes about half a second
39
to a second for it to actually change the actual theme
40
and the reason for that is because when you update state in React, it's going to re -render your entire component.
41
So it's going to run this entire function from top to bottom,
42
which means that this slow function gets called every single time that we render our app component.
43
So whether we're changing the theme, whether we're actually updating the number here, or whether some other component is causing this component to re -render,
44
you're going to be forcing it to go through that slow function over and over and over again.
45
And this is a really big performance problem when you have these slow functions that don't really change that often.
46
So React has a built -in way to solve this with a hook which is called use memo.
47
So we can import this use memo hook just like this.
48
And memo here stands for memoization, which essentially is the idea of caching a value so you don't have to re -compute it every single time.
49
So in our example, this slow function here takes an input of number,
50
and it's always going to give us the same output every time we give it the same input.
51
So we can cache that input value number and the output it gives us.
52
That way if the number doesn't change we don't have to recalculate our slow function over and over and over again.
53
And the easy way to use this is we just call use memo and we're going to pass it in a function.
54
And this function is going to be the thing that we actually mem -wise.
55
So this is the thing that we're caching which in our case is going to be this slow function.
56
We just want to make sure we return this slow function here, get the result from it.
57
And then just like all of the other hooks that we talked about like useEffect for example, we're going to have a list of dependencies here.
58
And in our case the only dependency of this is going to be our number here.
59
Our number is the only thing that changes.
60
And when our number changes we need to rerun the code inside this useMemo hook
61
but if our number doesn't change we don't need to rerun this slow function code.
62
So with that one simple change let's save and see what happened to our app.
63
It's loading over here we can change this to for example one
64
and you can see it has a kind of delay change it up again it's going to have a delay.
65
When we click change theme you're going to notice this is instantaneous I can click it as much as I want to.
66
And that's because when we go through our code, we click change theme, it causes our component to re -render.
67
So it calls this function, does all this stuff, and it gets down to use memo.
68
And it says, well, our number is exactly the same as what it was last time.
69
So we're not going to recall this slow function here because we already know what the result is.
70
It's the exact same thing as it was last time.
71
So we're saving ourselves from having to recalculate this number with the slow function,
72
and we only are forcing ourselves to do this when we actually update this number inside of our input here.
73
So we're essentially only running this slow code when we have to, and not running it when we don't actually need to.
74
Now this seems really nice, and you may think, let me just use memo everywhere, why not just memwise everything?
75
Like this theme styles here, why don't I memwise that based on this dark boolean variable?
76
And the reason you don't want to memwise everything is because it does give you some performance overheads and some memory overhead.
77
For example, this use memo function must be called every single render of your component.
78
So you're calling an additional function.
79
And also it's saving the value, this previous value in some memory variable.
80
So you're forcing your memory to get larger every time you use memo
81
because you have to store an additional variable in memory to store that previous value.
82
Now this isn't a huge deal.
83
But especially where you don't need it,
84
it's going to cause additional memory usage and additional performance problems when just not using it would have been better.
85
So I highly recommend you only use memo in this case when you actually need the performance benefits, when the function you're calling is incredibly slow.
86
Then use memo is great.
87
But there's also a second use case for use memo, and that is something called referential equality.
88
And if you aren't really familiar with value versus reference in JavaScript.
89
I have a video on that topic I'll link in the cards in description.
90
But essentially what that is saying is when you try to compare two different variables in JavaScript,
91
it's going to compare their reference in the case of objects and arrays.
92
So for example, this theme styles here is a specific object.
93
And if I were to duplicate this theme styles, for example, theme styles two right here,
94
you would think that theme styles and theme style 2 are equal to each other, but in JavaScript they reference different objects.
95
They have the same values in the object, but the actual reference to the object itself is different.
96
So these two values are actually not equal to each other, which is something really important.
97
And as you know, inside of hooks in React, we have this array here of all of our dependencies, and when our dependencies change,
98
it's going to rerun our hook.
99
So let's take a quick example where we have a use effect
100
and we want to run this use effect every single time that our theme styles here changes.
101
So we're just going to have a really simple console .log
102
theme changed and down here inside of our dependencies I'm going to have our theme styles.
103
Let's just get rid of this console down here and of course we need to make sure we import use effect
104
just like that.
105
And now if I just really quickly inspect this page here, pull this over, go to the console,
106
you can see we get our theme changed being printed out.
107
And when we change our theme, you can see it's continually printing out theme changed.
108
But a problem you'll notice if I clear this out is when we change our number, it's also causing this theme change to be run.
109
And the reason for that is that referential equality.
110
What's happening is every time we run our function, we get a new theme styles object being created.
111
And this new theme style object is not the same as the old theme styles, even though they have the exact same values in the object,
112
they reference different places in memory.
113
And that's really important to know.
114
So in order to make sure that we only ever run useEffect when our theme style object actually gets re -updated, we can use memo again.
115
So what we can do is wrap this in a use memo, make sure you pass it a function,
116
and in here we're just going to return that object.
117
We just indent all this properly, and in here for our dependencies we're just going to put
118
that dark variable because that's the only thing that this object depends on.
119
Now if we were to rerun this you can see a change in our theme causes this to update our theme, logs it out, but
120
when we change our number you're going to notice we don't actually get anything printed in our console
121
and only when we click change theme.
122
And the reason for this is that now we are wrapping this object inside of memorization.
123
Essentially, if our dark variable doesn't change, we don't re -update our theme styles.
124
So we get the exact same reference as we had the previous time we rendered our app.
125
So now our use effect is comparing our old theme styles with our new theme styles, but they both reference the exact same object.
126
So these are the two big use cases for use memo.
127
The first and most obvious is when you want to make a slow function,
128
wrap it in this use memo so that it doesn't recompute every single time you render your component,
129
and it only computes when you actually need the value from that function since the input's actually changed.
130
The second use case, which is a little bit trickier, is this idea of referential equality.
131
Whenever you want to make sure the reference of an object
132
or an array is exactly the same as it was the last time you rendered if none of the internal workings changed,
133
you're going to want to use memo here to make sure
134
that you only update the reference of
135
that object whenever the actual contents of the object change instead of updating every single time you render.
136
And that's all there is to the use memo hook.
137
If you enjoyed this video, make sure to check out my full react course linked in the description to get a full overview of everything
138
you need to know about react.
139
Thank you very much for watching and have a good day.
同频道
FOR YOU
推荐视频
本课的词汇与口语要点
这节口语课以视频“Learn useMemo In 10 Minutes”为素材。 视频中反复出现的词有:theme, function, memo, slow, object。 这段视频共有 139 个句子、2166 个单词可供跟读。 讲话部分时长为 10:41。 说话人语速较快,每分钟约 203 个词,会出现较多连读和弱读。 87% 的单词属于英语最常用的 3,000 词,词汇容易理解。
视频中的重点词汇
视频中 15 个值得学习的单词,附发音和释义:
| 单词 | 发音 | 释义 |
|---|---|---|
| memo 名词 | /ˈmɛməʊ̯/ | 備忘錄 /备忘录, 便箋 /便笺 |
| hook 名词 | /hʊk/ | 鉤 /钩, 鉤子 /钩子 |
| component 名词 | /kəmˈpoʊ.nənt/ | 元件, 部件 |
| input 名词 | /ˈɪnpʊt/ | 輸入 /输入 |
| essentially 副词 | /ɪˈsɛnʃəli/ | 本質上 /本质上, 基本上 |
| compare 动词 | /kəmˈpɛɚ/ | 比, 比較 /比较 |
| output 名词 | /ˈaʊtpʊt/ | 輸出 /输出 |
| delay 名词 | /dɪˈleɪ̯/ | 遲延 /迟延, 延遲 /延迟 |
| equality 名词 | /ɪˈkwɑ.lɪ.ti/ | 平等, 均等 |
| wrap 动词 | /ɹæp/ | 包裝 /包装, 包 |
| cache 名词 | /kæʃ/ | 緩存 /缓存, 快取 |
| loop 名词 | /luːp/ | 圈, 環 /环 |
| import 名词 | /ˈɪm.pɔɹt/ | 進口貨 /进口货, 舶來品 /舶来品 |
| overhead 名词 | /ˈəʊvəˌhɛd/ | 間接費用 /间接费用 |
| topic 名词 | /ˈtɑpɪk/ | 主題 /主题, 話題 /话题 |
视频中出现的短语动词
| 单词 | 释义 |
|---|---|
| check out 动词 | 退房 |
| go through 动词 | 通過 /通过 |
视频中的语法
说话人最常用的结构,并附上视频中的原话:
| 结构 | 视频中的用法 |
|---|---|
| 被动语态 be + 过去分词 — 强调发生了什么,而不是谁做的 | is toggled · is called · must be called |
| 定语从句 who / which + 从句 — 补充说明人或事物 | app, which has · light, which is · hook which is |
需要注意的发音
说话人用了 48 次缩略和弱读形式,例如 we're, you're, don't。请按听到的简短形式来说。
- “sh” 和 “zh” 音: essentially /ɪˈsɛnʃəli/, cache /kæʃ/, referential /ɹɛfəˈɹɛnʃəl/, memoization /ˌmɛmoʊaɪˈzeɪʃən/, memorization /ˈmem(ə)ɹaɪˌzeɪʃn̩/
- 长单词——注意重音位置: dependency /dɪˈpɛndənsi/, essentially /ɪˈsɛnʃəli/, equality /ɪˈkwɑ.lɪ.ti/, incredibly /ɪŋˈkɹɛdɪbli/, continually /kənˈtɪnjuəli/
中文母语者容易读错的音:
- /v/ — 不要读成 /w/,上齿轻触下唇: variable [ˈvæɹ.i.ə.bl̩], overhead /ˈəʊvəˌhɛd/, server /ˈsɝvɚ/, solve /sɒlv/, versus /ˈvɝsəs/
- 词尾辅音 — 要读清楚,后面不要加元音: hook /hʊk/, component /kəmˈpoʊ.nənt/, react /ɹiːˈækt/, input /ˈɪnpʊt/, output /ˈaʊtpʊt/
- /r/ 和 /l/ 的区别: variable [ˈvæɹ.i.ə.bl̩], recall /ɹɪˈkɔl/, incredibly /ɪŋˈkɹɛdɪbli/, scroll /skɹoʊl/, referential /ɹɛfəˈɹɛnʃəl/
如何用这段视频练习
- 先完整听一遍视频,不要开口,记下不认识的单词。
- 先用 0.75 倍速逐句跟读,熟练之后再回到正常速度。
- 录下自己的声音并与原声对比,特别注意 memo, hook, component 这类单词。
什么是跟读法?
跟读法 (Shadowing) 是一种有科学依据的语言学习技巧,最初开发用于专业口译员的培训,并由多语言者Alexander Arguelles博士普及。这个方法简单而强大:您在听英语母语原声的同时立即大声重复——就像是一个延迟1-2秒紧跟说话者的影子。与被动听力或语法练习不同,跟读法强迫您的大脑和口腔肌肉同时处理并模仿真实的讲话模式。研究表明它能显着提高发音准确性,语调,节奏,连读,听力理解和口语流利度——使其成为雅思口语备考和真实英语交流最有效的方法之一。













