All language subtitles for 004 Working with State & Events_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 Download
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,100 --> 00:00:05,740 So let's now convert this Users component 2 00:00:05,740 --> 00:00:08,420 to a class-based component. 3 00:00:08,420 --> 00:00:12,660 For this, we again should import something from react. 4 00:00:12,660 --> 00:00:14,850 Let me restructure these imports. 5 00:00:14,850 --> 00:00:19,310 We should import Component from react 6 00:00:20,380 --> 00:00:23,600 because we now build our users component 7 00:00:23,600 --> 00:00:28,320 by creating a class named Users which extends Component. 8 00:00:28,320 --> 00:00:30,630 That's what I explained in the last lecture 9 00:00:30,630 --> 00:00:34,713 and that's how you always build your class-based components. 10 00:00:35,890 --> 00:00:38,730 In there we can now add a render method 11 00:00:38,730 --> 00:00:42,620 and in that render method we return to jsx code 12 00:00:42,620 --> 00:00:44,260 that should be rendered. 13 00:00:44,260 --> 00:00:46,410 So, I'll copy this return statement 14 00:00:46,410 --> 00:00:48,253 and bring it into my render method. 15 00:00:49,670 --> 00:00:54,010 Now we learned that we can access props with these props 16 00:00:54,010 --> 00:00:56,330 but here I'm not working with props 17 00:00:56,330 --> 00:00:58,960 instead, I'm relying on state 18 00:00:58,960 --> 00:01:01,480 and I am pointing at a function 19 00:01:01,480 --> 00:01:04,030 that should be executed when the button is clicked. 20 00:01:05,290 --> 00:01:08,670 Now let's start by adding that function. 21 00:01:08,670 --> 00:01:10,590 In class-based components, 22 00:01:10,590 --> 00:01:15,030 you now don't add a function inside of the render method 23 00:01:15,030 --> 00:01:17,340 though that would technically be possible 24 00:01:17,340 --> 00:01:19,960 but it wouldn't behave correctly. 25 00:01:19,960 --> 00:01:22,300 But instead, since this is a class, 26 00:01:22,300 --> 00:01:24,930 you group together functionality 27 00:01:24,930 --> 00:01:27,510 by grouping it all in that class. 28 00:01:27,510 --> 00:01:30,330 So you would simply add a method 29 00:01:30,330 --> 00:01:33,483 named toggleUsersHandler like this. 30 00:01:34,500 --> 00:01:36,520 There are also alternative ways 31 00:01:36,520 --> 00:01:38,790 of writing a method in classes 32 00:01:38,790 --> 00:01:41,680 but this is the most default and standard way 33 00:01:41,680 --> 00:01:43,213 of defining a method. 34 00:01:45,330 --> 00:01:48,600 Now we have the method, but we don't have the state. 35 00:01:48,600 --> 00:01:52,860 And managing state worked totally different 36 00:01:52,860 --> 00:01:56,230 in the past with class-based components. 37 00:01:56,230 --> 00:02:00,200 With react16.8 hooks were introduced 38 00:02:00,200 --> 00:02:02,500 and we could use to UseState hook 39 00:02:02,500 --> 00:02:05,540 to manage state and functional components. 40 00:02:05,540 --> 00:02:08,453 And it is very simple to manage state then. 41 00:02:10,030 --> 00:02:13,520 Prior to 16.8 class-based components 42 00:02:13,520 --> 00:02:17,100 were the only kind of component that could manage state. 43 00:02:17,100 --> 00:02:20,120 So you always had to write class-based components 44 00:02:20,120 --> 00:02:22,280 when you needed state. 45 00:02:22,280 --> 00:02:26,710 And there you basically needed to do two things, 46 00:02:26,710 --> 00:02:29,520 initialize and define your state, 47 00:02:29,520 --> 00:02:33,650 like we're doing it with UseState when we first call it 48 00:02:33,650 --> 00:02:35,950 and then updated when needed, 49 00:02:35,950 --> 00:02:39,810 like we're doing it here in the toggleUsersHandler. 50 00:02:39,810 --> 00:02:42,870 Now to define state and class-based components, 51 00:02:42,870 --> 00:02:45,180 you would use the constructor. 52 00:02:45,180 --> 00:02:48,200 The constructor function is automatically called 53 00:02:48,200 --> 00:02:51,090 whenever this clause is instantiated 54 00:02:51,090 --> 00:02:54,070 which happens when react encounters, 55 00:02:54,070 --> 00:02:57,023 your class being used as a component. 56 00:02:58,030 --> 00:03:01,600 And then in the constructor, you can do initialization work 57 00:03:01,600 --> 00:03:03,610 like initializing state. 58 00:03:03,610 --> 00:03:08,280 And this would be done by accessing this.state 59 00:03:08,280 --> 00:03:10,770 and setting it equal to an object. 60 00:03:10,770 --> 00:03:13,640 And this is super crucial now. 61 00:03:13,640 --> 00:03:18,640 With class-based components, your state always important, 62 00:03:20,150 --> 00:03:23,240 always is an object. 63 00:03:23,240 --> 00:03:27,410 With functional components, your state can be anything. 64 00:03:27,410 --> 00:03:31,210 It can be just a Boolean, just a string, just a number 65 00:03:31,210 --> 00:03:34,210 but it can also be an object, but it's flexible 66 00:03:34,210 --> 00:03:35,653 it can be anything. 67 00:03:36,630 --> 00:03:39,490 With class-based components, that's not the case. 68 00:03:39,490 --> 00:03:42,120 Here your state always is an object 69 00:03:43,440 --> 00:03:45,970 because with class-based components, 70 00:03:45,970 --> 00:03:50,440 you always group all the state's license and pieces 71 00:03:50,440 --> 00:03:54,510 you might need in a component into one state object. 72 00:03:54,510 --> 00:03:56,520 With functional components 73 00:03:56,520 --> 00:03:59,520 instead if you have multiple state pieces, 74 00:03:59,520 --> 00:04:02,403 you could use multiple UseState calls. 75 00:04:03,450 --> 00:04:06,770 You could also create just one state object 76 00:04:06,770 --> 00:04:08,470 and group it together there 77 00:04:08,470 --> 00:04:12,040 but that is optional with functional components. 78 00:04:12,040 --> 00:04:15,893 And I do talk about that in the state course section. 79 00:04:16,920 --> 00:04:19,870 With class-based components, this is not optional 80 00:04:19,870 --> 00:04:23,050 there you have to group all the state 81 00:04:23,050 --> 00:04:25,710 that makes up a component into one object. 82 00:04:25,710 --> 00:04:30,053 No matter if these state pieces are closely related or not. 83 00:04:31,040 --> 00:04:34,550 So here we would then have our showUsers state 84 00:04:34,550 --> 00:04:37,640 and set this to true initially let's say. 85 00:04:37,640 --> 00:04:39,403 And if we had moreState 86 00:04:40,450 --> 00:04:43,493 then we would simply add this as a extra property. 87 00:04:44,420 --> 00:04:46,690 You can of course also have nestedState, 88 00:04:46,690 --> 00:04:48,500 you could have a nestedObject. 89 00:04:48,500 --> 00:04:53,010 You can have a eraseState that all works 90 00:04:53,010 --> 00:04:56,383 but you have one state object which groups it all together. 91 00:04:58,210 --> 00:05:00,430 Then if you wanna change your state 92 00:05:00,430 --> 00:05:02,780 like in toggleUsersHandler, 93 00:05:02,780 --> 00:05:06,550 you don't do this by accessing this state 94 00:05:06,550 --> 00:05:11,113 and changing it like this, that's not how you do it. 95 00:05:12,580 --> 00:05:17,580 Instead, you call a special method, setState, this.setState, 96 00:05:20,460 --> 00:05:22,580 and this method on this class 97 00:05:22,580 --> 00:05:25,800 is of course also provided by the component class 98 00:05:25,800 --> 00:05:27,363 we are inheriting from. 99 00:05:28,720 --> 00:05:33,720 This setState then also always takes an object. 100 00:05:34,750 --> 00:05:39,750 And this object now contains the new state, you wanna set, 101 00:05:39,820 --> 00:05:44,240 but very important, it won't override the old state 102 00:05:44,240 --> 00:05:47,240 but instead react will behind the scenes 103 00:05:47,240 --> 00:05:50,130 merge the object you're passing here 104 00:05:50,130 --> 00:05:51,733 with your existing state. 105 00:05:52,760 --> 00:05:57,120 So if I had another state here, which I don't need here 106 00:05:57,120 --> 00:06:02,120 if I had that and I then would set showUsers to false, 107 00:06:02,420 --> 00:06:04,770 showUsers would be set to false 108 00:06:04,770 --> 00:06:06,920 but the old state would be capped. 109 00:06:06,920 --> 00:06:08,680 The other state would be capped 110 00:06:08,680 --> 00:06:12,800 because react merges this update with the existing state 111 00:06:12,800 --> 00:06:15,420 it doesn't override the state. 112 00:06:15,420 --> 00:06:18,190 And that's different with useState, 113 00:06:18,190 --> 00:06:21,080 here when you call the state updating function 114 00:06:21,080 --> 00:06:24,070 you always override the old state 115 00:06:24,070 --> 00:06:27,343 with whatever you pass to the updating function, 116 00:06:28,250 --> 00:06:30,210 unless it is a function in which case 117 00:06:30,210 --> 00:06:33,050 we executes that to derive the new state. 118 00:06:33,050 --> 00:06:35,290 But you override the old state, 119 00:06:35,290 --> 00:06:37,910 it's not merged automatically by react 120 00:06:37,910 --> 00:06:39,740 if you would want merging 121 00:06:39,740 --> 00:06:42,210 because you are managing some state object, 122 00:06:42,210 --> 00:06:46,120 you would have to write the logic for this on your own. 123 00:06:46,120 --> 00:06:49,780 With class-based components and setState that's different, 124 00:06:49,780 --> 00:06:52,870 there react mergers this for you. 125 00:06:52,870 --> 00:06:56,020 Now here, of course I don't always wanna set this to false, 126 00:06:56,020 --> 00:06:59,010 I wanna set it to the opposite of what it was before 127 00:06:59,010 --> 00:07:00,890 and therefore here setState 128 00:07:00,890 --> 00:07:03,810 just like the state updating function from useState 129 00:07:04,670 --> 00:07:09,630 also supports such a updating function. 130 00:07:09,630 --> 00:07:13,480 So here we can also pass a function to setState 131 00:07:13,480 --> 00:07:16,720 alternatively to the object with the new state. 132 00:07:16,720 --> 00:07:18,260 And we should do that 133 00:07:18,260 --> 00:07:22,420 if our new state depends on the previous state. 134 00:07:22,420 --> 00:07:24,040 Nonetheless, it's not enough 135 00:07:24,040 --> 00:07:27,090 to just return a new state value like this, 136 00:07:27,090 --> 00:07:30,110 because here I would be returning something invalid 137 00:07:30,110 --> 00:07:31,990 because our state is an object. 138 00:07:31,990 --> 00:07:35,540 Instead here, we have to change this a little bit 139 00:07:35,540 --> 00:07:37,660 and return a new object 140 00:07:37,660 --> 00:07:40,700 just as you have to pass our object to set state 141 00:07:40,700 --> 00:07:42,390 if you use that function form 142 00:07:42,390 --> 00:07:45,090 you have to return an object. 143 00:07:45,090 --> 00:07:50,090 And there, I said showUsers to curState.showUsers 144 00:07:50,580 --> 00:07:52,023 and invert that. 145 00:07:53,070 --> 00:07:56,240 And again, what we return here will be merged 146 00:07:56,240 --> 00:07:57,610 with the old state. 147 00:07:57,610 --> 00:08:01,633 So this second state piece here will not be lost. 148 00:08:02,620 --> 00:08:05,260 And that's how we update state. 149 00:08:05,260 --> 00:08:09,160 Now to use it, we go down to the render method 150 00:08:09,160 --> 00:08:11,400 where I need my state 151 00:08:11,400 --> 00:08:15,620 and here we can now access this.state.showUsers 152 00:08:15,620 --> 00:08:18,080 to access this state object 153 00:08:18,080 --> 00:08:21,500 and then the showUsers property, 154 00:08:21,500 --> 00:08:25,670 which we set up here and which we manage and update here. 155 00:08:25,670 --> 00:08:29,423 And we do it as in all the places where we need that state. 156 00:08:31,840 --> 00:08:35,090 Now of course, I also wanted to arrive to users list, 157 00:08:35,090 --> 00:08:37,669 therefore I'll grab this constant here 158 00:08:37,669 --> 00:08:39,630 and add to this to my render method 159 00:08:41,280 --> 00:08:44,480 because we still can of course define help 160 00:08:44,480 --> 00:08:46,870 or consents like this in the render method, 161 00:08:46,870 --> 00:08:49,563 that all is valid and works. 162 00:08:50,690 --> 00:08:53,680 One thing, one last thing is missing though 163 00:08:53,680 --> 00:08:55,680 and that's this method here. 164 00:08:55,680 --> 00:08:58,540 I added the toggleUserHandler method, 165 00:08:58,540 --> 00:09:03,270 but you don't call it by using it like this. 166 00:09:03,270 --> 00:09:06,510 Instead, it's a method off this clause here 167 00:09:06,510 --> 00:09:11,510 and therefore we need to point at this toggleUserHandler 168 00:09:11,540 --> 00:09:14,883 to point at this method part of this class. 169 00:09:15,970 --> 00:09:18,110 But even that wouldn't work 170 00:09:18,110 --> 00:09:22,170 due to how this works in JavaScript. 171 00:09:22,170 --> 00:09:24,400 And that has nothing to do with react 172 00:09:24,400 --> 00:09:28,860 that this keyword can simply be tricky in JavaScript. 173 00:09:28,860 --> 00:09:30,440 Attached you find a resource 174 00:09:30,440 --> 00:09:33,073 which allows you to learn more about this. 175 00:09:34,230 --> 00:09:37,090 What you need to do in the end is you need to make sure 176 00:09:37,090 --> 00:09:41,300 that this keyword inside of your method 177 00:09:41,300 --> 00:09:44,470 refers to the surrounding class 178 00:09:44,470 --> 00:09:47,070 and it wouldn't by default, 179 00:09:47,070 --> 00:09:51,880 when your method is called upon any event like to click. 180 00:09:51,880 --> 00:09:56,880 So to fix this, we can for example use bind and bind this 181 00:09:58,090 --> 00:10:00,620 which simply means that this keyword 182 00:10:00,620 --> 00:10:03,420 inside of this method here 183 00:10:03,420 --> 00:10:08,420 is now set to have the same context or the same value 184 00:10:08,690 --> 00:10:12,810 as this keyword when this code is evaluated 185 00:10:12,810 --> 00:10:16,803 and here that this keyword will refer to this class. 186 00:10:18,750 --> 00:10:23,090 Again, this is just related to how JavaScript works. 187 00:10:23,090 --> 00:10:26,760 It is not required in the functional component world 188 00:10:26,760 --> 00:10:29,340 because there we just define a function 189 00:10:29,340 --> 00:10:31,870 right before we point at it. 190 00:10:31,870 --> 00:10:34,670 There we don't use that this keyword at all 191 00:10:34,670 --> 00:10:37,163 because we're not working inside of a class. 192 00:10:38,610 --> 00:10:40,630 With that, we can again remove 193 00:10:40,630 --> 00:10:43,310 the functional component or a commented out 194 00:10:44,240 --> 00:10:46,120 and then save everything. 195 00:10:46,120 --> 00:10:49,060 And now if I reload, we get an error, 196 00:10:49,060 --> 00:10:52,550 Must call super constructor in derived class, 197 00:10:52,550 --> 00:10:54,360 that is indeed true. 198 00:10:54,360 --> 00:10:57,080 When you add the constructor to your class 199 00:10:57,080 --> 00:11:02,080 and you extend another class, you need to call super 200 00:11:02,120 --> 00:11:05,130 which calls the constructer of the super class 201 00:11:05,130 --> 00:11:07,740 so if the class were inheriting from. 202 00:11:07,740 --> 00:11:11,300 And with that, if we reload this works 203 00:11:11,300 --> 00:11:16,300 and it works as before now with class components only. 204 00:11:16,410 --> 00:11:17,760 So as you can tell, 205 00:11:17,760 --> 00:11:21,903 it's simply a different way of writing your components. 206 00:11:22,930 --> 00:11:26,140 Of course, now we can also get rid of the useState import 207 00:11:26,140 --> 00:11:27,830 because we're not using it anymore 208 00:11:27,830 --> 00:11:29,770 and as mentioned before already, 209 00:11:29,770 --> 00:11:33,320 you also can't use it in your class based components. 210 00:11:33,320 --> 00:11:36,023 None of the react hooks can be used there. 16774

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