All language subtitles for 002 What are _Side Effects_ & Introducing useEffect_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,090 --> 00:00:03,840 So what is an effect? 2 00:00:03,840 --> 00:00:05,750 Also called a side effect. 3 00:00:05,750 --> 00:00:08,310 And I will use these terms interchangeably. 4 00:00:08,310 --> 00:00:09,740 So when I say effect 5 00:00:09,740 --> 00:00:11,270 I mean side effect 6 00:00:11,270 --> 00:00:12,740 and the other way around 7 00:00:12,740 --> 00:00:14,560 But what is that? 8 00:00:14,560 --> 00:00:16,140 Well, let's not forget 9 00:00:16,140 --> 00:00:19,230 that our components in the React app 10 00:00:19,230 --> 00:00:21,570 and our React app as a whole 11 00:00:21,570 --> 00:00:24,860 and also especially the React library itself 12 00:00:24,860 --> 00:00:26,920 has one main job 13 00:00:26,920 --> 00:00:28,500 to render the UI, 14 00:00:28,500 --> 00:00:30,640 to React to our user input, 15 00:00:30,640 --> 00:00:33,930 to re-render the UI when it's needed. 16 00:00:33,930 --> 00:00:37,330 That is what we focused on this far. 17 00:00:37,330 --> 00:00:39,010 With state and events 18 00:00:39,010 --> 00:00:40,260 and all of that. 19 00:00:40,260 --> 00:00:43,810 The main job was to bring something onto the screen 20 00:00:43,810 --> 00:00:45,420 and to make sure that the user 21 00:00:45,420 --> 00:00:47,910 may interact with that something 22 00:00:47,910 --> 00:00:50,060 and that what's shown on the screen 23 00:00:50,060 --> 00:00:52,570 may change based on certain events. 24 00:00:52,570 --> 00:00:54,560 For example, based on a button click 25 00:00:54,560 --> 00:00:56,283 or some text being entered. 26 00:00:57,560 --> 00:01:00,060 So therefore that's the main job of React 27 00:01:00,060 --> 00:01:02,130 and of our application. 28 00:01:02,130 --> 00:01:05,960 We wanna evaluate and render the JSX code and the Dom. 29 00:01:05,960 --> 00:01:08,510 We wanna manage state and props 30 00:01:08,510 --> 00:01:11,560 to make sure that every component has the data it needs 31 00:01:11,560 --> 00:01:14,660 and that we reflect the user input correctly. 32 00:01:14,660 --> 00:01:17,810 We wanna React to user events as mentioned 33 00:01:17,810 --> 00:01:19,490 and of course React is there 34 00:01:19,490 --> 00:01:22,170 to also re-evaluate our components 35 00:01:22,170 --> 00:01:24,120 and their JSX code 36 00:01:24,120 --> 00:01:26,973 and manipulate the real Dom as needed. 37 00:01:27,970 --> 00:01:30,940 So that's all baked into React with the tools 38 00:01:30,940 --> 00:01:33,410 and features we learned this far 39 00:01:33,410 --> 00:01:35,410 and which we will dive into 40 00:01:35,410 --> 00:01:37,030 throughout this course of course. 41 00:01:37,030 --> 00:01:40,860 So something like to useState Hook, props and so on. 42 00:01:40,860 --> 00:01:42,223 That's the main job. 43 00:01:43,090 --> 00:01:45,860 And if we have a look at a React component, 44 00:01:45,860 --> 00:01:49,810 this is a component from this modules project, 45 00:01:49,810 --> 00:01:51,570 which I'll dive into in a second. 46 00:01:51,570 --> 00:01:54,770 Then we learned that a component is just a function 47 00:01:54,770 --> 00:01:57,320 therefore it's executed top to bottom 48 00:01:57,320 --> 00:01:59,470 and everything in that function 49 00:01:59,470 --> 00:02:03,060 in the end deals with bringing something onto the screen 50 00:02:03,060 --> 00:02:07,970 and/or with reacting to user input to clicks and so on. 51 00:02:07,970 --> 00:02:10,090 So that's what this function is all about. 52 00:02:10,090 --> 00:02:11,920 We may useState in there, 53 00:02:11,920 --> 00:02:14,080 we may have other functions in there, 54 00:02:14,080 --> 00:02:16,150 which for example are triggered upon 55 00:02:16,150 --> 00:02:17,837 a button press and so on. 56 00:02:17,837 --> 00:02:20,540 But everything we have in this component function 57 00:02:20,540 --> 00:02:24,063 is concerned with bringing something onto the screen. 58 00:02:25,770 --> 00:02:28,580 Now, therefore side effects 59 00:02:28,580 --> 00:02:30,410 are everything else 60 00:02:30,410 --> 00:02:34,500 that might be happening in your application. 61 00:02:34,500 --> 00:02:38,960 And a good example here is sending a Http request 62 00:02:38,960 --> 00:02:42,170 or storing something in browser storage. 63 00:02:42,170 --> 00:02:44,360 In local storage for example. 64 00:02:44,360 --> 00:02:47,700 But you could also think about timers or intervals 65 00:02:47,700 --> 00:02:49,263 which you set in your code. 66 00:02:50,190 --> 00:02:51,710 These are all tasks 67 00:02:51,710 --> 00:02:54,200 which you of course often have to consider 68 00:02:54,200 --> 00:02:55,610 in your application. 69 00:02:55,610 --> 00:02:58,160 For example, a lot of web applications 70 00:02:58,160 --> 00:03:00,450 needs to send Http requests 71 00:03:00,450 --> 00:03:02,060 to backend servers 72 00:03:02,060 --> 00:03:05,620 but these tasks are all not related 73 00:03:05,620 --> 00:03:07,440 to bringing something onto the screen. 74 00:03:07,440 --> 00:03:09,900 At least not directly. 75 00:03:09,900 --> 00:03:13,260 Of course you might be sending a Http request 76 00:03:13,260 --> 00:03:15,430 to then draw something onto the screen 77 00:03:15,430 --> 00:03:17,060 once you got the response, 78 00:03:17,060 --> 00:03:19,540 but sending the request itself 79 00:03:19,540 --> 00:03:22,690 and handling potential errors and so on 80 00:03:22,690 --> 00:03:25,060 that's not something you need React for, 81 00:03:25,060 --> 00:03:27,940 that's not something React cares about, 82 00:03:27,940 --> 00:03:29,770 that's not what React is. 83 00:03:29,770 --> 00:03:33,000 Therefore it's not something that is related 84 00:03:33,000 --> 00:03:35,680 to drawing something onto the screen directly 85 00:03:35,680 --> 00:03:37,740 or anything like that. 86 00:03:37,740 --> 00:03:39,180 So these are therefore tasks 87 00:03:39,180 --> 00:03:40,930 which must happen outside 88 00:03:40,930 --> 00:03:43,820 of the normal component evaluation. 89 00:03:43,820 --> 00:03:47,003 So outside of your normal component function. 90 00:03:48,200 --> 00:03:51,320 Because you must not forget that this function, 91 00:03:51,320 --> 00:03:53,340 this app function for example 92 00:03:53,340 --> 00:03:57,200 is re-executed by React automatically 93 00:03:57,200 --> 00:04:00,170 whenever for example this state 94 00:04:00,170 --> 00:04:02,810 in this component function changes. 95 00:04:02,810 --> 00:04:06,090 Whenever this is logged in state would change here. 96 00:04:06,090 --> 00:04:08,040 This entire function 97 00:04:08,040 --> 00:04:09,240 re-runs 98 00:04:09,240 --> 00:04:11,810 and React then basically checks 99 00:04:11,810 --> 00:04:14,990 what the new result of this function execution. 100 00:04:14,990 --> 00:04:17,670 So the new JSX code looks like 101 00:04:17,670 --> 00:04:20,420 and if compared to the previous result 102 00:04:20,420 --> 00:04:24,990 it should go to the real Dom and make some changes there. 103 00:04:24,990 --> 00:04:28,110 And we'll dive deeper into how exactly React works 104 00:04:28,110 --> 00:04:30,150 under the hood in a separate section. 105 00:04:30,150 --> 00:04:32,180 But this is what you learn this far 106 00:04:32,180 --> 00:04:33,943 and this is what React does. 107 00:04:34,840 --> 00:04:36,590 Now since this is what's happening 108 00:04:36,590 --> 00:04:39,380 if we would send a Http request 109 00:04:39,380 --> 00:04:41,970 for example, directly here. 110 00:04:41,970 --> 00:04:43,740 Then we would send this request 111 00:04:43,740 --> 00:04:45,400 whenever this function re-runs. 112 00:04:45,400 --> 00:04:48,220 So for example, whenever this state changes 113 00:04:48,220 --> 00:04:51,240 and that might sometimes be what you want 114 00:04:51,240 --> 00:04:53,670 but definitely not always. 115 00:04:53,670 --> 00:04:57,740 And if in response to your Http request 116 00:04:57,740 --> 00:05:00,630 you for example eventually change some state, 117 00:05:00,630 --> 00:05:03,350 you would even create an infinite loop. 118 00:05:03,350 --> 00:05:04,700 Because you would send the request 119 00:05:04,700 --> 00:05:06,380 whenever the function re-runs. 120 00:05:06,380 --> 00:05:08,390 And in response to the request 121 00:05:08,390 --> 00:05:11,910 you change some state which triggers this function again. 122 00:05:11,910 --> 00:05:16,560 So therefore such side effects should not go directly 123 00:05:16,560 --> 00:05:18,750 into this component function 124 00:05:18,750 --> 00:05:22,180 because that would most likely create bugs, 125 00:05:22,180 --> 00:05:26,410 infinite loops or simply send too many Http requests 126 00:05:26,410 --> 00:05:28,440 to stick with that example. 127 00:05:28,440 --> 00:05:30,550 Therefore we'll have a better tool 128 00:05:30,550 --> 00:05:32,030 for handling side effects 129 00:05:32,030 --> 00:05:34,360 and that's a special React Hook 130 00:05:34,360 --> 00:05:35,700 which we can use. 131 00:05:35,700 --> 00:05:37,710 The useEffect()Hook. 132 00:05:37,710 --> 00:05:41,320 The useEffect()Hook is simply another built in Hook. 133 00:05:41,320 --> 00:05:42,800 So another function 134 00:05:42,800 --> 00:05:46,420 you can run inside of your component function 135 00:05:46,420 --> 00:05:48,830 that will do something special. 136 00:05:48,830 --> 00:05:50,620 The useEffect()Hook is called 137 00:05:50,620 --> 00:05:53,873 with two arguments with two parameters. 138 00:05:54,730 --> 00:05:58,060 The first argument is a function 139 00:05:58,060 --> 00:06:02,650 that should be executed after every component evaluation 140 00:06:02,650 --> 00:06:05,900 if the specified dependencies changed. 141 00:06:05,900 --> 00:06:08,200 And the specified dependencies 142 00:06:08,200 --> 00:06:10,653 are the second argument that you pass in. 143 00:06:11,500 --> 00:06:14,700 That's an array full of dependencies 144 00:06:14,700 --> 00:06:17,290 and you will see how these dependencies look like 145 00:06:17,290 --> 00:06:18,550 throughout this module. 146 00:06:18,550 --> 00:06:20,860 So an array full of dependencies 147 00:06:20,860 --> 00:06:23,510 and whenever such a dependency changes 148 00:06:23,510 --> 00:06:25,863 that first function will re-run. 149 00:06:27,300 --> 00:06:29,270 And therefore in that first function 150 00:06:29,270 --> 00:06:31,750 you can put any side effect code 151 00:06:31,750 --> 00:06:34,240 and that code will then only execute 152 00:06:34,240 --> 00:06:38,120 when the dependencies specified by you changed 153 00:06:38,120 --> 00:06:40,840 and not when the component re-renders. 154 00:06:40,840 --> 00:06:43,960 So only when your dependencies changed. 155 00:06:43,960 --> 00:06:45,930 Now, let's have a look at that in action 156 00:06:45,930 --> 00:06:48,110 and for that I prepared a little project 157 00:06:48,110 --> 00:06:50,160 which we're also going to have a look at. 11642

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