All language subtitles for 006 Lifecycle Methods In Action_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
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
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian Download
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:02,130 --> 00:00:04,130 Now to convert the user finder 2 00:00:04,130 --> 00:00:06,190 to a class-based component, 3 00:00:06,190 --> 00:00:09,920 I will again import component from React, 4 00:00:09,920 --> 00:00:12,890 and then the final class named user finder 5 00:00:12,890 --> 00:00:15,493 which extends component of course. 6 00:00:17,620 --> 00:00:19,570 Now in here, we need a constructor 7 00:00:19,570 --> 00:00:22,600 because we got some state which should be initialized 8 00:00:22,600 --> 00:00:25,710 by setting this state to an object. 9 00:00:25,710 --> 00:00:29,110 Now here, I actually got two state slices, 10 00:00:29,110 --> 00:00:32,960 the entered search term and then the list of users, 11 00:00:32,960 --> 00:00:37,960 and therefore I'll add my filtered users here 12 00:00:38,160 --> 00:00:41,030 and set that equal to dummy users 13 00:00:41,030 --> 00:00:43,730 and then add my search term 14 00:00:43,730 --> 00:00:46,130 and set this equal to an empty string initially. 15 00:00:47,980 --> 00:00:52,530 Next we got a function here to search change handler, 16 00:00:52,530 --> 00:00:57,130 which I'll implement as a method in the user finder clause. 17 00:00:57,130 --> 00:01:00,410 Because that is then, the function, the methods 18 00:01:00,410 --> 00:01:03,330 that should be executed to update the state. 19 00:01:03,330 --> 00:01:05,450 At least a part of the state, 20 00:01:05,450 --> 00:01:08,350 it should update that search term. 21 00:01:08,350 --> 00:01:10,390 Now we get to event object here 22 00:01:10,390 --> 00:01:14,090 because this is bound to the change event on the input, 23 00:01:14,090 --> 00:01:16,590 and we then use the event object to read 24 00:01:16,590 --> 00:01:18,660 the value entered by the user, 25 00:01:18,660 --> 00:01:21,780 and therefore, we can expect this event object here 26 00:01:21,780 --> 00:01:26,240 as well, to then set our state important here 27 00:01:26,240 --> 00:01:28,770 always to an object, which is then merged 28 00:01:28,770 --> 00:01:32,300 by React and updates the search term part 29 00:01:32,300 --> 00:01:35,483 of that state to event target value. 30 00:01:36,450 --> 00:01:37,950 And only that part. 31 00:01:37,950 --> 00:01:39,480 Again, it will be merged, 32 00:01:39,480 --> 00:01:42,253 so the filtered users, won't be lost. 33 00:01:43,860 --> 00:01:46,690 Of course, we also need to add a render method 34 00:01:46,690 --> 00:01:49,270 where we should return our JS X code. 35 00:01:49,270 --> 00:01:53,960 So grabbed this return statement and add this here as well. 36 00:01:53,960 --> 00:01:58,670 In here, I should point at this search change handler 37 00:01:58,670 --> 00:02:02,480 and also to ensure that this keyword works correctly 38 00:02:02,480 --> 00:02:07,380 in the method, bind this as explained in the last lectures, 39 00:02:07,380 --> 00:02:09,900 and here for the filtered users, 40 00:02:09,900 --> 00:02:12,393 we should refer to this .state.filteredusers. 41 00:02:15,020 --> 00:02:18,070 And with that, we transformed this component. 42 00:02:18,070 --> 00:02:22,390 At least almost one crucial part is missing, 43 00:02:22,390 --> 00:02:26,770 and that would be the use effect function here 44 00:02:26,770 --> 00:02:29,513 which we can't use in a class based component. 45 00:02:30,790 --> 00:02:34,150 Now we could derive our filtered users 46 00:02:34,150 --> 00:02:36,040 and set them in our state update 47 00:02:36,040 --> 00:02:39,120 here in the search change handler. 48 00:02:39,120 --> 00:02:41,190 That would be possible. 49 00:02:41,190 --> 00:02:44,350 But I wanna show you how these lifecycle methods work 50 00:02:44,350 --> 00:02:47,480 and therefore I'll use an alternative approach. 51 00:02:47,480 --> 00:02:52,480 We can add component, did update here, 52 00:02:52,640 --> 00:02:57,420 and this method will be called automatically by React, 53 00:02:57,420 --> 00:03:00,910 whenever this component is re-evaluated 54 00:03:00,910 --> 00:03:03,980 because it's state changed, for example. 55 00:03:03,980 --> 00:03:08,520 So therefore in here we could set our new state 56 00:03:09,720 --> 00:03:11,283 for the filtered users. 57 00:03:12,550 --> 00:03:14,880 So here we can copy this logic 58 00:03:14,880 --> 00:03:18,010 from the user finder functional component, 59 00:03:18,010 --> 00:03:23,010 and call this set state to set the filtered users equal 60 00:03:23,810 --> 00:03:26,693 to dummy users filter and so on. 61 00:03:27,570 --> 00:03:30,230 However, if we want to do it like this, 62 00:03:30,230 --> 00:03:32,600 we would create an infinite loop. 63 00:03:32,600 --> 00:03:35,720 Because component did update is now executed 64 00:03:35,720 --> 00:03:38,620 every time this component changed. 65 00:03:38,620 --> 00:03:41,830 When we set the state, the component will change. 66 00:03:41,830 --> 00:03:43,870 So then this will execute again 67 00:03:43,870 --> 00:03:45,750 and it will change the state again. 68 00:03:45,750 --> 00:03:47,600 And it will execute again. 69 00:03:47,600 --> 00:03:50,413 You get my point, this would be an infinite loop. 70 00:03:51,440 --> 00:03:54,370 Therefore, what we need to do is we need to check 71 00:03:54,370 --> 00:03:57,950 whether the state we're interested in changed. 72 00:03:57,950 --> 00:04:01,260 In this case if the search term changed. 73 00:04:01,260 --> 00:04:04,240 For that component did update receives 74 00:04:04,240 --> 00:04:06,370 two arguments which help us. 75 00:04:06,370 --> 00:04:09,520 The previous props and the previous state. 76 00:04:09,520 --> 00:04:12,420 So the last props and state snapshot 77 00:04:12,420 --> 00:04:15,780 before the current component update. 78 00:04:15,780 --> 00:04:20,779 And with that we can check if prevProps search terms 79 00:04:20,829 --> 00:04:24,843 with search term was different, 80 00:04:27,030 --> 00:04:29,640 then the current search term. 81 00:04:29,640 --> 00:04:33,980 So this state search term, 82 00:04:36,870 --> 00:04:38,510 only if it is different. 83 00:04:38,510 --> 00:04:41,470 So only if the search term changed, 84 00:04:41,470 --> 00:04:44,160 I wanna execute this logic. 85 00:04:44,160 --> 00:04:47,260 If something else changed, like for example 86 00:04:47,260 --> 00:04:50,110 the filtered users, which we are updating here 87 00:04:50,110 --> 00:04:54,910 by calling set state, this method component did update, 88 00:04:54,910 --> 00:04:58,410 we'll run again, but we won't make it into this if block. 89 00:04:58,410 --> 00:05:00,510 So we won't update the state again, 90 00:05:00,510 --> 00:05:03,600 and we won't have an infinite loop, therefore. 91 00:05:03,600 --> 00:05:07,620 Here by the way, it's not all to be this state search term. 92 00:05:07,620 --> 00:05:10,140 And now with that, 93 00:05:10,140 --> 00:05:15,140 we replace use effect with component did update. 94 00:05:15,810 --> 00:05:19,630 This also shows you how nice use effect is. 95 00:05:19,630 --> 00:05:24,310 It's very short and by specifying the dependencies here, 96 00:05:24,310 --> 00:05:27,290 we don't have to add an if check inside of it. 97 00:05:27,290 --> 00:05:29,330 We need to add an if check here 98 00:05:29,330 --> 00:05:33,180 for component did update, to prevent an infinite loop 99 00:05:33,180 --> 00:05:35,870 but we don't need to do this with use effect 100 00:05:35,870 --> 00:05:39,670 because we specified search term as a dependency. 101 00:05:39,670 --> 00:05:43,610 So automatically this affect function will only 102 00:05:43,610 --> 00:05:47,520 be executed by React if that dependency changed. 103 00:05:47,520 --> 00:05:51,640 And our changes and other reasons for this component 104 00:05:51,640 --> 00:05:55,170 to re-render are ignored for this effect, 105 00:05:55,170 --> 00:05:57,800 because of this dependencies array. 106 00:05:57,800 --> 00:05:59,900 Now, one thing I just noticed by the way, 107 00:05:59,900 --> 00:06:03,820 of course I don't wanna compare prevProps, but prevState. 108 00:06:03,820 --> 00:06:07,680 The previous state search terms should be compared. 109 00:06:07,680 --> 00:06:10,900 If you would be updating based on prop changes. 110 00:06:10,900 --> 00:06:12,790 So you got some new prop values 111 00:06:12,790 --> 00:06:15,680 and you wanna add the component there for updates, 112 00:06:15,680 --> 00:06:18,930 then you could compare previous props to current props 113 00:06:18,930 --> 00:06:22,220 but here it's the state change, which matters to us 114 00:06:22,220 --> 00:06:24,670 the state change inside of this component, 115 00:06:24,670 --> 00:06:27,620 and hence we compare our previous state, 116 00:06:27,620 --> 00:06:31,660 a specific slice of that state with the current state 117 00:06:31,660 --> 00:06:33,873 and that specific slice of that state. 118 00:06:35,030 --> 00:06:38,450 Now we should also add super in the constructor by the way 119 00:06:38,450 --> 00:06:42,860 before we forget this, and now we transformed this component 120 00:06:42,860 --> 00:06:45,020 and we can hence remove user finder 121 00:06:45,020 --> 00:06:48,633 in the functional forum or simply comment it out. 122 00:06:49,700 --> 00:06:54,700 And now if I go back this again, works as before, 123 00:06:56,320 --> 00:06:59,150 but now with class-based components 124 00:06:59,150 --> 00:07:01,540 and component did update. 125 00:07:01,540 --> 00:07:05,200 And as I mentioned before in this module, 126 00:07:05,200 --> 00:07:07,580 it's a different mental model. 127 00:07:07,580 --> 00:07:10,510 When you work with these lifecycle methods, 128 00:07:10,510 --> 00:07:15,510 you primarily think about when will this method be called. 129 00:07:15,560 --> 00:07:18,380 Which is the case when the component updates. 130 00:07:18,380 --> 00:07:20,620 And then you might need to add if checks 131 00:07:20,620 --> 00:07:24,490 to restrict your logic in there with use of fact 132 00:07:24,490 --> 00:07:25,670 you don't care about 133 00:07:25,670 --> 00:07:28,580 whether the component is rendered for the first time 134 00:07:28,580 --> 00:07:31,290 or if it updated or whatever, you care about 135 00:07:31,290 --> 00:07:33,700 the fact that search term changed. 136 00:07:33,700 --> 00:07:37,250 And whenever that changes, you run this logic. 137 00:07:37,250 --> 00:07:41,400 Which is a very simple mental model in a positive way. 138 00:07:41,400 --> 00:07:44,230 Nonetheless, you could use components did update 139 00:07:44,230 --> 00:07:47,050 and state, as you saw here. 140 00:07:47,050 --> 00:07:50,510 Now, besides state we also got ever life cycle methods, 141 00:07:50,510 --> 00:07:53,490 most importantly component did mount 142 00:07:53,490 --> 00:07:56,270 and component will unmount. 143 00:07:56,270 --> 00:08:00,300 Now let me show you these life cycle methods as well. 144 00:08:00,300 --> 00:08:03,590 Let's say our dummy users are loaded 145 00:08:03,590 --> 00:08:06,390 from a server from a database. 146 00:08:06,390 --> 00:08:08,860 So we're sending an HTTP request. 147 00:08:08,860 --> 00:08:10,120 We're not doing that here, 148 00:08:10,120 --> 00:08:12,170 but let's imagine we're doing that. 149 00:08:12,170 --> 00:08:15,870 So initially filtered users might be an MTRA. 150 00:08:15,870 --> 00:08:19,380 Then when the component is rendered for the first time, 151 00:08:19,380 --> 00:08:22,370 we wanna send a request to the server. 152 00:08:22,370 --> 00:08:26,510 Hence we can't use component did update in this scenario 153 00:08:26,510 --> 00:08:30,000 because I don't wanna fetch the users over and over again. 154 00:08:30,000 --> 00:08:32,059 You might need to do that in other apps 155 00:08:32,059 --> 00:08:35,400 but not in this imaginary example. 156 00:08:35,400 --> 00:08:37,250 Instead, we wanna fetch the users 157 00:08:37,250 --> 00:08:40,080 when this component is rendered for the first time, 158 00:08:40,080 --> 00:08:42,690 and that is something we would typically do 159 00:08:42,690 --> 00:08:44,533 with component did mount. 160 00:08:45,620 --> 00:08:48,690 You add component did mount like that, 161 00:08:48,690 --> 00:08:52,720 and then in there you could send your age 162 00:08:52,720 --> 00:08:55,560 to http request and handle all of that, 163 00:08:55,560 --> 00:08:58,450 and eventually you would update your state 164 00:08:58,450 --> 00:09:00,910 and set your state in this case 165 00:09:00,910 --> 00:09:03,530 to set filtered users to dummy users, 166 00:09:03,530 --> 00:09:06,610 which might've been fetched from some server. 167 00:09:06,610 --> 00:09:09,520 Again, it's just a dummy example here. 168 00:09:09,520 --> 00:09:10,870 That's what you could do. 169 00:09:10,870 --> 00:09:13,090 We don't need an if check here 170 00:09:13,090 --> 00:09:17,470 because component did mount will only run once 171 00:09:17,470 --> 00:09:19,810 when the component initially was rendered 172 00:09:19,810 --> 00:09:22,000 for the first time. 173 00:09:22,000 --> 00:09:24,150 If then is updated thereafter, 174 00:09:24,150 --> 00:09:27,280 component did mount will not execute, 175 00:09:27,280 --> 00:09:30,293 instead, components did update will execute. 176 00:09:31,340 --> 00:09:33,430 So therefore that's how we could write this, 177 00:09:33,430 --> 00:09:37,660 and if we do, we essentially got the same result as before 178 00:09:37,660 --> 00:09:41,530 but now we're loading this when the component is mounted. 179 00:09:41,530 --> 00:09:45,590 The equivalent to that in the functional component world, 180 00:09:45,590 --> 00:09:47,980 would be to call use effect 181 00:09:47,980 --> 00:09:52,780 with no dependencies or even with dependencies, 182 00:09:52,780 --> 00:09:56,450 it would execute this function when the component is mounted 183 00:09:56,450 --> 00:09:59,900 for the first time, because it's treats the dependency 184 00:09:59,900 --> 00:10:04,590 as to have changed if we had no dependency before 185 00:10:04,590 --> 00:10:07,090 because the component wasn't rendered 186 00:10:07,090 --> 00:10:08,680 and now it is rendered and different, 187 00:10:08,680 --> 00:10:10,720 we do have this dependency. 188 00:10:10,720 --> 00:10:14,230 So to use a fact function will also always execute 189 00:10:14,230 --> 00:10:16,653 when the component is initially mounted. 190 00:10:17,970 --> 00:10:21,700 Now last but not least, we have component will unmount, 191 00:10:21,700 --> 00:10:25,610 and I can show this method in the user component. 192 00:10:25,610 --> 00:10:28,833 Because the list of users is rendered conditionally, 193 00:10:29,910 --> 00:10:32,823 so it is actually removed if I click hide users. 194 00:10:33,690 --> 00:10:35,890 Therefore, in the user component, 195 00:10:35,890 --> 00:10:38,370 which has also a class-based component, 196 00:10:38,370 --> 00:10:41,393 I can add component will unmount, 197 00:10:42,530 --> 00:10:47,150 and in there I'll just console log user will unmount. 198 00:10:49,720 --> 00:10:51,700 And if we now save this and go back 199 00:10:51,700 --> 00:10:53,653 and open the developer tools, 200 00:10:56,670 --> 00:11:00,540 here, like this, 201 00:11:00,540 --> 00:11:02,110 if I click hide users, 202 00:11:02,110 --> 00:11:05,020 you see user will unmount three times. 203 00:11:05,020 --> 00:11:06,927 Why three times? 204 00:11:06,927 --> 00:11:09,980 Because we used the user component three times 205 00:11:09,980 --> 00:11:12,220 we rendered free users in the end 206 00:11:12,220 --> 00:11:16,000 and all free instances offered component were removed 207 00:11:16,000 --> 00:11:17,980 and all these life cycle methods are run 208 00:11:17,980 --> 00:11:20,430 for every component instance, of course 209 00:11:20,430 --> 00:11:21,620 just like user effect 210 00:11:21,620 --> 00:11:26,220 at state work for every component instance stand alone. 211 00:11:26,220 --> 00:11:28,600 And these are the lifecycle methods. 212 00:11:28,600 --> 00:11:32,010 It's a different mental model, but in the end with it 213 00:11:32,010 --> 00:11:34,150 you can of course achieve the same 214 00:11:34,150 --> 00:11:36,820 as you can with these React hooks, 215 00:11:36,820 --> 00:11:38,640 with use effect and so on. 216 00:11:38,640 --> 00:11:40,850 It just as a bit more a code 217 00:11:40,850 --> 00:11:44,593 and it could be the more difficult mental model. 17448

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