跟读练习: Learn React Hooks: useEffect - Simply Explained! - 通过YouTube学习英语口语

C2
Welcome to the last useEffect tutorial you're ever going to have to watch.
⏸ 已暂停
91
如果句子过短或过长,请点击 Edit 进行调整。
1
Welcome to the last useEffect tutorial you're ever going to have to watch.
2
I promise you after watching this video, you're going to know everything there is to know about useEffect.
3
You're going to know how to use it, when to use it, how it works, and you're going to be able to go out and teach this to someone else and look like a senior developer. Does this sound good to you?
4
Cool. Let's jump on my computer screen and learn about useEffect.
5
All right, cool. So we're now on my computer screen and I have here a very simple application that I'm going to use to teach you how to use useEffect in React. So first useEffect.
6
What is it? Well, it's a hook from React that is used to perform side effects in our application.
7
Side effects, you can really think about them as the name implies, as things happening as a consequence of something else. You can think about this in terms of medicine, right? You take some sort of medication that has an intended effect, and then as a byproduct of that effect, you get some side effects in code.
8
It's the same thing.
9
Something happens in our application, something changes, something gets triggered. And as a side effect, we have something else that happens. useEffect in code is used for side effects.
10
And to keep it simple, in most applications, side effects are going to be a result of state changing.
11
You have some state in your application, it goes from one value to another, and that causes a side effect to happen that you can control. That is also very important because unlike medication, you can control what the side effects are, and what they react to.
12
That is the beauty of code, you get to have complete control in how you structure your state and your side effects. Cool.
13
So with that being said, let's actually build our first side effects. So this application, as you can see, is really, really simple. It is a simple counter application with a button to increment and a button to decrement.
14
This works as expected. And this is the code for this.
15
In this application, all that we have is state, we have nothing else, no side effects, all that we have is some piece of state or count, and then another updater function that updates the count variable. That is a very simple application. And now we're going to add introduce our first side effect. So how do we think about side effects, right? We want something to happen as a result of count changing. Because as we've said, useEffect is used whenever you want to trigger something, when some piece of state changes. In this case, the only thing that can change is our count. So we're going to react to the count using a side effect using useEffect. So what I'm first going to do is I'm going to show you how the structure of useEffect works. And then from then, we're going to build slowly and add our functionalities to our side effects. So I'm going to import useEffect from React.
16
Like so. And then I'm just going to write the most simple form of useEffect without giving it any code to run. So that looks like this useEffect. And then open a parenthesis, open an arrow function empty for now, because we don't want any code to run, put a comma, and then give it an empty dependency array, which we will talk about in just a moment. With this, we have created our first side effect. This is active, it is doing something. But actually, it's doing nothing. It is running this empty function, which does nothing. But the hook itself is now fully functional. And we have technically our first side effect. So this is the most basic form. This is how you get a side effect without actually doing anything. Before we actually add some functionality to this, I want to outline the different parts of useEffect, because they are very important to understand. And each part has a specific purpose. And you need to understand all of them to get the full picture of useEffect and how it works. So what I'm going to do is I'm going to just describe them to you, add comments in the representative parts. And then we will then add code and actually build our side effect.
17
So the first part of useEffect is the actual code to run, which will be inside of the curly brackets here. And that is the code that we want to run. So what I'm going to do is I'm going to say the code that we want to run, right, pretty simple. The second part is this dependency array right here.
18
Now this is very important, because this is what you use to tell useEffect what it should listen to, what it should react to, what it should listen to, to run this code here. If you don't provide it anything, it will not listen to anything. And then it will not do the behavior that you might want. So I will put a comment, the dependency array.
19
And then there is another part to useEffect that currently is invisible to us. But that doesn't mean it doesn't exist.
20
And it is invisible because it is optional, you don't have to provide this, but it is still part of useEffect.
21
And it is also a very important part to this.
22
And that is the optional return function.
23
So what I'm going to do is I'm going to put a comment here and say optional return function.
24
So with this, I'm going to leave the space here.
25
So with this, we have our useEffect, we have the three parts of the code of the hook. And now we're going to actually start implementing some code to see how this works.
26
So what do we want to do? We said that we wanted to react to count, we wanted to do something when count changes. The simplest thing that we could do here without actually adding some complex functionality to our simple app is to log in the console, the count as it changes, right?
27
That's simple enough. Whenever count changes, we want the side effect to be a log on our console that shows us what the count is. So to do that, we're going to add this console log in the part of the useEffect where it says the code that we want to run.
28
So I'm going to go here, and I'm going to put console.log, and then I'm going to say the count is colon, and then pass the count here.
29
And then if I save this, and then I refresh, and then I open my console, you will see that we have something logged. The count is 0, which is great.
30
This is the first step to getting useEffect to work.
31
Now something very important here is that we haven't given anything to our dependency array.
32
And we haven't added any extra configuration to useEffect.
33
And it's still ran the code, it's still ran this piece of code.
34
The important thing that you need to understand about useEffect is that no matter what you provide in the dependency array or how you provide it, it is guaranteed to run at least once.
35
And that is when the component mounts.
36
So when the component mounts, this runs, it is guaranteed to run at least once with the code that you give it in the body.
37
And you can see it right here, right?
38
We have our application, I reloaded the component mounted, and then we had a console log that the count is zero. That is very important to understand because you need to know that you're guaranteed to have this run at least once on mount.
39
And there is no way around this using just useEffect. Cool. So now let's look at what happens if I press increment, I'm incrementing and the count is + 1, the count is 2, the count is 3 but nothing is being logged on our console, which is wrong because we want our console to log the count every time that it changes.
40
The reason for this is we haven't provided anything to the dependency array.
41
Remember how I mentioned that this dependency array is used to tell useEffect what it should listen to.
42
And what variables it should react, which means what should I listen to to run this code here.
43
If you give it an empty dependency array, it is only going to run that first initial guaranteed time. So to fix this, all that we have to do is add count in this dependency array. And also before we do that, I want you to look that if I hover over here, you'll see that VS code or any editor that you're using will probably have this, it's going to warn us that react hook useEffect has a missing dependency, 'count'.
44
Either include it or remove the dependency array. This is telling us that hey, you're doing some code that is dependent on count, but you haven't provided it in the dependency array.
45
Are you sure that this is what you want to do?
46
And in this case, it is correct, right, because we want to log our count as it is being updated, but we haven't provided count in the dependency array.
47
So the editor is being helpful. And it is telling us that, hey, you should maybe add count to the dependency array. So to fix that, we are going to add count to the dependency array.
48
There you go. Now if I just do a clean refresh so that we have a fresh start, you'll see that again, the first time it is locked, it doesn't matter that I now put count in the dependency array, it is guaranteed to run at least once. And then when I press increment, it is going to increment the count and log the count here, 1, increment again, log the count, 2, increment again, log the count, 3.
49
Our useEffect now works as we intended to we gave it count, we gave it some code to run.
50
And now this useEffect is hooked to the count. And whenever the count changes, either by these buttons or some other part of the code, it doesn't matter, all that it cares about is that when count is different, it will lock the count, it will perform the side effect as we told it to.
51
Cool. So there we have our useEffect, it is fully functional.
52
Now there is this optional return function that I talked about, that we haven't used anything. Well, it's fine that we haven't used it because for one, it is optional. And for two, in this simple example, we have nothing to clean up, right?
53
This is the cleanup function. And I'm going to show you how this works, because it's very important, because in some cases, you do need to have a cleanup function, and you do need to understand the lifecycle of useEffect, how does useEffect run its code, and what is the order in which it runs the code.
54
So what I'm going to do is I'm going to put another console log in the cleanup function, and show you how this works.
55
So I'm going to do return, and then open an arrow function again, do this, and then in the body of the function, I'm going to put console.log.
56
And then I'm going to say I am being cleaned up.
57
And I'm going to save this, save and then refresh.
58
So I now have a return function, which again is optional, I didn't need to put this and our code doesn't depend on it.
59
But we have this console log, which I'm using for demonstration purposes.
60
Look what happens when I click increment.
61
You see, before the count, the new count, before that's being logged, I get I am being cleaned up.
62
If I press it again, I get, I am being cleaned up again.
63
And then the count is to press it again, cleaned up.
64
And then the count is 3.
65
Why does this happen? Well, this is where we have to understand how useEffect works.
66
useEffect will first run the code on mount of the component, whatever it is inside.
67
As we can see here, the count is zero, that is going to be logged guaranteed at least once on mount. And then if you give it a dependency array, whenever something in that dependency array changes, the useEffect hook will destroy itself. And it will run this cleanup function before doing so. And then it's going to be recreated with the new value.
68
That is how useEffect is able to log the new value every time it's being updated, because it is being destroyed, it is then cleaning up after itself.
69
And then it is rerunning with the new value.
70
This is very important because this is how useEffect works.
71
And by extension, this is generally how react works under the hood.
72
Everything depends on renders.
73
And everything is a function of the render.
74
Every render, the state has a new value, all the hooks are reset with that value. And then they perform the code with these updated values.
75
So that's what you're seeing here. Now, in this case, like I said, we didn't need to put an optional function because there is nothing to clean up. But you can think of some other use cases where maybe you have a timeout that you started here that depends on some value.
76
And then when that value changes, you need to clear the timeout. Because if not, you're going to create a new timeout whenever the hook is being recreated.
77
And then you're going to have two timeouts, and you're probably going to have some bugs in your application. So this return function is useful for clearing timeouts. It's useful for unsubscribing to things, if you have an event listener, and you want to listen to something as it changes, you want to unsubscribe to it when it's no longer needed, when this component is destroyed. And you can think of 1000 other cases where you might need to have a return function.
78
But again, in this context, we didn't need to. And one more thing that I want to mention is that if you didn't provide anything in this array, this return function, you might think that it never runs, but it actually still runs. But this time, it runs on unmount of the component.
79
Because when the component unmounts, this effect is no longer needed. And you might still have a timeout, you might still have an event listener or something that needs to be cleaned up.
80
You can really think about this if you've heard about class components in React or even use them.
81
You can really think about this as component did mount, right?
82
you're guaranteed to have something run whenever the component mounts. And then this is component did unmount, sorry, component will unmount, which is the code that is run right before the component unmounts.
83
This is the functional equivalent to that. Cool.
84
So that was useEffect.
85
Now you're able to go out and use this in any application that you work on.
86
And I guarantee you, you are going to have to use this because this is a very fundamental hook in React, this and useState, so you're better sure to learn them and understand them properly.
87
And like I promised you at the beginning of the video, you're now able to go out and teach this to somebody else and look like a senior developer.
88
If you've enjoyed this video, make sure to leave a like and subscribe because it really does help me out a lot.
89
I'm going to be posting a lot more of these types of videos in the future React tutorials in all shapes and sizes. They're coming. Don't worry.
90
My name is Darius Cosden. This is Cosden Solutions.
91
Thank you so much for watching once again, and I will see you all in the next video.

下载应用

AI 为你说出的每个句子打分

TRENDING

热门

为什么要通过这个视频练习口语?

在学习英语的过程中,口语练习是提升沟通能力的关键。通过观看“Learn React Hooks: useEffect - Simply Explained!”这段视频,您不仅可以学习到React中的useEffect的用法,更可以通过模仿视频中的讲解方式,实现在日常交流中的流利表达。在观看视频时,建议您跟读每一个句子,这样可以有效提高您的英语发音和口语表达能力。通过这种方式,您将能够自信地分享技术知识,增强您的职业技能,同时练习英语口语,以符合国际职场的标准。

语法与表达在语境中

  • 被动语态的使用:视频中提到“被用来执行副作用”,此处的被动结构提升了句子的专业性,适合技术领域的表达。
  • 状语从句的引入:如“因为我们想要在count变化时做某事”,这类句子结构能帮助您制作更加丰富的表达,使您的对话更具逻辑性。
  • 不定式的运用:在讲解中,提到“为了让副作用生效”,不定式在句子中引导目的,帮助加强信息传达的明确性。

这些语法结构不仅能帮助您在技术交流中更加清晰,同时也能丰富您的英语口语练习内容。当您模仿这些表达时,可以参考如何将其应用于自己的职业领域或日常对话中。

常见发音陷阱

在视频中,有几个词语可能会对学习者造成挑战:

  • “useEffect”: 该词的音节很短,学习者常常在发音时吃力。练习时请多注意将“use”和“Effect”的发音分开,尤其是在快速说出时。
  • “side effects”: 此词组在连读部分可能会使不熟悉的人发音模糊。请反复练习这两个词的拼读,以提高英语发音。
  • “application”: 这个词的重音位置对母语者和非母语者的发音都可能影响沟通。确定重音并多加练习可以帮助避免误解。

在练习中,多运用“提高英语发音”等关键词,通过英语影子跟读等方式加强学习。利用这种方法,您可以更好地掌握发音技巧,在英语口语练习的过程中提升自信。

什么是跟读法?

跟读法 (Shadowing) 是一种有科学依据的语言学习技巧,最初开发用于专业口译员的培训,并由多语言者Alexander Arguelles博士普及。这个方法简单而强大:您在听英语母语原声的同时立即大声重复——就像是一个延迟1-2秒紧跟说话者的影子。与被动听力或语法练习不同,跟读法强迫您的大脑和口腔肌肉同时处理并模仿真实的讲话模式。研究表明它能显着提高发音准确性,语调,节奏,连读,听力理解和口语流利度——使其成为雅思口语备考和真实英语交流最有效的方法之一。

请我们喝杯咖啡