All language subtitles for 003 Adding a First Class-based Component_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,200 --> 00:00:05,200 Now to dive into class-based components, 2 00:00:05,200 --> 00:00:08,280 I got another starting project for you. 3 00:00:08,280 --> 00:00:11,360 And you'll find it attached to this lecture of course. 4 00:00:11,360 --> 00:00:15,180 Simply download it, extract it, run npn install, 5 00:00:15,180 --> 00:00:16,960 and then npn start, 6 00:00:16,960 --> 00:00:20,693 and you will see this simple demo app here. 7 00:00:21,570 --> 00:00:22,940 Here we've got a button, 8 00:00:22,940 --> 00:00:25,683 where we can toggle this list of users. 9 00:00:26,670 --> 00:00:28,530 Now that's all not too fancy. 10 00:00:28,530 --> 00:00:30,900 And it's simply built by, in the end, 11 00:00:30,900 --> 00:00:34,240 using two components and the app component. 12 00:00:34,240 --> 00:00:37,780 We've got the users component with some dummy users. 13 00:00:37,780 --> 00:00:40,940 And there we got the toggle user handler function. 14 00:00:40,940 --> 00:00:42,290 And then in the end here, 15 00:00:42,290 --> 00:00:45,300 we simply create our list of users, 16 00:00:45,300 --> 00:00:47,780 by replicating the user component. 17 00:00:47,780 --> 00:00:50,410 And we got a button which triggers that function 18 00:00:50,410 --> 00:00:53,320 which changes the state with user state, 19 00:00:53,320 --> 00:00:58,320 which controls whether that users list is visible or not. 20 00:00:58,460 --> 00:01:00,720 And inside the user component 21 00:01:00,720 --> 00:01:04,239 I simply render a list item with some styling 22 00:01:04,239 --> 00:01:05,763 and I'm using props. 23 00:01:06,720 --> 00:01:08,750 So that's all not too fancy. 24 00:01:08,750 --> 00:01:09,583 And that's of course, 25 00:01:09,583 --> 00:01:13,420 all something you learn throughout this course. 26 00:01:13,420 --> 00:01:15,910 Now let's learn how we could write 27 00:01:15,910 --> 00:01:20,230 this user component as a class-based component. 28 00:01:20,230 --> 00:01:24,730 So how we can transform this to a class-based component. 29 00:01:24,730 --> 00:01:28,740 And for this, we start with the class keyword. 30 00:01:28,740 --> 00:01:29,573 Which again, 31 00:01:29,573 --> 00:01:33,900 just to make this really clear is built into JavaScript. 32 00:01:33,900 --> 00:01:35,910 It's not a React concept. 33 00:01:35,910 --> 00:01:40,400 Modern JavaScript supports classes. 34 00:01:40,400 --> 00:01:43,550 Now here, we would define a class named user. 35 00:01:43,550 --> 00:01:46,400 And when we define a class in Java script, 36 00:01:46,400 --> 00:01:47,750 it looks like this. 37 00:01:47,750 --> 00:01:50,120 class, then the name of the class, 38 00:01:50,120 --> 00:01:54,173 and then curly braces in which we define the class now. 39 00:01:55,300 --> 00:01:58,070 And inside of these curly braces, 40 00:01:58,070 --> 00:02:01,820 we can now start adding methods to this class. 41 00:02:01,820 --> 00:02:04,610 And for example, we can add a constructor method. 42 00:02:04,610 --> 00:02:07,630 Here, we could do some initialization work 43 00:02:07,630 --> 00:02:09,610 and I'll come back to this in a second, 44 00:02:09,610 --> 00:02:12,950 but actually I got no initialization work in this component. 45 00:02:12,950 --> 00:02:16,570 So I will just remove the constructor. 46 00:02:16,570 --> 00:02:19,223 Instead, we should add a render method. 47 00:02:20,150 --> 00:02:21,600 Now, theoretically, 48 00:02:21,600 --> 00:02:25,510 we can add as many methods as we want to a class 49 00:02:25,510 --> 00:02:27,633 and we can name them however we want. 50 00:02:28,540 --> 00:02:33,540 Render just is a specific method expected by React, 51 00:02:33,620 --> 00:02:38,100 which React will call when it finds a component being used 52 00:02:38,100 --> 00:02:39,410 in JS X code. 53 00:02:39,410 --> 00:02:41,570 Like we're doing it here. 54 00:02:41,570 --> 00:02:44,590 And then React will call the render method 55 00:02:44,590 --> 00:02:48,150 to find out what should be rendered to the screen. 56 00:02:48,150 --> 00:02:50,840 So the render method in a clause in the end, 57 00:02:50,840 --> 00:02:53,960 is your equivalent to your return statement 58 00:02:53,960 --> 00:02:57,090 in a functional component, you could say. 59 00:02:57,090 --> 00:02:58,410 And in the render method, 60 00:02:58,410 --> 00:03:01,440 you then also need to return the result, 61 00:03:01,440 --> 00:03:02,923 that should be rendered. 62 00:03:04,010 --> 00:03:07,060 So that's now how we can rebuild this. 63 00:03:07,060 --> 00:03:09,980 Though, one important thing is missing here. 64 00:03:09,980 --> 00:03:11,960 And that would be the props. 65 00:03:11,960 --> 00:03:14,420 In the function component, 66 00:03:14,420 --> 00:03:17,650 we receive props as a parameter. 67 00:03:17,650 --> 00:03:21,200 Automatically passed in by React. 68 00:03:21,200 --> 00:03:24,890 In the class based component, that's not the case. 69 00:03:24,890 --> 00:03:29,200 Here, the render method does not receive props. 70 00:03:29,200 --> 00:03:32,090 Instead though we need to do something else. 71 00:03:32,090 --> 00:03:33,910 Not just for props, 72 00:03:33,910 --> 00:03:37,888 but to make the class based component work in general. 73 00:03:37,888 --> 00:03:42,770 We need to import component from React. 74 00:03:44,900 --> 00:03:49,060 And then we need to extend component here, 75 00:03:49,060 --> 00:03:53,180 which is also a built in concept in modern Java script. 76 00:03:53,180 --> 00:03:57,190 You can build classes which extend other classes. 77 00:03:57,190 --> 00:04:00,520 So which inherit from other classes. 78 00:04:00,520 --> 00:04:03,510 And now our custom user class, 79 00:04:03,510 --> 00:04:06,490 inherits from the component class 80 00:04:06,490 --> 00:04:08,570 which is defined by React. 81 00:04:08,570 --> 00:04:11,300 Now this component class, 82 00:04:11,300 --> 00:04:13,410 adds important functionality, 83 00:04:13,410 --> 00:04:16,310 to make your class work as a component. 84 00:04:16,310 --> 00:04:18,750 And that's the important part for us. 85 00:04:18,750 --> 00:04:23,730 Now, it also adds a couple of important properties. 86 00:04:23,730 --> 00:04:26,670 For example, the props property, 87 00:04:26,670 --> 00:04:30,113 which we can now access with this props. 88 00:04:31,150 --> 00:04:34,250 This is only possible because we extend component. 89 00:04:34,250 --> 00:04:35,170 Because of that, 90 00:04:35,170 --> 00:04:38,520 there will be a props property in our clause 91 00:04:38,520 --> 00:04:41,970 which we can access through this keyword. 92 00:04:41,970 --> 00:04:44,790 And that contains all the props data 93 00:04:44,790 --> 00:04:48,020 we received for our component. 94 00:04:48,020 --> 00:04:51,110 And now we've got the class-based equivalent 95 00:04:51,110 --> 00:04:53,250 to our functional component, 96 00:04:53,250 --> 00:04:56,053 which we can now remove or comment out. 97 00:04:57,000 --> 00:04:58,260 And I'll do the latter, 98 00:04:58,260 --> 00:05:02,820 so that we still see both in side by side comparison. 99 00:05:02,820 --> 00:05:03,653 As you can tell, 100 00:05:03,653 --> 00:05:06,400 the functional component is a bit leaner, 101 00:05:06,400 --> 00:05:10,730 which is one of the main reasons why they are more popular. 102 00:05:10,730 --> 00:05:13,700 But other than that it is the same now. 103 00:05:13,700 --> 00:05:17,902 We also still export user and now we can save this. 104 00:05:17,902 --> 00:05:21,330 And if I reload, we get the same app as before 105 00:05:21,330 --> 00:05:24,380 and it works and looks like before. 106 00:05:24,380 --> 00:05:28,020 And that is also a very important takeaway here. 107 00:05:28,020 --> 00:05:31,370 Class-based components can work together 108 00:05:31,370 --> 00:05:34,160 with functional components. 109 00:05:34,160 --> 00:05:39,160 Users is a functional component, but it renders user, 110 00:05:39,209 --> 00:05:41,890 which is a class-based component. 111 00:05:41,890 --> 00:05:44,470 And that is absolutely no problem. 112 00:05:44,470 --> 00:05:47,340 A class-based component could also render 113 00:05:47,340 --> 00:05:50,900 a functional component that would also work. 114 00:05:50,900 --> 00:05:53,280 In the end, these are just components, 115 00:05:53,280 --> 00:05:56,470 and you can mix and match them as you want. 116 00:05:56,470 --> 00:06:01,090 In reality, you will most likely build a project 117 00:06:01,090 --> 00:06:04,320 by either sticking to just functional 118 00:06:04,320 --> 00:06:07,670 or to just class-based components though. 119 00:06:07,670 --> 00:06:11,480 You can mix and match, but that will mostly matter 120 00:06:11,480 --> 00:06:14,910 if you are working on an existing application 121 00:06:14,910 --> 00:06:16,970 and you're adding new features 122 00:06:16,970 --> 00:06:20,040 and you may be also step by step migrate, 123 00:06:20,040 --> 00:06:22,820 from class-based to functional. 124 00:06:22,820 --> 00:06:25,420 If you're starting a brand new project, 125 00:06:25,420 --> 00:06:28,470 you will probably go with your favorite approach, 126 00:06:28,470 --> 00:06:30,810 whatever that is and then stick to it. 127 00:06:30,810 --> 00:06:33,430 But you can mix and match it. 128 00:06:33,430 --> 00:06:36,930 And that is how you build class-based components. 129 00:06:36,930 --> 00:06:39,040 Now, however, I did mention that, 130 00:06:39,040 --> 00:06:41,680 we had to use them in the past, 131 00:06:41,680 --> 00:06:43,940 when it came to managing state, 132 00:06:43,940 --> 00:06:47,420 like we're doing it here in the user's JS file. 133 00:06:47,420 --> 00:06:50,760 Therefore let's find out how that works, 134 00:06:50,760 --> 00:06:52,823 with class-based components now. 10421

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