All language subtitles for 012 Redux with Class-based Components_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,150 Now, even though 2 00:00:03,150 --> 00:00:05,330 it's not the focus of this course 3 00:00:05,330 --> 00:00:08,830 because it's not the common way of writing components, 4 00:00:08,830 --> 00:00:12,920 I wanna briefly talk about class-based components 5 00:00:12,920 --> 00:00:15,660 instead of functional components. 6 00:00:15,660 --> 00:00:17,360 Because whilst nowadays 7 00:00:17,360 --> 00:00:21,020 we typically do use functional components only, 8 00:00:21,020 --> 00:00:24,000 there still are a lot of projects out there 9 00:00:24,000 --> 00:00:26,970 that do use class-based components instead. 10 00:00:26,970 --> 00:00:30,050 And there of course also are simply people who prefer that, 11 00:00:30,050 --> 00:00:33,800 and there is nothing wrong with class-based components. 12 00:00:33,800 --> 00:00:35,410 Therefore, I also wanna show you 13 00:00:35,410 --> 00:00:38,430 how this counter component would look like 14 00:00:38,430 --> 00:00:41,403 if we would build it as a class-based component. 15 00:00:42,380 --> 00:00:46,690 For this, I'll add a class named counter here, 16 00:00:46,690 --> 00:00:51,690 and that class needs to extend the react component object, 17 00:00:53,150 --> 00:00:55,330 the react component class. 18 00:00:55,330 --> 00:01:00,330 So I'll import from react, I will import components, 19 00:01:02,420 --> 00:01:04,819 whoops, component like this. 20 00:01:04,819 --> 00:01:08,760 And with that imported here we extend component. 21 00:01:08,760 --> 00:01:09,830 Now, that's what you learned 22 00:01:09,830 --> 00:01:12,920 in a separate module about class-based components. 23 00:01:12,920 --> 00:01:14,083 That's how they work. 24 00:01:15,120 --> 00:01:18,210 Now, in here, we then can add a constructor 25 00:01:18,210 --> 00:01:23,000 to initialize our state, but here we don't have any state. 26 00:01:23,000 --> 00:01:26,200 Instead here, we then add the render method, 27 00:01:26,200 --> 00:01:29,060 which should of course return this JS X code. 28 00:01:29,060 --> 00:01:32,733 So I'll copy that and return this JS X code here. 29 00:01:33,750 --> 00:01:35,480 But now we need to add increment 30 00:01:35,480 --> 00:01:37,660 and decrement handler methods, 31 00:01:37,660 --> 00:01:39,540 a toggle counter handler method, 32 00:01:39,540 --> 00:01:42,000 even though we're not doing anything with that yet. 33 00:01:42,000 --> 00:01:43,440 And we then need to make sure 34 00:01:43,440 --> 00:01:45,663 that we get access to our counter. 35 00:01:46,930 --> 00:01:49,240 So let's add a couple of methods here. 36 00:01:49,240 --> 00:01:52,560 Let's start with the increment handler method, 37 00:01:52,560 --> 00:01:55,020 then the decrement handler method, 38 00:01:55,020 --> 00:01:57,523 and also the toggle counter handler method. 39 00:01:58,540 --> 00:02:02,393 And this is how we can add methods in JavaScript classes. 40 00:02:04,230 --> 00:02:08,470 Now, down here, we now wanna hook this up correctly 41 00:02:08,470 --> 00:02:10,550 by using this increment handler 42 00:02:10,550 --> 00:02:14,760 and this decrement handler and this toggle counter handler, 43 00:02:14,760 --> 00:02:18,800 referring to these methods, which are part of this class. 44 00:02:18,800 --> 00:02:21,100 And then the question now is, 45 00:02:21,100 --> 00:02:24,440 how do we get access to Redux here? 46 00:02:24,440 --> 00:02:27,280 In the functional component we used hooks. 47 00:02:27,280 --> 00:02:29,830 Use dispatch and use selector, 48 00:02:29,830 --> 00:02:33,433 but hooks are not usable in class-based components. 49 00:02:34,420 --> 00:02:36,910 Now, I briefly did mention it before, 50 00:02:36,910 --> 00:02:41,040 react Redux also exports a connect function, 51 00:02:41,040 --> 00:02:43,390 which is a function that helps you connect 52 00:02:43,390 --> 00:02:46,810 class-based components to Redux. 53 00:02:46,810 --> 00:02:50,350 Actually, you could also use it on functional components, 54 00:02:50,350 --> 00:02:53,530 but for functional components, using these hooks 55 00:02:53,530 --> 00:02:56,130 is simply more convenient. 56 00:02:56,130 --> 00:02:57,870 But no matter if it's a functional 57 00:02:57,870 --> 00:03:01,000 or a class-based component, you can use connect. 58 00:03:01,000 --> 00:03:02,740 And therefore we will use connect here 59 00:03:02,740 --> 00:03:05,320 on this class-based component. 60 00:03:05,320 --> 00:03:06,960 How do we use it? 61 00:03:06,960 --> 00:03:09,250 When we export our counter, 62 00:03:09,250 --> 00:03:12,670 we don't export the counter component like this. 63 00:03:12,670 --> 00:03:17,550 Instead we call connect and now connect when executed, 64 00:03:17,550 --> 00:03:21,610 will actually return a new function as a value, 65 00:03:21,610 --> 00:03:24,900 which we then execute again. 66 00:03:24,900 --> 00:03:27,250 And then we pass our component 67 00:03:27,250 --> 00:03:30,803 to that returned function as our argument. 68 00:03:31,670 --> 00:03:33,180 So this can look strange, 69 00:03:33,180 --> 00:03:36,610 but connect is a so-called higher order component. 70 00:03:36,610 --> 00:03:39,010 We execute the connect function. 71 00:03:39,010 --> 00:03:41,690 It then returns a new function, 72 00:03:41,690 --> 00:03:46,510 and we execute this returned, this new function as well. 73 00:03:46,510 --> 00:03:50,050 And to this returned function, we pass counter. 74 00:03:50,050 --> 00:03:52,330 Now, why do we do it like this? 75 00:03:52,330 --> 00:03:55,690 Because to connect, when we execute this, 76 00:03:55,690 --> 00:03:58,040 we also pass something. 77 00:03:58,040 --> 00:04:01,880 Connect also wants some arguments. 78 00:04:01,880 --> 00:04:05,000 Connect also wants some arguments. 79 00:04:05,000 --> 00:04:07,660 It wants two arguments to be precise, 80 00:04:07,660 --> 00:04:10,700 and both arguments are functions. 81 00:04:10,700 --> 00:04:12,610 I'll write them separately here. 82 00:04:12,610 --> 00:04:17,279 The first function is a function that maps Redux state 83 00:04:17,279 --> 00:04:21,620 to props, which will be received in this component then. 84 00:04:21,620 --> 00:04:26,030 Hence we call this function map state to props. 85 00:04:26,030 --> 00:04:27,750 That's not a name you must use, 86 00:04:27,750 --> 00:04:31,580 but a convention which you will see in a lot of projects. 87 00:04:31,580 --> 00:04:34,900 Now, this is a function which receives the Redux state, 88 00:04:34,900 --> 00:04:39,480 and then this returns an object 89 00:04:39,480 --> 00:04:41,940 where the keys will be available 90 00:04:41,940 --> 00:04:44,650 as props in the receiving component. 91 00:04:44,650 --> 00:04:48,530 So in the counter component, and the values of those keys, 92 00:04:48,530 --> 00:04:53,010 that is then the logic for drilling into that Redux state. 93 00:04:53,010 --> 00:04:58,010 So here we could expose a counter prop by using counter 94 00:04:58,280 --> 00:05:01,940 as a key here, this key named therefore is up to you. 95 00:05:01,940 --> 00:05:05,250 And the value is state.counter. 96 00:05:05,250 --> 00:05:08,580 So we then pick the counter value from the Redux state, 97 00:05:08,580 --> 00:05:12,030 and bind that value to the counter prop. 98 00:05:12,030 --> 00:05:14,316 That's what we do here in the end. 99 00:05:14,316 --> 00:05:15,170 And that's of course, similar 100 00:05:15,170 --> 00:05:17,150 to what we do with use selector. 101 00:05:17,150 --> 00:05:19,040 Here we also get the state 102 00:05:19,040 --> 00:05:21,360 and drill into the state to get the counter, 103 00:05:21,360 --> 00:05:24,320 and then store that in a counter const. 104 00:05:24,320 --> 00:05:26,100 That's basically the equivalent 105 00:05:26,100 --> 00:05:31,100 when not doing it with hooks, but with the connect function. 106 00:05:31,120 --> 00:05:33,640 Because now map state to props 107 00:05:33,640 --> 00:05:36,623 is the first argument we pass to connect. 108 00:05:37,960 --> 00:05:40,960 The second argument is another function, 109 00:05:40,960 --> 00:05:44,970 which is typically called map dispatch to props. 110 00:05:44,970 --> 00:05:48,080 It's the equivalent to use dispatch. 111 00:05:48,080 --> 00:05:52,370 Now the idea is to store dispatch functions in props. 112 00:05:52,370 --> 00:05:56,340 So that in the counter component, we have certain props 113 00:05:56,340 --> 00:05:59,670 which we can execute as a function, which will then 114 00:05:59,670 --> 00:06:04,373 when executed dispatch an action to the Redux store. 115 00:06:05,880 --> 00:06:07,930 For this map dispatch to props 116 00:06:07,930 --> 00:06:12,470 receives the dispatch function as an argument automatically, 117 00:06:12,470 --> 00:06:14,540 just like map states to props. 118 00:06:14,540 --> 00:06:16,920 The map dispatch to props function 119 00:06:16,920 --> 00:06:20,420 will be executed for you by Redux. 120 00:06:20,420 --> 00:06:23,790 And then in here, we also return an object 121 00:06:23,790 --> 00:06:26,370 where the keys are prop names 122 00:06:26,370 --> 00:06:29,070 which we can then use in the component. 123 00:06:29,070 --> 00:06:32,303 So for example, increment as a name, 124 00:06:33,370 --> 00:06:37,320 and the value is then another function 125 00:06:37,320 --> 00:06:42,140 in which we call dispatch and then set up our action. 126 00:06:42,140 --> 00:06:43,500 Now, for example here, 127 00:06:43,500 --> 00:06:45,783 an action where the type is increment. 128 00:06:46,750 --> 00:06:49,300 This might look strange, but in the end, this ensures 129 00:06:49,300 --> 00:06:53,390 that in the counter component, we'll have an increment prop 130 00:06:53,390 --> 00:06:54,830 which we can use. 131 00:06:54,830 --> 00:06:57,090 This prop holds this function, 132 00:06:57,090 --> 00:06:58,900 so we can execute this function 133 00:06:58,900 --> 00:07:01,170 from inside the component thereafter. 134 00:07:01,170 --> 00:07:04,740 And when we do so, we will call dispatch 135 00:07:04,740 --> 00:07:07,150 and dispatch does action. 136 00:07:07,150 --> 00:07:10,550 And we can therefore also do this for decrement 137 00:07:10,550 --> 00:07:14,263 and dispatch the action with a type of decrement. 138 00:07:15,230 --> 00:07:18,350 And now that's the second argument we pass to connect, 139 00:07:18,350 --> 00:07:21,543 a pointer at this map, dispatch to props function. 140 00:07:22,430 --> 00:07:24,010 These are just pointers. 141 00:07:24,010 --> 00:07:26,720 We don't execute map stage to props 142 00:07:26,720 --> 00:07:30,160 or map dispatch to props here, we just point at them. 143 00:07:30,160 --> 00:07:34,100 They will be executed for us by react Redux. 144 00:07:34,100 --> 00:07:36,890 And when using connect, react Redux 145 00:07:36,890 --> 00:07:38,950 will still set up a subscription 146 00:07:38,950 --> 00:07:40,860 and manage a subscription for you. 147 00:07:40,860 --> 00:07:42,610 It will do all of that. 148 00:07:42,610 --> 00:07:44,030 It's just an alternative 149 00:07:44,030 --> 00:07:47,530 to using the use dispatch and use selector hooks. 150 00:07:47,530 --> 00:07:51,560 And obviously that's a bit shorter and easier, I would say. 151 00:07:51,560 --> 00:07:53,720 But if you're working with class-based components, 152 00:07:53,720 --> 00:07:57,720 you can't use hooks, and then this is your equivalent. 153 00:07:57,720 --> 00:08:00,820 So therefore now to take advantage of these props 154 00:08:00,820 --> 00:08:05,330 which we map into our component in the counter component. 155 00:08:05,330 --> 00:08:07,110 In increment handler, 156 00:08:07,110 --> 00:08:12,110 we can now execute this.props increment 157 00:08:13,820 --> 00:08:16,470 because on our props in this component 158 00:08:16,470 --> 00:08:18,900 we'll have a prop named increment 159 00:08:18,900 --> 00:08:21,273 because of our mapping here. 160 00:08:23,290 --> 00:08:25,380 And in the decrement handler, 161 00:08:25,380 --> 00:08:28,323 we therefore can execute this props decrement. 162 00:08:29,850 --> 00:08:33,159 And here in our JS X code, 163 00:08:33,159 --> 00:08:35,500 where we wanna output the counter, 164 00:08:35,500 --> 00:08:38,169 we would use this props counter 165 00:08:39,150 --> 00:08:42,080 because we received a counter through props 166 00:08:42,080 --> 00:08:45,100 because of us mapping this counter state 167 00:08:45,100 --> 00:08:46,653 to the counter prop here. 168 00:08:48,040 --> 00:08:49,633 Now, to make sure that everything works 169 00:08:49,633 --> 00:08:52,360 because JavaScript works the way it does, 170 00:08:52,360 --> 00:08:54,060 we need to call bind here 171 00:08:54,060 --> 00:08:58,830 on the increment and decrement handler bindings in JSX, 172 00:08:58,830 --> 00:09:01,110 and bind this to make sure 173 00:09:01,110 --> 00:09:04,010 that the disc keyword inside of these methods 174 00:09:04,010 --> 00:09:05,910 refers to the class. 175 00:09:05,910 --> 00:09:08,280 So that's something we need to add here. 176 00:09:08,280 --> 00:09:10,330 And then we can comment out 177 00:09:10,330 --> 00:09:12,910 this functional counter component 178 00:09:12,910 --> 00:09:14,700 so that we don't have the same name 179 00:09:14,700 --> 00:09:17,170 being used twice in this file. 180 00:09:17,170 --> 00:09:20,350 And if we now save this, it's reloaded, 181 00:09:20,350 --> 00:09:22,270 and we can still use it as before. 182 00:09:22,270 --> 00:09:25,913 It works as before, but now with a class-based component. 183 00:09:26,800 --> 00:09:28,640 Again, class-based components 184 00:09:28,640 --> 00:09:32,060 are not the focus of this course, but they do exist, 185 00:09:32,060 --> 00:09:34,690 they are valid, they are still getting used 186 00:09:34,690 --> 00:09:37,780 in a lot of projects, and therefore you should know 187 00:09:37,780 --> 00:09:41,000 how to connect those to Redux as well. 188 00:09:41,000 --> 00:09:44,260 Nonetheless, I will now, again, comment out 189 00:09:45,390 --> 00:09:47,883 all that code here, essentially. 190 00:09:49,680 --> 00:09:52,913 And comment the functional counter component back in. 191 00:09:54,630 --> 00:09:59,630 And then export this functional counter component again, 192 00:10:00,720 --> 00:10:04,500 and get rid of all the imports which we don't need anymore, 193 00:10:04,500 --> 00:10:07,440 but I'll keep the commented code down there 194 00:10:07,440 --> 00:10:09,360 so that we have it for reference. 195 00:10:09,360 --> 00:10:11,320 And now you learned about both. 196 00:10:11,320 --> 00:10:13,610 Now, we will stick to functional components, 197 00:10:13,610 --> 00:10:16,360 but it is worth knowing about class-based components 198 00:10:16,360 --> 00:10:18,683 and the connect function as well. 15751

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