Shadowing Practice: Learn useMemo In 10 Minutes - Learn English Speaking with Video

Kies een oefenmodus

Elke modus opent een volledig scherm oefenruimte.

139 zinnen
Als zinnen te kort of te lang zijn, klik op Bewerken om ze aan te passen.
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.

App downloaden

Alles wat je nodig hebt om vloeiend te spreken

AI UitspraakScoor elke zin
IPA OefeningBeheers elk geluid
WoordenschatBouw je woordenschat op
WoordenspelLeren terwijl je speelt

Zelfde Kanaal

FOR YOU

Aanbevolen Video

Woordenschat en spreektips bij deze les

Deze spreekles is gebaseerd op de video “Learn useMemo In 10 Minutes”. Deze woorden komen het vaakst terug: theme, function, memo, slow, object. Deze video bevat 139 zinnen en 2166 woorden om na te spreken. Het gesproken deel duurt 10:41. De spreker praat snel, ongeveer 203 woorden per minuut, dus reken op verbonden en ingeslikte klanken. 87% van de woorden hoort bij de 3.000 meest gebruikte Engelse woorden, dus de woordenschat is goed te volgen.

Belangrijke woorden in deze video

15 woorden uit de video die het leren waard zijn, met uitspraak en betekenis:

WoordUitspraakBetekenis
render werkwoord/ˈɹɛn.dɚ/veroorzaken
hook zelfstandig naamwoord/hʊk/haak
component zelfstandig naamwoord/kəmˈpoʊ.nənt/onderdeel, component
react werkwoord/ɹiːˈækt/reageren
variable bijvoeglijk naamwoord[ˈvæɹ.i.ə.bl̩]veranderlijk, variabel
dependency zelfstandig naamwoord/dɪˈpɛndənsi/afhankelijkheid
essentially bijwoord/ɪˈsɛnʃəli/essentieel
compare werkwoord/kəmˈpɛɚ/vergelijken
output zelfstandig naamwoord/ˈaʊtpʊt/productie
delay zelfstandig naamwoord/dɪˈleɪ̯/vertraging, oponthoud
equality zelfstandig naamwoord/ɪˈkwɑ.lɪ.ti/gelijkheid
wrap werkwoord/ɹæp/inpakken, omhullen
cache zelfstandig naamwoord/kæʃ/cache
loop zelfstandig naamwoord/luːp/lus, strop
import zelfstandig naamwoord/ˈɪm.pɔɹt/import, invoer

Phrasal verbs die je zult horen

WoordBetekenis
check out werkwoordonderzoeken

Grammatica in deze video

De structuren die de spreker het meest gebruikt, met de exacte woorden uit de video:

StructuurIn de video
Lijdende vorm be + voltooid deelwoord — het gaat om wat er gebeurt, niet om wie het doetis toggled · is called · must be called
Betrekkelijke bijzinnen who / which + zin — extra informatie over een persoon of dingapp, which has · light, which is · hook which is

Uitspraak om op te letten

De spreker gebruikt 48 samentrekkingen en verkorte vormen, zoals we're, you're, don't. Spreek ze kort uit, zoals je ze hoort.

  • De klanken “sh” en “zh”: essentially /ɪˈsɛnʃəli/, cache /kæʃ/, referential /ɹɛfəˈɹɛnʃəl/, memoization /ˌmɛmoʊaɪˈzeɪʃən/, memorization /ˈmem(ə)ɹaɪˌzeɪʃn̩/
  • Lange woorden — let op de klemtoon: dependency /dɪˈpɛndənsi/, essentially /ɪˈsɛnʃəli/, equality /ɪˈkwɑ.lɪ.ti/, incredibly /ɪŋˈkɹɛdɪbli/, continually /kənˈtɪnjuəli/

Klanken die Nederlandstaligen lastig vinden:

  • Stemhebbende eindmedeklinker — /b/, /d/, /g/, /z/, /v/ niet verscherpen: overhead /ˈəʊvəˌhɛd/, solve /sɒlv/, log /lɔɡ/, usage /ˈjuːsɪd͡ʒ/, depend /dɪˈpɛnd/
  • /æ/ — opener dan de Nederlandse “e”: react /ɹiːˈækt/, variable [ˈvæɹ.i.ə.bl̩], wrap /ɹæp/, cache /kæʃ/, arrow /ˈæɹəʊ/
  • /g/ — een harde plofklank, geen Nederlandse “g”: log /lɔɡ/, toggle /ˈtɒɡ.əl/

Zo oefen je met deze video

  1. Luister de hele video één keer zonder te spreken en noteer de woorden die je niet kent.
  2. Begin op 0,75× snelheid, spreek zin voor zin na en ga terug naar normale snelheid zodra het makkelijk gaat.
  3. Neem jezelf op en vergelijk met het origineel; let daarbij op woorden als render, hook, component.

Wat is de Shadowing-techniek?

Shadowing is een wetenschappelijk onderbouwde taalleermethode die oorspronkelijk is ontwikkeld voor professionele tolkentraining en gepopulariseerd door polyglot Dr. Alexander Arguelles. De methode is eenvoudig maar krachtig: je luistert naar native Engelse audio en herhaalt het onmiddellijk hardop — als een schaduw die de spreker volgt met slechts 1–2 seconden vertraging. In tegenstelling tot passief luisteren of grammaticadrills, dwingt shadowing je hersenen en mondspieren om echte spraakpatronen tegelijkertijd te verwerken en te reproduceren. Onderzoek toont aan dat het de uitspraaknauwkeurigheid, intonatie, ritme, verbonden spraak, luisterbegrip en spreekvaardigheid aanzienlijk verbetert — waardoor het een van de meest effectieve methoden is voor IELTS Speaking-voorbereiding en echte Engelse communicatie.

Shadowing-techniek: lees de volledige stap-voor-stap-gids →