All language subtitles for 004 useEffect & Dependencies_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:02,300 --> 00:00:04,260 So we saw useEffect in action, 2 00:00:04,260 --> 00:00:07,780 here in the scenario with Node dependencies. 3 00:00:07,780 --> 00:00:10,640 Now, often you need dependencies 4 00:00:10,640 --> 00:00:14,440 because you don't just want to run this effect function once 5 00:00:14,440 --> 00:00:16,149 when the app starts up 6 00:00:16,149 --> 00:00:19,830 but indeed, after every component reevaluation 7 00:00:19,830 --> 00:00:21,940 if a certain dependency changed. 8 00:00:21,940 --> 00:00:25,083 And an example can be found in the login component. 9 00:00:25,950 --> 00:00:30,230 That's the component where I actually render this form here. 10 00:00:30,230 --> 00:00:32,729 This form here, that's being rendered here. 11 00:00:32,729 --> 00:00:36,110 And as you see, there is actually some validation built in. 12 00:00:36,110 --> 00:00:37,830 If I click in here and I click out, 13 00:00:37,830 --> 00:00:40,173 you for example see this is marked as red. 14 00:00:41,050 --> 00:00:43,700 And also the button is only enabled 15 00:00:43,700 --> 00:00:48,700 if I have entered something valid in both inputs here right? 16 00:00:49,950 --> 00:00:52,270 Otherwise the button is disabled. 17 00:00:52,270 --> 00:00:55,240 So we get some validation and currently, 18 00:00:55,240 --> 00:00:57,170 the validation works such, 19 00:00:57,170 --> 00:01:00,500 that for every keystroke on the email field, 20 00:01:00,500 --> 00:01:02,420 this emailChangeHandler runs 21 00:01:02,420 --> 00:01:04,290 and it sets the form as valid 22 00:01:04,290 --> 00:01:06,690 if the email contains the @ symbol 23 00:01:06,690 --> 00:01:09,280 and the entered password was correct 24 00:01:09,280 --> 00:01:11,790 and we get the same for the password change handler. 25 00:01:11,790 --> 00:01:14,210 And I also got the validate email handlers 26 00:01:14,210 --> 00:01:16,950 where I set email and password as valid 27 00:01:16,950 --> 00:01:21,180 whenever the inputs blur, so whenever they lose focus, 28 00:01:21,180 --> 00:01:23,640 so that they also are marked as invalid 29 00:01:23,640 --> 00:01:26,060 if I just click in there, enter nothing 30 00:01:26,060 --> 00:01:27,423 and click somewhere else. 31 00:01:29,520 --> 00:01:32,940 Now, where could we use useEffect here? 32 00:01:32,940 --> 00:01:37,940 Well, for example, to restructure our validation logic here 33 00:01:39,240 --> 00:01:43,210 instead of having essentially the same logic 34 00:01:43,210 --> 00:01:46,200 just well, swapped basically, 35 00:01:46,200 --> 00:01:48,750 in the email and the password change handler, 36 00:01:48,750 --> 00:01:51,920 we could use useEffect to have one place 37 00:01:51,920 --> 00:01:56,920 where we mark the form as valid or invalid with one logic, 38 00:01:57,850 --> 00:01:59,220 which should trigger, 39 00:01:59,220 --> 00:02:03,113 whenever either the email or the password changed. 40 00:02:03,990 --> 00:02:07,973 And that's where we will need extra dependency. 41 00:02:09,100 --> 00:02:14,060 So therefore here, we can again import useEffect 42 00:02:14,060 --> 00:02:16,600 and then here after our state definitions, 43 00:02:16,600 --> 00:02:20,620 we can call useEffect and pass this first function to it 44 00:02:20,620 --> 00:02:22,853 and have our array of dependencies. 45 00:02:23,980 --> 00:02:27,500 Now in this first function, I will call setFormIsValid 46 00:02:28,560 --> 00:02:31,190 but of course I will not use the event target value 47 00:02:31,190 --> 00:02:36,190 but I will check if the enteredEmail includes the @ symbol 48 00:02:36,230 --> 00:02:40,123 and the enterPassword is longer than six characters. 49 00:02:42,350 --> 00:02:45,660 Now, like this, this would only run once 50 00:02:45,660 --> 00:02:48,830 and that's when this component first is rendered. 51 00:02:48,830 --> 00:02:51,550 So therefore if I remove that logic from the password 52 00:02:51,550 --> 00:02:53,690 and the email change handler, 53 00:02:53,690 --> 00:02:57,700 you will see that I can reload this and I can type in there. 54 00:02:57,700 --> 00:03:01,513 And my form, well, this button never gets enabled. 55 00:03:02,350 --> 00:03:04,290 And the reason for that is simply that, 56 00:03:04,290 --> 00:03:06,830 this effect only ran once as I said, 57 00:03:06,830 --> 00:03:10,530 when the component rendered for the first time. 58 00:03:10,530 --> 00:03:14,120 Now that was the right behavior in the app component 59 00:03:14,120 --> 00:03:15,790 with our login state. 60 00:03:15,790 --> 00:03:18,890 It's definitely the wrong behavior here, though. 61 00:03:18,890 --> 00:03:20,920 We want to reevaluate this 62 00:03:20,920 --> 00:03:24,710 and rerun this form validation state setting function 63 00:03:24,710 --> 00:03:27,843 for every keystroke in email and password change handler. 64 00:03:29,200 --> 00:03:31,980 Now, we could remove the dependencies array. 65 00:03:31,980 --> 00:03:35,950 Now don't save this because this would crash your page. 66 00:03:35,950 --> 00:03:38,430 This would make sure that this runs 67 00:03:38,430 --> 00:03:41,561 whenever this component was reevaluated. 68 00:03:41,561 --> 00:03:43,440 The data for effectively in this case 69 00:03:43,440 --> 00:03:45,910 it's the same as if we would have moved 70 00:03:45,910 --> 00:03:49,950 this code directly here into the component function itself, 71 00:03:49,950 --> 00:03:53,180 because now without any dependencies, 72 00:03:53,180 --> 00:03:56,090 this code reruns whenever the component was rerendered 73 00:03:56,090 --> 00:03:58,040 and since we set state in here, 74 00:03:58,040 --> 00:04:01,050 this would trigger a rerender cycle itself 75 00:04:01,050 --> 00:04:03,320 hence we would have an infinite loop. 76 00:04:03,320 --> 00:04:05,520 So, saying nothing about the dependencies 77 00:04:05,520 --> 00:04:08,200 is not what we want, but no dependencies 78 00:04:08,200 --> 00:04:12,000 is all not what we want, then it would only run once. 79 00:04:12,000 --> 00:04:14,910 Well, indeed there is a simple rule, 80 00:04:14,910 --> 00:04:19,760 you add as dependencies, what you're using 81 00:04:19,760 --> 00:04:22,480 in your side effect function. 82 00:04:22,480 --> 00:04:27,480 So in this case, we are using, the setFormisValid function. 83 00:04:27,840 --> 00:04:30,260 We are using enteredEmail 84 00:04:30,260 --> 00:04:33,650 and we are using enteredPassword. 85 00:04:33,650 --> 00:04:35,810 Includes and trim are only called 86 00:04:35,810 --> 00:04:39,780 on those enteredEmail and enterPassword fields then. 87 00:04:39,780 --> 00:04:44,260 So we have setFormIsValid, enteredEmail and enteredPassword. 88 00:04:44,260 --> 00:04:47,890 These are our three things we're using here. 89 00:04:47,890 --> 00:04:52,030 So therefore, here, between these brackets, 90 00:04:52,030 --> 00:04:55,007 you would add, setFormIsValid, enteredEmail 91 00:04:57,311 --> 00:05:00,400 and enteredPassword, just like this. 92 00:05:00,400 --> 00:05:02,588 Important for setFormIsValid, 93 00:05:02,588 --> 00:05:04,940 you of course don't execute it, 94 00:05:04,940 --> 00:05:07,890 that would add the result of this function execution 95 00:05:07,890 --> 00:05:10,810 as a dependency, but instead you added like this 96 00:05:10,810 --> 00:05:12,730 to add a pointer at this function 97 00:05:12,730 --> 00:05:16,140 so that you essentially add the function itself 98 00:05:16,140 --> 00:05:17,233 as a dependency. 99 00:05:18,150 --> 00:05:19,600 And this tells React, 100 00:05:19,600 --> 00:05:24,330 that after every login component function execution, 101 00:05:24,330 --> 00:05:26,410 it will rerun this useEffect function 102 00:05:26,410 --> 00:05:28,603 but only if either setFormIsValid, 103 00:05:30,250 --> 00:05:34,120 or enteredEmail or enteredPassword, 104 00:05:34,120 --> 00:05:37,710 changed in the last component rerender cycle. 105 00:05:37,710 --> 00:05:39,930 If neither of the three changed, 106 00:05:39,930 --> 00:05:43,453 this effect function will not rerun. 107 00:05:44,320 --> 00:05:48,130 Now actually, you can also omit setFormIsValid 108 00:05:48,130 --> 00:05:51,540 because those state updating functions by default 109 00:05:51,540 --> 00:05:55,320 are insured by React to never change. 110 00:05:55,320 --> 00:05:57,610 So these functions will always be the same 111 00:05:57,610 --> 00:06:02,270 across rerender cycles, so you can omit them. 112 00:06:02,270 --> 00:06:04,210 enteredEmail and enteredPassword 113 00:06:04,210 --> 00:06:05,810 could have changed of course. 114 00:06:05,810 --> 00:06:07,820 To be precise, with every keystroke, 115 00:06:07,820 --> 00:06:09,093 one of them will change. 116 00:06:10,320 --> 00:06:14,623 So if we now save this and we try this again, 117 00:06:15,720 --> 00:06:18,467 you see the login button still enables and disables, 118 00:06:18,467 --> 00:06:20,040 and the reason for that is, 119 00:06:20,040 --> 00:06:21,950 that now useEffect function reruns 120 00:06:21,950 --> 00:06:25,620 whenever enteredEmail or enteredPassword changed. 121 00:06:25,620 --> 00:06:29,280 So that's another scenario where useEffect shines. 122 00:06:29,280 --> 00:06:32,650 It helps us make sure that we have one code, 123 00:06:32,650 --> 00:06:37,550 in one place, instead of as before in multiple places 124 00:06:37,550 --> 00:06:41,890 which reruns, whenever one of the dependencies changed. 125 00:06:41,890 --> 00:06:45,290 And that's also something where you use useEffect. 126 00:06:45,290 --> 00:06:48,450 So it's not just for when a component 127 00:06:48,450 --> 00:06:50,670 was created for the first time, 128 00:06:50,670 --> 00:06:54,600 but it's equally common to use it to rerun logic 129 00:06:54,600 --> 00:06:57,470 when certain data, typically some state 130 00:06:57,470 --> 00:06:59,353 or some props changed. 131 00:07:00,360 --> 00:07:03,750 Now, one aspect that could be confusing here, 132 00:07:03,750 --> 00:07:08,750 is that of course now, inside of this useEffect example, 133 00:07:08,870 --> 00:07:11,760 we're not working with local storage. 134 00:07:11,760 --> 00:07:14,610 We're not sending an HTTP request. 135 00:07:14,610 --> 00:07:18,340 We're not setting a timer or anything like that. 136 00:07:18,340 --> 00:07:21,930 Instead we are updating the React state 137 00:07:21,930 --> 00:07:24,860 and that could be confusing if you'll recall 138 00:07:24,860 --> 00:07:27,210 that slide from earlier, 139 00:07:27,210 --> 00:07:30,930 where I had a slightly different separation. 140 00:07:30,930 --> 00:07:34,170 So to clear that confusion, you must not forget, 141 00:07:34,170 --> 00:07:38,380 that it's called useEffect, and it has one main job, 142 00:07:38,380 --> 00:07:41,600 it's there to handle side effects 143 00:07:42,490 --> 00:07:47,490 and often side effects are http requests and so on, 144 00:07:48,360 --> 00:07:50,830 but it's also a side effect, 145 00:07:50,830 --> 00:07:55,760 if we listen to every keystroke and save that entered data 146 00:07:55,760 --> 00:08:00,350 as we're doing it in the emailChangeHandler for example 147 00:08:00,350 --> 00:08:03,880 and we then wanna trigger another action 148 00:08:03,880 --> 00:08:05,770 in response to that. 149 00:08:05,770 --> 00:08:09,450 So checking and updating that form validity, 150 00:08:09,450 --> 00:08:12,790 in response to a keystroke in the email 151 00:08:12,790 --> 00:08:14,390 or a password field, 152 00:08:14,390 --> 00:08:17,960 that is also something you could call a side effect. 153 00:08:17,960 --> 00:08:22,100 It's a side effect of the user entering data. 154 00:08:22,100 --> 00:08:24,650 So that's why this should not confuse you. 155 00:08:24,650 --> 00:08:29,390 useEffect in general, is a super important hook 156 00:08:29,390 --> 00:08:31,710 that helps you deal with code 157 00:08:31,710 --> 00:08:36,010 that should be executed in response to something. 158 00:08:36,010 --> 00:08:39,299 And something could be the component being loaded. 159 00:08:39,299 --> 00:08:42,350 It could be the email address being updated. 160 00:08:42,350 --> 00:08:46,010 It could be anything, whenever you have an action 161 00:08:46,010 --> 00:08:50,510 that should be executed in response to some other action, 162 00:08:50,510 --> 00:08:52,490 that is a side effect 163 00:08:52,490 --> 00:08:56,490 and that is where a useEffect is able to help you. 164 00:08:56,490 --> 00:08:59,363 That's just a little addition I wanna make here. 13296

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.