All language subtitles for 029 Using the useEffect() Hook_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,230 --> 00:00:05,410 So how can we work around that problem? 2 00:00:05,410 --> 00:00:09,690 This is such a common problem and such a common scenario 3 00:00:09,690 --> 00:00:12,400 that React has a solution for it. 4 00:00:12,400 --> 00:00:15,570 There is another hook which you can import from React 5 00:00:15,570 --> 00:00:17,653 and that's the useEffect hook. 6 00:00:18,780 --> 00:00:23,170 UseEffect is a hook that allows you to run some code 7 00:00:23,170 --> 00:00:25,560 under certain conditions. 8 00:00:25,560 --> 00:00:28,930 At the moment this code for sending the request 9 00:00:28,930 --> 00:00:32,680 runs always when the component function runs. 10 00:00:32,680 --> 00:00:36,730 With useEffect we'll be able to restrict this 11 00:00:36,730 --> 00:00:40,810 and define conditions when this code should run. 12 00:00:40,810 --> 00:00:44,110 So that it does not always run. 13 00:00:44,110 --> 00:00:46,500 For this we call useEffect 14 00:00:46,500 --> 00:00:48,960 we execute this useEffect function 15 00:00:48,960 --> 00:00:52,020 directly in our component function 16 00:00:52,020 --> 00:00:56,470 and then useEffect once two arguments. 17 00:00:56,470 --> 00:00:59,270 The first argument is a function. 18 00:00:59,270 --> 00:01:03,410 And here I will pass in an anonymous arrow function. 19 00:01:03,410 --> 00:01:06,440 The second argument is an array 20 00:01:06,440 --> 00:01:09,600 and array of dependencies. 21 00:01:09,600 --> 00:01:11,853 And I'll come back to this array in a second. 22 00:01:12,750 --> 00:01:16,610 Now, inside of this first argument inside of this function 23 00:01:16,610 --> 00:01:19,830 we can execute our batch code here. 24 00:01:19,830 --> 00:01:24,293 So cut this and add this into this effect function. 25 00:01:25,200 --> 00:01:28,190 Now this effect function is first argument here 26 00:01:28,190 --> 00:01:30,143 which we passed to useEffect 27 00:01:30,143 --> 00:01:34,340 will be executed by React on our behalf. 28 00:01:34,340 --> 00:01:37,070 But only under certain circumstances 29 00:01:37,070 --> 00:01:40,800 not always when this component re-runs. 30 00:01:40,800 --> 00:01:44,040 I'll come back to those circumstances in a second. 31 00:01:44,040 --> 00:01:47,900 Now with that, we make sure that our fetch request here 32 00:01:47,900 --> 00:01:50,950 our fetch function is only executed 33 00:01:50,950 --> 00:01:54,440 when React executes this function. 34 00:01:54,440 --> 00:01:58,260 Now when does React execute this function though? 35 00:01:58,260 --> 00:02:02,970 If we would not specify this second argument this array. 36 00:02:02,970 --> 00:02:04,610 If we would omit this 37 00:02:04,610 --> 00:02:07,480 then this effect function would execute 38 00:02:07,480 --> 00:02:10,930 whenever this component function executes. 39 00:02:10,930 --> 00:02:13,900 So then we don't gain anything. 40 00:02:13,900 --> 00:02:16,940 That's why you need this second argument. 41 00:02:16,940 --> 00:02:18,380 You technically don't need it. 42 00:02:18,380 --> 00:02:20,530 It's not required, but if you omit it 43 00:02:20,530 --> 00:02:24,230 there is no difference compared to just running the code 44 00:02:24,230 --> 00:02:25,530 in the component function. 45 00:02:26,400 --> 00:02:29,280 With this second argument added though 46 00:02:29,280 --> 00:02:30,980 React will check 47 00:02:30,980 --> 00:02:32,850 the values 48 00:02:32,850 --> 00:02:34,700 you add, to this array 49 00:02:34,700 --> 00:02:38,690 and compare them to their equivalents 50 00:02:38,690 --> 00:02:42,950 when this effect function was executed the last time. 51 00:02:42,950 --> 00:02:46,820 Now, if that's an empty array, there are no dependencies. 52 00:02:46,820 --> 00:02:50,660 And then React will only execute this function 53 00:02:50,660 --> 00:02:53,810 when this component function rendered and executed 54 00:02:53,810 --> 00:02:55,680 for the first time. 55 00:02:55,680 --> 00:02:59,850 And for subsequent executions of this component function 56 00:02:59,850 --> 00:03:02,160 this effect function will not run 57 00:03:02,160 --> 00:03:04,460 because we have no dependencies 58 00:03:04,460 --> 00:03:08,780 so the values of the dependencies are always the same 59 00:03:08,780 --> 00:03:11,083 because there are no values. 60 00:03:12,400 --> 00:03:16,530 Now, if you would add a dependency here, like let's say 61 00:03:16,530 --> 00:03:19,800 the Isloading state snapshot 62 00:03:19,800 --> 00:03:21,040 which you should not. 63 00:03:21,040 --> 00:03:24,060 But if we would have this as a dependency here 64 00:03:24,060 --> 00:03:28,433 then this function would execute whenever the value of 65 00:03:28,433 --> 00:03:30,610 Isloading changed. 66 00:03:30,610 --> 00:03:32,660 So if that value does not change 67 00:03:32,660 --> 00:03:35,060 if that always stays the same 68 00:03:35,060 --> 00:03:37,990 then this would also not execute again. 69 00:03:37,990 --> 00:03:40,350 But if Isloading was false 70 00:03:40,350 --> 00:03:44,570 and then you somewhere update it's state to true, 71 00:03:44,570 --> 00:03:47,840 then the value of Isloading would have changed 72 00:03:47,840 --> 00:03:51,023 and then this effect function would execute again. 73 00:03:52,210 --> 00:03:54,750 Now, in reality, you don't need to think 74 00:03:54,750 --> 00:03:58,520 which values should trigger your effect function 75 00:03:58,520 --> 00:04:00,960 instead there is a simple rule. 76 00:04:00,960 --> 00:04:02,900 In your dependencies array 77 00:04:02,900 --> 00:04:06,400 you should add all external values 78 00:04:06,400 --> 00:04:09,550 your effect function relies on. 79 00:04:09,550 --> 00:04:13,260 So in this case, there are no external values. 80 00:04:13,260 --> 00:04:16,260 The fetch function is a built in browser function 81 00:04:16,260 --> 00:04:19,890 it's not one of our component props or state. 82 00:04:19,890 --> 00:04:24,340 And other than that we don't use any state or prop values 83 00:04:24,340 --> 00:04:28,210 in this effect function that would belong to our component. 84 00:04:28,210 --> 00:04:31,680 If we would extract some data from props here though 85 00:04:31,680 --> 00:04:34,050 if we would, for example, get the URL 86 00:04:34,900 --> 00:04:37,160 from props.url let's say 87 00:04:37,160 --> 00:04:39,800 then props would be a dependency 88 00:04:39,800 --> 00:04:42,510 and we should add props here. 89 00:04:42,510 --> 00:04:43,830 But that's not the case here 90 00:04:43,830 --> 00:04:46,250 but that is how it would (indistinct) then. 91 00:04:46,250 --> 00:04:48,800 Set Isoading and set loaded meetups 92 00:04:48,800 --> 00:04:52,400 actually technically would be external dependencies 93 00:04:52,400 --> 00:04:55,820 because these are constants defined outside 94 00:04:55,820 --> 00:04:57,520 of this effect function. 95 00:04:57,520 --> 00:05:01,710 But these state updating functions are an exception. 96 00:05:01,710 --> 00:05:05,960 You can add them here and that would not be incorrect 97 00:05:05,960 --> 00:05:09,380 that these functions are external dependencies. 98 00:05:09,380 --> 00:05:11,720 And whenever these functions change 99 00:05:11,720 --> 00:05:13,730 this effect should run again 100 00:05:13,730 --> 00:05:16,860 because then these functions might do different things 101 00:05:16,860 --> 00:05:19,900 than they did the last time they were executed 102 00:05:19,900 --> 00:05:22,880 but React actually guarantees that these 103 00:05:22,880 --> 00:05:26,420 state updating functions will never change. 104 00:05:26,420 --> 00:05:29,720 They will always do exactly the same thing. 105 00:05:29,720 --> 00:05:34,190 And therefore it's allowed to omit these functions here. 106 00:05:34,190 --> 00:05:38,730 And hence in our scenario here, we use useEffect like this. 107 00:05:38,730 --> 00:05:40,970 With an empty dependencies array 108 00:05:40,970 --> 00:05:44,170 since we have no external dependencies here 109 00:05:44,170 --> 00:05:47,840 and therefore this code will only run once 110 00:05:47,840 --> 00:05:51,653 when this component is rendered for the first time. 111 00:05:52,880 --> 00:05:57,360 Now I dive way deeper into useEffect with more examples 112 00:05:57,360 --> 00:06:02,360 and also more features of this hook in my course. 113 00:06:02,380 --> 00:06:06,700 But what you learn here, is the core foundation of useEffect 114 00:06:06,700 --> 00:06:08,930 which you have to understand 115 00:06:08,930 --> 00:06:12,490 that you can use it for running such side effects 116 00:06:12,490 --> 00:06:13,900 as they are called. 117 00:06:13,900 --> 00:06:16,350 Code which does not directly influence 118 00:06:16,350 --> 00:06:18,240 what's showing up on the screen. 119 00:06:18,240 --> 00:06:20,040 And that for such side effects 120 00:06:20,040 --> 00:06:22,790 useEffect is the proper solution 121 00:06:22,790 --> 00:06:26,283 for controlling when this code should run. 122 00:06:27,790 --> 00:06:30,950 Now with that, we actually should also set 123 00:06:30,950 --> 00:06:32,910 Isloading to true here 124 00:06:32,910 --> 00:06:35,560 at the beginning of this effect function 125 00:06:35,560 --> 00:06:38,300 so that whenever this affect would run again 126 00:06:38,300 --> 00:06:40,290 we set this to true again. 127 00:06:40,290 --> 00:06:43,300 It does run again here, but still that is cleaner. 128 00:06:43,300 --> 00:06:44,810 I would argue. 129 00:06:44,810 --> 00:06:47,530 And then we sent this request and ultimately 130 00:06:47,530 --> 00:06:49,890 we update our loading state again 131 00:06:49,890 --> 00:06:54,600 and we set our loaded meetups to the data we fetched here. 132 00:06:54,600 --> 00:06:57,640 And now with that, its still won't work 133 00:06:57,640 --> 00:07:00,130 because setting the data like this is incorrect 134 00:07:00,130 --> 00:07:01,510 as it turns out 135 00:07:01,510 --> 00:07:04,350 but we don't have an infinite loop. 136 00:07:04,350 --> 00:07:08,060 And I hope I could make it clear why that's the case. 137 00:07:08,060 --> 00:07:11,360 If we now save everything, our app crashes 138 00:07:11,360 --> 00:07:14,170 because as I said, we're setting the wrong data 139 00:07:14,170 --> 00:07:16,630 but we don't have an infinite loop. 140 00:07:16,630 --> 00:07:19,560 So now what's wrong with that data though. 141 00:07:19,560 --> 00:07:21,670 The problem with our data is that 142 00:07:21,670 --> 00:07:24,400 when we fetch meetups from Firebase, 143 00:07:24,400 --> 00:07:27,720 we actually don't get an array. 144 00:07:27,720 --> 00:07:30,840 Instead, we get an object where these 145 00:07:30,840 --> 00:07:34,000 cryptic auto-generated IDs 146 00:07:34,000 --> 00:07:36,340 act as properties. 147 00:07:36,340 --> 00:07:40,090 So when we send the get request to our Firebase API 148 00:07:40,090 --> 00:07:44,180 we get back an object with two properties in this case 149 00:07:44,180 --> 00:07:46,490 and these here are our property names. 150 00:07:46,490 --> 00:07:50,600 And then nest it inside of these properties 151 00:07:50,600 --> 00:07:54,253 we got nested objects with the actual meetup data. 152 00:07:55,240 --> 00:07:57,910 Now here in our component though 153 00:07:57,910 --> 00:07:59,860 we expect an array. 154 00:07:59,860 --> 00:08:04,140 Because in meetup list we map on our data 155 00:08:04,140 --> 00:08:08,960 and that only exists on an array, not on our object. 156 00:08:08,960 --> 00:08:12,540 Hence we need to transform the data here in all meetups 157 00:08:12,540 --> 00:08:15,020 before we set our state. 158 00:08:15,020 --> 00:08:16,750 Once we fetched the data 159 00:08:16,750 --> 00:08:19,460 so in this second, then block here 160 00:08:19,460 --> 00:08:21,177 we wanna transform the data. 161 00:08:21,177 --> 00:08:25,170 And we can do this for creating a helper array here 162 00:08:25,170 --> 00:08:26,880 meetups like this. 163 00:08:26,880 --> 00:08:30,150 And then a four in loop where we go through 164 00:08:30,150 --> 00:08:32,860 all our keys in data. 165 00:08:32,860 --> 00:08:35,090 So all the keys in this data object 166 00:08:35,090 --> 00:08:37,250 we fetch from Firebase. 167 00:08:37,250 --> 00:08:40,049 And these keys will be this 168 00:08:40,049 --> 00:08:42,000 random IDs here. 169 00:08:42,000 --> 00:08:45,000 These unique IDs that will be our keys. 170 00:08:45,000 --> 00:08:48,880 And then we create a new meetup here 171 00:08:49,810 --> 00:08:52,230 for every key through which we loop. 172 00:08:52,230 --> 00:08:55,860 So for every meetup that's stored on Firebase 173 00:08:55,860 --> 00:08:58,660 and we can set the ID equal to key 174 00:08:58,660 --> 00:09:02,420 since that is that auto-generated ID. 175 00:09:02,420 --> 00:09:07,300 And then just distribute data key into this object. 176 00:09:07,300 --> 00:09:10,940 So we access the nested object for the given key. 177 00:09:10,940 --> 00:09:13,460 So we access the nested object here. 178 00:09:13,460 --> 00:09:15,860 And then we just use the spread operator 179 00:09:15,860 --> 00:09:18,510 which is a default JavaScript operator 180 00:09:18,510 --> 00:09:22,510 to copy all the key value pairs of this nested object 181 00:09:22,510 --> 00:09:23,913 into this object. 182 00:09:24,810 --> 00:09:28,550 And that constructs meetup object as we needed. 183 00:09:28,550 --> 00:09:31,390 And then we just need to push that onto 184 00:09:31,390 --> 00:09:33,540 our helper array here. 185 00:09:33,540 --> 00:09:35,980 And then it's this helper meetups array 186 00:09:35,980 --> 00:09:40,263 which we wanna set as our loaded meetups data. 187 00:09:41,610 --> 00:09:45,510 And now if we do that, if we transform the data like this 188 00:09:45,510 --> 00:09:50,220 if we save this and reload, we see loading briefly 189 00:09:50,220 --> 00:09:53,440 but then we see all our meetups. 190 00:09:53,440 --> 00:09:57,220 And that's how we can fetch data in this case from Firebase 191 00:09:57,220 --> 00:10:00,250 but in general with useEffect 192 00:10:00,250 --> 00:10:03,460 fetching data with a loading and a data state 193 00:10:03,460 --> 00:10:05,903 without causing an infinite loop. 15132

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