All language subtitles for Learn React 18 with Redux Toolkit Full Tutorial for Beginners.en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English Download
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,000 --> 00:00:05,339 John similga is back with this massive 2 00:00:02,639 --> 00:00:07,620 react 18 course John is one of the most 3 00:00:05,339 --> 00:00:09,540 viewed instructors on our Channel this 4 00:00:07,620 --> 00:00:11,700 course is perfect for people brand new 5 00:00:09,540 --> 00:00:13,440 to react and for people who want to 6 00:00:11,700 --> 00:00:15,179 learn about the latest version of react 7 00:00:13,440 --> 00:00:17,940 for their front-end web development 8 00:00:15,179 --> 00:00:20,340 projects hey what's up I'm John from 9 00:00:17,940 --> 00:00:23,340 recording addict then I'm super excited 10 00:00:20,340 --> 00:00:25,260 to present the react 18 and Redux 11 00:00:23,340 --> 00:00:27,720 toolkit tutorial video 12 00:00:25,260 --> 00:00:31,380 recently react released its latest 13 00:00:27,720 --> 00:00:33,719 version react 18 and I believe it's the 14 00:00:31,380 --> 00:00:36,360 perfect time to explore all of the 15 00:00:33,719 --> 00:00:37,440 exciting new features such as suspense 16 00:00:36,360 --> 00:00:40,020 API 17 00:00:37,440 --> 00:00:42,780 extra libraries such as Redux toolkit 18 00:00:40,020 --> 00:00:44,820 and build tools such as wheat 19 00:00:42,780 --> 00:00:47,040 in addition we'll be discussing the 20 00:00:44,820 --> 00:00:50,160 latest and most useful vs code 21 00:00:47,040 --> 00:00:52,559 extensions and configurations so long 22 00:00:50,160 --> 00:00:55,440 story short in this video we're going to 23 00:00:52,559 --> 00:00:58,260 learn react from the very scratch using 24 00:00:55,440 --> 00:01:00,539 its latest and greatest features as well 25 00:00:58,260 --> 00:01:02,100 as tooling I really don't want to bore 26 00:01:00,539 --> 00:01:04,440 you with the long list of things we're 27 00:01:02,100 --> 00:01:06,900 going to cover in this video so if 28 00:01:04,440 --> 00:01:09,360 you're looking for something specific or 29 00:01:06,900 --> 00:01:11,820 simply just need some persuasion please 30 00:01:09,360 --> 00:01:13,020 reference the timestamps available in 31 00:01:11,820 --> 00:01:15,420 the description 32 00:01:13,020 --> 00:01:17,640 since it's not my first rodeo 33 00:01:15,420 --> 00:01:20,159 effectively I believe it's already a 34 00:01:17,640 --> 00:01:23,100 third time I'm recording a react 35 00:01:20,159 --> 00:01:24,000 tutorial video I added a bunch of useful 36 00:01:23,100 --> 00:01:26,640 stuff 37 00:01:24,000 --> 00:01:29,280 for example a readme file with all of 38 00:01:26,640 --> 00:01:31,560 the notes bunch of challenges to 39 00:01:29,280 --> 00:01:34,080 immediately test our knowledge and of 40 00:01:31,560 --> 00:01:36,840 course tons of useful resources where 41 00:01:34,080 --> 00:01:38,100 you can find more info on some specific 42 00:01:36,840 --> 00:01:40,380 topic 43 00:01:38,100 --> 00:01:43,079 as a quick sign note this content is 44 00:01:40,380 --> 00:01:45,119 part of my rear course and since quite 45 00:01:43,079 --> 00:01:47,040 often I get this question 46 00:01:45,119 --> 00:01:48,299 what's the difference between the video 47 00:01:47,040 --> 00:01:50,340 and the course 48 00:01:48,299 --> 00:01:53,399 let me just answer it here 49 00:01:50,340 --> 00:01:55,860 first course contains way more content 50 00:01:53,399 --> 00:01:58,799 at the moment it's somewhere around 60 51 00:01:55,860 --> 00:02:01,680 hours specifically it contains more 52 00:01:58,799 --> 00:02:04,259 tutorials for advanced libraries and 53 00:02:01,680 --> 00:02:05,280 more complex custom projects 54 00:02:04,259 --> 00:02:08,459 second 55 00:02:05,280 --> 00:02:11,400 if there are any changes let's say react 56 00:02:08,459 --> 00:02:13,739 War one of the libraries comes out with 57 00:02:11,400 --> 00:02:16,319 an update I'm able to update the course 58 00:02:13,739 --> 00:02:19,080 content which unfortunately is 59 00:02:16,319 --> 00:02:22,200 impossible to do with the YouTube video 60 00:02:19,080 --> 00:02:25,200 and third I provide assistance 61 00:02:22,200 --> 00:02:28,620 so if you get stuck on some specific 62 00:02:25,200 --> 00:02:31,379 topic or project during the course I'll 63 00:02:28,620 --> 00:02:34,860 help you to troubleshoot the issue 64 00:02:31,379 --> 00:02:38,700 lastly if you enjoy the content and want 65 00:02:34,860 --> 00:02:41,660 to enroll in my react or any of my other 66 00:02:38,700 --> 00:02:45,500 web dev courses just navigate to 67 00:02:41,660 --> 00:02:48,780 johnsburg.com again the URL is www 68 00:02:45,500 --> 00:02:52,319 johnsmilk.com sign up for my newsletter 69 00:02:48,780 --> 00:02:53,220 and get any of my courses for just ten 70 00:02:52,319 --> 00:02:55,140 dollars 71 00:02:53,220 --> 00:02:57,360 simply choose the course you want to 72 00:02:55,140 --> 00:03:00,660 attend and I'll provide a discount code 73 00:02:57,360 --> 00:03:03,300 for you to use at the checkout alright 74 00:03:00,660 --> 00:03:04,920 and welcome to the course and we're 75 00:03:03,300 --> 00:03:07,920 going to kick things off 76 00:03:04,920 --> 00:03:10,739 by quickly covering what in the world is 77 00:03:07,920 --> 00:03:11,459 react and there's no better place to 78 00:03:10,739 --> 00:03:14,519 start 79 00:03:11,459 --> 00:03:17,239 than official Docs which by the way are 80 00:03:14,519 --> 00:03:22,280 located at this URL 81 00:03:17,239 --> 00:03:25,260 react.js.org again the URL is www 82 00:03:22,280 --> 00:03:28,379 .react.js.org and once we navigate there 83 00:03:25,260 --> 00:03:31,560 we're greeted by this one profound 84 00:03:28,379 --> 00:03:34,980 sentence react is a JavaScript library 85 00:03:31,560 --> 00:03:38,400 for building user interfaces 86 00:03:34,980 --> 00:03:41,640 that's it my friends not a 10-page essay 87 00:03:38,400 --> 00:03:43,080 just a short concise one sentence 88 00:03:41,640 --> 00:03:45,540 explanation 89 00:03:43,080 --> 00:03:47,840 and while I'm a big fan 90 00:03:45,540 --> 00:03:52,140 of such straight to the point answers 91 00:03:47,840 --> 00:03:54,599 let me elaborate a bit on that so react 92 00:03:52,140 --> 00:03:57,000 was developed by Facebook 93 00:03:54,599 --> 00:04:00,420 um it's still maintained by Facebook but 94 00:03:57,000 --> 00:04:03,120 as you know now the company name is meta 95 00:04:00,420 --> 00:04:05,220 its initial release was all the way back 96 00:04:03,120 --> 00:04:08,519 in 2013. 97 00:04:05,220 --> 00:04:11,580 and currently it is by far the most 98 00:04:08,519 --> 00:04:13,260 popular JavaScript library to build user 99 00:04:11,580 --> 00:04:15,780 interfaces 100 00:04:13,260 --> 00:04:20,160 as a side note some of react competitors 101 00:04:15,780 --> 00:04:21,600 are angular View and swelt now if 102 00:04:20,160 --> 00:04:23,340 there's one thing I want you to take 103 00:04:21,600 --> 00:04:26,699 away from this video 104 00:04:23,340 --> 00:04:30,419 it's this one when it comes to react 105 00:04:26,699 --> 00:04:32,639 it's all about components 106 00:04:30,419 --> 00:04:36,720 and you can think of components as 107 00:04:32,639 --> 00:04:39,540 independent chunks of user interfaces 108 00:04:36,720 --> 00:04:43,259 components can be as small as one HTML 109 00:04:39,540 --> 00:04:44,280 element let's say a button or a heading 110 00:04:43,259 --> 00:04:48,120 2. 111 00:04:44,280 --> 00:04:51,540 or you can be a rebel and jam your 112 00:04:48,120 --> 00:04:53,699 entire Facebook clone in one component 113 00:04:51,540 --> 00:04:56,699 at the end of the day a lot of it 114 00:04:53,699 --> 00:04:59,460 depends on your preference and approach 115 00:04:56,699 --> 00:05:01,259 in reality though you'll probably avoid 116 00:04:59,460 --> 00:05:04,199 the one component route 117 00:05:01,259 --> 00:05:07,500 since such approach somewhat defeats the 118 00:05:04,199 --> 00:05:08,759 entire purpose of using react in the 119 00:05:07,500 --> 00:05:11,160 first place 120 00:05:08,759 --> 00:05:13,139 you see the benefit of component is that 121 00:05:11,160 --> 00:05:15,840 you can build bunch of 122 00:05:13,139 --> 00:05:19,979 independent isolated and most 123 00:05:15,840 --> 00:05:22,320 importantly reusable user interfaces but 124 00:05:19,979 --> 00:05:26,039 you can then piece it together just like 125 00:05:22,320 --> 00:05:29,300 the Lego blocks and as a result build 126 00:05:26,039 --> 00:05:31,500 even super big and complex apps without 127 00:05:29,300 --> 00:05:33,240 going insane 128 00:05:31,500 --> 00:05:35,940 well your app can have as many 129 00:05:33,240 --> 00:05:37,440 components as you like it will always 130 00:05:35,940 --> 00:05:40,199 have at least one 131 00:05:37,440 --> 00:05:42,900 called a root component we already 132 00:05:40,199 --> 00:05:44,460 glossed over it a bit but just to 133 00:05:42,900 --> 00:05:47,460 reiterate 134 00:05:44,460 --> 00:05:50,400 the major benefits of using components 135 00:05:47,460 --> 00:05:53,639 and essentially react to develop your 136 00:05:50,400 --> 00:05:56,280 next app are following you can build 137 00:05:53,639 --> 00:05:59,759 independent pieces of user interfaces 138 00:05:56,280 --> 00:06:03,720 meaning changing logic or layout in one 139 00:05:59,759 --> 00:06:06,360 component will not break your whole app 140 00:06:03,720 --> 00:06:11,100 and once the component is ready to go 141 00:06:06,360 --> 00:06:14,520 you can reuse it all throughout your app 142 00:06:11,100 --> 00:06:17,160 but the component code is still stored 143 00:06:14,520 --> 00:06:19,199 in one place so if you ever need to make 144 00:06:17,160 --> 00:06:21,120 some changes you don't have to run 145 00:06:19,199 --> 00:06:23,940 around like a headless chicken 146 00:06:21,120 --> 00:06:27,000 simply locate the component apply the 147 00:06:23,940 --> 00:06:29,759 changes and all of the instances will be 148 00:06:27,000 --> 00:06:32,340 automatically updated and of course 149 00:06:29,759 --> 00:06:33,840 let's not forget about the speed you see 150 00:06:32,340 --> 00:06:36,300 behind the scenes react is using 151 00:06:33,840 --> 00:06:38,039 something called a virtual Dom where 152 00:06:36,300 --> 00:06:40,800 only the component that needs to be 153 00:06:38,039 --> 00:06:43,440 updated is affected and what's really 154 00:06:40,800 --> 00:06:45,300 cool it's done without re-rendering the 155 00:06:43,440 --> 00:06:48,000 whole app which in turn of course 156 00:06:45,300 --> 00:06:50,400 increases the speed of your final 157 00:06:48,000 --> 00:06:53,520 product and as a result the user 158 00:06:50,400 --> 00:06:55,740 experience as well before I let you go I 159 00:06:53,520 --> 00:06:58,259 also want to show you great example of 160 00:06:55,740 --> 00:07:00,660 react components in action and I'm going 161 00:06:58,259 --> 00:07:03,180 to use our beloved Twitter 162 00:07:00,660 --> 00:07:06,060 if we take a look at the sidebar we can 163 00:07:03,180 --> 00:07:09,840 see list of links 164 00:07:06,060 --> 00:07:11,520 and you'll notice this repeating pattern 165 00:07:09,840 --> 00:07:15,419 where each link 166 00:07:11,520 --> 00:07:17,400 as the icon as well as the text 167 00:07:15,419 --> 00:07:20,099 so with the react what we can do we can 168 00:07:17,400 --> 00:07:21,780 set up a component that is going to 169 00:07:20,099 --> 00:07:25,020 accept 170 00:07:21,780 --> 00:07:27,300 those two things the icon 171 00:07:25,020 --> 00:07:28,860 and a text 172 00:07:27,300 --> 00:07:31,560 and essentially once we have the 173 00:07:28,860 --> 00:07:34,020 structure in place every time 174 00:07:31,560 --> 00:07:37,440 we want to use that link We simply need 175 00:07:34,020 --> 00:07:40,380 to provide the data show the icon 176 00:07:37,440 --> 00:07:42,780 and the text and what's really cool 177 00:07:40,380 --> 00:07:46,139 if we want to change something about the 178 00:07:42,780 --> 00:07:48,900 structure we only need to do that in one 179 00:07:46,139 --> 00:07:51,740 place and then all of the instances 180 00:07:48,900 --> 00:07:53,880 where we use the link are going to be 181 00:07:51,740 --> 00:07:57,000 automatically updated 182 00:07:53,880 --> 00:07:58,740 and if we keep looking 183 00:07:57,000 --> 00:08:02,400 in reality 184 00:07:58,740 --> 00:08:05,039 there's tons of repeating patterns not 185 00:08:02,400 --> 00:08:08,220 just in Twitter but pretty much in any 186 00:08:05,039 --> 00:08:10,440 app or site that you see for example 187 00:08:08,220 --> 00:08:13,979 this feed notice again we have the photo 188 00:08:10,440 --> 00:08:17,099 we have the name of the person who's 189 00:08:13,979 --> 00:08:20,759 posting We have the Twitter handle and I 190 00:08:17,099 --> 00:08:24,360 can go on and on and on so notice with 191 00:08:20,759 --> 00:08:25,979 every post only the values are changing 192 00:08:24,360 --> 00:08:29,699 so it's the same deal 193 00:08:25,979 --> 00:08:32,459 we set up a structure and then we just 194 00:08:29,699 --> 00:08:34,320 pass in the data and if you're looking 195 00:08:32,459 --> 00:08:37,140 at it you're like well okay what's the 196 00:08:34,320 --> 00:08:38,339 big deal well try doing that with HTML 197 00:08:37,140 --> 00:08:43,140 and CSS 198 00:08:38,339 --> 00:08:45,899 you see with just HTML we don't have any 199 00:08:43,140 --> 00:08:48,360 kind of templating correct 200 00:08:45,899 --> 00:08:49,500 so effectively we'll have to hard code 201 00:08:48,360 --> 00:08:51,899 all of this 202 00:08:49,500 --> 00:08:53,820 but that's not the case with react and 203 00:08:51,899 --> 00:08:56,760 if you're still not convinced hopefully 204 00:08:53,820 --> 00:09:00,959 by the time I show you the first 205 00:08:56,760 --> 00:09:03,720 component you'll see why react is so 206 00:09:00,959 --> 00:09:06,420 popular when it comes to building user 207 00:09:03,720 --> 00:09:09,180 interfaces when it comes to course 208 00:09:06,420 --> 00:09:11,240 requirements my expectation is that 209 00:09:09,180 --> 00:09:16,320 you're familiar with fundamentals of 210 00:09:11,240 --> 00:09:18,300 HTML CSS and most importantly JavaScript 211 00:09:16,320 --> 00:09:21,720 I can honestly say 212 00:09:18,300 --> 00:09:24,120 that the more JavaScript you know 213 00:09:21,720 --> 00:09:28,260 especially es6 214 00:09:24,120 --> 00:09:30,300 the easier is going to be pick up react 215 00:09:28,260 --> 00:09:33,540 since at the end of the day 216 00:09:30,300 --> 00:09:36,779 react is just JavaScript 217 00:09:33,540 --> 00:09:39,240 so if you're familiar with basic tags 218 00:09:36,779 --> 00:09:41,820 the general concept 219 00:09:39,240 --> 00:09:45,839 behind CSS selectors 220 00:09:41,820 --> 00:09:46,860 array methods and for example a spread 221 00:09:45,839 --> 00:09:48,779 operator 222 00:09:46,860 --> 00:09:51,300 you'll be in good shape 223 00:09:48,779 --> 00:09:53,100 now it's not the end of the world if 224 00:09:51,300 --> 00:09:54,180 you're still getting comfortable with 225 00:09:53,100 --> 00:09:56,220 JavaScript 226 00:09:54,180 --> 00:09:59,940 just keep in mind that once in a while 227 00:09:56,220 --> 00:10:01,860 you'll have to do some extra studying 228 00:09:59,940 --> 00:10:04,080 yes of course 229 00:10:01,860 --> 00:10:07,620 I'll try to cover 230 00:10:04,080 --> 00:10:11,459 even the straight up vanilla.js features 231 00:10:07,620 --> 00:10:13,980 in as much detail as I possibly can 232 00:10:11,459 --> 00:10:17,339 but at the end of the day it's a area 233 00:10:13,980 --> 00:10:19,980 course so if you still need more info 234 00:10:17,339 --> 00:10:21,600 please be prepared to utilize external 235 00:10:19,980 --> 00:10:24,000 resources 236 00:10:21,600 --> 00:10:26,640 and one such resource is my YouTube 237 00:10:24,000 --> 00:10:29,760 channel coding addict 238 00:10:26,640 --> 00:10:30,839 more specifically JavaScript nuggets 239 00:10:29,760 --> 00:10:33,360 playlist 240 00:10:30,839 --> 00:10:36,420 so before each video 241 00:10:33,360 --> 00:10:37,380 where we will utilize some vanilla.js 242 00:10:36,420 --> 00:10:40,500 feature 243 00:10:37,380 --> 00:10:41,700 I'll share a link to a corresponding 244 00:10:40,500 --> 00:10:44,880 video 245 00:10:41,700 --> 00:10:47,760 where I cover that feature in vanilla.js 246 00:10:44,880 --> 00:10:50,640 environment and hopefully that way you 247 00:10:47,760 --> 00:10:53,519 can simply watch the video get up to 248 00:10:50,640 --> 00:10:54,300 speed with what the specific feature is 249 00:10:53,519 --> 00:10:57,899 doing 250 00:10:54,300 --> 00:11:00,000 and continue with the course videos all 251 00:10:57,899 --> 00:11:01,680 right and up next let's work on our Dev 252 00:11:00,000 --> 00:11:04,980 environment 253 00:11:01,680 --> 00:11:08,220 is it when it comes to react it requires 254 00:11:04,980 --> 00:11:09,420 more tooling then straight up vanilla 255 00:11:08,220 --> 00:11:12,420 jazz 256 00:11:09,420 --> 00:11:13,500 where we only need text error and a 257 00:11:12,420 --> 00:11:15,720 browser 258 00:11:13,500 --> 00:11:17,339 now not to worry 259 00:11:15,720 --> 00:11:18,839 the setup is still extremely 260 00:11:17,339 --> 00:11:21,180 straightforward 261 00:11:18,839 --> 00:11:22,620 and you'll only need to do it once 262 00:11:21,180 --> 00:11:25,620 meaning 263 00:11:22,620 --> 00:11:28,200 once the setup is in place you will be 264 00:11:25,620 --> 00:11:30,420 able to start your react project 265 00:11:28,200 --> 00:11:32,519 in a lightning speed 266 00:11:30,420 --> 00:11:33,839 okay and let's start with the browser 267 00:11:32,519 --> 00:11:36,180 yes 268 00:11:33,839 --> 00:11:37,980 since we'll be developing Web projects 269 00:11:36,180 --> 00:11:39,779 we do need a browser 270 00:11:37,980 --> 00:11:42,360 I personally use Chrome 271 00:11:39,779 --> 00:11:44,519 but technically it's optional 272 00:11:42,360 --> 00:11:46,680 you'll be able to follow along with a 273 00:11:44,519 --> 00:11:48,899 different browser as well 274 00:11:46,680 --> 00:11:52,440 just keep in mind that here and there 275 00:11:48,899 --> 00:11:55,920 your results might slightly differ 276 00:11:52,440 --> 00:11:59,100 now let me be very clear I'm not saying 277 00:11:55,920 --> 00:12:00,720 that your entire project won't work on a 278 00:11:59,100 --> 00:12:03,000 different browser 279 00:12:00,720 --> 00:12:05,640 keep in mind that each browser has some 280 00:12:03,000 --> 00:12:08,880 default presets and therefore the same 281 00:12:05,640 --> 00:12:12,480 element for example input in different 282 00:12:08,880 --> 00:12:14,300 browsers can look slightly different 283 00:12:12,480 --> 00:12:17,820 if you also want to download the Chrome 284 00:12:14,300 --> 00:12:20,700 go to your favorite search engine which 285 00:12:17,820 --> 00:12:24,120 in my case is Google Chrome type install 286 00:12:20,700 --> 00:12:26,579 Google Chrome click on a link and follow 287 00:12:24,120 --> 00:12:29,160 the instructions all right and up next 288 00:12:26,579 --> 00:12:31,740 we've got text editor 289 00:12:29,160 --> 00:12:34,380 I'm going to use Visual Studio code 290 00:12:31,740 --> 00:12:37,860 pretty much a standard these days 291 00:12:34,380 --> 00:12:42,300 but just like the browser you can follow 292 00:12:37,860 --> 00:12:45,540 along with a different text editor the 293 00:12:42,300 --> 00:12:48,720 only downside here and there I will show 294 00:12:45,540 --> 00:12:49,860 some Nifty ways to speed up our 295 00:12:48,720 --> 00:12:52,680 development 296 00:12:49,860 --> 00:12:54,060 and you'll have to search for 297 00:12:52,680 --> 00:12:57,540 alternative 298 00:12:54,060 --> 00:13:01,139 available in your text editor 299 00:12:57,540 --> 00:13:03,360 as far as my configuration I keep it as 300 00:13:01,139 --> 00:13:06,360 close to default as possible 301 00:13:03,360 --> 00:13:08,339 simply because I'm recording content as 302 00:13:06,360 --> 00:13:10,800 far as my extensions I'm not going to 303 00:13:08,339 --> 00:13:13,980 cover them in this video since in my 304 00:13:10,800 --> 00:13:15,720 opinion a more solid approach is to 305 00:13:13,980 --> 00:13:18,660 Showcase what the extension is doing 306 00:13:15,720 --> 00:13:21,899 with the actual example 307 00:13:18,660 --> 00:13:25,440 so once we reach a point where one of 308 00:13:21,899 --> 00:13:30,060 the extensions becomes useful I'll stop 309 00:13:25,440 --> 00:13:32,459 and explain in detail and don't worry 310 00:13:30,060 --> 00:13:34,740 you won't need all of these extensions 311 00:13:32,459 --> 00:13:37,380 to follow along 312 00:13:34,740 --> 00:13:40,380 and the same goes for settings Json 313 00:13:37,380 --> 00:13:43,620 I will showcase the code once we 314 00:13:40,380 --> 00:13:46,920 actually need to work in the file 315 00:13:43,620 --> 00:13:51,660 now just in case you are like super 316 00:13:46,920 --> 00:13:55,380 eager to see my config right away 317 00:13:51,660 --> 00:13:59,579 just navigate to my GitHub profile 318 00:13:55,380 --> 00:14:01,620 and look for this repo vs code setup 319 00:13:59,579 --> 00:14:07,560 2022. 320 00:14:01,620 --> 00:14:10,380 again the repo is vs code setup 2022. 321 00:14:07,560 --> 00:14:15,060 lastly if you want to install Visual 322 00:14:10,380 --> 00:14:18,360 Studio code just navigate to this URL 323 00:14:15,060 --> 00:14:21,300 and follow the instructions 324 00:14:18,360 --> 00:14:24,420 not bad not bad and before we install 325 00:14:21,300 --> 00:14:27,959 node.js let's quickly talk about this 326 00:14:24,420 --> 00:14:30,779 scary thing the terminal or if you use 327 00:14:27,959 --> 00:14:33,000 Windows the command prompt and first 328 00:14:30,779 --> 00:14:36,120 let's just establish that terminal is 329 00:14:33,000 --> 00:14:37,980 simply an alternative way to communicate 330 00:14:36,120 --> 00:14:40,920 with our computer 331 00:14:37,980 --> 00:14:43,860 so instead of graphical interface we 332 00:14:40,920 --> 00:14:46,139 just type commands also don't worry we 333 00:14:43,860 --> 00:14:49,500 won't run any dangerous commands in this 334 00:14:46,139 --> 00:14:52,680 course like wiping out the entire file 335 00:14:49,500 --> 00:14:54,600 system or anything like that for the 336 00:14:52,680 --> 00:14:57,480 most part we'll run some basic commands 337 00:14:54,600 --> 00:15:00,120 like CD which stands for changing 338 00:14:57,480 --> 00:15:04,440 directory as well as commands provided 339 00:15:00,120 --> 00:15:06,420 by create react app a tool which we will 340 00:15:04,440 --> 00:15:09,300 install in few videos 341 00:15:06,420 --> 00:15:12,180 lastly throughout the course I'll also 342 00:15:09,300 --> 00:15:13,860 utilize the integrated terminal and 343 00:15:12,180 --> 00:15:16,639 visual studio code 344 00:15:13,860 --> 00:15:20,040 please keep in mind that we can use them 345 00:15:16,639 --> 00:15:21,480 interchangeably essentially you'll get 346 00:15:20,040 --> 00:15:22,980 the same results 347 00:15:21,480 --> 00:15:24,959 and the only reason why I prefer 348 00:15:22,980 --> 00:15:28,079 integrated terminal and visual studio 349 00:15:24,959 --> 00:15:30,560 code is simply because it right away 350 00:15:28,079 --> 00:15:34,320 points to the existing project 351 00:15:30,560 --> 00:15:36,779 effectively it requires less work and up 352 00:15:34,320 --> 00:15:39,420 next let's install node.js on our 353 00:15:36,779 --> 00:15:43,519 machine now if you're not familiar with 354 00:15:39,420 --> 00:15:47,339 node.js it's a cross platform JavaScript 355 00:15:43,519 --> 00:15:50,040 runtime environment which allows us to 356 00:15:47,339 --> 00:15:51,600 build fast and scalable Network 357 00:15:50,040 --> 00:15:54,480 applications 358 00:15:51,600 --> 00:15:57,180 if you want to check whether the node is 359 00:15:54,480 --> 00:16:00,120 already present on your machine just 360 00:15:57,180 --> 00:16:01,320 navigate back to the terminal and type 361 00:16:00,120 --> 00:16:04,260 node 362 00:16:01,320 --> 00:16:08,160 hyphen hyphen version or dash dash 363 00:16:04,260 --> 00:16:11,820 version again the command is node hyphen 364 00:16:08,160 --> 00:16:15,120 hyphen version press return and if it 365 00:16:11,820 --> 00:16:17,699 returns some numbers you're good to go 366 00:16:15,120 --> 00:16:21,180 however if it returns something along 367 00:16:17,699 --> 00:16:23,220 the lines of command not found please 368 00:16:21,180 --> 00:16:27,180 continue with the video 369 00:16:23,220 --> 00:16:32,300 now in order to install a brand new node 370 00:16:27,180 --> 00:16:36,300 instance we need to navigate to this URL 371 00:16:32,300 --> 00:16:39,779 nodejs.org or you can simply go to your 372 00:16:36,300 --> 00:16:41,639 search engine type install node and 373 00:16:39,779 --> 00:16:44,480 most likely this is going to be the 374 00:16:41,639 --> 00:16:47,040 first link but again the URL is 375 00:16:44,480 --> 00:16:49,500 nodejs.org it will right away detect 376 00:16:47,040 --> 00:16:52,800 which operating system you're using 377 00:16:49,500 --> 00:16:55,699 and in my case that is mac and I highly 378 00:16:52,800 --> 00:16:59,699 highly highly highly highly recommend 379 00:16:55,699 --> 00:17:02,220 installing the LTS version instead of 380 00:16:59,699 --> 00:17:05,220 the current one again 381 00:17:02,220 --> 00:17:09,419 I recommend installing the LTS version 382 00:17:05,220 --> 00:17:12,360 since that way there's a smaller chance 383 00:17:09,419 --> 00:17:15,120 you'll have to chase some weird bugs 384 00:17:12,360 --> 00:17:18,059 you see we will use some external 385 00:17:15,120 --> 00:17:20,880 libraries and it takes some time for 386 00:17:18,059 --> 00:17:23,160 those libraries to catch up with the 387 00:17:20,880 --> 00:17:26,520 node versions as well 388 00:17:23,160 --> 00:17:29,760 so if you go with LTS which is a stable 389 00:17:26,520 --> 00:17:32,460 version there's a higher chance library 390 00:17:29,760 --> 00:17:35,700 or a package will work without any 391 00:17:32,460 --> 00:17:37,799 issues which can't be said about the 392 00:17:35,700 --> 00:17:39,740 current version 393 00:17:37,799 --> 00:17:45,240 the time of this recording 394 00:17:39,740 --> 00:17:46,020 the latest LTS version is 1812 but of 395 00:17:45,240 --> 00:17:47,660 course 396 00:17:46,020 --> 00:17:50,940 since you're watching this in the future 397 00:17:47,660 --> 00:17:53,280 your number most likely will be 398 00:17:50,940 --> 00:17:56,039 different it will be higher with that 399 00:17:53,280 --> 00:17:58,740 said let me emphasize something just 400 00:17:56,039 --> 00:17:59,880 because you're using different node 401 00:17:58,740 --> 00:18:03,419 version 402 00:17:59,880 --> 00:18:05,880 most likely a newer node version does 403 00:18:03,419 --> 00:18:09,740 not mean does not mean that the course 404 00:18:05,880 --> 00:18:13,440 content is old or invalid 405 00:18:09,740 --> 00:18:17,160 effectively we only use node to run our 406 00:18:13,440 --> 00:18:19,980 react app basically we'll use node to 407 00:18:17,160 --> 00:18:23,280 create a new react app instance 408 00:18:19,980 --> 00:18:25,860 so newer or older node version does not 409 00:18:23,280 --> 00:18:27,780 affect our code again let me be very 410 00:18:25,860 --> 00:18:29,280 clear and the reason why I'm being so 411 00:18:27,780 --> 00:18:32,400 annoying because I get these questions 412 00:18:29,280 --> 00:18:35,580 and Q a so I just want to address them 413 00:18:32,400 --> 00:18:37,740 here and if by any chance a newer node 414 00:18:35,580 --> 00:18:41,460 version will introduce some breaking 415 00:18:37,740 --> 00:18:45,240 changes to our entire setup rest assured 416 00:18:41,460 --> 00:18:48,840 I will update the course content so 417 00:18:45,240 --> 00:18:51,600 again download the LTS version follow 418 00:18:48,840 --> 00:18:53,880 the instructions and once the install is 419 00:18:51,600 --> 00:18:56,520 complete type again 420 00:18:53,880 --> 00:18:59,280 in a terminal node 421 00:18:56,520 --> 00:19:03,360 dash dash version again the command is 422 00:18:59,280 --> 00:19:05,760 node bash Dash and version and if 423 00:19:03,360 --> 00:19:07,860 everything is correct you'll see the 424 00:19:05,760 --> 00:19:10,140 node version 425 00:19:07,860 --> 00:19:12,539 however if you get the error please 426 00:19:10,140 --> 00:19:14,640 utilize your search engine and 427 00:19:12,539 --> 00:19:16,320 troubleshoot the issue since you won't 428 00:19:14,640 --> 00:19:18,600 be able to follow along with the 429 00:19:16,320 --> 00:19:20,760 upcoming videos and once we have 430 00:19:18,600 --> 00:19:23,160 successfully installed node 431 00:19:20,760 --> 00:19:25,260 now let's finally spin up our first 432 00:19:23,160 --> 00:19:28,200 react application 433 00:19:25,260 --> 00:19:32,220 like I mentioned before unlike vanilla 434 00:19:28,200 --> 00:19:34,260 Jazz react does require more tooling but 435 00:19:32,220 --> 00:19:35,580 luckily for us we won't have to do it 436 00:19:34,260 --> 00:19:38,460 manually 437 00:19:35,580 --> 00:19:41,700 for the longest time the most popular 438 00:19:38,460 --> 00:19:44,039 option for scaffolding react apps from 439 00:19:41,700 --> 00:19:46,799 scratch was packaged by the name of 440 00:19:44,039 --> 00:19:50,520 create react app 441 00:19:46,799 --> 00:19:52,740 but as everything in web dev things are 442 00:19:50,520 --> 00:19:54,900 changing and they're changing fast and 443 00:19:52,740 --> 00:19:57,059 as I'm recording the third iteration of 444 00:19:54,900 --> 00:19:59,640 the react tutorial 445 00:19:57,059 --> 00:20:03,419 actually there's a better and more 446 00:19:59,640 --> 00:20:07,380 popular option called Veet and yes it is 447 00:20:03,419 --> 00:20:10,799 pronounced Veet not white now since you 448 00:20:07,380 --> 00:20:14,400 will encounter create react app code 449 00:20:10,799 --> 00:20:17,520 examples in bunch of videos and blog 450 00:20:14,400 --> 00:20:21,140 posts during the first part of react 451 00:20:17,520 --> 00:20:25,380 tutorial so during the fundamentals part 452 00:20:21,140 --> 00:20:28,559 we are going to use create react app and 453 00:20:25,380 --> 00:20:32,220 once we get to Advanced react Concepts 454 00:20:28,559 --> 00:20:35,280 then we're going to switch to beat and 455 00:20:32,220 --> 00:20:38,100 of course I'll discuss why at this time 456 00:20:35,280 --> 00:20:38,820 beat is a better option once we get 457 00:20:38,100 --> 00:20:41,640 there 458 00:20:38,820 --> 00:20:45,660 don't worry both of them have similar 459 00:20:41,640 --> 00:20:48,539 setups so it's going to be a very easy 460 00:20:45,660 --> 00:20:50,460 transition I promise you so as I was 461 00:20:48,539 --> 00:20:52,860 saying during the first part of react 462 00:20:50,460 --> 00:20:56,760 tutorial we are going to utilize package 463 00:20:52,860 --> 00:21:00,179 called create react app and you can 464 00:20:56,760 --> 00:21:03,720 easily find it by just typing create 465 00:21:00,179 --> 00:21:06,720 react app in your search engine 466 00:21:03,720 --> 00:21:10,200 it was created and it's being supported 467 00:21:06,720 --> 00:21:12,179 by the Facebook team which means that we 468 00:21:10,200 --> 00:21:16,020 can always be sure 469 00:21:12,179 --> 00:21:17,460 that it uses all the latest and more 470 00:21:16,020 --> 00:21:19,559 importantly 471 00:21:17,460 --> 00:21:21,419 correct setup 472 00:21:19,559 --> 00:21:23,940 and if you go through docs you'll see 473 00:21:21,419 --> 00:21:25,799 that the main goal of this package 474 00:21:23,940 --> 00:21:29,340 is to 475 00:21:25,799 --> 00:21:31,080 speed up our development basically with 476 00:21:29,340 --> 00:21:34,260 the help of this package we can right 477 00:21:31,080 --> 00:21:37,320 away start building our awesome react 478 00:21:34,260 --> 00:21:40,200 applications instead of tinkering 479 00:21:37,320 --> 00:21:44,940 and getting frustrated with complex 480 00:21:40,200 --> 00:21:47,820 config setups and effectively 481 00:21:44,940 --> 00:21:51,799 in order to get started we want to open 482 00:21:47,820 --> 00:21:54,480 up our terminal navigate to our working 483 00:21:51,799 --> 00:21:58,500 directory in my case that is going to be 484 00:21:54,480 --> 00:22:02,039 desktop and run of this command npx 485 00:21:58,500 --> 00:22:05,880 create react app and the name of your 486 00:22:02,039 --> 00:22:06,659 application so in their example it's my 487 00:22:05,880 --> 00:22:09,120 app 488 00:22:06,659 --> 00:22:10,380 now before we do that let me explain the 489 00:22:09,120 --> 00:22:13,440 npm part 490 00:22:10,380 --> 00:22:17,820 you see when we install node 491 00:22:13,440 --> 00:22:18,960 we also install npm or node package 492 00:22:17,820 --> 00:22:21,720 manager 493 00:22:18,960 --> 00:22:24,059 which allows us to quickly install 494 00:22:21,720 --> 00:22:26,340 external packages 495 00:22:24,059 --> 00:22:30,480 and normally the command is going to be 496 00:22:26,340 --> 00:22:32,940 npm install or npmi for short and then 497 00:22:30,480 --> 00:22:36,240 the package name and don't worry if this 498 00:22:32,940 --> 00:22:39,240 sounds iffy it will make all the sense 499 00:22:36,240 --> 00:22:42,299 in the world once we install our first 500 00:22:39,240 --> 00:22:45,000 package and yes we'll do that throughout 501 00:22:42,299 --> 00:22:50,039 the course now why this command starts 502 00:22:45,000 --> 00:22:53,340 with npx instead of npm simply because 503 00:22:50,039 --> 00:22:56,100 instead of installing the create react 504 00:22:53,340 --> 00:22:58,200 app package on our machine which by the 505 00:22:56,100 --> 00:23:00,299 way used to be their old approach 506 00:22:58,200 --> 00:23:03,179 so we needed to install that globally 507 00:23:00,299 --> 00:23:05,220 and then we were able to create that new 508 00:23:03,179 --> 00:23:09,000 react application instance 509 00:23:05,220 --> 00:23:12,299 now we want to execute this package 510 00:23:09,000 --> 00:23:15,720 in order to create that new react app 511 00:23:12,299 --> 00:23:18,480 instance so instead of installing create 512 00:23:15,720 --> 00:23:22,020 react app on our machine we will just 513 00:23:18,480 --> 00:23:24,960 execute it and as a result we get a 514 00:23:22,020 --> 00:23:29,280 brand new react application on our 515 00:23:24,960 --> 00:23:32,039 machine and yes basically every time 516 00:23:29,280 --> 00:23:35,220 we want to start working on new react 517 00:23:32,039 --> 00:23:38,100 app we'll have to repeat the same steps 518 00:23:35,220 --> 00:23:40,500 so let's try it out I mean you can type 519 00:23:38,100 --> 00:23:41,940 it manually you can copy it's really up 520 00:23:40,500 --> 00:23:44,460 to you in my case I think I'm just going 521 00:23:41,940 --> 00:23:45,299 to copy I'm going to navigate back and 522 00:23:44,460 --> 00:23:46,860 first 523 00:23:45,299 --> 00:23:49,020 let's just 524 00:23:46,860 --> 00:23:50,940 get on the same page 525 00:23:49,020 --> 00:23:54,419 so as I'm looking at my thermal I can 526 00:23:50,940 --> 00:23:55,559 see that I'm sitting in the root and I 527 00:23:54,419 --> 00:23:58,799 can definitely 528 00:23:55,559 --> 00:24:01,679 check that by just typing LS which 529 00:23:58,799 --> 00:24:04,740 effectively is just going to give me all 530 00:24:01,679 --> 00:24:07,799 the directories that I have and notice 531 00:24:04,740 --> 00:24:10,020 here I have desktop so in my case since 532 00:24:07,799 --> 00:24:11,880 I want to set this project 533 00:24:10,020 --> 00:24:14,460 in desktop 534 00:24:11,880 --> 00:24:16,320 I'm going to use CD which is change 535 00:24:14,460 --> 00:24:19,020 directory and then navigate to the 536 00:24:16,320 --> 00:24:22,080 desktop now do you have to 537 00:24:19,020 --> 00:24:24,360 work on a desktop no of course not 538 00:24:22,080 --> 00:24:26,940 for example you can set it up in your 539 00:24:24,360 --> 00:24:30,179 documents this part is really up to you 540 00:24:26,940 --> 00:24:32,940 so I'm going to go with CD then 541 00:24:30,179 --> 00:24:36,120 stop so now I can clearly see that 542 00:24:32,940 --> 00:24:38,520 instead of root I'm located in a desktop 543 00:24:36,120 --> 00:24:40,440 again pretty much 544 00:24:38,520 --> 00:24:42,720 the same idea as with graphical 545 00:24:40,440 --> 00:24:45,179 interface the difference is that now of 546 00:24:42,720 --> 00:24:46,799 course we're just typing the command and 547 00:24:45,179 --> 00:24:50,400 then I'm going to copy and paste this 548 00:24:46,799 --> 00:24:53,159 command now let me right away show you a 549 00:24:50,400 --> 00:24:55,440 possible error that you might get and 550 00:24:53,159 --> 00:24:57,480 therefore I'll just keep this my app and 551 00:24:55,440 --> 00:25:00,360 I'll change that in a second so let's 552 00:24:57,480 --> 00:25:05,159 say if you run this command 553 00:25:00,360 --> 00:25:08,580 and it spins back the following error a 554 00:25:05,159 --> 00:25:11,280 fix that works for me is to add at 555 00:25:08,580 --> 00:25:13,799 latest at the end of create react app 556 00:25:11,280 --> 00:25:16,200 now if the install 557 00:25:13,799 --> 00:25:19,799 is successful then of course you don't 558 00:25:16,200 --> 00:25:24,360 need to do anything but if you run into 559 00:25:19,799 --> 00:25:30,779 the same error just go with NPH then 560 00:25:24,360 --> 00:25:32,820 create react app then add add latest and 561 00:25:30,779 --> 00:25:35,520 then come up with a name so in my case 562 00:25:32,820 --> 00:25:36,299 I'm going to call this tutorial like so 563 00:25:35,520 --> 00:25:40,080 again 564 00:25:36,299 --> 00:25:43,500 I'm located in the desktop we run npx 565 00:25:40,080 --> 00:25:46,500 because we want to execute this package 566 00:25:43,500 --> 00:25:49,140 and in order to avoid this bug we just 567 00:25:46,500 --> 00:25:52,080 go with ADD latest and then whatever is 568 00:25:49,140 --> 00:25:55,740 the name of our project and in my case 569 00:25:52,080 --> 00:25:57,539 that is tutorial press enter 570 00:25:55,740 --> 00:26:00,000 and the 571 00:25:57,539 --> 00:26:02,640 package is going to start setting up the 572 00:26:00,000 --> 00:26:03,659 application now it takes a little bit of 573 00:26:02,640 --> 00:26:06,900 time 574 00:26:03,659 --> 00:26:10,679 so I'll stop the video and I'll resume 575 00:26:06,900 --> 00:26:13,740 it once the install is complete okay it 576 00:26:10,679 --> 00:26:16,320 looks like the install is complete and 577 00:26:13,740 --> 00:26:18,960 like I mentioned before essentially we 578 00:26:16,320 --> 00:26:20,100 have two places where we can run more 579 00:26:18,960 --> 00:26:22,799 commands 580 00:26:20,100 --> 00:26:25,919 we can do that in the terminal or we can 581 00:26:22,799 --> 00:26:28,740 do that in the integrated terminal so 582 00:26:25,919 --> 00:26:33,299 notice here we have this npm start run 583 00:26:28,740 --> 00:26:35,220 build test as well as the eject and I'll 584 00:26:33,299 --> 00:26:37,620 talk about these commands a little bit 585 00:26:35,220 --> 00:26:40,140 later first I just want to spin up the 586 00:26:37,620 --> 00:26:43,799 application and in order to do that we 587 00:26:40,140 --> 00:26:46,080 need to run npm start and notice over 588 00:26:43,799 --> 00:26:49,020 here pretty much says what the command 589 00:26:46,080 --> 00:26:53,159 is doing it starts up the development 590 00:26:49,020 --> 00:26:55,020 server on our machine now 591 00:26:53,159 --> 00:26:57,179 like I just said we have two places 592 00:26:55,020 --> 00:26:58,740 where we can run that we can use the 593 00:26:57,179 --> 00:27:01,500 terminal but 594 00:26:58,740 --> 00:27:04,140 make sure that you navigate to this 595 00:27:01,500 --> 00:27:05,940 actual project because at the moment I'm 596 00:27:04,140 --> 00:27:08,940 located in the desktop 597 00:27:05,940 --> 00:27:11,940 so the fastest one is basically to drag 598 00:27:08,940 --> 00:27:15,419 and drop this one and once I press enter 599 00:27:11,940 --> 00:27:18,360 notice now I'm located in tutorial 600 00:27:15,419 --> 00:27:20,460 so now I can run this npm start and 601 00:27:18,360 --> 00:27:22,980 essentially it's going to spin up the 602 00:27:20,460 --> 00:27:25,740 dev server however I prefer the 603 00:27:22,980 --> 00:27:28,020 integrated terminal that is in the 604 00:27:25,740 --> 00:27:31,260 visual studio code and how we can run 605 00:27:28,020 --> 00:27:34,020 that well we first need to open up our 606 00:27:31,260 --> 00:27:37,440 text error when we need to open up this 607 00:27:34,020 --> 00:27:40,200 project in our text error which again 608 00:27:37,440 --> 00:27:41,700 I can simply drag and drop and once 609 00:27:40,200 --> 00:27:44,700 we're done with that we want to open up 610 00:27:41,700 --> 00:27:47,940 the integrated terminal and in my case 611 00:27:44,700 --> 00:27:51,600 the shorthand for that is control and 612 00:27:47,940 --> 00:27:54,720 tilde and tilde key is located just to 613 00:27:51,600 --> 00:27:56,640 the left of the one key so press this 614 00:27:54,720 --> 00:27:58,740 one and notice you'll have the terminal 615 00:27:56,640 --> 00:28:00,240 and again what's really cool about this 616 00:27:58,740 --> 00:28:03,240 integrated terminal 617 00:28:00,240 --> 00:28:05,940 that we're right away located in this 618 00:28:03,240 --> 00:28:08,580 particular project so I don't have to 619 00:28:05,940 --> 00:28:11,460 worry whether I'm in a desktop or I'm in 620 00:28:08,580 --> 00:28:13,740 some other folder I know for sure that 621 00:28:11,460 --> 00:28:17,820 integrated terminal 622 00:28:13,740 --> 00:28:20,100 always point back to my project 623 00:28:17,820 --> 00:28:23,100 and let's spin up that application so 624 00:28:20,100 --> 00:28:24,179 I'm going to go over here with npm and 625 00:28:23,100 --> 00:28:27,179 start 626 00:28:24,179 --> 00:28:28,200 and effectively this is going to spin up 627 00:28:27,179 --> 00:28:30,120 our 628 00:28:28,200 --> 00:28:32,940 react application 629 00:28:30,120 --> 00:28:37,500 and we'll have our application on 630 00:28:32,940 --> 00:28:40,020 localhost 3000 and this is how our 631 00:28:37,500 --> 00:28:40,860 initial application is going to look 632 00:28:40,020 --> 00:28:44,340 like 633 00:28:40,860 --> 00:28:47,340 as a side note I prefer setting up vs 634 00:28:44,340 --> 00:28:50,400 code and browser side by side since that 635 00:28:47,340 --> 00:28:53,279 way I don't have to switch screens 636 00:28:50,400 --> 00:28:54,419 if I want to showcase something in the 637 00:28:53,279 --> 00:28:56,940 browser 638 00:28:54,419 --> 00:28:59,640 but it's totally optional you're not 639 00:28:56,940 --> 00:29:00,720 required to do that also it's a Mac 640 00:28:59,640 --> 00:29:04,020 thing 641 00:29:00,720 --> 00:29:05,100 so effectively it has nothing to do with 642 00:29:04,020 --> 00:29:07,200 react 643 00:29:05,100 --> 00:29:09,419 so here's what I want to do I want to 644 00:29:07,200 --> 00:29:12,480 move this one down 645 00:29:09,419 --> 00:29:14,700 then I'll make it bigger smaller again 646 00:29:12,480 --> 00:29:17,940 now it's going to be on my desktop 647 00:29:14,700 --> 00:29:21,000 essentially I'll grab the code 648 00:29:17,940 --> 00:29:23,580 and then set it side by side 649 00:29:21,000 --> 00:29:27,720 I'll spend the next video 650 00:29:23,580 --> 00:29:30,659 going over the files and folders that we 651 00:29:27,720 --> 00:29:34,440 can find in our new instance 652 00:29:30,659 --> 00:29:37,500 in this video I'll simply showcase 653 00:29:34,440 --> 00:29:39,000 that every time we make some changes in 654 00:29:37,500 --> 00:29:42,059 our code 655 00:29:39,000 --> 00:29:44,820 the dev server will right away update 656 00:29:42,059 --> 00:29:47,820 the browser as well 657 00:29:44,820 --> 00:29:50,340 now you don't have to code along again 658 00:29:47,820 --> 00:29:52,260 we haven't covered what is the source 659 00:29:50,340 --> 00:29:54,720 folder and all that but in my case I'm 660 00:29:52,260 --> 00:29:57,960 going to go to Source folder then app.js 661 00:29:54,720 --> 00:30:01,020 and instead of this paragraph I'm just 662 00:29:57,960 --> 00:30:03,659 going to say I don't know maybe heading 663 00:30:01,020 --> 00:30:08,240 1 so I'll just change from the paragraph 664 00:30:03,659 --> 00:30:11,460 to a heading 1 and then I'll say react 665 00:30:08,240 --> 00:30:16,020 tutorial once you save check it out 666 00:30:11,460 --> 00:30:18,299 right away we see the latest version in 667 00:30:16,020 --> 00:30:19,260 the browser which is really really 668 00:30:18,299 --> 00:30:22,679 awesome 669 00:30:19,260 --> 00:30:26,100 it definitely speeds up our development 670 00:30:22,679 --> 00:30:30,000 and like I said in the following video 671 00:30:26,100 --> 00:30:33,059 I'll go over the files and folders that 672 00:30:30,000 --> 00:30:36,059 we can find in our new react app 673 00:30:33,059 --> 00:30:38,520 instance okay and once we're done with 674 00:30:36,059 --> 00:30:40,200 the install now let's cover react 675 00:30:38,520 --> 00:30:42,600 fundamentals 676 00:30:40,200 --> 00:30:43,740 as a side note if you ever want to 677 00:30:42,600 --> 00:30:46,799 reference 678 00:30:43,740 --> 00:30:50,100 the complete source code just navigate 679 00:30:46,799 --> 00:30:51,360 to my GitHub profile and look for this 680 00:30:50,100 --> 00:30:53,220 repo 681 00:30:51,360 --> 00:30:55,500 more specifically search for 682 00:30:53,220 --> 00:30:58,220 fundamentals directory 683 00:30:55,500 --> 00:31:01,440 also in there you'll find a readme file 684 00:30:58,220 --> 00:31:04,500 with all of the topics we're going to 685 00:31:01,440 --> 00:31:06,840 cover in the fundamentals section and 686 00:31:04,500 --> 00:31:09,659 yes I will reference this file quite a 687 00:31:06,840 --> 00:31:12,299 bit in the upcoming videos as far as the 688 00:31:09,659 --> 00:31:14,760 readme I tried such approach in my meron 689 00:31:12,299 --> 00:31:18,480 course and since students seem to really 690 00:31:14,760 --> 00:31:21,120 like it I decided to implement it in my 691 00:31:18,480 --> 00:31:23,220 other courses as well basically the goal 692 00:31:21,120 --> 00:31:26,580 of the readme file is to save your time 693 00:31:23,220 --> 00:31:29,460 on note-taking and to allow you to 694 00:31:26,580 --> 00:31:32,159 complete tasks independently if that's 695 00:31:29,460 --> 00:31:34,799 your preferred way of learning content 696 00:31:32,159 --> 00:31:36,779 all right and once we have created our 697 00:31:34,799 --> 00:31:39,539 first react app now let's quickly 698 00:31:36,779 --> 00:31:41,159 discuss files and folders provided by 699 00:31:39,539 --> 00:31:44,399 create react app 700 00:31:41,159 --> 00:31:46,740 and first up we've got node modules and 701 00:31:44,399 --> 00:31:48,600 effectively in there you'll find all of 702 00:31:46,740 --> 00:31:51,120 our dependencies 703 00:31:48,600 --> 00:31:54,500 essentially the dependencies our project 704 00:31:51,120 --> 00:31:56,820 is using and if you're iffy on the whole 705 00:31:54,500 --> 00:31:58,799 dependencies concept just wait a little 706 00:31:56,820 --> 00:32:01,740 bit in few minutes we'll cover 707 00:31:58,799 --> 00:32:04,260 package.json and once we do that it's 708 00:32:01,740 --> 00:32:06,539 going to be easier to see the big 709 00:32:04,260 --> 00:32:09,120 picture after that we've got public 710 00:32:06,539 --> 00:32:12,419 folder which contains static assets 711 00:32:09,120 --> 00:32:14,399 including index.html 712 00:32:12,419 --> 00:32:16,140 which basically is the thing that is 713 00:32:14,399 --> 00:32:19,260 served to the browser 714 00:32:16,140 --> 00:32:23,220 in other words index HTML is responsible 715 00:32:19,260 --> 00:32:25,919 for whatever you see in the browser and 716 00:32:23,220 --> 00:32:30,179 if you decide to inspect index HTML file 717 00:32:25,919 --> 00:32:31,320 you'll see that it's pretty typical HTML 718 00:32:30,179 --> 00:32:34,080 file 719 00:32:31,320 --> 00:32:35,340 so if you crack it open you'll see that 720 00:32:34,080 --> 00:32:38,220 it has 721 00:32:35,340 --> 00:32:41,520 typical tags it has the head 722 00:32:38,220 --> 00:32:42,779 element it also has the body one and in 723 00:32:41,520 --> 00:32:46,020 the head element 724 00:32:42,779 --> 00:32:49,919 we can change the title get the fonts 725 00:32:46,020 --> 00:32:51,240 CSS maybe even a favicon and just to 726 00:32:49,919 --> 00:32:53,159 show you 727 00:32:51,240 --> 00:32:55,919 that I'm not making this up I'm gonna 728 00:32:53,159 --> 00:32:59,220 navigate there I'll search for title 729 00:32:55,919 --> 00:33:02,480 tags and let's right here react tutorial 730 00:32:59,220 --> 00:33:02,480 or whatever 731 00:33:02,580 --> 00:33:08,220 value you want to provide and check it 732 00:33:04,559 --> 00:33:11,580 out the moment I change it 733 00:33:08,220 --> 00:33:14,760 I also can see that in the browser now 734 00:33:11,580 --> 00:33:18,720 one thing that is very very important 735 00:33:14,760 --> 00:33:22,940 is this div with ID of root 736 00:33:18,720 --> 00:33:25,799 believe it or not our entire application 737 00:33:22,940 --> 00:33:28,260 effectively lives over here and I know 738 00:33:25,799 --> 00:33:30,419 it sounds somewhat unbelievable but 739 00:33:28,260 --> 00:33:31,620 effectively our workflow is going to be 740 00:33:30,419 --> 00:33:34,019 following 741 00:33:31,620 --> 00:33:37,679 we'll set up whatever logic 742 00:33:34,019 --> 00:33:39,240 we want or need in The Source One In The 743 00:33:37,679 --> 00:33:43,140 Source folder 744 00:33:39,240 --> 00:33:45,720 and then it's going to get injected in 745 00:33:43,140 --> 00:33:48,720 this route and essentially as a result 746 00:33:45,720 --> 00:33:50,880 we'll render our application in the 747 00:33:48,720 --> 00:33:53,399 browser and don't worry I will come back 748 00:33:50,880 --> 00:33:54,419 to this concept quite a few times 749 00:33:53,399 --> 00:33:56,100 because 750 00:33:54,419 --> 00:33:58,200 I fully understand that the first time 751 00:33:56,100 --> 00:34:00,539 you see that you're like whoa that's 752 00:33:58,200 --> 00:34:03,480 some really impressive stuff yep I agree 753 00:34:00,539 --> 00:34:06,539 after that we've got Source folder which 754 00:34:03,480 --> 00:34:09,960 basically is the brain of our app 755 00:34:06,539 --> 00:34:12,240 and therefore in there we'll do all of 756 00:34:09,960 --> 00:34:13,619 our work now I'm not going to cover any 757 00:34:12,240 --> 00:34:16,379 of the files 758 00:34:13,619 --> 00:34:18,540 in Source folder for now since create 759 00:34:16,379 --> 00:34:20,820 react app creates bunch of boilerplate 760 00:34:18,540 --> 00:34:22,859 and actually I prefer 761 00:34:20,820 --> 00:34:24,899 especially 762 00:34:22,859 --> 00:34:27,659 while we're covering fundamentals 763 00:34:24,899 --> 00:34:31,619 setting everything up together from the 764 00:34:27,659 --> 00:34:34,220 scratch but in short in the source we 765 00:34:31,619 --> 00:34:38,520 will set up all our components Pages 766 00:34:34,220 --> 00:34:41,520 utils functions assets CSS and whatever 767 00:34:38,520 --> 00:34:43,859 else we need for our project 768 00:34:41,520 --> 00:34:45,839 as we continue with the course you'll 769 00:34:43,859 --> 00:34:48,659 see that there are really no 770 00:34:45,839 --> 00:34:52,200 restrictions on the folder structure 771 00:34:48,659 --> 00:34:53,639 inside source so the way you manage your 772 00:34:52,200 --> 00:34:56,839 code really comes down to your 773 00:34:53,639 --> 00:35:00,480 preference as long as there's index.js 774 00:34:56,839 --> 00:35:04,320 which is going to be our JavaScript 775 00:35:00,480 --> 00:35:07,800 entry point so remember the ID root in 776 00:35:04,320 --> 00:35:10,440 the index.html once we're done adding 777 00:35:07,800 --> 00:35:14,520 our functionality like I said it's going 778 00:35:10,440 --> 00:35:18,359 to get injected in there and as a result 779 00:35:14,520 --> 00:35:21,420 we will render our application in the 780 00:35:18,359 --> 00:35:24,180 browser after Source we've got dot git 781 00:35:21,420 --> 00:35:26,119 ignore which specifies which files will 782 00:35:24,180 --> 00:35:28,440 be ignored by The Source control 783 00:35:26,119 --> 00:35:31,680 effectively get 784 00:35:28,440 --> 00:35:35,280 so if you navigate there you'll see a 785 00:35:31,680 --> 00:35:38,160 list of files and folders that are going 786 00:35:35,280 --> 00:35:41,040 to be ignored once we push our project 787 00:35:38,160 --> 00:35:43,619 up to GitHub for example after that we 788 00:35:41,040 --> 00:35:46,859 got package log but let's just skip it 789 00:35:43,619 --> 00:35:48,359 for now and first let's discuss package 790 00:35:46,859 --> 00:35:50,339 Json 791 00:35:48,359 --> 00:35:53,640 and effectively 792 00:35:50,339 --> 00:35:56,700 package Json contains useful info about 793 00:35:53,640 --> 00:36:00,060 our project now we will mostly be 794 00:35:56,700 --> 00:36:02,099 interested in two things in script as 795 00:36:00,060 --> 00:36:04,380 well as the dependencies 796 00:36:02,099 --> 00:36:06,900 now package.json is not specific to 797 00:36:04,380 --> 00:36:10,500 react apps pretty much every node 798 00:36:06,900 --> 00:36:12,900 project has one so let's take a peek 799 00:36:10,500 --> 00:36:15,839 essentially like I said useful info 800 00:36:12,900 --> 00:36:18,960 about our project and you'll also most 801 00:36:15,839 --> 00:36:20,940 likely hear this term manifest file so 802 00:36:18,960 --> 00:36:23,040 in here we can see the name you can see 803 00:36:20,940 --> 00:36:25,640 the version and like I said 804 00:36:23,040 --> 00:36:29,040 we are mostly interested in two things 805 00:36:25,640 --> 00:36:31,560 independencies our project is using so 806 00:36:29,040 --> 00:36:34,740 these are the main dependencies as well 807 00:36:31,560 --> 00:36:36,300 as the script so these are the commands 808 00:36:34,740 --> 00:36:38,160 we can run 809 00:36:36,300 --> 00:36:40,800 in our project 810 00:36:38,160 --> 00:36:44,099 that's why when we run npm start 811 00:36:40,800 --> 00:36:47,579 essentially it sets up our application 812 00:36:44,099 --> 00:36:48,960 and yes we'll discuss the other commands 813 00:36:47,579 --> 00:36:52,020 a little bit later for now we're 814 00:36:48,960 --> 00:36:55,020 literally interested in this one the npm 815 00:36:52,020 --> 00:36:57,359 start now when it comes to commands 816 00:36:55,020 --> 00:36:59,940 normally you go with npm run and then 817 00:36:57,359 --> 00:37:02,880 the command but with start we can simply 818 00:36:59,940 --> 00:37:05,880 go with npm and then start 819 00:37:02,880 --> 00:37:08,040 now if you 820 00:37:05,880 --> 00:37:10,020 picked up the node modules you probably 821 00:37:08,040 --> 00:37:12,960 noticed that it's huge 822 00:37:10,020 --> 00:37:14,520 it's literally massive but in the 823 00:37:12,960 --> 00:37:16,619 package.json we actually have way less 824 00:37:14,520 --> 00:37:18,240 dependencies so what's up with that 825 00:37:16,619 --> 00:37:20,579 or you see so these are the main 826 00:37:18,240 --> 00:37:22,740 dependencies our project is using but we 827 00:37:20,579 --> 00:37:24,660 need to keep in mind that every time we 828 00:37:22,740 --> 00:37:27,180 get the dependency so some kind of 829 00:37:24,660 --> 00:37:29,040 Library some other devs were kind enough 830 00:37:27,180 --> 00:37:31,619 to set up and essentially we're just 831 00:37:29,040 --> 00:37:33,420 utilizing the code in that Library 832 00:37:31,619 --> 00:37:35,400 well those libraries can have 833 00:37:33,420 --> 00:37:37,920 dependencies on their own 834 00:37:35,400 --> 00:37:40,079 which is the case over here 835 00:37:37,920 --> 00:37:42,180 so every time we'll install dependency 836 00:37:40,079 --> 00:37:43,920 which of course it will do during the 837 00:37:42,180 --> 00:37:46,980 course there's going to be more 838 00:37:43,920 --> 00:37:49,140 dependencies because the dependency has 839 00:37:46,980 --> 00:37:51,660 its own dependencies hopefully I'm 840 00:37:49,140 --> 00:37:53,700 making myself clear so again these are 841 00:37:51,660 --> 00:37:56,640 the main dependencies and then 842 00:37:53,700 --> 00:38:00,359 since they have dependencies on their 843 00:37:56,640 --> 00:38:02,099 own the node modules is quite big and 844 00:38:00,359 --> 00:38:04,980 that's actually one of the reasons why 845 00:38:02,099 --> 00:38:08,160 you will always see it in git ignore I 846 00:38:04,980 --> 00:38:10,619 mean in most cases so when we're pushing 847 00:38:08,160 --> 00:38:13,920 this up to a GitHub 848 00:38:10,619 --> 00:38:15,900 normally known modules are not included 849 00:38:13,920 --> 00:38:19,200 because they're huge and there's also no 850 00:38:15,900 --> 00:38:21,680 need if you have package Json the moment 851 00:38:19,200 --> 00:38:24,359 you will run npm install 852 00:38:21,680 --> 00:38:26,820 essentially you'll right away install 853 00:38:24,359 --> 00:38:30,480 all dependencies so not just the main 854 00:38:26,820 --> 00:38:33,780 libraries but also the dependencies that 855 00:38:30,480 --> 00:38:35,760 they are using and that's why we also 856 00:38:33,780 --> 00:38:39,180 need a package lock 857 00:38:35,760 --> 00:38:40,020 because essentially it's a snapshot of 858 00:38:39,180 --> 00:38:42,900 our 859 00:38:40,020 --> 00:38:45,420 entire dependency tree now I can tell 860 00:38:42,900 --> 00:38:47,040 you right away that you really are not 861 00:38:45,420 --> 00:38:49,140 going to do any work in the package lock 862 00:38:47,040 --> 00:38:51,599 or node modules so 863 00:38:49,140 --> 00:38:54,000 this is just the general info and pretty 864 00:38:51,599 --> 00:38:56,099 much you can forget about them all of 865 00:38:54,000 --> 00:38:57,540 our work is going to happen in the 866 00:38:56,099 --> 00:39:00,359 source and then yes once in a while 867 00:38:57,540 --> 00:39:02,460 we'll navigate to a package Json we'll 868 00:39:00,359 --> 00:39:05,280 discuss some package and yes we'll add 869 00:39:02,460 --> 00:39:07,800 few commands of our own but that's later 870 00:39:05,280 --> 00:39:09,839 in the course and lastly we have a 871 00:39:07,800 --> 00:39:11,579 readme file which essentially is a 872 00:39:09,839 --> 00:39:14,220 markdown file where you can share more 873 00:39:11,579 --> 00:39:17,460 info about the project for example build 874 00:39:14,220 --> 00:39:19,260 instructions and summary and while we're 875 00:39:17,460 --> 00:39:21,180 still on the topic of readme I just want 876 00:39:19,260 --> 00:39:25,920 to showcase how you can quickly get the 877 00:39:21,180 --> 00:39:28,200 readme that I provided now again you 878 00:39:25,920 --> 00:39:30,900 don't have to do it 879 00:39:28,200 --> 00:39:33,000 it's totally up to you but just in case 880 00:39:30,900 --> 00:39:36,000 you're interested navigate to that repo 881 00:39:33,000 --> 00:39:38,940 look for fundamentals then click on 882 00:39:36,000 --> 00:39:41,040 readme then you want to go with raw 883 00:39:38,940 --> 00:39:42,839 essentially select everything 884 00:39:41,040 --> 00:39:45,420 go back now of course I already have 885 00:39:42,839 --> 00:39:47,880 these values here select everything 886 00:39:45,420 --> 00:39:51,540 that's currently in a readme copy and 887 00:39:47,880 --> 00:39:53,280 paste and you are good to go now one 888 00:39:51,540 --> 00:39:55,560 last thing that I quickly want to 889 00:39:53,280 --> 00:39:58,440 mention here and there your results 890 00:39:55,560 --> 00:40:00,900 might differ a little bit as far as the 891 00:39:58,440 --> 00:40:03,359 browser just because 892 00:40:00,900 --> 00:40:06,000 I'm using zoom level 893 00:40:03,359 --> 00:40:08,280 so at the moment I have 175 that's 894 00:40:06,000 --> 00:40:11,220 pretty typical for me but here and there 895 00:40:08,280 --> 00:40:14,400 you might see me going more or less 896 00:40:11,220 --> 00:40:17,640 since that way I can showcase some stuff 897 00:40:14,400 --> 00:40:19,980 better again not a big deal it doesn't 898 00:40:17,640 --> 00:40:22,380 mean that the code is different I'm just 899 00:40:19,980 --> 00:40:25,079 saying that visually here and there our 900 00:40:22,380 --> 00:40:28,380 results might differ just because I use 901 00:40:25,079 --> 00:40:30,720 specific zoom level that's about it and 902 00:40:28,380 --> 00:40:33,660 up next we're going to remove some 903 00:40:30,720 --> 00:40:36,960 boilerplate code well create react app 904 00:40:33,660 --> 00:40:39,780 is super useful tool unfortunately it 905 00:40:36,960 --> 00:40:42,599 adds quite a bit of boilerplate code and 906 00:40:39,780 --> 00:40:45,180 especially while we're covering react 907 00:40:42,599 --> 00:40:47,160 fundamentals I prefer setting up 908 00:40:45,180 --> 00:40:49,079 everything from scratch 909 00:40:47,160 --> 00:40:50,460 since that way we can build everything 910 00:40:49,079 --> 00:40:53,640 together 911 00:40:50,460 --> 00:40:56,940 and in the process discuss the main 912 00:40:53,640 --> 00:41:00,119 purpose of some specific file or code 913 00:40:56,940 --> 00:41:02,280 snippet so that said our first task is 914 00:41:00,119 --> 00:41:06,960 going to be somewhat straightforward 915 00:41:02,280 --> 00:41:09,180 remove most of the boilerplate and we're 916 00:41:06,960 --> 00:41:13,500 going to do that in the following way 917 00:41:09,180 --> 00:41:16,619 navigate to your sidebar remove the 918 00:41:13,500 --> 00:41:19,020 entire Source folder event create one 919 00:41:16,619 --> 00:41:22,320 from the scratch yes I know 920 00:41:19,020 --> 00:41:24,119 this might sound redundant but this is 921 00:41:22,320 --> 00:41:26,820 what we're gonna do and then inside of 922 00:41:24,119 --> 00:41:28,920 it we want to create index.js I can tell 923 00:41:26,820 --> 00:41:31,980 you right away that your application is 924 00:41:28,920 --> 00:41:34,740 going to break don't worry we'll fix it 925 00:41:31,980 --> 00:41:35,760 in the next video so let's navigate over 926 00:41:34,740 --> 00:41:37,980 here 927 00:41:35,760 --> 00:41:39,060 notice the source folder I want to 928 00:41:37,980 --> 00:41:42,599 remove it 929 00:41:39,060 --> 00:41:44,400 yep the entire folder it goes to trash 930 00:41:42,599 --> 00:41:47,880 then we want to create a new folder 931 00:41:44,400 --> 00:41:50,099 let's call it source and inside of the 932 00:41:47,880 --> 00:41:52,940 source let's create that file 933 00:41:50,099 --> 00:41:55,680 our JavaScript entry point so the 934 00:41:52,940 --> 00:41:57,720 index.js and once we do that like I said 935 00:41:55,680 --> 00:42:00,480 our application is going to look 936 00:41:57,720 --> 00:42:03,240 different don't worry we'll fix it in 937 00:42:00,480 --> 00:42:04,740 the next video and also in this video I 938 00:42:03,240 --> 00:42:08,640 want to mention that throughout the 939 00:42:04,740 --> 00:42:12,839 course I'll toggle the sidebar just so I 940 00:42:08,640 --> 00:42:16,920 have more real estate and in my case the 941 00:42:12,839 --> 00:42:19,380 shortcut is command B now if you're 942 00:42:16,920 --> 00:42:21,839 using different operating system there's 943 00:42:19,380 --> 00:42:25,320 a great possibility that your shortcuts 944 00:42:21,839 --> 00:42:27,900 are different and in order to get the 945 00:42:25,320 --> 00:42:30,839 shortcuts for your Visual Studio code 946 00:42:27,900 --> 00:42:34,500 just navigate to this Cog and then look 947 00:42:30,839 --> 00:42:36,720 for keyboard shortcuts so for example if 948 00:42:34,500 --> 00:42:41,280 I mention toggle sidebar 949 00:42:36,720 --> 00:42:44,160 you can go here and then type toggle and 950 00:42:41,280 --> 00:42:47,820 then sidebar and somewhere 951 00:42:44,160 --> 00:42:50,040 in there you'll find your shortcut and 952 00:42:47,820 --> 00:42:53,339 the reason why I prefer using such 953 00:42:50,040 --> 00:42:56,940 shortcuts is because they save a bunch 954 00:42:53,339 --> 00:43:00,839 of time and in the long run massively 955 00:42:56,940 --> 00:43:03,660 speed up the development all right and 956 00:43:00,839 --> 00:43:07,680 now let's finally set up our first 957 00:43:03,660 --> 00:43:10,680 component and in order to create 958 00:43:07,680 --> 00:43:13,380 a component in react 959 00:43:10,680 --> 00:43:14,220 effectively this is what we'll need to 960 00:43:13,380 --> 00:43:17,280 do 961 00:43:14,220 --> 00:43:21,060 and as you're looking at it your first 962 00:43:17,280 --> 00:43:24,359 thought probably is wait a minute 963 00:43:21,060 --> 00:43:27,000 guy with a weird accent that looks a lot 964 00:43:24,359 --> 00:43:28,440 like a JavaScript function 965 00:43:27,000 --> 00:43:32,460 and guess what 966 00:43:28,440 --> 00:43:35,640 you're absolutely positively 100 correct 967 00:43:32,460 --> 00:43:38,880 yes in order to create a component in 968 00:43:35,640 --> 00:43:41,280 react essentially we need to create a 969 00:43:38,880 --> 00:43:42,599 function yes of course there are 970 00:43:41,280 --> 00:43:44,220 differences 971 00:43:42,599 --> 00:43:45,300 essentially that's the whole point of 972 00:43:44,220 --> 00:43:48,180 the section 973 00:43:45,300 --> 00:43:53,099 is to show you 974 00:43:48,180 --> 00:43:55,680 what differences are there between the 975 00:43:53,099 --> 00:43:57,839 good old vanilla.js function 976 00:43:55,680 --> 00:44:00,560 and the components that we'll be 977 00:43:57,839 --> 00:44:04,319 creating react but always always always 978 00:44:00,560 --> 00:44:06,420 when you think of component think of 979 00:44:04,319 --> 00:44:09,240 function every time we want to create a 980 00:44:06,420 --> 00:44:12,240 component think I need to create a 981 00:44:09,240 --> 00:44:15,359 function NES it can be 982 00:44:12,240 --> 00:44:17,900 function with good old function keyword 983 00:44:15,359 --> 00:44:20,940 or you can create the arrow function 984 00:44:17,900 --> 00:44:21,960 that part doesn't really matter like I 985 00:44:20,940 --> 00:44:24,960 said 986 00:44:21,960 --> 00:44:27,960 a lot depends on your preference now we 987 00:44:24,960 --> 00:44:30,180 will cover more rules later 988 00:44:27,960 --> 00:44:32,880 for now I just want you to be aware that 989 00:44:30,180 --> 00:44:34,500 we want to start our components with 990 00:44:32,880 --> 00:44:36,359 capital letter 991 00:44:34,500 --> 00:44:38,040 that's very important yes we're creating 992 00:44:36,359 --> 00:44:39,420 a function but we want to start with a 993 00:44:38,040 --> 00:44:42,180 capital letter 994 00:44:39,420 --> 00:44:46,440 and we must must must must must return 995 00:44:42,180 --> 00:44:49,319 some kind of HTML from this function 996 00:44:46,440 --> 00:44:50,819 now technically this HTML is called js6 997 00:44:49,319 --> 00:44:53,339 but 998 00:44:50,819 --> 00:44:55,800 you'll see in a bunch of blog posts and 999 00:44:53,339 --> 00:44:56,760 you'll hear me throughout this course as 1000 00:44:55,800 --> 00:44:59,339 well 1001 00:44:56,760 --> 00:45:01,920 effectively to use them interchangeably 1002 00:44:59,339 --> 00:45:04,680 yes what we're doing from the function 1003 00:45:01,920 --> 00:45:06,780 we are returning HDML 1004 00:45:04,680 --> 00:45:09,900 essentially this is going to be the tag 1005 00:45:06,780 --> 00:45:12,119 that is displayed on screen and 1006 00:45:09,900 --> 00:45:14,700 technically you can return empty one 1007 00:45:12,119 --> 00:45:16,560 you're just not going to see any content 1008 00:45:14,700 --> 00:45:19,980 on the screen so once we know this info 1009 00:45:16,560 --> 00:45:23,880 let's just go to index.js and let's 1010 00:45:19,980 --> 00:45:26,579 create that function again the actual 1011 00:45:23,880 --> 00:45:29,160 syntax is really up to you in my case I 1012 00:45:26,579 --> 00:45:31,980 think I'm gonna go with good old 1013 00:45:29,160 --> 00:45:34,740 function keyword just because I feel 1014 00:45:31,980 --> 00:45:37,920 like good old function today then let's 1015 00:45:34,740 --> 00:45:39,839 type return and whatever we want to 1016 00:45:37,920 --> 00:45:42,000 display on our screen so I'm not going 1017 00:45:39,839 --> 00:45:43,680 to be very original and I'm just going 1018 00:45:42,000 --> 00:45:45,140 to say my 1019 00:45:43,680 --> 00:45:48,119 first 1020 00:45:45,140 --> 00:45:51,480 component so we're done 1021 00:45:48,119 --> 00:45:53,160 but once we save nothing happens 1022 00:45:51,480 --> 00:45:54,540 and you're like wait a minute you just 1023 00:45:53,160 --> 00:45:56,339 said that we're going to create the 1024 00:45:54,540 --> 00:45:58,920 function and we're going to be good to 1025 00:45:56,339 --> 00:46:00,960 go so normally every time we'll create a 1026 00:45:58,920 --> 00:46:03,800 component we'll need to do these two 1027 00:46:00,960 --> 00:46:07,560 things we'll need to create a function 1028 00:46:03,800 --> 00:46:10,920 AKA component and we'll need to export 1029 00:46:07,560 --> 00:46:13,619 it now if you're not familiar with es6 1030 00:46:10,920 --> 00:46:16,440 modules don't worry about it we'll spend 1031 00:46:13,619 --> 00:46:17,940 quite a few videos on it and essentially 1032 00:46:16,440 --> 00:46:21,300 what we're doing here is we're just 1033 00:46:17,940 --> 00:46:24,180 exporting this particular component and 1034 00:46:21,300 --> 00:46:27,119 then we can use it anywhere in our 1035 00:46:24,180 --> 00:46:29,520 application again we'll focus on that 1036 00:46:27,119 --> 00:46:33,480 a little bit later I really want to I 1037 00:46:29,520 --> 00:46:35,760 kind of hone in on the react stuff first 1038 00:46:33,480 --> 00:46:37,619 again if you're not familiar don't worry 1039 00:46:35,760 --> 00:46:39,720 we'll cover it if you are familiar you 1040 00:46:37,619 --> 00:46:41,880 know that essentially we're exporting as 1041 00:46:39,720 --> 00:46:44,280 default so we can just grab it anywhere 1042 00:46:41,880 --> 00:46:46,619 in our application and we can render it 1043 00:46:44,280 --> 00:46:50,040 essentially on the screen now this is 1044 00:46:46,619 --> 00:46:51,420 different though in the index.js that's 1045 00:46:50,040 --> 00:46:53,700 where we're setting up our root 1046 00:46:51,420 --> 00:46:56,280 component remember I said that we can 1047 00:46:53,700 --> 00:46:57,480 have as many components as we want I 1048 00:46:56,280 --> 00:46:59,339 mean you can have thousands and 1049 00:46:57,480 --> 00:47:01,980 thousands thousands of components but 1050 00:46:59,339 --> 00:47:04,680 you'll always have at least one 1051 00:47:01,980 --> 00:47:08,040 so that's the root component that's the 1052 00:47:04,680 --> 00:47:11,579 sucker that we will inject into that 1053 00:47:08,040 --> 00:47:13,680 idea route remember we talked about it 1054 00:47:11,579 --> 00:47:15,300 uh when we discuss the folder structure 1055 00:47:13,680 --> 00:47:17,940 I said that there's a public one there's 1056 00:47:15,300 --> 00:47:21,240 index HTML and then there is this famous 1057 00:47:17,940 --> 00:47:24,180 ID root so now what we want to do 1058 00:47:21,240 --> 00:47:28,560 we want to inject this greeting sucker 1059 00:47:24,180 --> 00:47:29,520 into that ID route and then since this 1060 00:47:28,560 --> 00:47:32,700 is 1061 00:47:29,520 --> 00:47:34,619 our JavaScript entry point we do need to 1062 00:47:32,700 --> 00:47:37,619 add a little bit more code now please 1063 00:47:34,619 --> 00:47:40,079 keep in mind just like this code it's 1064 00:47:37,619 --> 00:47:43,319 not specific to react this is es6 1065 00:47:40,079 --> 00:47:46,560 modules so in here we export stuff away 1066 00:47:43,319 --> 00:47:48,119 from the file and in here we import and 1067 00:47:46,560 --> 00:47:50,099 we're actually importing from the 1068 00:47:48,119 --> 00:47:53,280 libraries basically from our 1069 00:47:50,099 --> 00:47:55,260 dependencies again if you are even less 1070 00:47:53,280 --> 00:47:56,940 a little bit later 1071 00:47:55,260 --> 00:47:59,220 I'll cover 1072 00:47:56,940 --> 00:48:01,619 everything in detail okay don't worry 1073 00:47:59,220 --> 00:48:02,700 about it for now just worry about the 1074 00:48:01,619 --> 00:48:04,260 component 1075 00:48:02,700 --> 00:48:07,319 and the function you need to create 1076 00:48:04,260 --> 00:48:08,640 that's it and essentially as far as this 1077 00:48:07,319 --> 00:48:11,819 entry point 1078 00:48:08,640 --> 00:48:13,740 we want to grab react and react Dom and 1079 00:48:11,819 --> 00:48:15,180 notice this one is coming from react 1080 00:48:13,740 --> 00:48:18,720 package and this one is coming from 1081 00:48:15,180 --> 00:48:21,060 react Dom forward slash client 1082 00:48:18,720 --> 00:48:22,920 and then restore our function that 1083 00:48:21,060 --> 00:48:25,440 doesn't change and then we want to go to 1084 00:48:22,920 --> 00:48:28,319 react Dom create a root 1085 00:48:25,440 --> 00:48:30,420 so there's a special method that creates 1086 00:48:28,319 --> 00:48:34,560 the root and it's looking for one thing 1087 00:48:30,420 --> 00:48:36,599 it's looking for that ID now how we can 1088 00:48:34,560 --> 00:48:40,560 select ID in vanilla address we go 1089 00:48:36,599 --> 00:48:42,599 document get element by ID root and then 1090 00:48:40,560 --> 00:48:44,760 eventually we go with a root dot render 1091 00:48:42,599 --> 00:48:46,859 again if you're looking in this code and 1092 00:48:44,760 --> 00:48:49,440 you're like whoa this got really 1093 00:48:46,859 --> 00:48:51,540 confusing really fast this is going to 1094 00:48:49,440 --> 00:48:53,400 be your typical setup and I'll show you 1095 00:48:51,540 --> 00:48:56,460 the extension that actually sets up all 1096 00:48:53,400 --> 00:48:58,200 the code for you this is only for our 1097 00:48:56,460 --> 00:49:01,319 root 1098 00:48:58,200 --> 00:49:03,960 component for our JavaScript entry point 1099 00:49:01,319 --> 00:49:05,099 and I can tell you right away that once 1100 00:49:03,960 --> 00:49:07,440 we set it up 1101 00:49:05,099 --> 00:49:09,480 we'll pretty much forget about all this 1102 00:49:07,440 --> 00:49:11,220 code that's why again I know I've said 1103 00:49:09,480 --> 00:49:13,260 this already ten thousand times 1104 00:49:11,220 --> 00:49:15,800 Focus please on this one rest of the 1105 00:49:13,260 --> 00:49:19,200 stuff we'll figure out as we continue 1106 00:49:15,800 --> 00:49:21,119 with the section so first let's grab 1107 00:49:19,200 --> 00:49:24,240 those two Imports we're looking for 1108 00:49:21,119 --> 00:49:26,700 react so let me type over here if you 1109 00:49:24,240 --> 00:49:29,579 want of course you can copy and paste 1110 00:49:26,700 --> 00:49:32,640 from the readme so let's go here with 1111 00:49:29,579 --> 00:49:34,619 the react and as I said I'll most likely 1112 00:49:32,640 --> 00:49:36,540 vs code is going to give you the order 1113 00:49:34,619 --> 00:49:38,700 import if it doesn't then again just 1114 00:49:36,540 --> 00:49:41,280 look for react then we want to go with 1115 00:49:38,700 --> 00:49:43,680 import react 1116 00:49:41,280 --> 00:49:47,640 and then Dom and then this is coming 1117 00:49:43,680 --> 00:49:50,940 from react Dom Library forward slash 1118 00:49:47,640 --> 00:49:54,540 and client and as I say note 1119 00:49:50,940 --> 00:49:57,900 they change the syntax in react 18. 1120 00:49:54,540 --> 00:50:00,960 so prior to that there was react Dom 1121 00:49:57,900 --> 00:50:04,140 render now they create that route first 1122 00:50:00,960 --> 00:50:06,060 so right after the greeting we want to 1123 00:50:04,140 --> 00:50:10,140 go with const 1124 00:50:06,060 --> 00:50:12,480 and we'll create a root so react Dom and 1125 00:50:10,140 --> 00:50:15,119 now we're looking for that create root 1126 00:50:12,480 --> 00:50:16,619 and now let's select 1127 00:50:15,119 --> 00:50:19,319 the ID 1128 00:50:16,619 --> 00:50:22,079 with the value of root so this is where 1129 00:50:19,319 --> 00:50:24,780 we go back to vanilla JavaScript and we 1130 00:50:22,079 --> 00:50:28,500 go with get element by ID 1131 00:50:24,780 --> 00:50:31,200 and yes we're looking for the ID of root 1132 00:50:28,500 --> 00:50:34,400 and once we have all of this in place we 1133 00:50:31,200 --> 00:50:38,040 want to go with a root and then render 1134 00:50:34,400 --> 00:50:41,220 and here we want to pass our component 1135 00:50:38,040 --> 00:50:43,560 now when we create a component we create 1136 00:50:41,220 --> 00:50:46,859 a function correct we create a 1137 00:50:43,560 --> 00:50:49,680 JavaScript function now when we want to 1138 00:50:46,859 --> 00:50:52,920 render it the syntax is following we go 1139 00:50:49,680 --> 00:50:55,680 here with the angle bracket so then the 1140 00:50:52,920 --> 00:50:57,780 name of the component and then one thing 1141 00:50:55,680 --> 00:51:00,839 we need to know about react that we 1142 00:50:57,780 --> 00:51:03,000 always always need to self-close it 1143 00:51:00,839 --> 00:51:04,800 so this is again one of the rules that 1144 00:51:03,000 --> 00:51:07,020 we'll discuss a little bit later just 1145 00:51:04,800 --> 00:51:09,300 remember that when you pass in the 1146 00:51:07,020 --> 00:51:12,599 component you have two options you can 1147 00:51:09,300 --> 00:51:14,760 pass it like so with the opening one 1148 00:51:12,599 --> 00:51:16,680 and the closing one even though there's 1149 00:51:14,760 --> 00:51:20,339 nothing in between 1150 00:51:16,680 --> 00:51:22,800 yes that's the case or you can self 1151 00:51:20,339 --> 00:51:25,200 close it but you always always 1152 00:51:22,800 --> 00:51:27,780 have to do that you cannot just leave it 1153 00:51:25,200 --> 00:51:30,059 like this notice it right away 1154 00:51:27,780 --> 00:51:31,920 shows this red squiggly align and that 1155 00:51:30,059 --> 00:51:34,200 means that there is error in our 1156 00:51:31,920 --> 00:51:37,200 application and once we save it we 1157 00:51:34,200 --> 00:51:39,300 should see my first component in the 1158 00:51:37,200 --> 00:51:41,700 browser now if you don't just go back 1159 00:51:39,300 --> 00:51:43,559 here and refresh since we're messing 1160 00:51:41,700 --> 00:51:46,500 with the source folder and all that 1161 00:51:43,559 --> 00:51:47,640 maybe create react app just needs a 1162 00:51:46,500 --> 00:51:51,000 little nudge 1163 00:51:47,640 --> 00:51:53,819 now if for some weird reason you're 1164 00:51:51,000 --> 00:51:57,480 still stuck on this error don't panic 1165 00:51:53,819 --> 00:52:00,780 since we tinkered with index.js we just 1166 00:51:57,480 --> 00:52:03,780 need to give create react app A little 1167 00:52:00,780 --> 00:52:07,140 nudge basically we need to restart the 1168 00:52:03,780 --> 00:52:10,680 server so navigate to your integrated 1169 00:52:07,140 --> 00:52:13,440 terminal press Ctrl C which is going to 1170 00:52:10,680 --> 00:52:17,579 stop the server and then restart the 1171 00:52:13,440 --> 00:52:18,900 server with npm start again navigate to 1172 00:52:17,579 --> 00:52:22,200 the terminal 1173 00:52:18,900 --> 00:52:24,960 press Ctrl C which is going to stop the 1174 00:52:22,200 --> 00:52:26,940 server and then just restart the server 1175 00:52:24,960 --> 00:52:29,940 with npm 1176 00:52:26,940 --> 00:52:32,819 start and at the very end of the video I 1177 00:52:29,940 --> 00:52:35,220 just want to showcase the casing so if I 1178 00:52:32,819 --> 00:52:37,559 go here and if I let's say go with 1179 00:52:35,220 --> 00:52:41,339 greeting and then greeting over here 1180 00:52:37,559 --> 00:52:43,319 again the same deal we'll have an error 1181 00:52:41,339 --> 00:52:45,780 notice I have nothing on the screen and 1182 00:52:43,319 --> 00:52:48,180 if I open developer tools and warning 1183 00:52:45,780 --> 00:52:50,460 with that greeting blah blah blah blah 1184 00:52:48,180 --> 00:52:52,140 blah so again we don't want to do that 1185 00:52:50,460 --> 00:52:54,540 we always want to start with capital 1186 00:52:52,140 --> 00:52:57,420 letter and once I fix the casing of 1187 00:52:54,540 --> 00:52:59,339 course everything works as expected 1188 00:52:57,420 --> 00:53:02,040 we're done setting up our first 1189 00:52:59,339 --> 00:53:03,900 component if there's one thing I want 1190 00:53:02,040 --> 00:53:07,740 you to take away from this video 1191 00:53:03,900 --> 00:53:09,180 every time you think of component think 1192 00:53:07,740 --> 00:53:13,020 of function 1193 00:53:09,180 --> 00:53:14,940 that's it if you do that you'll be solid 1194 00:53:13,020 --> 00:53:17,099 and once we're done with our first 1195 00:53:14,940 --> 00:53:19,859 component I think this is an excellent 1196 00:53:17,099 --> 00:53:22,920 time for me to show you 1197 00:53:19,859 --> 00:53:26,579 the extensions I use as well as my 1198 00:53:22,920 --> 00:53:29,160 settings Json setup and first let's 1199 00:53:26,579 --> 00:53:32,400 start with two easy ones so the ones 1200 00:53:29,160 --> 00:53:35,220 that don't require much explanation and 1201 00:53:32,400 --> 00:53:38,099 that is auto rename tag and highlight 1202 00:53:35,220 --> 00:53:41,460 matching tag so if you want to take a 1203 00:53:38,099 --> 00:53:45,359 look at those extensions so that's how 1204 00:53:41,460 --> 00:53:47,579 the auto or name tag looks like and then 1205 00:53:45,359 --> 00:53:49,920 the Highlight one looks like this 1206 00:53:47,579 --> 00:53:51,359 essentially you just go search for 1207 00:53:49,920 --> 00:53:54,180 extensions 1208 00:53:51,359 --> 00:53:58,619 you install it in some cases you might 1209 00:53:54,180 --> 00:54:00,960 need to restart the text error and after 1210 00:53:58,619 --> 00:54:04,319 that you're pretty much good to go as 1211 00:54:00,960 --> 00:54:07,160 far as the auto rename tag if I go here 1212 00:54:04,319 --> 00:54:09,839 and let's say select a setting two 1213 00:54:07,160 --> 00:54:12,900 notice right away I'm changing the 1214 00:54:09,839 --> 00:54:14,700 values in the opening one as well as the 1215 00:54:12,900 --> 00:54:18,480 closing one so if I go here with heading 1216 00:54:14,700 --> 00:54:21,119 3 and save yep I'll have the heading 1217 00:54:18,480 --> 00:54:24,599 three now in the browser now when it 1218 00:54:21,119 --> 00:54:26,579 comes to the matching tags this is 1219 00:54:24,599 --> 00:54:29,640 technically optional you don't have to 1220 00:54:26,579 --> 00:54:32,520 do it but I just prefer when I select 1221 00:54:29,640 --> 00:54:36,180 the element I can right away see both of 1222 00:54:32,520 --> 00:54:39,960 these tags and again the extension is 1223 00:54:36,180 --> 00:54:41,700 this one now once you install 1224 00:54:39,960 --> 00:54:43,980 probably your results are going to be 1225 00:54:41,700 --> 00:54:47,940 tiny bit different just because I 1226 00:54:43,980 --> 00:54:49,920 customized the way it looks so let me go 1227 00:54:47,940 --> 00:54:50,880 here to the settings Json and showcase 1228 00:54:49,920 --> 00:54:52,140 that 1229 00:54:50,880 --> 00:54:54,960 and again 1230 00:54:52,140 --> 00:54:56,520 remember repo I shared that's where you 1231 00:54:54,960 --> 00:54:58,079 can get the code so let's keep on 1232 00:54:56,520 --> 00:54:58,859 scrolling and notice over here I have 1233 00:54:58,079 --> 00:55:02,460 this 1234 00:54:58,859 --> 00:55:05,640 highlight matching tag Styles and then I 1235 00:55:02,460 --> 00:55:07,500 added these values and essentially if 1236 00:55:05,640 --> 00:55:10,380 you want to know your options just 1237 00:55:07,500 --> 00:55:13,260 navigate to the docs of the extension 1238 00:55:10,380 --> 00:55:14,940 essentially in there they provide all of 1239 00:55:13,260 --> 00:55:18,359 the details 1240 00:55:14,940 --> 00:55:21,119 so those are the easy ones now let's 1241 00:55:18,359 --> 00:55:22,079 actually start working with some heavy 1242 00:55:21,119 --> 00:55:24,660 lifters 1243 00:55:22,079 --> 00:55:27,540 first one is prettier which is really 1244 00:55:24,660 --> 00:55:30,660 awesome because it formats the code 1245 00:55:27,540 --> 00:55:33,000 so if I go back to index.js and if I do 1246 00:55:30,660 --> 00:55:34,619 something silly like this again there's 1247 00:55:33,000 --> 00:55:36,660 technically nothing wrong with my code 1248 00:55:34,619 --> 00:55:39,119 but imagine 1249 00:55:36,660 --> 00:55:41,700 if all my files look like this 1250 00:55:39,119 --> 00:55:44,760 I mean you would have to agree that it's 1251 00:55:41,700 --> 00:55:45,599 somewhat annoying so once I save check 1252 00:55:44,760 --> 00:55:48,300 it out 1253 00:55:45,599 --> 00:55:51,480 everything is back 1254 00:55:48,300 --> 00:55:55,559 to tip top shape why well because I use 1255 00:55:51,480 --> 00:55:58,680 this extension by the name of prettier 1256 00:55:55,559 --> 00:56:02,280 once you install this extension you'll 1257 00:55:58,680 --> 00:56:06,300 also need to add some modifications to 1258 00:56:02,280 --> 00:56:08,579 the settings Json so let me showcase 1259 00:56:06,300 --> 00:56:11,400 that as far as the settings we have two 1260 00:56:08,579 --> 00:56:12,839 options so essentially we have this the 1261 00:56:11,400 --> 00:56:14,819 GUI 1262 00:56:12,839 --> 00:56:17,339 this is where we can basically see and 1263 00:56:14,819 --> 00:56:19,800 then we have settings Json where 1264 00:56:17,339 --> 00:56:22,079 essentially we add rules 1265 00:56:19,800 --> 00:56:24,300 to the Json file 1266 00:56:22,079 --> 00:56:27,359 now as far as the settings the ones that 1267 00:56:24,300 --> 00:56:30,359 I prefer are format 1268 00:56:27,359 --> 00:56:33,720 and then I have format on paste and 1269 00:56:30,359 --> 00:56:35,520 format on Save and also very very 1270 00:56:33,720 --> 00:56:38,339 important otherwise it's not going to 1271 00:56:35,520 --> 00:56:41,099 work you wanna 1272 00:56:38,339 --> 00:56:43,800 set up your default formatter as a 1273 00:56:41,099 --> 00:56:47,839 pre-year so let me go back over here 1274 00:56:43,800 --> 00:56:50,880 and then we want to go with default 1275 00:56:47,839 --> 00:56:52,559 format yep over here notice I selected 1276 00:56:50,880 --> 00:56:55,200 prayer so this is what you need to do 1277 00:56:52,559 --> 00:56:56,339 most likely you'll have this none and if 1278 00:56:55,200 --> 00:56:59,339 that's the case it's not going to work 1279 00:56:56,339 --> 00:57:00,900 even after you install it and then once 1280 00:56:59,339 --> 00:57:04,140 you select this once you go with 1281 00:57:00,900 --> 00:57:08,339 prettier in settings Json 1282 00:57:04,140 --> 00:57:09,780 essentially you will have no that's for 1283 00:57:08,339 --> 00:57:11,640 the Emma that's not what I want to do 1284 00:57:09,780 --> 00:57:14,280 with my showcase 3M is probably going to 1285 00:57:11,640 --> 00:57:17,700 be easier you'll see editor for my own 1286 00:57:14,280 --> 00:57:20,400 paste format on Save yada yada then we 1287 00:57:17,700 --> 00:57:23,040 have this one the default formatter and 1288 00:57:20,400 --> 00:57:24,960 then these are just the rules that I 1289 00:57:23,040 --> 00:57:27,119 applied again you can search for them 1290 00:57:24,960 --> 00:57:30,180 here in the GUI or you can add them 1291 00:57:27,119 --> 00:57:31,740 directly here in settings Json so as you 1292 00:57:30,180 --> 00:57:34,920 can see my preference is the single 1293 00:57:31,740 --> 00:57:37,619 quote and I don't use the semicolons so 1294 00:57:34,920 --> 00:57:40,440 if I change that around of course 1295 00:57:37,619 --> 00:57:42,240 allow the double quotes and I'll have 1296 00:57:40,440 --> 00:57:44,040 the semicolons 1297 00:57:42,240 --> 00:57:47,520 this is totally 1298 00:57:44,040 --> 00:57:49,559 again up to you you don't have to use 1299 00:57:47,520 --> 00:57:52,440 the same settings 1300 00:57:49,559 --> 00:57:55,319 then we've got Emmett so throughout the 1301 00:57:52,440 --> 00:57:57,240 course you'll see me 1302 00:57:55,319 --> 00:58:00,119 essentially do something like this where 1303 00:57:57,240 --> 00:58:02,819 I go with heading 2 and let's say if I 1304 00:58:00,119 --> 00:58:05,700 want to go with an ID I go with the 1305 00:58:02,819 --> 00:58:07,920 hashtag and I'm going to call this I 1306 00:58:05,700 --> 00:58:10,859 don't know something something over here 1307 00:58:07,920 --> 00:58:13,079 and then if let's say I want to add a 1308 00:58:10,859 --> 00:58:15,660 class I'm going to go with DOT so this 1309 00:58:13,079 --> 00:58:17,339 adds a class and again this is going to 1310 00:58:15,660 --> 00:58:19,200 be some value 1311 00:58:17,339 --> 00:58:21,359 and check it out I right away have this 1312 00:58:19,200 --> 00:58:24,420 option where I simply need to press 1313 00:58:21,359 --> 00:58:25,440 return or the Tab Key at least that's my 1314 00:58:24,420 --> 00:58:27,960 setup 1315 00:58:25,440 --> 00:58:30,119 in some cases students have said that 1316 00:58:27,960 --> 00:58:32,520 they only can use tab in some cases they 1317 00:58:30,119 --> 00:58:34,079 say that they can only use return again 1318 00:58:32,520 --> 00:58:36,420 that's something that you need to check 1319 00:58:34,079 --> 00:58:39,720 in my case I can use either of them and 1320 00:58:36,420 --> 00:58:42,780 notice the moment I press the enter or 1321 00:58:39,720 --> 00:58:45,680 tab I right away have this value now 1322 00:58:42,780 --> 00:58:49,140 we'll discuss why this is a class name 1323 00:58:45,680 --> 00:58:51,720 not a simple class a little bit later 1324 00:58:49,140 --> 00:58:54,240 don't focus on that one but I'm simply 1325 00:58:51,720 --> 00:58:56,720 trying to say is that 1326 00:58:54,240 --> 00:58:58,799 in order to speed up development 1327 00:58:56,720 --> 00:59:02,460 effectively I'm not going to type the 1328 00:58:58,799 --> 00:59:04,020 opening tag and the closing tag 1329 00:59:02,460 --> 00:59:06,480 essentially I'm just going to type what 1330 00:59:04,020 --> 00:59:07,740 element I want to add and then e-file 1331 00:59:06,480 --> 00:59:09,299 use some classes I'm just going to go 1332 00:59:07,740 --> 00:59:12,240 Dot and then the class name the class 1333 00:59:09,299 --> 00:59:14,220 name or the ID now these are not going 1334 00:59:12,240 --> 00:59:16,500 to be that often but classes for sure 1335 00:59:14,220 --> 00:59:19,619 will do this way so we go with heading 2 1336 00:59:16,500 --> 00:59:22,200 and now let me again type my first and 1337 00:59:19,619 --> 00:59:25,460 then component 1338 00:59:22,200 --> 00:59:29,700 and essentially this is done by Emmett 1339 00:59:25,460 --> 00:59:32,700 and Emmett comes by default 1340 00:59:29,700 --> 00:59:35,160 in vs code so it's right away available 1341 00:59:32,700 --> 00:59:39,119 however 1342 00:59:35,160 --> 00:59:42,660 if we want Emma to work 1343 00:59:39,119 --> 00:59:45,000 in react we need to add this code 1344 00:59:42,660 --> 00:59:47,819 to our Json 1345 00:59:45,000 --> 00:59:51,420 now you can also search for it in the 1346 00:59:47,819 --> 00:59:52,559 GUI but to tell honestly I find this 1347 00:59:51,420 --> 00:59:55,200 approach 1348 00:59:52,559 --> 00:59:58,859 more straightforward or basically you go 1349 00:59:55,200 --> 01:00:02,040 with press code and once you add it 1350 00:59:58,859 --> 01:00:05,520 you're going to be good to go if you 1351 01:00:02,040 --> 01:00:07,559 want you can copy it from my readme or 1352 01:00:05,520 --> 01:00:10,140 you can just pause the video and type it 1353 01:00:07,559 --> 01:00:11,819 that's really up to you just make sure 1354 01:00:10,140 --> 01:00:13,440 that you have this code there's the 1355 01:00:11,819 --> 01:00:15,599 Showcase that I'm not making this up 1356 01:00:13,440 --> 01:00:18,299 I'll try to find it yep it's over here 1357 01:00:15,599 --> 01:00:20,940 so I'm at include languages and we want 1358 01:00:18,299 --> 01:00:23,880 to add JavaScript and then JavaScript 1359 01:00:20,940 --> 01:00:28,559 react and once you do that Emmett is 1360 01:00:23,880 --> 01:00:32,220 going to work in react effectively 1361 01:00:28,559 --> 01:00:35,359 in our component and I guess the last 1362 01:00:32,220 --> 01:00:38,940 thing I want to cover in this video is 1363 01:00:35,359 --> 01:00:41,000 the awesome snippet extension and this 1364 01:00:38,940 --> 01:00:43,920 is super super super 1365 01:00:41,000 --> 01:00:46,440 helpful extension uh you're looking for 1366 01:00:43,920 --> 01:00:49,440 this one now I'm too lazy to say the 1367 01:00:46,440 --> 01:00:52,319 whole name just look for es7 and then 1368 01:00:49,440 --> 01:00:54,680 somewhere there you'll see Snippets and 1369 01:00:52,319 --> 01:00:56,540 essentially this 1370 01:00:54,680 --> 01:01:01,559 allows us 1371 01:00:56,540 --> 01:01:03,359 to set up our components really fast so 1372 01:01:01,559 --> 01:01:05,819 remember I said this is going to be our 1373 01:01:03,359 --> 01:01:09,119 typical component 1374 01:01:05,819 --> 01:01:10,859 we don't have to type it ourselves and 1375 01:01:09,119 --> 01:01:13,500 we haven't covered 1376 01:01:10,859 --> 01:01:16,380 the exports and imports and all that but 1377 01:01:13,500 --> 01:01:19,619 I'll showcase the typical setup so let's 1378 01:01:16,380 --> 01:01:21,180 navigate to the source and we want to 1379 01:01:19,619 --> 01:01:22,680 create a new file so every time we'll 1380 01:01:21,180 --> 01:01:24,480 need a new component 1381 01:01:22,680 --> 01:01:26,339 we'll create a new file because 1382 01:01:24,480 --> 01:01:30,059 components sometimes can be really big 1383 01:01:26,339 --> 01:01:32,339 again this is really up to you if you 1384 01:01:30,059 --> 01:01:35,700 want to jam all your components in one 1385 01:01:32,339 --> 01:01:38,280 file who am I to judge you so let's go 1386 01:01:35,700 --> 01:01:40,260 here with new file and we can go with 1387 01:01:38,280 --> 01:01:41,880 uppercase or we can go with lowercase 1388 01:01:40,260 --> 01:01:43,020 just remember that the extension 1389 01:01:41,880 --> 01:01:45,240 essentially 1390 01:01:43,020 --> 01:01:47,520 is going to 1391 01:01:45,240 --> 01:01:51,059 set up the component 1392 01:01:47,520 --> 01:01:53,280 based on file name so I'm in a testing I 1393 01:01:51,059 --> 01:01:56,460 have installed this extension so 1394 01:01:53,280 --> 01:01:58,559 everything's in play I don't need to 1395 01:01:56,460 --> 01:02:02,040 add anything 1396 01:01:58,559 --> 01:02:04,980 to the settings Json pretty much once 1397 01:02:02,040 --> 01:02:06,900 your install you're good to go and then 1398 01:02:04,980 --> 01:02:08,819 you'll see in the dark that they have 1399 01:02:06,900 --> 01:02:10,980 tons and tons and tons and tons of 1400 01:02:08,819 --> 01:02:12,240 Snippets that they provide and 1401 01:02:10,980 --> 01:02:13,380 essentially you just want to click on 1402 01:02:12,240 --> 01:02:17,460 Snippets 1403 01:02:13,380 --> 01:02:19,140 and then be prepared to spend the rest 1404 01:02:17,460 --> 01:02:20,940 of the day going through those Snippets 1405 01:02:19,140 --> 01:02:22,500 so I'm not going to cover all of them of 1406 01:02:20,940 --> 01:02:25,980 course you can 1407 01:02:22,500 --> 01:02:28,440 spend some time in the darks what we're 1408 01:02:25,980 --> 01:02:29,400 gonna use pretty much all throughout the 1409 01:02:28,440 --> 01:02:33,299 course 1410 01:02:29,400 --> 01:02:35,540 r a f c e which is a arrow function 1411 01:02:33,299 --> 01:02:39,420 right away with export and then 1412 01:02:35,540 --> 01:02:41,700 rfce which is a regular function with 1413 01:02:39,420 --> 01:02:43,980 the export so if we go to the testing 1414 01:02:41,700 --> 01:02:46,920 once we have installed the extension 1415 01:02:43,980 --> 01:02:48,660 notice I have essentially right away 1416 01:02:46,920 --> 01:02:52,440 these Snippets so this is going to 1417 01:02:48,660 --> 01:02:55,859 create a component as a arrow function 1418 01:02:52,440 --> 01:02:58,859 and notice how the names match 1419 01:02:55,859 --> 01:03:03,359 and if I go with the other one 1420 01:02:58,859 --> 01:03:04,799 if I go here with the r 1421 01:03:03,359 --> 01:03:07,500 f 1422 01:03:04,799 --> 01:03:09,480 c e this is going to create a regular 1423 01:03:07,500 --> 01:03:11,880 function now don't let this fool you 1424 01:03:09,480 --> 01:03:13,020 notice over here how we're using the 1425 01:03:11,880 --> 01:03:16,559 lowercase 1426 01:03:13,020 --> 01:03:18,780 it's only going to work because we will 1427 01:03:16,559 --> 01:03:19,920 import it and we'll set it up with the 1428 01:03:18,780 --> 01:03:22,559 uppercase 1429 01:03:19,920 --> 01:03:25,200 and therefore for the most part you'll 1430 01:03:22,559 --> 01:03:27,540 see me essentially setting up files with 1431 01:03:25,200 --> 01:03:30,000 the uppercase right away so that way I 1432 01:03:27,540 --> 01:03:31,980 know that there are going to be no issue 1433 01:03:30,000 --> 01:03:34,859 now one last thing that I want to 1434 01:03:31,980 --> 01:03:39,299 mention about this particular extension 1435 01:03:34,859 --> 01:03:43,319 is that once you initially install it 1436 01:03:39,299 --> 01:03:45,780 most likely you'll see this import for 1437 01:03:43,319 --> 01:03:48,480 react on top of the file and I think 1438 01:03:45,780 --> 01:03:52,020 probably is going to be faster if I just 1439 01:03:48,480 --> 01:03:53,880 showcase the settings one so 1440 01:03:52,020 --> 01:03:56,280 let's navigate to the settings we're 1441 01:03:53,880 --> 01:03:58,859 looking for react Snippets and we have 1442 01:03:56,280 --> 01:04:00,780 this import react so let me navigate 1443 01:03:58,859 --> 01:04:03,859 there so settings here 1444 01:04:00,780 --> 01:04:07,020 then we want to go with react Snippets 1445 01:04:03,859 --> 01:04:08,220 and notice over here how my one is 1446 01:04:07,020 --> 01:04:10,740 unchecked 1447 01:04:08,220 --> 01:04:12,299 most likely once you install it it's 1448 01:04:10,740 --> 01:04:14,760 going to be checked 1449 01:04:12,299 --> 01:04:17,579 so in here notice it says that we need 1450 01:04:14,760 --> 01:04:21,359 to restart vs code and all that 1451 01:04:17,579 --> 01:04:23,099 and effectively what's going to happen 1452 01:04:21,359 --> 01:04:24,780 pretty much every time you run the 1453 01:04:23,099 --> 01:04:27,660 snippet again doesn't really matter 1454 01:04:24,780 --> 01:04:31,380 which one you'll get this import react 1455 01:04:27,660 --> 01:04:35,099 from react now that used to be 1456 01:04:31,380 --> 01:04:37,040 the older syntax so prior to react 1457 01:04:35,099 --> 01:04:40,440 version 17 1458 01:04:37,040 --> 01:04:43,859 we had to also right away import react 1459 01:04:40,440 --> 01:04:45,960 we don't have to do that anymore now if 1460 01:04:43,859 --> 01:04:48,240 you have the import 1461 01:04:45,960 --> 01:04:49,500 there's nothing wrong with that 1462 01:04:48,240 --> 01:04:51,540 so 1463 01:04:49,500 --> 01:04:53,040 the code is not going to be wrong just 1464 01:04:51,540 --> 01:04:55,319 because you have the import 1465 01:04:53,040 --> 01:04:58,799 but since technically we don't need to 1466 01:04:55,319 --> 01:05:01,980 import essentially we can set up our 1467 01:04:58,799 --> 01:05:04,319 Snippets extension to avoid importing 1468 01:05:01,980 --> 01:05:05,339 react for pretty much every component 1469 01:05:04,319 --> 01:05:08,760 and again 1470 01:05:05,339 --> 01:05:10,980 if you navigate to settings GUI look for 1471 01:05:08,760 --> 01:05:13,380 react Snippets and then you just need to 1472 01:05:10,980 --> 01:05:16,500 uncheck it yep again we'll have to 1473 01:05:13,380 --> 01:05:18,299 restart the vs code and just to Showcase 1474 01:05:16,500 --> 01:05:20,339 that I'm not making this up I'm going to 1475 01:05:18,299 --> 01:05:23,700 remove the testing one 1476 01:05:20,339 --> 01:05:24,839 let me create a new file I'll say test 1477 01:05:23,700 --> 01:05:29,040 over here 1478 01:05:24,839 --> 01:05:30,420 JS and now let me run the snippet I 1479 01:05:29,040 --> 01:05:33,720 actually prefer 1480 01:05:30,420 --> 01:05:35,339 the arrow function syntax so most likely 1481 01:05:33,720 --> 01:05:37,799 throughout the course 1482 01:05:35,339 --> 01:05:40,079 that's the syntax that I'm going to use 1483 01:05:37,799 --> 01:05:42,299 and now check it out I don't have any 1484 01:05:40,079 --> 01:05:45,599 import for react again there's nothing 1485 01:05:42,299 --> 01:05:48,359 wrong if you do but I just showcased how 1486 01:05:45,599 --> 01:05:51,780 we can remove that and also my component 1487 01:05:48,359 --> 01:05:53,579 name matches exactly to my file name so 1488 01:05:51,780 --> 01:05:56,940 those are my extensions as well as 1489 01:05:53,579 --> 01:06:00,480 settings Json setup in the long run it 1490 01:05:56,940 --> 01:06:03,660 will greatly speed up your workflow 1491 01:06:00,480 --> 01:06:07,260 and now we're ready to dive back into 1492 01:06:03,660 --> 01:06:09,599 the awesome world of react all right and 1493 01:06:07,260 --> 01:06:12,900 up next let's see how I react 1494 01:06:09,599 --> 01:06:16,140 create jsx under the hood 1495 01:06:12,900 --> 01:06:17,819 and first let me just reiterate the main 1496 01:06:16,140 --> 01:06:18,839 rules that we want to start with capital 1497 01:06:17,819 --> 01:06:21,240 letter 1498 01:06:18,839 --> 01:06:23,579 as far as our component and we must 1499 01:06:21,240 --> 01:06:24,900 return something now it looks like in 1500 01:06:23,579 --> 01:06:27,359 the latest version 1501 01:06:24,900 --> 01:06:29,039 if we don't return anything it doesn't 1502 01:06:27,359 --> 01:06:31,920 throw an error 1503 01:06:29,039 --> 01:06:34,559 but it used to do that in the previous 1504 01:06:31,920 --> 01:06:36,599 versions however if you're not returning 1505 01:06:34,559 --> 01:06:39,000 anything from component it kind of 1506 01:06:36,599 --> 01:06:42,539 defeats the entire purpose for the 1507 01:06:39,000 --> 01:06:45,900 component and as far as the jsx if you 1508 01:06:42,539 --> 01:06:47,819 find the whole syntax super weird let me 1509 01:06:45,900 --> 01:06:50,280 just reiterate that 1510 01:06:47,819 --> 01:06:51,599 the whole idea is to make our lives 1511 01:06:50,280 --> 01:06:54,720 easier 1512 01:06:51,599 --> 01:06:57,539 technically if we want to we can call 1513 01:06:54,720 --> 01:06:59,760 create element which essentially 1514 01:06:57,539 --> 01:07:02,640 is what the react is doing under the 1515 01:06:59,760 --> 01:07:04,640 hood but I'm pretty sure that by the end 1516 01:07:02,640 --> 01:07:07,859 of this video 1517 01:07:04,640 --> 01:07:11,700 you'll probably agree with me that using 1518 01:07:07,859 --> 01:07:14,520 jsx is a little bit easier so let's 1519 01:07:11,700 --> 01:07:16,500 navigate to index.js and this video 1520 01:07:14,520 --> 01:07:20,099 might be a little bit annoying because 1521 01:07:16,500 --> 01:07:21,839 I'll comment this one out the first one 1522 01:07:20,099 --> 01:07:24,539 since I don't wanna 1523 01:07:21,839 --> 01:07:26,520 essentially create two separate 1524 01:07:24,539 --> 01:07:29,579 components and then change the values 1525 01:07:26,520 --> 01:07:33,660 here as well so let me showcase how we 1526 01:07:29,579 --> 01:07:36,319 can create a component by calling react 1527 01:07:33,660 --> 01:07:39,240 dot create element because remember 1528 01:07:36,319 --> 01:07:41,460 react is a JavaScript library so 1529 01:07:39,240 --> 01:07:43,799 essentially under the hood we are using 1530 01:07:41,460 --> 01:07:47,940 JavaScript we are calling this method 1531 01:07:43,799 --> 01:07:50,400 but in order to make our lives easier we 1532 01:07:47,940 --> 01:07:52,740 can actually type here jsx 1533 01:07:50,400 --> 01:07:56,280 so first let me just 1534 01:07:52,740 --> 01:07:58,140 copy this comment this one out and let 1535 01:07:56,280 --> 01:07:59,039 me showcase that I can go here with 1536 01:07:58,140 --> 01:08:02,940 return 1537 01:07:59,039 --> 01:08:05,220 and I can type react Dot and then create 1538 01:08:02,940 --> 01:08:06,839 element and then here we need to provide 1539 01:08:05,220 --> 01:08:09,480 three things 1540 01:08:06,839 --> 01:08:11,819 what element we want to create 1541 01:08:09,480 --> 01:08:14,099 perhaps which we haven't covered so for 1542 01:08:11,819 --> 01:08:16,440 now we'll just pass in empty object and 1543 01:08:14,099 --> 01:08:18,480 then what value is going to be inside of 1544 01:08:16,440 --> 01:08:21,719 that element so let's try this one out 1545 01:08:18,480 --> 01:08:25,319 I'm going to go with heading 2. or pass 1546 01:08:21,719 --> 01:08:29,219 in the empty props empty object and I'll 1547 01:08:25,319 --> 01:08:31,380 say the typical hello world let me save 1548 01:08:29,219 --> 01:08:33,779 it and everything works 1549 01:08:31,380 --> 01:08:36,239 okay well that's awesome 1550 01:08:33,779 --> 01:08:39,000 but keep in mind that 1551 01:08:36,239 --> 01:08:41,460 our components are not going to be as 1552 01:08:39,000 --> 01:08:44,580 straightforward as this one what if 1553 01:08:41,460 --> 01:08:46,920 let's say I want a div and then inside 1554 01:08:44,580 --> 01:08:49,620 of the div I'm gonna have the heading 2. 1555 01:08:46,920 --> 01:08:51,179 like I said this video 1556 01:08:49,620 --> 01:08:53,339 is going to be a little bit more 1557 01:08:51,179 --> 01:08:55,859 annoying than the usual ones because 1558 01:08:53,339 --> 01:08:57,960 I'll toggle them back and forth 1559 01:08:55,859 --> 01:09:00,420 so if I want to do that I can simply go 1560 01:08:57,960 --> 01:09:04,140 here with a div 1561 01:09:00,420 --> 01:09:06,779 so so let me type the div tag and then 1562 01:09:04,140 --> 01:09:08,699 I'll place that heading 2 inside of it 1563 01:09:06,779 --> 01:09:11,759 so let me take this one out 1564 01:09:08,699 --> 01:09:14,339 set it up and of course technically 1565 01:09:11,759 --> 01:09:15,839 since it's a div we don't see that but I 1566 01:09:14,339 --> 01:09:19,140 can guarantee you 1567 01:09:15,839 --> 01:09:21,179 that the element exists 1568 01:09:19,140 --> 01:09:23,699 so if we take a look at the root notice 1569 01:09:21,179 --> 01:09:25,199 so I do have the div and then I have the 1570 01:09:23,699 --> 01:09:27,839 heading to 1571 01:09:25,199 --> 01:09:31,380 so the next question is well how we can 1572 01:09:27,839 --> 01:09:32,880 set this one up with create element 1573 01:09:31,380 --> 01:09:34,380 well 1574 01:09:32,880 --> 01:09:36,799 check it out 1575 01:09:34,380 --> 01:09:39,960 we need to go to react create element 1576 01:09:36,799 --> 01:09:42,000 then since the parent now is div that's 1577 01:09:39,960 --> 01:09:43,440 what we're creating again same deal with 1578 01:09:42,000 --> 01:09:44,640 the props don't worry about them just 1579 01:09:43,440 --> 01:09:46,859 empty object 1580 01:09:44,640 --> 01:09:50,400 and then since the heading 2 is sitting 1581 01:09:46,859 --> 01:09:52,739 inside of it again we type react dot 1582 01:09:50,400 --> 01:09:55,140 create element and then we pass 1583 01:09:52,739 --> 01:09:56,699 the data and hopefully you see where I'm 1584 01:09:55,140 --> 01:10:00,480 going with this 1585 01:09:56,699 --> 01:10:02,880 I mean you can have maybe 20 50 or 1586 01:10:00,480 --> 01:10:04,140 whatever elements in here in this one 1587 01:10:02,880 --> 01:10:05,400 component 1588 01:10:04,140 --> 01:10:06,780 and what do you think is going to be 1589 01:10:05,400 --> 01:10:11,340 easier 1590 01:10:06,780 --> 01:10:14,219 typing the HTML AKA jsx 1591 01:10:11,340 --> 01:10:16,739 or during these acrobatics now I'm not 1592 01:10:14,219 --> 01:10:18,300 going to set up the code if you want you 1593 01:10:16,739 --> 01:10:21,000 can copy and paste and you'll see that 1594 01:10:18,300 --> 01:10:24,179 yes it still works but you'd have to 1595 01:10:21,000 --> 01:10:26,760 agree with me as weird as it looks the 1596 01:10:24,179 --> 01:10:29,040 first time you see this kind of approach 1597 01:10:26,760 --> 01:10:30,780 in the long run 1598 01:10:29,040 --> 01:10:33,300 it's going to be easier 1599 01:10:30,780 --> 01:10:37,560 and more straightforward than calling 1600 01:10:33,300 --> 01:10:40,920 bunch of react dot create elements 1601 01:10:37,560 --> 01:10:43,080 all right and up next let's talk about 1602 01:10:40,920 --> 01:10:46,739 the jsx rules 1603 01:10:43,080 --> 01:10:49,020 so before we discussed the rules for the 1604 01:10:46,739 --> 01:10:51,600 components and again I'll repeat capital 1605 01:10:49,020 --> 01:10:54,960 letter and we need to return something 1606 01:10:51,600 --> 01:10:57,239 now let's talk about djsx rules so 1607 01:10:54,960 --> 01:10:59,580 essentially the stuff that we are 1608 01:10:57,239 --> 01:11:01,860 returning let's start with this one we 1609 01:10:59,580 --> 01:11:05,520 always always always always always need 1610 01:11:01,860 --> 01:11:07,920 to return a single element basically one 1611 01:11:05,520 --> 01:11:09,540 parent element so if we take a look at 1612 01:11:07,920 --> 01:11:12,300 our return 1613 01:11:09,540 --> 01:11:16,020 we can go here as wild and crazy as we 1614 01:11:12,300 --> 01:11:18,960 want so let's imagine that I want to go 1615 01:11:16,020 --> 01:11:21,960 here with div inside of the div I decide 1616 01:11:18,960 --> 01:11:24,960 that there's going to be a heading 3 and 1617 01:11:21,960 --> 01:11:28,860 in there I'll say hello people after 1618 01:11:24,960 --> 01:11:31,620 that we want to go with another list 1619 01:11:28,860 --> 01:11:33,600 in there there's going to be a list item 1620 01:11:31,620 --> 01:11:36,420 inside of the list item I'm gonna go 1621 01:11:33,600 --> 01:11:39,179 with that href for now I'm just gonna 1622 01:11:36,420 --> 01:11:41,820 add the 1623 01:11:39,179 --> 01:11:44,640 over here don't worry there's probably 1624 01:11:41,820 --> 01:11:47,280 going to be a warning in the console we 1625 01:11:44,640 --> 01:11:48,600 don't really care about it and let's say 1626 01:11:47,280 --> 01:11:50,520 over here 1627 01:11:48,600 --> 01:11:52,679 hello world 1628 01:11:50,520 --> 01:11:54,600 let's save it and as you can see 1629 01:11:52,679 --> 01:11:57,360 everything works there are no issues and 1630 01:11:54,600 --> 01:11:59,880 as I say not I don't want this one over 1631 01:11:57,360 --> 01:12:02,400 here however problems are going to start 1632 01:11:59,880 --> 01:12:04,920 if for some reason I understand that you 1633 01:12:02,400 --> 01:12:07,260 know what right next to this div I need 1634 01:12:04,920 --> 01:12:09,179 to have a heading too so not inside of 1635 01:12:07,260 --> 01:12:12,000 the div but right next to it 1636 01:12:09,179 --> 01:12:14,699 check it out right away I have this red 1637 01:12:12,000 --> 01:12:17,699 squiggly line pretty much running all 1638 01:12:14,699 --> 01:12:21,860 throughout my code why because we cannot 1639 01:12:17,699 --> 01:12:24,960 we cannot return in jsx 1640 01:12:21,860 --> 01:12:27,840 adjacent elements so we always always 1641 01:12:24,960 --> 01:12:29,520 need to return a one parent element so 1642 01:12:27,840 --> 01:12:32,400 what's the solution 1643 01:12:29,520 --> 01:12:34,920 first let me type some kind of code 1644 01:12:32,400 --> 01:12:37,620 I'm gonna go with hello world and 1645 01:12:34,920 --> 01:12:39,420 effectively what we want to do is to 1646 01:12:37,620 --> 01:12:41,460 wrap both of them 1647 01:12:39,420 --> 01:12:44,580 so for now I'm just gonna go with div 1648 01:12:41,460 --> 01:12:47,640 and I'll talk about why maybe it's not 1649 01:12:44,580 --> 01:12:50,400 the best way but let me grab the opening 1650 01:12:47,640 --> 01:12:53,820 and closing tag and check it out the 1651 01:12:50,400 --> 01:12:56,520 moment we save everything works again so 1652 01:12:53,820 --> 01:12:58,440 I know I already said this 1653 01:12:56,520 --> 01:13:00,600 a thousand times we need to return one 1654 01:12:58,440 --> 01:13:02,940 parent element so once we refactor we 1655 01:13:00,600 --> 01:13:05,340 are good to go now this is not a rule 1656 01:13:02,940 --> 01:13:08,280 but keep in mind that 1657 01:13:05,340 --> 01:13:10,679 whatever elements we set up they 1658 01:13:08,280 --> 01:13:13,020 actually live in the browser 1659 01:13:10,679 --> 01:13:14,699 so we are actually returning them it's 1660 01:13:13,020 --> 01:13:17,159 not like we're just making them up in a 1661 01:13:14,699 --> 01:13:20,400 code notice I have div div and then 1662 01:13:17,159 --> 01:13:22,260 heading two so again this is not a rule 1663 01:13:20,400 --> 01:13:24,300 if you're 1664 01:13:22,260 --> 01:13:27,000 favorite element is div you can 1665 01:13:24,300 --> 01:13:28,739 definitely do show who am I to judge but 1666 01:13:27,000 --> 01:13:31,920 I do suggest 1667 01:13:28,739 --> 01:13:34,260 sticking with HTML semantics where we 1668 01:13:31,920 --> 01:13:37,140 can use the section element we can use 1669 01:13:34,260 --> 01:13:40,080 the article and of course whatever 1670 01:13:37,140 --> 01:13:42,960 element you want to create so for nav we 1671 01:13:40,080 --> 01:13:44,340 can use nav footer header and hopefully 1672 01:13:42,960 --> 01:13:48,179 you see where I'm going with this again 1673 01:13:44,340 --> 01:13:51,179 it's not a rule if you want to add I 1674 01:13:48,179 --> 01:13:52,739 don't know 100 divs in your component 1675 01:13:51,179 --> 01:13:54,840 absolutely 1676 01:13:52,739 --> 01:13:55,860 react is not going to complain about it 1677 01:13:54,840 --> 01:13:59,100 however 1678 01:13:55,860 --> 01:14:02,460 as far as maintaining the code it might 1679 01:13:59,100 --> 01:14:05,219 be an issue down the road again just a 1680 01:14:02,460 --> 01:14:08,159 suggestion and also another approach we 1681 01:14:05,219 --> 01:14:10,920 can take if let's say you don't want to 1682 01:14:08,159 --> 01:14:14,400 add any semantic elements and you're 1683 01:14:10,920 --> 01:14:16,020 also maybe not in a fan club of div you 1684 01:14:14,400 --> 01:14:19,920 can add fragment 1685 01:14:16,020 --> 01:14:22,739 so fragment allows us to group elements 1686 01:14:19,920 --> 01:14:24,300 without adding those extra nodes and 1687 01:14:22,739 --> 01:14:27,719 essentially we have two ways that we can 1688 01:14:24,300 --> 01:14:30,060 use that we can go with react Dot and 1689 01:14:27,719 --> 01:14:32,400 then fragment so notice the syntax it's 1690 01:14:30,060 --> 01:14:35,520 kind of like a component opening and 1691 01:14:32,400 --> 01:14:37,260 closing one or there is a shorthand so 1692 01:14:35,520 --> 01:14:39,179 in here we can go with angle brackets 1693 01:14:37,260 --> 01:14:41,219 and we just need to make sure that we 1694 01:14:39,179 --> 01:14:44,580 close it so let's try it out I'm going 1695 01:14:41,219 --> 01:14:46,140 to go back to index.js let's say that I 1696 01:14:44,580 --> 01:14:48,000 don't want to go with Section which is 1697 01:14:46,140 --> 01:14:51,420 also an option of course so once I save 1698 01:14:48,000 --> 01:14:53,340 here notice now the section is returned 1699 01:14:51,420 --> 01:14:55,980 not a div 1700 01:14:53,340 --> 01:14:59,159 but if let's say I'm not a fan of that 1701 01:14:55,980 --> 01:15:02,640 type of approach I can just remove the 1702 01:14:59,159 --> 01:15:05,940 code and I can go to react Dot and then 1703 01:15:02,640 --> 01:15:07,560 fragment so we're importing react that's 1704 01:15:05,940 --> 01:15:10,920 something you'll need to do then if 1705 01:15:07,560 --> 01:15:13,620 that's the case and we go with react dot 1706 01:15:10,920 --> 01:15:15,480 fragment and once we save notice 1707 01:15:13,620 --> 01:15:18,900 that essentially 1708 01:15:15,480 --> 01:15:20,880 there's not that extra div so we have ID 1709 01:15:18,900 --> 01:15:23,179 root 1710 01:15:20,880 --> 01:15:26,100 but we don't have that div 1711 01:15:23,179 --> 01:15:28,260 type of syntax we only have this one div 1712 01:15:26,100 --> 01:15:30,780 that we have over here and like I said 1713 01:15:28,260 --> 01:15:33,719 the shorthand is following just remove 1714 01:15:30,780 --> 01:15:35,340 the stuff and everything is going to 1715 01:15:33,719 --> 01:15:38,820 work 1716 01:15:35,340 --> 01:15:42,900 after that let's talk about the camel 1717 01:15:38,820 --> 01:15:45,140 case property naming convention 1718 01:15:42,900 --> 01:15:49,140 if you're familiar when it comes to 1719 01:15:45,140 --> 01:15:52,620 attributes in the index.html 1720 01:15:49,140 --> 01:15:55,199 you write them in a following way for 1721 01:15:52,620 --> 01:16:00,000 example tab index on click 1722 01:15:55,199 --> 01:16:05,280 for read-only when it comes to jsx 1723 01:16:00,000 --> 01:16:08,460 don't be surprised if you see the camel 1724 01:16:05,280 --> 01:16:11,040 case property naming convention please 1725 01:16:08,460 --> 01:16:13,739 don't worry about this code effectively 1726 01:16:11,040 --> 01:16:16,860 what it's doing the values and all that 1727 01:16:13,739 --> 01:16:19,679 just focus on naming here so instead of 1728 01:16:16,860 --> 01:16:22,739 tab index we're going to type tab index 1729 01:16:19,679 --> 01:16:25,800 as a camel case if we want to use 4 for 1730 01:16:22,739 --> 01:16:28,980 the label in the jsx we'll use html4 1731 01:16:25,800 --> 01:16:30,360 again those are just the rules and of 1732 01:16:28,980 --> 01:16:31,860 course as we're progressing on the 1733 01:16:30,360 --> 01:16:34,620 course every time 1734 01:16:31,860 --> 01:16:37,199 we'll add that chemical case property 1735 01:16:34,620 --> 01:16:38,400 I'll tell you hey this is the rule that 1736 01:16:37,199 --> 01:16:39,480 we need to use and this is just 1737 01:16:38,400 --> 01:16:41,640 something that you need to remember 1738 01:16:39,480 --> 01:16:43,500 again please don't focus on this code 1739 01:16:41,640 --> 01:16:45,900 I'm not going to type it we're going to 1740 01:16:43,500 --> 01:16:47,580 cover all of this in great detail 1741 01:16:45,900 --> 01:16:50,699 essentially while we have those funny 1742 01:16:47,580 --> 01:16:54,179 curly brackets and all that for now just 1743 01:16:50,699 --> 01:16:56,159 don't be surprised if you see this type 1744 01:16:54,179 --> 01:16:59,040 of naming convention 1745 01:16:56,159 --> 01:17:01,140 also speaking of naming conventions we 1746 01:16:59,040 --> 01:17:02,820 don't have class something that I 1747 01:17:01,140 --> 01:17:05,400 already showed you when we work with 1748 01:17:02,820 --> 01:17:06,800 Emmett so if I go back and to this 1749 01:17:05,400 --> 01:17:10,320 device I want to add some kind of class 1750 01:17:06,800 --> 01:17:11,880 it's not going to be class so you can 1751 01:17:10,320 --> 01:17:14,460 type here like this but it's not going 1752 01:17:11,880 --> 01:17:16,140 to work we need to go with class name 1753 01:17:14,460 --> 01:17:18,600 again just the rule that we need to 1754 01:17:16,140 --> 01:17:20,640 remember that's it we need to go with 1755 01:17:18,600 --> 01:17:24,300 last name and then we provide some kind 1756 01:17:20,640 --> 01:17:26,460 of value over here some class name and 1757 01:17:24,300 --> 01:17:29,400 then if we take a look 1758 01:17:26,460 --> 01:17:32,280 somewhere there yep notice 1759 01:17:29,400 --> 01:17:35,460 eventually in the browser 1760 01:17:32,280 --> 01:17:38,400 it's set up as a class but in our code 1761 01:17:35,460 --> 01:17:41,580 it's going to be a class name and up 1762 01:17:38,400 --> 01:17:45,300 next let's discuss how we need to handle 1763 01:17:41,580 --> 01:17:47,820 elements that don't have the closing tag 1764 01:17:45,300 --> 01:17:50,460 so as you know in HTML we have some 1765 01:17:47,820 --> 01:17:53,580 elements that do have closing tags and 1766 01:17:50,460 --> 01:17:56,820 some that don't for example image or 1767 01:17:53,580 --> 01:17:57,840 input and HTML5 has somewhat spoiled as 1768 01:17:56,820 --> 01:17:59,880 well 1769 01:17:57,840 --> 01:18:01,380 if we want we can now omit the 1770 01:17:59,880 --> 01:18:04,560 self-closing and everything is going to 1771 01:18:01,380 --> 01:18:07,560 be rendered just fine however we cannot 1772 01:18:04,560 --> 01:18:10,860 do that in react all the elements that 1773 01:18:07,560 --> 01:18:13,260 don't have the closing tag we need to 1774 01:18:10,860 --> 01:18:16,020 self-close them so essentially you need 1775 01:18:13,260 --> 01:18:18,000 to add this forward slash 1776 01:18:16,020 --> 01:18:19,860 otherwise you'll get an error so if I 1777 01:18:18,000 --> 01:18:23,699 navigate over here and let's say I'm 1778 01:18:19,860 --> 01:18:25,800 gonna go with input now say text if I 1779 01:18:23,699 --> 01:18:27,239 remove the forward slash I'll right away 1780 01:18:25,800 --> 01:18:30,360 get the narrow 1781 01:18:27,239 --> 01:18:33,179 so make sure if you use the element that 1782 01:18:30,360 --> 01:18:37,140 doesn't have the closing tag 1783 01:18:33,179 --> 01:18:39,719 to self close it and lastly let's talk 1784 01:18:37,140 --> 01:18:42,120 about the formatting as you've probably 1785 01:18:39,719 --> 01:18:45,540 noticed 1786 01:18:42,120 --> 01:18:47,820 when it comes to return sometimes 1787 01:18:45,540 --> 01:18:51,900 we have these parentheses 1788 01:18:47,820 --> 01:18:53,940 and sometimes there are no parentheses 1789 01:18:51,900 --> 01:18:57,719 so what's up with that 1790 01:18:53,940 --> 01:18:58,920 well you see parentheses are here to 1791 01:18:57,719 --> 01:19:01,320 help us 1792 01:18:58,920 --> 01:19:04,739 technically you don't need to use them 1793 01:19:01,320 --> 01:19:08,699 so now let me go back to the heading 2. 1794 01:19:04,739 --> 01:19:11,460 and let's say hello world I save and 1795 01:19:08,699 --> 01:19:13,020 everything works however if 1796 01:19:11,460 --> 01:19:15,540 by mistake 1797 01:19:13,020 --> 01:19:18,000 I move this to the next line You'll see 1798 01:19:15,540 --> 01:19:21,000 that it right away gets grayed out 1799 01:19:18,000 --> 01:19:23,580 and essentially we'll have no content in 1800 01:19:21,000 --> 01:19:26,040 the browser so if you're not using 1801 01:19:23,580 --> 01:19:29,219 parentheses you need to make sure 1802 01:19:26,040 --> 01:19:32,880 that your opening tag is in the same 1803 01:19:29,219 --> 01:19:36,719 line as return so set it up here 1804 01:19:32,880 --> 01:19:38,699 and I removed one character and then we 1805 01:19:36,719 --> 01:19:40,679 can do whatever we want so again we save 1806 01:19:38,699 --> 01:19:43,980 it and then of course previous format 1807 01:19:40,679 --> 01:19:46,500 for us if you have parentheses you don't 1808 01:19:43,980 --> 01:19:48,480 need to worry about it and a friendly 1809 01:19:46,500 --> 01:19:50,699 suggestion keep in mind that we are 1810 01:19:48,480 --> 01:19:53,940 using prettier so a lot of times per 1811 01:19:50,699 --> 01:19:55,020 year we'll add them or we'll remove them 1812 01:19:53,940 --> 01:19:57,900 so 1813 01:19:55,020 --> 01:20:00,780 I wouldn't suggest stressing about it 1814 01:19:57,900 --> 01:20:02,760 just something to keep in mind so notice 1815 01:20:00,780 --> 01:20:04,980 over here if I have those parentheses it 1816 01:20:02,760 --> 01:20:07,739 doesn't really matter where the opening 1817 01:20:04,980 --> 01:20:09,719 tag is I save it and prettier removes it 1818 01:20:07,739 --> 01:20:12,300 for me anyway just keep in mind the 1819 01:20:09,719 --> 01:20:15,420 general concept if you want to add them 1820 01:20:12,300 --> 01:20:18,540 add them if not make sure that the 1821 01:20:15,420 --> 01:20:21,060 opening tag is in the same line as the 1822 01:20:18,540 --> 01:20:24,360 return okay and up next let's discuss 1823 01:20:21,060 --> 01:20:25,500 whether we can Nest components and the 1824 01:20:24,360 --> 01:20:28,920 short answer 1825 01:20:25,500 --> 01:20:29,699 is yes and let's also see how we can do 1826 01:20:28,920 --> 01:20:35,100 that 1827 01:20:29,699 --> 01:20:37,920 so if I go back to my index.js 1828 01:20:35,100 --> 01:20:40,560 um let's say I decide that my component 1829 01:20:37,920 --> 01:20:41,580 is going to be more complex I'll add a 1830 01:20:40,560 --> 01:20:44,880 div 1831 01:20:41,580 --> 01:20:47,880 I'll say inside of the dev heading 2 and 1832 01:20:44,880 --> 01:20:50,060 I'll call this John Doe and then right 1833 01:20:47,880 --> 01:20:54,060 below it we're gonna go with paragraph 1834 01:20:50,060 --> 01:20:56,340 This is my message 1835 01:20:54,060 --> 01:20:57,300 and then down Route I decide that you 1836 01:20:56,340 --> 01:20:59,520 know what 1837 01:20:57,300 --> 01:21:02,760 these pieces are reusable 1838 01:20:59,520 --> 01:21:04,620 or I just want to simplify this file I 1839 01:21:02,760 --> 01:21:05,760 don't want to have the setting to and 1840 01:21:04,620 --> 01:21:09,420 paragraph 1841 01:21:05,760 --> 01:21:11,640 how I can set this value in a different 1842 01:21:09,420 --> 01:21:14,040 component and then render that component 1843 01:21:11,640 --> 01:21:17,340 well when you start by creating a 1844 01:21:14,040 --> 01:21:19,560 component and I'll purposely set up 1845 01:21:17,340 --> 01:21:21,300 two Arrow functions one is going to be 1846 01:21:19,560 --> 01:21:24,000 implicit return 1847 01:21:21,300 --> 01:21:25,920 so basically without the curlies and the 1848 01:21:24,000 --> 01:21:28,260 other one is going to be with the curly 1849 01:21:25,920 --> 01:21:30,480 braces just to Showcase that yes of 1850 01:21:28,260 --> 01:21:33,000 course it is possible so let's start 1851 01:21:30,480 --> 01:21:34,980 here with const so that's my first 1852 01:21:33,000 --> 01:21:37,080 component person and this is where I'll 1853 01:21:34,980 --> 01:21:39,540 set up that implicit return I'll say 1854 01:21:37,080 --> 01:21:42,120 Arrow function and essentially what I 1855 01:21:39,540 --> 01:21:44,580 want to return is this 1856 01:21:42,120 --> 01:21:46,440 I'm gonna go with my adding two 1857 01:21:44,580 --> 01:21:48,480 then let's copy and paste and now let's 1858 01:21:46,440 --> 01:21:49,920 set up that message in this case though 1859 01:21:48,480 --> 01:21:53,219 we will go 1860 01:21:49,920 --> 01:21:56,280 without curly so now we do need to 1861 01:21:53,219 --> 01:21:58,260 provide explicitly that return otherwise 1862 01:21:56,280 --> 01:22:01,020 it's not going to work basically we're 1863 01:21:58,260 --> 01:22:03,840 not going to be adding anything here so 1864 01:22:01,020 --> 01:22:06,120 let me move this sucker up let me say 1865 01:22:03,840 --> 01:22:08,100 return and now of course I want to 1866 01:22:06,120 --> 01:22:10,860 change this I'm going to grab 1867 01:22:08,100 --> 01:22:13,860 and I want to copy and paste so I have 1868 01:22:10,860 --> 01:22:17,340 three components now I have the greeting 1869 01:22:13,860 --> 01:22:20,100 I have the person as well as the message 1870 01:22:17,340 --> 01:22:23,400 so how we can nest 1871 01:22:20,100 --> 01:22:25,140 components well we simply need to 1872 01:22:23,400 --> 01:22:27,600 reference the name 1873 01:22:25,140 --> 01:22:30,060 and again same rules apply that's where 1874 01:22:27,600 --> 01:22:33,179 we go here with this Capital case and 1875 01:22:30,060 --> 01:22:34,980 also remember we cannot just place them 1876 01:22:33,179 --> 01:22:37,500 side by side 1877 01:22:34,980 --> 01:22:39,120 so we will have to set up some kind of 1878 01:22:37,500 --> 01:22:40,679 parent now in my case that's div 1879 01:22:39,120 --> 01:22:43,380 remember 1880 01:22:40,679 --> 01:22:46,860 you can go with some other 1881 01:22:43,380 --> 01:22:48,960 element as far as HTML semantics or you 1882 01:22:46,860 --> 01:22:51,000 can go with react fragments so for me 1883 01:22:48,960 --> 01:22:53,100 I'm just going to keep the div basically 1884 01:22:51,000 --> 01:22:54,780 we just want to reference we want to go 1885 01:22:53,100 --> 01:22:58,140 here with person 1886 01:22:54,780 --> 01:23:01,199 and then we also want to display the 1887 01:22:58,140 --> 01:23:03,719 message NES before you ask 1888 01:23:01,199 --> 01:23:08,280 we can Nest another 1889 01:23:03,719 --> 01:23:09,840 50 000 components inside of these ones 1890 01:23:08,280 --> 01:23:11,880 so imagine 1891 01:23:09,840 --> 01:23:13,739 you go here you'll create more 1892 01:23:11,880 --> 01:23:16,199 components you'll Nest them here and 1893 01:23:13,739 --> 01:23:19,440 then all of them are going to be 1894 01:23:16,199 --> 01:23:21,480 displayed eventually in the greeting and 1895 01:23:19,440 --> 01:23:23,280 effectively this is how our applications 1896 01:23:21,480 --> 01:23:25,440 are going to look like 1897 01:23:23,280 --> 01:23:29,400 so have that root one 1898 01:23:25,440 --> 01:23:32,520 so single root component the convention 1899 01:23:29,400 --> 01:23:35,460 is to name it app but you don't have to 1900 01:23:32,520 --> 01:23:36,960 and inside of it we Nest rest of our 1901 01:23:35,460 --> 01:23:39,480 components 1902 01:23:36,960 --> 01:23:40,440 so all the components we need for our 1903 01:23:39,480 --> 01:23:44,040 app 1904 01:23:40,440 --> 01:23:47,219 for example inside of the app we missed 1905 01:23:44,040 --> 01:23:51,360 a page component which contains more 1906 01:23:47,219 --> 01:23:52,500 components navbar sidebar and a header 1907 01:23:51,360 --> 01:23:56,280 now 1908 01:23:52,500 --> 01:23:58,679 those components might or might not have 1909 01:23:56,280 --> 01:24:00,120 their own nested components and 1910 01:23:58,679 --> 01:24:00,960 hopefully you see where I'm going with 1911 01:24:00,120 --> 01:24:03,960 this 1912 01:24:00,960 --> 01:24:08,100 we inject our root component in a div 1913 01:24:03,960 --> 01:24:10,440 with an ID of root in the index.html 1914 01:24:08,100 --> 01:24:14,159 which is located in the public folder 1915 01:24:10,440 --> 01:24:17,120 and as a result our app is nicely 1916 01:24:14,159 --> 01:24:21,620 displayed in a browser regardless 1917 01:24:17,120 --> 01:24:24,540 regardless whether it has 5 or 300 1918 01:24:21,620 --> 01:24:27,659 components okay and up next let me 1919 01:24:24,540 --> 01:24:30,659 showcase a awesome extension for the 1920 01:24:27,659 --> 01:24:35,400 browser which effectively will allow us 1921 01:24:30,659 --> 01:24:38,400 to see our component structure so for 1922 01:24:35,400 --> 01:24:39,900 that I'm actually going to open up a new 1923 01:24:38,400 --> 01:24:43,739 browser 1924 01:24:39,900 --> 01:24:47,159 tab just so you can see it on full 1925 01:24:43,739 --> 01:24:48,659 screen then we want to look for these 1926 01:24:47,159 --> 01:24:52,320 three dots 1927 01:24:48,659 --> 01:24:54,000 more tools and then extensions now in my 1928 01:24:52,320 --> 01:24:57,659 case of course 1929 01:24:54,000 --> 01:24:59,280 the extensions are you present but since 1930 01:24:57,659 --> 01:25:01,320 I wanna 1931 01:24:59,280 --> 01:25:02,219 showcase how we can get one I'll remove 1932 01:25:01,320 --> 01:25:04,800 it 1933 01:25:02,219 --> 01:25:06,900 then we go over here 1934 01:25:04,800 --> 01:25:11,600 Chrome web store 1935 01:25:06,900 --> 01:25:14,940 and let's search for react 1936 01:25:11,600 --> 01:25:17,100 developer and then tools 1937 01:25:14,940 --> 01:25:22,159 get it over here 1938 01:25:17,100 --> 01:25:22,159 yep we want to add it ADD extension 1939 01:25:26,040 --> 01:25:32,340 and once everything is in place now 1940 01:25:28,679 --> 01:25:34,219 let's navigate to a local host 3000 and 1941 01:25:32,340 --> 01:25:36,659 once we do that we can close this tab 1942 01:25:34,219 --> 01:25:39,960 then inspect 1943 01:25:36,659 --> 01:25:42,900 and you'll see this components tab 1944 01:25:39,960 --> 01:25:45,179 once you install the extension you'll 1945 01:25:42,900 --> 01:25:46,860 have this one and now check it out now 1946 01:25:45,179 --> 01:25:50,040 it does complain about the strict mode 1947 01:25:46,860 --> 01:25:52,140 so you'll see this warning over here 1948 01:25:50,040 --> 01:25:54,120 don't worry about it everything works 1949 01:25:52,140 --> 01:25:56,520 fine and we'll discuss the strict mode a 1950 01:25:54,120 --> 01:25:58,980 little bit later so check it out this is 1951 01:25:56,520 --> 01:26:00,780 our root component the greeting again we 1952 01:25:58,980 --> 01:26:03,120 haven't discussed the props and all that 1953 01:26:00,780 --> 01:26:05,820 and functionality don't worry about it 1954 01:26:03,120 --> 01:26:09,480 for now just focus on this 1955 01:26:05,820 --> 01:26:12,060 so we can clearly see our component 1956 01:26:09,480 --> 01:26:15,600 structure and this is super useful 1957 01:26:12,060 --> 01:26:18,000 once your apps become big and complex 1958 01:26:15,600 --> 01:26:20,580 since it's much easier to troubleshoot 1959 01:26:18,000 --> 01:26:22,739 you can literally pinpoint the component 1960 01:26:20,580 --> 01:26:25,980 and then see what is happening inside 1961 01:26:22,739 --> 01:26:28,920 again the extension is react developer 1962 01:26:25,980 --> 01:26:32,699 tools and if you haven't installed it 1963 01:26:28,920 --> 01:26:36,659 already just go to Chrome web store and 1964 01:26:32,699 --> 01:26:39,480 set it up on your browser all right and 1965 01:26:36,659 --> 01:26:41,940 I think at this point we have acquired 1966 01:26:39,480 --> 01:26:45,860 enough knowledge to start working on our 1967 01:26:41,940 --> 01:26:49,500 first project the Amazon best sellers 1968 01:26:45,860 --> 01:26:51,900 effectively a list of best selling books 1969 01:26:49,500 --> 01:26:55,920 and of course in the process we'll cover 1970 01:26:51,900 --> 01:26:58,739 more interesting react topics for 1971 01:26:55,920 --> 01:27:00,179 example how to add CSS how to handle 1972 01:26:58,739 --> 01:27:03,060 images 1973 01:27:00,179 --> 01:27:07,560 what are the props just to name a few 1974 01:27:03,060 --> 01:27:08,639 and our goal is to implement some of the 1975 01:27:07,560 --> 01:27:12,360 features 1976 01:27:08,639 --> 01:27:15,199 from this Amazon page now of course 1977 01:27:12,360 --> 01:27:18,179 We're Not Gonna recreate the whole page 1978 01:27:15,199 --> 01:27:21,120 but the main features will try to 1979 01:27:18,179 --> 01:27:23,880 recreate using react 1980 01:27:21,120 --> 01:27:25,260 and we're going to start actually with 1981 01:27:23,880 --> 01:27:28,440 the structure 1982 01:27:25,260 --> 01:27:31,739 so in the index.js I want you to create 1983 01:27:28,440 --> 01:27:34,260 a book component where technically you 1984 01:27:31,739 --> 01:27:35,280 can call it whatever you want and then 1985 01:27:34,260 --> 01:27:38,699 in there 1986 01:27:35,280 --> 01:27:40,020 let's just set up three components now I 1987 01:27:38,699 --> 01:27:41,820 can tell you right away that later we 1988 01:27:40,020 --> 01:27:44,400 will refactor 1989 01:27:41,820 --> 01:27:46,980 to one component it's only one book 1990 01:27:44,400 --> 01:27:48,900 component we're starting this way just 1991 01:27:46,980 --> 01:27:50,460 because I want to emphasize one more 1992 01:27:48,900 --> 01:27:52,380 time 1993 01:27:50,460 --> 01:27:55,020 the way you set up the application is 1994 01:27:52,380 --> 01:27:55,860 really up to you there is no rule that 1995 01:27:55,020 --> 01:27:59,400 says 1996 01:27:55,860 --> 01:28:01,500 you have to do one way or the other way 1997 01:27:59,400 --> 01:28:03,239 if you want to create component for 1998 01:28:01,500 --> 01:28:05,580 every single element 1999 01:28:03,239 --> 01:28:08,340 you can definitely do so if you want to 2000 01:28:05,580 --> 01:28:11,520 jam your entire application in one 2001 01:28:08,340 --> 01:28:13,980 component that's also an option 2002 01:28:11,520 --> 01:28:16,679 so a lot of times it really comes down 2003 01:28:13,980 --> 01:28:18,960 to a preference so first 2004 01:28:16,679 --> 01:28:21,420 we'll create three components notice 2005 01:28:18,960 --> 01:28:22,679 effectively we're just returning some 2006 01:28:21,420 --> 01:28:25,920 elements here 2007 01:28:22,679 --> 01:28:29,219 and then eventually we will refactor it 2008 01:28:25,920 --> 01:28:31,560 so I have the book component and there I 2009 01:28:29,219 --> 01:28:33,480 have few more components and then I have 2010 01:28:31,560 --> 01:28:35,280 book list component 2011 01:28:33,480 --> 01:28:36,659 which we actually 2012 01:28:35,280 --> 01:28:39,179 render 2013 01:28:36,659 --> 01:28:40,440 so this is the one that we pass into a 2014 01:28:39,179 --> 01:28:42,420 render 2015 01:28:40,440 --> 01:28:45,780 so let's set up this one and then we'll 2016 01:28:42,420 --> 01:28:48,600 get the values from the Amazon so first 2017 01:28:45,780 --> 01:28:50,040 let me navigate to the index one so at 2018 01:28:48,600 --> 01:28:50,760 the moment I have this greeting and all 2019 01:28:50,040 --> 01:28:52,800 that 2020 01:28:50,760 --> 01:28:55,739 I won't need this I'm going to go with 2021 01:28:52,800 --> 01:29:00,179 const and let's start over here with 2022 01:28:55,739 --> 01:29:02,940 book and list so that's my component 2023 01:29:00,179 --> 01:29:05,940 essentially it's going to be a section 2024 01:29:02,940 --> 01:29:08,699 so let's set up a return over here 2025 01:29:05,940 --> 01:29:11,040 and for now it's going to be an empty 2026 01:29:08,699 --> 01:29:13,380 section so there's not going to be 2027 01:29:11,040 --> 01:29:15,780 anything in there then let's remove 2028 01:29:13,380 --> 01:29:19,440 these two and now let's set up that 2029 01:29:15,780 --> 01:29:21,600 component so const book that's the 2030 01:29:19,440 --> 01:29:23,940 component as far as return we're gonna 2031 01:29:21,600 --> 01:29:26,520 go here with an article so we're setting 2032 01:29:23,940 --> 01:29:29,280 up the article for the book and then 2033 01:29:26,520 --> 01:29:31,920 inside of the book component will render 2034 01:29:29,280 --> 01:29:34,560 three more components and also in a 2035 01:29:31,920 --> 01:29:37,860 process I want to showcase that yes 2036 01:29:34,560 --> 01:29:41,219 essentially we can Nest as many levels 2037 01:29:37,860 --> 01:29:43,380 deep as we want so notice in here we 2038 01:29:41,219 --> 01:29:45,659 have book list then book is going to be 2039 01:29:43,380 --> 01:29:48,300 rendered inside of it and then inside of 2040 01:29:45,659 --> 01:29:50,219 the book we'll have three more 2041 01:29:48,300 --> 01:29:52,440 components and let's start I guess over 2042 01:29:50,219 --> 01:29:54,600 here with an image so I'm gonna go here 2043 01:29:52,440 --> 01:29:57,060 and since we don't have the values yet 2044 01:29:54,600 --> 01:29:59,219 I'm going to provide the heading 2 but 2045 01:29:57,060 --> 01:30:01,860 eventually of course it's going to be a 2046 01:29:59,219 --> 01:30:04,860 proper image element so let me change 2047 01:30:01,860 --> 01:30:09,239 this and I'm going to go with heading 2. 2048 01:30:04,860 --> 01:30:12,239 and we'll just say image Place holder 2049 01:30:09,239 --> 01:30:13,920 now I'm purposely showing multiple ways 2050 01:30:12,239 --> 01:30:17,219 how we can set up D components so notice 2051 01:30:13,920 --> 01:30:19,980 this is going to be implicit return 2052 01:30:17,219 --> 01:30:22,199 but we can also set up the explicit one 2053 01:30:19,980 --> 01:30:23,159 so first let me change around and say 2054 01:30:22,199 --> 01:30:26,400 that 2055 01:30:23,159 --> 01:30:27,659 second one is going to be title and here 2056 01:30:26,400 --> 01:30:29,760 we're looking 2057 01:30:27,659 --> 01:30:33,060 forward heading two so that doesn't 2058 01:30:29,760 --> 01:30:35,280 change we only want to change the 2059 01:30:33,060 --> 01:30:37,679 content and I'm just gonna write here 2060 01:30:35,280 --> 01:30:40,980 title and then last one is going to be 2061 01:30:37,679 --> 01:30:43,800 the author author and in order to make 2062 01:30:40,980 --> 01:30:46,260 it more interesting let me just cut it 2063 01:30:43,800 --> 01:30:47,460 out let me set up the curlies and let's 2064 01:30:46,260 --> 01:30:48,960 go to the return 2065 01:30:47,460 --> 01:30:51,300 as you can see 2066 01:30:48,960 --> 01:30:54,239 the result is exactly the same 2067 01:30:51,300 --> 01:30:55,380 we'll have some elements rendered on the 2068 01:30:54,239 --> 01:30:58,260 screen 2069 01:30:55,380 --> 01:31:00,480 it really comes down to our preference 2070 01:30:58,260 --> 01:31:03,300 so it's not going to be a image 2071 01:31:00,480 --> 01:31:06,480 placeholder we'll type over here 2072 01:31:03,300 --> 01:31:09,360 author let's save it and we want to move 2073 01:31:06,480 --> 01:31:12,420 up now we want to render all three 2074 01:31:09,360 --> 01:31:16,860 components so let's start with the image 2075 01:31:12,420 --> 01:31:18,480 then we want to go with our I believe 2076 01:31:16,860 --> 01:31:20,699 what title 2077 01:31:18,480 --> 01:31:23,420 over here and then last one is going to 2078 01:31:20,699 --> 01:31:25,560 be the author and then in the book list 2079 01:31:23,420 --> 01:31:28,320 let's move up 2080 01:31:25,560 --> 01:31:29,940 and let's say that we want to render a 2081 01:31:28,320 --> 01:31:31,980 book 2082 01:31:29,940 --> 01:31:34,080 for now let's just render one book 2083 01:31:31,980 --> 01:31:38,280 component and then we'll worry about the 2084 01:31:34,080 --> 01:31:39,719 rest of them so let's save and in here I 2085 01:31:38,280 --> 01:31:41,460 still have the greeting so I want to 2086 01:31:39,719 --> 01:31:44,639 change that one around let's go with 2087 01:31:41,460 --> 01:31:46,139 book list once we save check it out so 2088 01:31:44,639 --> 01:31:48,840 this is the result 2089 01:31:46,139 --> 01:31:50,820 essentially I have these three elements 2090 01:31:48,840 --> 01:31:52,560 rendered on the screen and we can 2091 01:31:50,820 --> 01:31:54,540 definitely see that if we go to elements 2092 01:31:52,560 --> 01:31:56,940 and notice here this is the root like I 2093 01:31:54,540 --> 01:32:00,120 said this is going to be our entry point 2094 01:31:56,940 --> 01:32:02,159 and then I have the section and in there 2095 01:32:00,120 --> 01:32:04,020 I have the article and before we even 2096 01:32:02,159 --> 01:32:06,840 get the values let me showcase something 2097 01:32:04,020 --> 01:32:09,179 really cool so in react if I just copy 2098 01:32:06,840 --> 01:32:12,840 and paste basically if I set up more 2099 01:32:09,179 --> 01:32:14,340 instances of this book component notice 2100 01:32:12,840 --> 01:32:17,639 now of course they're right away 2101 01:32:14,340 --> 01:32:18,780 rendered on screen and what's even more 2102 01:32:17,639 --> 01:32:21,120 cool 2103 01:32:18,780 --> 01:32:24,780 if let's say I decide that you know what 2104 01:32:21,120 --> 01:32:27,780 it's not going to be a book title 2105 01:32:24,780 --> 01:32:30,780 what if I want to go with title 2106 01:32:27,780 --> 01:32:33,739 of the book 2107 01:32:30,780 --> 01:32:37,679 you can save it and check it out all the 2108 01:32:33,739 --> 01:32:40,800 component instances right away have that 2109 01:32:37,679 --> 01:32:42,320 value again really really cool that we 2110 01:32:40,800 --> 01:32:45,420 don't need to run around 2111 01:32:42,320 --> 01:32:47,159 and change those values manually 2112 01:32:45,420 --> 01:32:50,100 effectively we just change it in one 2113 01:32:47,159 --> 01:32:51,719 place and we're good to go so we're done 2114 01:32:50,100 --> 01:32:54,239 with the structure 2115 01:32:51,719 --> 01:32:55,860 now let's go back to read me and see 2116 01:32:54,239 --> 01:32:58,500 what we need to do 2117 01:32:55,860 --> 01:33:00,719 so essentially you have a few options 2118 01:32:58,500 --> 01:33:04,260 you can go to your search engine and 2119 01:33:00,719 --> 01:33:07,560 type Amazon best-selling books or you 2120 01:33:04,260 --> 01:33:09,540 can follow this link now of course if by 2121 01:33:07,560 --> 01:33:12,600 the time you're watching this the link 2122 01:33:09,540 --> 01:33:15,900 doesn't work I mean the 2123 01:33:12,600 --> 01:33:17,880 companies change their resources quite 2124 01:33:15,900 --> 01:33:19,440 often so if you navigate to this link 2125 01:33:17,880 --> 01:33:21,600 and it doesn't exist then of course 2126 01:33:19,440 --> 01:33:23,820 you'll have to search for it the reason 2127 01:33:21,600 --> 01:33:25,860 why I'm providing the link 2128 01:33:23,820 --> 01:33:28,380 is simply because 2129 01:33:25,860 --> 01:33:31,080 that way you can work with the same 2130 01:33:28,380 --> 01:33:33,840 exact list since Amazon has 2131 01:33:31,080 --> 01:33:35,400 quite a lot of those lists out there now 2132 01:33:33,840 --> 01:33:37,260 you'll still be able to follow along 2133 01:33:35,400 --> 01:33:38,760 don't worry about it I'm just saying 2134 01:33:37,260 --> 01:33:41,520 just in case you want to work with the 2135 01:33:38,760 --> 01:33:43,440 same exact list I'm providing a link and 2136 01:33:41,520 --> 01:33:46,440 before we continue let me make something 2137 01:33:43,440 --> 01:33:49,380 extremely extremely clear 2138 01:33:46,440 --> 01:33:53,100 you won't have to buy anything there 2139 01:33:49,380 --> 01:33:56,699 again you won't have to buy anything 2140 01:33:53,100 --> 01:34:00,179 from Amazon and second this is not some 2141 01:33:56,699 --> 01:34:02,940 kind of lame attempt to trick you into 2142 01:34:00,179 --> 01:34:06,480 using an affiliate link 2143 01:34:02,940 --> 01:34:09,239 this is not an affiliate link it's 2144 01:34:06,480 --> 01:34:10,980 literally just the URL to the list that 2145 01:34:09,239 --> 01:34:12,000 I'm using I want to make this extremely 2146 01:34:10,980 --> 01:34:13,980 clear 2147 01:34:12,000 --> 01:34:15,659 and essentially we're just using it 2148 01:34:13,980 --> 01:34:18,780 because I don't want to come up with 2149 01:34:15,659 --> 01:34:22,920 these values I want to provide the real 2150 01:34:18,780 --> 01:34:25,320 values so once I'm done with this boring 2151 01:34:22,920 --> 01:34:28,440 round now let me navigate 2152 01:34:25,320 --> 01:34:31,620 to my list and again you can actually 2153 01:34:28,440 --> 01:34:33,120 copy and paste here or you can click 2154 01:34:31,620 --> 01:34:35,400 command and then 2155 01:34:33,120 --> 01:34:36,900 follow the link just remember that if 2156 01:34:35,400 --> 01:34:39,120 you have the same setup it's going to 2157 01:34:36,900 --> 01:34:40,560 open over here so of course that's why 2158 01:34:39,120 --> 01:34:44,219 I'll grab this one 2159 01:34:40,560 --> 01:34:45,659 navigate copy and paste and unless by 2160 01:34:44,219 --> 01:34:49,860 the time you're watching the video 2161 01:34:45,659 --> 01:34:52,139 Amazon has already removed the page this 2162 01:34:49,860 --> 01:34:54,179 is what you should see obviously your 2163 01:34:52,139 --> 01:34:55,980 values might be different but the idea 2164 01:34:54,179 --> 01:34:58,380 is going to be exactly the same 2165 01:34:55,980 --> 01:35:00,600 and effectively this is where we want to 2166 01:34:58,380 --> 01:35:03,120 grab those values I want to start with 2167 01:35:00,600 --> 01:35:05,219 image then I want to get the 2168 01:35:03,120 --> 01:35:06,719 title and after that I want to get the 2169 01:35:05,219 --> 01:35:09,719 author now I'm going to open them 2170 01:35:06,719 --> 01:35:11,280 because it's going to be easier to copy 2171 01:35:09,719 --> 01:35:12,960 and paste the values and let's start 2172 01:35:11,280 --> 01:35:16,139 with an image so essentially here's what 2173 01:35:12,960 --> 01:35:19,560 we want to do we're going to go to copy 2174 01:35:16,139 --> 01:35:21,360 image address and once it's saved in the 2175 01:35:19,560 --> 01:35:23,340 clipboard let's navigate back to 2176 01:35:21,360 --> 01:35:26,219 index.js and where we have the 2177 01:35:23,340 --> 01:35:29,520 placeholder now let's go with that image 2178 01:35:26,219 --> 01:35:31,920 tag and then let's provide those values 2179 01:35:29,520 --> 01:35:33,900 so in here we want to go with the source 2180 01:35:31,920 --> 01:35:36,540 and source is going to be equal to 2181 01:35:33,900 --> 01:35:39,300 whatever URL 2182 01:35:36,540 --> 01:35:41,219 we have in a clipboard and then as far 2183 01:35:39,300 --> 01:35:43,440 as the alternative well I'm going to 2184 01:35:41,219 --> 01:35:45,179 provide the title for that 2185 01:35:43,440 --> 01:35:48,900 so in order to get the title I already 2186 01:35:45,179 --> 01:35:50,639 opened a book in a new tab so let me 2187 01:35:48,900 --> 01:35:51,840 navigate here now I'm not going to grab 2188 01:35:50,639 --> 01:35:55,199 the whole thing 2189 01:35:51,840 --> 01:35:56,820 it's kind of a Overkill let me just copy 2190 01:35:55,199 --> 01:36:00,600 and paste here the title 2191 01:35:56,820 --> 01:36:02,760 and the same here with the heading too 2192 01:36:00,600 --> 01:36:05,400 and lastly we want to get the author 2193 01:36:02,760 --> 01:36:07,860 which I believe the fastest way is just 2194 01:36:05,400 --> 01:36:09,900 a copy like this 2195 01:36:07,860 --> 01:36:13,320 so let me provide here where I have the 2196 01:36:09,900 --> 01:36:15,840 heading for let's save it and yes it's 2197 01:36:13,320 --> 01:36:18,840 gigantic and all that don't worry we'll 2198 01:36:15,840 --> 01:36:21,239 add a little bit of CSS later and 2199 01:36:18,840 --> 01:36:23,400 essentially we'll fix the issue again 2200 01:36:21,239 --> 01:36:26,580 the cool thing is that 2201 01:36:23,400 --> 01:36:28,860 since we set up multiple book instances 2202 01:36:26,580 --> 01:36:30,480 you're gonna right away see that they're 2203 01:36:28,860 --> 01:36:32,940 rendered on the screen and whatever 2204 01:36:30,480 --> 01:36:36,000 changes will make in the individual 2205 01:36:32,940 --> 01:36:39,780 components will right away 2206 01:36:36,000 --> 01:36:42,600 be displayed on the screen now we don't 2207 01:36:39,780 --> 01:36:45,120 need that many book instances so you 2208 01:36:42,600 --> 01:36:47,040 know what for now why don't we just 2209 01:36:45,120 --> 01:36:50,880 leave four simply because we'll be 2210 01:36:47,040 --> 01:36:52,739 setting up a grid layout pretty soon so 2211 01:36:50,880 --> 01:36:55,860 if you have the same result 2212 01:36:52,739 --> 01:36:59,760 we're good to go and in the next video 2213 01:36:55,860 --> 01:37:02,639 we'll take a look how we can add CSS to 2214 01:36:59,760 --> 01:37:05,520 our react project beautiful at this 2215 01:37:02,639 --> 01:37:07,980 point we know how we can render HTML 2216 01:37:05,520 --> 01:37:10,440 but what about CSS 2217 01:37:07,980 --> 01:37:13,620 because as you can see I mean it's cute 2218 01:37:10,440 --> 01:37:16,620 to have this kind of list but 2219 01:37:13,620 --> 01:37:20,340 you probably agree with me that it does 2220 01:37:16,620 --> 01:37:24,860 require some CSS and essentially it's 2221 01:37:20,340 --> 01:37:28,260 not as difficult as one might think 2222 01:37:24,860 --> 01:37:31,440 effectively we just need to create a CSS 2223 01:37:28,260 --> 01:37:32,580 file now as always name is really up to 2224 01:37:31,440 --> 01:37:35,520 you 2225 01:37:32,580 --> 01:37:38,940 but I'm gonna go with index CSS since my 2226 01:37:35,520 --> 01:37:40,920 JavaScript file is index.js we want to 2227 01:37:38,940 --> 01:37:45,900 set up the Styles and lastly we want to 2228 01:37:40,920 --> 01:37:47,820 import that in our index.js file now I 2229 01:37:45,900 --> 01:37:49,860 can tell you right away that 2230 01:37:47,820 --> 01:37:52,920 for bigger projects 2231 01:37:49,860 --> 01:37:54,420 most likely you'll have a folder with 2232 01:37:52,920 --> 01:37:55,560 CSS files 2233 01:37:54,420 --> 01:37:57,120 which again 2234 01:37:55,560 --> 01:37:58,560 is totally 2235 01:37:57,120 --> 01:38:01,260 okay 2236 01:37:58,560 --> 01:38:03,600 you can definitely do so you'll just 2237 01:38:01,260 --> 01:38:05,820 have to change the path if you want to 2238 01:38:03,600 --> 01:38:08,400 set up the entire folder with bunch of 2239 01:38:05,820 --> 01:38:11,580 CSS files you can definitely do so 2240 01:38:08,400 --> 01:38:13,080 they don't have to be separate files in 2241 01:38:11,580 --> 01:38:17,159 the source 2242 01:38:13,080 --> 01:38:19,199 and as far as the logic I just wanna 2243 01:38:17,159 --> 01:38:22,320 create a reset 2244 01:38:19,199 --> 01:38:24,960 and then add to a body 2245 01:38:22,320 --> 01:38:26,699 from family background and a color then 2246 01:38:24,960 --> 01:38:28,080 we'll import and then we'll worry about 2247 01:38:26,699 --> 01:38:30,239 the classes 2248 01:38:28,080 --> 01:38:32,520 so let's start over here so I'm going to 2249 01:38:30,239 --> 01:38:34,620 navigate to Source again if you want you 2250 01:38:32,520 --> 01:38:36,179 can create a folder just your path is 2251 01:38:34,620 --> 01:38:37,800 going to change I really want to 2252 01:38:36,179 --> 01:38:40,020 emphasize that 2253 01:38:37,800 --> 01:38:42,300 because I keep getting questions about 2254 01:38:40,020 --> 01:38:44,520 it so I want to answer them here so 2255 01:38:42,300 --> 01:38:46,860 let's start here with index CSS as 2256 01:38:44,520 --> 01:38:48,239 always if you want to call this banana 2257 01:38:46,860 --> 01:38:51,420 CSS 2258 01:38:48,239 --> 01:38:52,920 it's also a cool option let's go here 2259 01:38:51,420 --> 01:38:56,340 with that reset 2260 01:38:52,920 --> 01:38:58,080 so margin margin zero 2261 01:38:56,340 --> 01:39:03,179 batting zero 2262 01:38:58,080 --> 01:39:05,940 and box sizing equal to a border box 2263 01:39:03,179 --> 01:39:07,739 border box okay beautiful and then let's 2264 01:39:05,940 --> 01:39:09,600 select the body now of course as you're 2265 01:39:07,739 --> 01:39:10,800 looking at the browser you won't see any 2266 01:39:09,600 --> 01:39:13,739 changes 2267 01:39:10,800 --> 01:39:16,199 in order for those changes to take place 2268 01:39:13,739 --> 01:39:18,239 we do need to import so first let's just 2269 01:39:16,199 --> 01:39:20,940 set up those Styles we're gonna go here 2270 01:39:18,239 --> 01:39:24,719 with font family and I'm going to set it 2271 01:39:20,940 --> 01:39:25,920 equal to a system font slightly that is 2272 01:39:24,719 --> 01:39:28,739 actually 2273 01:39:25,920 --> 01:39:31,440 my preference then let's go with 2274 01:39:28,739 --> 01:39:35,639 background and we're gonna go with 2275 01:39:31,440 --> 01:39:39,120 hashtag F1 F5 2276 01:39:35,639 --> 01:39:40,560 and f8 okay so that's the gray one and 2277 01:39:39,120 --> 01:39:42,420 then as far as the color I'm gonna go 2278 01:39:40,560 --> 01:39:47,219 with hashtag 222 2279 01:39:42,420 --> 01:39:50,580 let's save it like I said the browser 2280 01:39:47,219 --> 01:39:53,460 still displays the same list and now 2281 01:39:50,580 --> 01:39:56,219 let's navigate to index.js and we want 2282 01:39:53,460 --> 01:39:59,340 to import that index CSS 2283 01:39:56,219 --> 01:40:02,340 now later when we import JavaScript 2284 01:39:59,340 --> 01:40:03,960 files you'll see that we don't need to 2285 01:40:02,340 --> 01:40:06,900 add extension 2286 01:40:03,960 --> 01:40:09,719 and for the most part we'll say what we 2287 01:40:06,900 --> 01:40:12,239 want to import whether that is some kind 2288 01:40:09,719 --> 01:40:15,300 of array or whether that is the 2289 01:40:12,239 --> 01:40:18,120 component when it comes to CSS we want 2290 01:40:15,300 --> 01:40:21,480 to grab the entire file and we must must 2291 01:40:18,120 --> 01:40:24,540 must must provide the file extension so 2292 01:40:21,480 --> 01:40:27,239 since I have index CSS in the index.js I 2293 01:40:24,540 --> 01:40:30,540 want to go with import and then like I 2294 01:40:27,239 --> 01:40:33,179 said we need to go with the path so 2295 01:40:30,540 --> 01:40:35,280 this is our own asset 2296 01:40:33,179 --> 01:40:37,560 these are assets that are coming from 2297 01:40:35,280 --> 01:40:39,600 the libraries so when it comes to 2298 01:40:37,560 --> 01:40:42,719 libraries we don't need to provide a 2299 01:40:39,600 --> 01:40:45,060 path libraries are installed so we just 2300 01:40:42,719 --> 01:40:47,520 need to provide the library name now 2301 01:40:45,060 --> 01:40:51,239 when it comes to our own assets we need 2302 01:40:47,520 --> 01:40:54,000 to provide a path to that asset and it's 2303 01:40:51,239 --> 01:40:57,179 always going to start with DOT now there 2304 01:40:54,000 --> 01:41:00,060 might be two dots if we have to go one 2305 01:40:57,179 --> 01:41:01,860 level up but one that means that we're 2306 01:41:00,060 --> 01:41:04,139 in the same folder 2307 01:41:01,860 --> 01:41:06,540 so in the same folder we go with Dot and 2308 01:41:04,139 --> 01:41:09,000 then forward slash and notice it right 2309 01:41:06,540 --> 01:41:13,020 away offers me the test now I don't want 2310 01:41:09,000 --> 01:41:15,900 to test instead what I want is index and 2311 01:41:13,020 --> 01:41:19,260 then Dot and like I said the entire 2312 01:41:15,900 --> 01:41:20,940 file name including the extension with 2313 01:41:19,260 --> 01:41:24,300 the JavaScript files you won't have to 2314 01:41:20,940 --> 01:41:27,900 do that and the moment we save we should 2315 01:41:24,300 --> 01:41:29,040 see some changes and check it out it 2316 01:41:27,900 --> 01:41:31,679 looks like 2317 01:41:29,040 --> 01:41:33,719 some changes were applied now it's kind 2318 01:41:31,679 --> 01:41:36,239 of hard to see so let me navigate to the 2319 01:41:33,719 --> 01:41:37,920 big screen so notice this is our Giant 2320 01:41:36,239 --> 01:41:39,659 image and then we have some text then 2321 01:41:37,920 --> 01:41:41,940 this is the background now if you want 2322 01:41:39,659 --> 01:41:44,580 to take a look as always 2323 01:41:41,940 --> 01:41:47,699 just go to developer tools and you'll 2324 01:41:44,580 --> 01:41:51,179 notice that we have some Styles already 2325 01:41:47,699 --> 01:41:54,960 okay awesome so now let's keep working 2326 01:41:51,179 --> 01:41:57,179 I want to style the book list and I also 2327 01:41:54,960 --> 01:41:59,100 want to style the book 2328 01:41:57,179 --> 01:42:01,020 my single component 2329 01:41:59,100 --> 01:42:02,280 and essentially we just want to add the 2330 01:42:01,020 --> 01:42:06,000 class 2331 01:42:02,280 --> 01:42:08,580 now of course you can style in the index 2332 01:42:06,000 --> 01:42:10,260 CSS like you normally would let's say if 2333 01:42:08,580 --> 01:42:12,600 you want to select the section but in 2334 01:42:10,260 --> 01:42:15,239 most cases you would use some kind of 2335 01:42:12,600 --> 01:42:18,719 class and we just need to remember that 2336 01:42:15,239 --> 01:42:21,960 in react there is no class so I'm gonna 2337 01:42:18,719 --> 01:42:24,719 go here with some value and if I'll save 2338 01:42:21,960 --> 01:42:27,360 if we navigate to the browser 2339 01:42:24,719 --> 01:42:29,940 in the console we should see invalid 2340 01:42:27,360 --> 01:42:31,080 down property Clash did you mean class 2341 01:42:29,940 --> 01:42:33,960 name 2342 01:42:31,080 --> 01:42:37,500 so yes when it comes to react if we want 2343 01:42:33,960 --> 01:42:40,139 to add a class in the jsx we need to go 2344 01:42:37,500 --> 01:42:41,219 with class name not just class and once 2345 01:42:40,139 --> 01:42:43,920 we do that 2346 01:42:41,219 --> 01:42:45,119 it's exactly the same as we normally 2347 01:42:43,920 --> 01:42:47,400 would in 2348 01:42:45,119 --> 01:42:49,560 HTML basically we add the class and 2349 01:42:47,400 --> 01:42:51,480 we're good to go we have the Styles and 2350 01:42:49,560 --> 01:42:53,940 we don't need to worry about anything 2351 01:42:51,480 --> 01:42:55,800 else just the name is a little bit 2352 01:42:53,940 --> 01:42:58,500 different and in here I'm going to go 2353 01:42:55,800 --> 01:43:02,400 with book list 2354 01:42:58,500 --> 01:43:04,260 so book list and then the same I'll 2355 01:43:02,400 --> 01:43:06,420 apply to the book 2356 01:43:04,260 --> 01:43:07,619 so let me copy and paste I want to go to 2357 01:43:06,420 --> 01:43:10,560 the article 2358 01:43:07,619 --> 01:43:11,820 and then instead of book list it's going 2359 01:43:10,560 --> 01:43:15,060 to be a book 2360 01:43:11,820 --> 01:43:21,480 and basically I want to set up some kind 2361 01:43:15,060 --> 01:43:21,480 of grid layout once we get to 768 2362 01:43:21,960 --> 01:43:26,219 um as far as the book well I just want 2363 01:43:24,179 --> 01:43:28,500 to add some background make sure that 2364 01:43:26,219 --> 01:43:31,260 the image sits nicely in the card as 2365 01:43:28,500 --> 01:43:34,560 well as add some styles to you a heading 2366 01:43:31,260 --> 01:43:37,920 2. so let's navigate back to index CSS 2367 01:43:34,560 --> 01:43:41,219 first let's just grab the book list I'm 2368 01:43:37,920 --> 01:43:44,580 going to set up the width to be 90 of 2369 01:43:41,219 --> 01:43:47,340 the screen width then Max width is going 2370 01:43:44,580 --> 01:43:49,920 to be 1170. 2371 01:43:47,340 --> 01:43:52,800 so it's always going to be at least 90 2372 01:43:49,920 --> 01:43:55,619 percent of the screen width but it's 2373 01:43:52,800 --> 01:43:57,540 never going to be bigger than 1170. I 2374 01:43:55,619 --> 01:44:00,119 want to add some margin top bottom five 2375 01:43:57,540 --> 01:44:01,980 Rems I want to place it in the center so 2376 01:44:00,119 --> 01:44:04,739 I'm going to go with Auto and then I'll 2377 01:44:01,980 --> 01:44:07,860 right away display grid 2378 01:44:04,739 --> 01:44:09,719 and as far as the gap for the rows and 2379 01:44:07,860 --> 01:44:11,040 the columns it's going to be two Rems 2380 01:44:09,719 --> 01:44:14,820 again 2381 01:44:11,040 --> 01:44:17,820 the problem right now is the image 2382 01:44:14,820 --> 01:44:20,340 since the size is quite big that's why 2383 01:44:17,820 --> 01:44:21,900 we have this look so you know what I'm 2384 01:44:20,340 --> 01:44:23,699 actually going to skip a little bit and 2385 01:44:21,900 --> 01:44:26,659 I'm just going to say book so now I'm 2386 01:44:23,699 --> 01:44:29,219 selecting the article that has the class 2387 01:44:26,659 --> 01:44:31,500 and you know what I'll right away add 2388 01:44:29,219 --> 01:44:33,480 this IMG so I'll select the image and 2389 01:44:31,500 --> 01:44:36,900 I'm just going to say that width should 2390 01:44:33,480 --> 01:44:39,840 always be 100 so now it will nicely fit 2391 01:44:36,900 --> 01:44:43,199 in our card and object fit is going to 2392 01:44:39,840 --> 01:44:45,000 be equal to a cover we save it and 2393 01:44:43,199 --> 01:44:47,940 notice now of course 2394 01:44:45,000 --> 01:44:51,600 our project looks more presentable now 2395 01:44:47,940 --> 01:44:53,159 back to the book list once we get to 768 2396 01:44:51,600 --> 01:44:57,380 I want to set up that three column 2397 01:44:53,159 --> 01:45:02,340 layout so in here let's go with media 2398 01:44:57,380 --> 01:45:04,320 media okay screen and we're going to add 2399 01:45:02,340 --> 01:45:07,860 a Min width 2400 01:45:04,320 --> 01:45:09,840 so let's go here with and and min 2401 01:45:07,860 --> 01:45:11,360 width and that is going to be equal to 2402 01:45:09,840 --> 01:45:15,000 seven 2403 01:45:11,360 --> 01:45:16,080 68 pixels so once we get to that screen 2404 01:45:15,000 --> 01:45:19,260 size 2405 01:45:16,080 --> 01:45:23,040 we'll have that three column layout so 2406 01:45:19,260 --> 01:45:25,020 book list here and then inside of it 2407 01:45:23,040 --> 01:45:28,800 let's go with grid template columns 2408 01:45:25,020 --> 01:45:31,380 that's the property repeat and three and 2409 01:45:28,800 --> 01:45:33,420 one fraction let's save it as you can 2410 01:45:31,380 --> 01:45:35,760 see on a small screen 2411 01:45:33,420 --> 01:45:38,580 there's no difference but once we get to 2412 01:45:35,760 --> 01:45:42,300 the big screen we have nice three column 2413 01:45:38,580 --> 01:45:43,199 layout and now let me just continue with 2414 01:45:42,300 --> 01:45:45,780 the book 2415 01:45:43,199 --> 01:45:47,400 first I want to add some background and 2416 01:45:45,780 --> 01:45:51,239 I'm gonna go with the white one so 2417 01:45:47,400 --> 01:45:53,460 hashtag FFF then we want to go with 2418 01:45:51,239 --> 01:45:55,920 border radius and that is going to be 2419 01:45:53,460 --> 01:45:57,300 equal to 1 Rems then we'll add a little 2420 01:45:55,920 --> 01:46:00,719 bit of padding 2421 01:45:57,300 --> 01:46:03,780 in my case I'm gonna go with two Rems 2422 01:46:00,719 --> 01:46:05,400 and then text align Center so Place 2423 01:46:03,780 --> 01:46:08,219 everything in the center 2424 01:46:05,400 --> 01:46:10,920 we already have the image one so so we 2425 01:46:08,219 --> 01:46:14,460 really need to focus on heading two 2426 01:46:10,920 --> 01:46:19,199 that's it so book heading two 2427 01:46:14,460 --> 01:46:20,820 and then margin top one REM and as far 2428 01:46:19,199 --> 01:46:25,500 as the font size I think I'm gonna go 2429 01:46:20,820 --> 01:46:28,199 with one Rems and now what I can do is 2430 01:46:25,500 --> 01:46:31,560 set up the zoom again to 175 since 2431 01:46:28,199 --> 01:46:33,659 everything is nicely displayed in the 2432 01:46:31,560 --> 01:46:34,739 browser and again if we take a look at 2433 01:46:33,659 --> 01:46:38,280 the 2434 01:46:34,739 --> 01:46:40,860 bigger browser window we can also see a 2435 01:46:38,280 --> 01:46:43,980 nice list so that's how we can add 2436 01:46:40,860 --> 01:46:48,540 the most basic CSS to our react project 2437 01:46:43,980 --> 01:46:50,340 we create a CSS file again I'm going to 2438 01:46:48,540 --> 01:46:53,340 repeat yes 2439 01:46:50,340 --> 01:46:55,380 can be index it can be shaken back CSS 2440 01:46:53,340 --> 01:46:57,900 whatever if you want to set up the 2441 01:46:55,380 --> 01:46:59,580 folder the only difference is when you 2442 01:46:57,900 --> 01:47:01,920 import then of course you need to start 2443 01:46:59,580 --> 01:47:04,020 with DOT since it's going to be in the 2444 01:47:01,920 --> 01:47:06,300 same folder then forward slash the 2445 01:47:04,020 --> 01:47:08,280 folder name and then the file 2446 01:47:06,300 --> 01:47:10,619 effectively we want to import everything 2447 01:47:08,280 --> 01:47:12,420 so later with JavaScript you'll see how 2448 01:47:10,619 --> 01:47:14,400 we're importing something specific from 2449 01:47:12,420 --> 01:47:17,760 the file whether that is component 2450 01:47:14,400 --> 01:47:20,880 whether that is a data but with CSS we 2451 01:47:17,760 --> 01:47:23,940 grab everything and we need to provide 2452 01:47:20,880 --> 01:47:25,639 always always a extension as well which 2453 01:47:23,940 --> 01:47:28,380 is not something we're gonna have to do 2454 01:47:25,639 --> 01:47:30,960 with the JavaScript files 2455 01:47:28,380 --> 01:47:34,860 with this in place now let's talk about 2456 01:47:30,960 --> 01:47:36,780 the images in react okay and once we 2457 01:47:34,860 --> 01:47:39,719 know how we can apply 2458 01:47:36,780 --> 01:47:42,600 CSS to our react project now let's talk 2459 01:47:39,719 --> 01:47:43,920 about images and I can tell you right 2460 01:47:42,600 --> 01:47:46,619 away that 2461 01:47:43,920 --> 01:47:48,600 it's an optional video technically if 2462 01:47:46,619 --> 01:47:51,540 you don't want to follow along you don't 2463 01:47:48,600 --> 01:47:53,639 have to but again it's the case where 2464 01:47:51,540 --> 01:47:56,820 I'm getting questions about it in the 2465 01:47:53,639 --> 01:47:59,639 course q a so I just thought that it's 2466 01:47:56,820 --> 01:48:01,619 very useful if I showcase everything 2467 01:47:59,639 --> 01:48:04,980 step by step 2468 01:48:01,619 --> 01:48:07,380 and essentially when it comes to images 2469 01:48:04,980 --> 01:48:10,679 we have three options 2470 01:48:07,380 --> 01:48:13,320 we have external images that are hosted 2471 01:48:10,679 --> 01:48:14,699 on a different server please keep 2472 01:48:13,320 --> 01:48:16,380 something in mind 2473 01:48:14,699 --> 01:48:18,360 this image 2474 01:48:16,380 --> 01:48:21,000 is hosted 2475 01:48:18,360 --> 01:48:22,199 on a different server 2476 01:48:21,000 --> 01:48:23,639 so 2477 01:48:22,199 --> 01:48:25,380 Amazon 2478 01:48:23,639 --> 01:48:27,780 essentially 2479 01:48:25,380 --> 01:48:31,380 is providing this asset so we're not 2480 01:48:27,780 --> 01:48:34,320 doing anything we just take this URL 2481 01:48:31,380 --> 01:48:35,940 and we provide it for our image tag and 2482 01:48:34,320 --> 01:48:38,159 you can definitely take this URL and 2483 01:48:35,940 --> 01:48:40,260 you'll see that this is the image 2484 01:48:38,159 --> 01:48:42,659 so hopefully that is clear that's our 2485 01:48:40,260 --> 01:48:45,179 first option so let's say when we'll be 2486 01:48:42,659 --> 01:48:48,659 serving our data from our own database 2487 01:48:45,179 --> 01:48:50,699 or getting some external API 2488 01:48:48,659 --> 01:48:53,340 if there's going to be an asset for 2489 01:48:50,699 --> 01:48:56,900 example image asset then there's always 2490 01:48:53,340 --> 01:49:00,300 going to be a URL that points back to 2491 01:48:56,900 --> 01:49:01,860 some kind of external server so that's 2492 01:49:00,300 --> 01:49:03,900 our first option 2493 01:49:01,860 --> 01:49:06,179 pretty straightforward correct just 2494 01:49:03,900 --> 01:49:08,100 provide the URL and we're good to go 2495 01:49:06,179 --> 01:49:11,340 and after that we have two options for 2496 01:49:08,100 --> 01:49:13,440 local images one in the public folder 2497 01:49:11,340 --> 01:49:17,820 which we're going to cover in this video 2498 01:49:13,440 --> 01:49:19,560 but since it's less performant 2499 01:49:17,820 --> 01:49:22,139 it's not something we're going to 2500 01:49:19,560 --> 01:49:24,300 implement in our projects 2501 01:49:22,139 --> 01:49:26,400 and then we have local images in the 2502 01:49:24,300 --> 01:49:29,580 source folder which is a better solution 2503 01:49:26,400 --> 01:49:32,520 for assets since under the hood they get 2504 01:49:29,580 --> 01:49:34,320 optimized so throughout the course we'll 2505 01:49:32,520 --> 01:49:36,420 stick with the source folder if we have 2506 01:49:34,320 --> 01:49:38,460 local images but since we haven't 2507 01:49:36,420 --> 01:49:40,440 covered the imports and exports and all 2508 01:49:38,460 --> 01:49:43,619 that and since we'll have to utilize the 2509 01:49:40,440 --> 01:49:45,780 S6 modules for that for now 2510 01:49:43,619 --> 01:49:48,420 we'll just put it on hold 2511 01:49:45,780 --> 01:49:51,000 but since I want you to be aware of this 2512 01:49:48,420 --> 01:49:52,679 option of public folder even though it's 2513 01:49:51,000 --> 01:49:55,080 not something we're going to implement 2514 01:49:52,679 --> 01:49:57,420 since it's less performant in this video 2515 01:49:55,080 --> 01:50:00,000 I'll show you how we can set it up again 2516 01:49:57,420 --> 01:50:01,199 I'm getting questions in course q a 2517 01:50:00,000 --> 01:50:03,659 so 2518 01:50:01,199 --> 01:50:06,060 I might as well cover everything step by 2519 01:50:03,659 --> 01:50:08,520 step and essentially the steps are 2520 01:50:06,060 --> 01:50:10,380 following we want to save the image it 2521 01:50:08,520 --> 01:50:12,000 can be any image but of course I'll use 2522 01:50:10,380 --> 01:50:14,820 same image 2523 01:50:12,000 --> 01:50:17,400 we want to create a images folder in 2524 01:50:14,820 --> 01:50:20,520 public now technically you don't have to 2525 01:50:17,400 --> 01:50:23,580 again create the folder but since in 2526 01:50:20,520 --> 01:50:25,560 most cases you'll have multiple assets 2527 01:50:23,580 --> 01:50:28,139 for your project it kind of makes sense 2528 01:50:25,560 --> 01:50:30,119 to set up the folder structure as long 2529 01:50:28,139 --> 01:50:32,460 as that image is in a public it's going 2530 01:50:30,119 --> 01:50:34,380 to be available so keep in mind 2531 01:50:32,460 --> 01:50:36,659 technically you don't have to have the 2532 01:50:34,380 --> 01:50:39,239 images folder and then we want to copy 2533 01:50:36,659 --> 01:50:41,699 and paste the image we downloaded 2534 01:50:39,239 --> 01:50:44,880 we want to rename it now that's totally 2535 01:50:41,699 --> 01:50:47,100 optional but in my case I'll do that and 2536 01:50:44,880 --> 01:50:49,980 then in a source where we have the image 2537 01:50:47,100 --> 01:50:54,119 stack we want to replace the URL 2538 01:50:49,980 --> 01:50:55,800 so instead of the external one we'll use 2539 01:50:54,119 --> 01:50:58,380 the internal one and in the process 2540 01:50:55,800 --> 01:51:01,560 you'll see that pretty much any asset we 2541 01:50:58,380 --> 01:51:04,860 add to the public is right away publicly 2542 01:51:01,560 --> 01:51:06,780 available hopefully that makes sense so 2543 01:51:04,860 --> 01:51:09,239 in my case I know that I'm going to call 2544 01:51:06,780 --> 01:51:10,679 this book one and in the source notice 2545 01:51:09,239 --> 01:51:12,659 I'm going to go forward slash images 2546 01:51:10,679 --> 01:51:14,100 because it's in the folder if it 2547 01:51:12,659 --> 01:51:15,780 wouldn't be in folder then of course I 2548 01:51:14,100 --> 01:51:19,619 would just go with DOT forward slash and 2549 01:51:15,780 --> 01:51:23,460 then the file name and then book and the 2550 01:51:19,619 --> 01:51:25,679 full extension so let's try this one out 2551 01:51:23,460 --> 01:51:26,639 I'm going to navigate here I believe I 2552 01:51:25,679 --> 01:51:29,880 can actually 2553 01:51:26,639 --> 01:51:31,739 get it from the source as well but just 2554 01:51:29,880 --> 01:51:34,199 to Showcase that of course 2555 01:51:31,739 --> 01:51:36,960 we can get it from the entire list let's 2556 01:51:34,199 --> 01:51:39,960 navigate here notice the save image as 2557 01:51:36,960 --> 01:51:43,980 option and I'm gonna go right away over 2558 01:51:39,960 --> 01:51:46,380 here and I'm gonna say book and one 2559 01:51:43,980 --> 01:51:48,840 let's save the file now it's on a 2560 01:51:46,380 --> 01:51:51,659 desktop I want to navigate 2561 01:51:48,840 --> 01:51:53,400 to my desktop I'm going to open up my 2562 01:51:51,659 --> 01:51:56,520 project 2563 01:51:53,400 --> 01:51:58,260 okay then very important we want to 2564 01:51:56,520 --> 01:52:00,119 place this in the public so not in a 2565 01:51:58,260 --> 01:52:03,360 source The Source option will cover 2566 01:52:00,119 --> 01:52:05,520 later we want to go with images 2567 01:52:03,360 --> 01:52:09,000 then we want to drag and drop this 2568 01:52:05,520 --> 01:52:11,820 sucker here and then this one over here 2569 01:52:09,000 --> 01:52:13,500 and once it's available you'll actually 2570 01:52:11,820 --> 01:52:17,760 notice 2571 01:52:13,500 --> 01:52:20,100 if you take the URL now 2572 01:52:17,760 --> 01:52:22,920 again in my case the name is following 2573 01:52:20,100 --> 01:52:25,679 but if you use a different name then of 2574 01:52:22,920 --> 01:52:28,739 course you need to use that one if you 2575 01:52:25,679 --> 01:52:32,520 take here this forward slash and added 2576 01:52:28,739 --> 01:52:34,679 to your localhost 3000 you'll see that 2577 01:52:32,520 --> 01:52:37,619 right away it's available so I have this 2578 01:52:34,679 --> 01:52:40,199 localhost 3000 so that's the server 2579 01:52:37,619 --> 01:52:41,580 that I'm working with notice if we copy 2580 01:52:40,199 --> 01:52:45,000 and paste 2581 01:52:41,580 --> 01:52:48,000 the full path so images and then book 2582 01:52:45,000 --> 01:52:50,340 and whatever extension if I press here 2583 01:52:48,000 --> 01:52:52,860 notice this is going to be my image so 2584 01:52:50,340 --> 01:52:56,219 whatever we place in the public right 2585 01:52:52,860 --> 01:52:58,500 away is publicly available and what that 2586 01:52:56,219 --> 01:52:59,340 means is that we can also use it in our 2587 01:52:58,500 --> 01:53:01,860 code 2588 01:52:59,340 --> 01:53:03,840 so now we just want to navigate back to 2589 01:53:01,860 --> 01:53:07,500 index.js 2590 01:53:03,840 --> 01:53:10,199 and instead of this external URL 2591 01:53:07,500 --> 01:53:15,060 we want to go dot forward slash 2592 01:53:10,199 --> 01:53:17,219 then images and then book one on the 2593 01:53:15,060 --> 01:53:19,260 extension so essentially whatever we 2594 01:53:17,219 --> 01:53:22,380 place over there 2595 01:53:19,260 --> 01:53:25,679 is immediately available in our code 2596 01:53:22,380 --> 01:53:30,360 as well as in the browser 2597 01:53:25,679 --> 01:53:33,239 again the reason why this is not a 2598 01:53:30,360 --> 01:53:35,340 popular option is because 2599 01:53:33,239 --> 01:53:38,040 these assets 2600 01:53:35,340 --> 01:53:40,320 don't get optimized so later when we'll 2601 01:53:38,040 --> 01:53:43,260 place our Assets in the source folder 2602 01:53:40,320 --> 01:53:45,000 under the hood they get optimized 2603 01:53:43,260 --> 01:53:48,060 and therefore 2604 01:53:45,000 --> 01:53:49,560 the end result is better our application 2605 01:53:48,060 --> 01:53:51,360 is faster 2606 01:53:49,560 --> 01:53:53,340 but yes 2607 01:53:51,360 --> 01:53:58,800 you also have an option to 2608 01:53:53,340 --> 01:54:02,040 put your assets in the public and then 2609 01:53:58,800 --> 01:54:04,500 access them in a code or provide them in 2610 01:54:02,040 --> 01:54:08,580 the browser and lastly I just want to 2611 01:54:04,500 --> 01:54:12,480 showcase that if you go back to your 2612 01:54:08,580 --> 01:54:15,420 project and again inspect the elements 2613 01:54:12,480 --> 01:54:18,659 you'll see now that the path 2614 01:54:15,420 --> 01:54:21,480 URL path actually goes back to our own 2615 01:54:18,659 --> 01:54:24,119 server and notice again this is going to 2616 01:54:21,480 --> 01:54:27,659 be the full path localhost so that's our 2617 01:54:24,119 --> 01:54:32,580 server then images and then book one 2618 01:54:27,659 --> 01:54:33,420 so since the image the asset is on the 2619 01:54:32,580 --> 01:54:35,639 same 2620 01:54:33,420 --> 01:54:38,340 server we don't need to go with the full 2621 01:54:35,639 --> 01:54:40,800 URL essentially we just need to set up a 2622 01:54:38,340 --> 01:54:44,520 path that is relative 2623 01:54:40,800 --> 01:54:47,460 to our public folder hopefully it's 2624 01:54:44,520 --> 01:54:51,780 clear and up next let's talk about CSS 2625 01:54:47,460 --> 01:54:53,940 in jsx all right and once we're familiar 2626 01:54:51,780 --> 01:54:56,280 how we can add 2627 01:54:53,940 --> 01:54:59,400 styles using external style sheet now 2628 01:54:56,280 --> 01:55:03,060 let's take a look how we can add CSS 2629 01:54:59,400 --> 01:55:04,679 in our jsx and effectively we just need 2630 01:55:03,060 --> 01:55:09,420 to pick the element 2631 01:55:04,679 --> 01:55:13,260 we want to apply our styles to and then 2632 01:55:09,420 --> 01:55:16,139 we need to go with style attribute and 2633 01:55:13,260 --> 01:55:17,460 we need to set it equal to the curly 2634 01:55:16,139 --> 01:55:20,940 braces 2635 01:55:17,460 --> 01:55:23,880 and essentially these curly braces just 2636 01:55:20,940 --> 01:55:26,159 mean that we're going back to the 2637 01:55:23,880 --> 01:55:29,159 JavaScript plan and we'll discuss them 2638 01:55:26,159 --> 01:55:34,320 in more detail in one of the following 2639 01:55:29,159 --> 01:55:36,060 videos when we cover the rules of jsx 2640 01:55:34,320 --> 01:55:39,119 basically when we cover how we can add 2641 01:55:36,060 --> 01:55:41,820 JavaScript to jsx for now just focus 2642 01:55:39,119 --> 01:55:44,760 that you need to provide the curly 2643 01:55:41,820 --> 01:55:47,699 brushes and then inside of it we need to 2644 01:55:44,760 --> 01:55:49,920 provide a JavaScript object so please 2645 01:55:47,699 --> 01:55:52,800 don't think that it's some kind of 2646 01:55:49,920 --> 01:55:55,260 special 2647 01:55:52,800 --> 01:55:57,780 react syntax where you need to go with 2648 01:55:55,260 --> 01:56:00,300 these double curlies no 2649 01:55:57,780 --> 01:56:02,580 effectively since we need to provide the 2650 01:56:00,300 --> 01:56:05,400 object here that's why I have this 2651 01:56:02,580 --> 01:56:07,739 result we always start with one curly 2652 01:56:05,400 --> 01:56:09,960 braces and then at the end of the video 2653 01:56:07,739 --> 01:56:12,060 I'll show you how we essentially can 2654 01:56:09,960 --> 01:56:15,060 just pass the reference to the object 2655 01:56:12,060 --> 01:56:18,000 now when God's about the object is that 2656 01:56:15,060 --> 01:56:21,659 it's a JavaScript object what that means 2657 01:56:18,000 --> 01:56:24,420 is that if you have a CSS property 2658 01:56:21,659 --> 01:56:26,219 that has The Hyphen you'll have to 2659 01:56:24,420 --> 01:56:29,159 capitalize and as far as the values 2660 01:56:26,219 --> 01:56:31,679 they'll have to be here as string so 2661 01:56:29,159 --> 01:56:33,980 let's try it out here I'm going to save 2662 01:56:31,679 --> 01:56:36,719 let's navigate to 2663 01:56:33,980 --> 01:56:38,760 index.js and let's look for a heading 2664 01:56:36,719 --> 01:56:41,580 four so like I said we go here with 2665 01:56:38,760 --> 01:56:43,619 style notice automatically it's set up 2666 01:56:41,580 --> 01:56:46,440 with the curly braces and here we 2667 01:56:43,619 --> 01:56:48,960 provide that JavaScript object 2668 01:56:46,440 --> 01:56:50,820 so let's start here with color and as 2669 01:56:48,960 --> 01:56:54,960 far as the value again key value Pairs 2670 01:56:50,820 --> 01:56:57,960 and then the values are as strings so 2671 01:56:54,960 --> 01:57:02,100 hashtag 617 2672 01:56:57,960 --> 01:57:05,159 and we want to go d98 then we want to go 2673 01:57:02,100 --> 01:57:08,520 with font size so let's add a comma not 2674 01:57:05,159 --> 01:57:11,280 a semicolon and go with font size and 2675 01:57:08,520 --> 01:57:13,280 that is equal to again 2676 01:57:11,280 --> 01:57:14,960 quotation marks 2677 01:57:13,280 --> 01:57:18,239 0.75 2678 01:57:14,960 --> 01:57:21,179 Rems by the way it looks like in the 2679 01:57:18,239 --> 01:57:22,739 readme I'm missing the RM part 2680 01:57:21,179 --> 01:57:24,300 so of course in your case that's not 2681 01:57:22,739 --> 01:57:27,239 going to be the case anymore so let me 2682 01:57:24,300 --> 01:57:29,340 save here and then 2683 01:57:27,239 --> 01:57:32,040 we're looking for a third one and that 2684 01:57:29,340 --> 01:57:35,280 is going to be margin and again we are 2685 01:57:32,040 --> 01:57:37,679 not going with margin hyphen top we go 2686 01:57:35,280 --> 01:57:39,980 here with margin top and we want to set 2687 01:57:37,679 --> 01:57:42,900 it equal to 0.5 2688 01:57:39,980 --> 01:57:45,860 Rems let's save it 2689 01:57:42,900 --> 01:57:49,739 as you can see the styles were applied 2690 01:57:45,860 --> 01:57:51,480 but one gotcha we need to be aware of I 2691 01:57:49,739 --> 01:57:53,099 guess one more gotcha we need to be 2692 01:57:51,480 --> 01:57:55,260 aware of 2693 01:57:53,099 --> 01:57:56,400 at the end of the day these are inline 2694 01:57:55,260 --> 01:58:00,300 styles 2695 01:57:56,400 --> 01:58:01,560 so what that means General CSS rules 2696 01:58:00,300 --> 01:58:04,440 apply 2697 01:58:01,560 --> 01:58:06,360 so what am I talking about well if let's 2698 01:58:04,440 --> 01:58:09,540 say 2699 01:58:06,360 --> 01:58:13,219 you'll try to select the same heading 4 2700 01:58:09,540 --> 01:58:16,380 in CSS and try to change the properties 2701 01:58:13,219 --> 01:58:19,020 that we already set up over here with 2702 01:58:16,380 --> 01:58:20,580 the inline one it's not going to work so 2703 01:58:19,020 --> 01:58:23,460 letter spacing yeah it's going to work 2704 01:58:20,580 --> 01:58:26,880 since we don't have it over here 2705 01:58:23,460 --> 01:58:28,440 but as far as the color if I try to go 2706 01:58:26,880 --> 01:58:29,639 with red it's not going to work and in 2707 01:58:28,440 --> 01:58:30,540 order to speed this up I'm just going to 2708 01:58:29,639 --> 01:58:32,460 copy 2709 01:58:30,540 --> 01:58:34,500 so we're going to go here copy and paste 2710 01:58:32,460 --> 01:58:37,560 the notice yeah the letter spacing 2711 01:58:34,500 --> 01:58:39,780 change but not the color now why am I 2712 01:58:37,560 --> 01:58:42,239 telling you that is because later 2713 01:58:39,780 --> 01:58:44,699 as you're progressing with react you'll 2714 01:58:42,239 --> 01:58:46,020 most likely start working with external 2715 01:58:44,699 --> 01:58:49,619 libraries 2716 01:58:46,020 --> 01:58:51,599 I don't know maybe a slider library or a 2717 01:58:49,619 --> 01:58:54,900 model Library whatever 2718 01:58:51,599 --> 01:58:57,179 and one thing to be aware of 2719 01:58:54,900 --> 01:58:58,139 those libraries do use inline Styles 2720 01:58:57,179 --> 01:59:01,440 here and there 2721 01:58:58,139 --> 01:59:03,960 some quite often some less so keep in 2722 01:59:01,440 --> 01:59:08,040 mind if you'll want to change some 2723 01:59:03,960 --> 01:59:10,739 styles that the library provides 2724 01:59:08,040 --> 01:59:14,280 sometimes it's not going to be enough to 2725 01:59:10,739 --> 01:59:16,560 just go to the CSS and change it 2726 01:59:14,280 --> 01:59:18,119 if the library is using inline Styles 2727 01:59:16,560 --> 01:59:20,219 you'll have to figure out how you can 2728 01:59:18,119 --> 01:59:22,320 modify those inline Styles and only then 2729 01:59:20,219 --> 01:59:24,380 you will be successful so don't get 2730 01:59:22,320 --> 01:59:26,940 frustrated if you install the library 2731 01:59:24,380 --> 01:59:28,800 and you try to 2732 01:59:26,940 --> 01:59:30,480 add your own Styles but it's not working 2733 01:59:28,800 --> 01:59:33,900 always always 2734 01:59:30,480 --> 01:59:35,820 first check the elements and if you see 2735 01:59:33,900 --> 01:59:38,760 the inline Styles you know that you'll 2736 01:59:35,820 --> 01:59:41,040 have to do more work and lastly I just 2737 01:59:38,760 --> 01:59:42,420 want to mention that yes there is an 2738 01:59:41,040 --> 01:59:43,800 alternative 2739 01:59:42,420 --> 01:59:47,639 remember 2740 01:59:43,800 --> 01:59:49,619 we're providing here a object now we're 2741 01:59:47,639 --> 01:59:51,360 writing JavaScript correct 2742 01:59:49,619 --> 01:59:53,460 so what we can do 2743 01:59:51,360 --> 01:59:57,060 well we can create one 2744 01:59:53,460 --> 01:59:59,219 and we can create the object here we can 2745 01:59:57,060 --> 02:00:01,619 create it outside of the component that 2746 01:59:59,219 --> 02:00:04,440 doesn't really matter and we can just 2747 02:00:01,619 --> 02:00:06,599 provide the reference notice how the 2748 02:00:04,440 --> 02:00:08,940 values don't change 2749 02:00:06,599 --> 02:00:11,099 values exactly are the same 2750 02:00:08,940 --> 02:00:14,639 the only difference is that now instead 2751 02:00:11,099 --> 02:00:16,800 of passing that object directly 2752 02:00:14,639 --> 02:00:19,820 we just set up the reference so let's 2753 02:00:16,800 --> 02:00:22,980 try it out I'm going to go to index.js 2754 02:00:19,820 --> 02:00:25,679 and this is the case where if you have 2755 02:00:22,980 --> 02:00:28,199 of course the implicit return you will 2756 02:00:25,679 --> 02:00:29,760 have to refactor to explicit since I'm 2757 02:00:28,199 --> 02:00:32,280 going to be setting up this object over 2758 02:00:29,760 --> 02:00:33,780 here so you won't be able to do that 2759 02:00:32,280 --> 02:00:36,719 with a title 2760 02:00:33,780 --> 02:00:41,880 but since the author is right away 2761 02:00:36,719 --> 02:00:44,699 set up as a explicit I'm good to go so 2762 02:00:41,880 --> 02:00:49,260 let me go here and set up that long name 2763 02:00:44,699 --> 02:00:52,260 so const and then inline heading 2764 02:00:49,260 --> 02:00:54,480 Styles and that is equal to my object 2765 02:00:52,260 --> 02:00:55,440 and effectively I'll just cut it out 2766 02:00:54,480 --> 02:00:58,380 here 2767 02:00:55,440 --> 02:01:01,080 I'll cut it out here copy and paste and 2768 02:00:58,380 --> 02:01:02,719 then we go with inline heading Styles 2769 02:01:01,080 --> 02:01:05,280 now the reason why I'm showing you that 2770 02:01:02,719 --> 02:01:07,440 is because again 2771 02:01:05,280 --> 02:01:11,340 if you'll work with someone else's code 2772 02:01:07,440 --> 02:01:14,639 you might see either of these Solutions 2773 02:01:11,340 --> 02:01:18,000 you might see the object passed in 2774 02:01:14,639 --> 02:01:21,179 directly or you might see them 2775 02:01:18,000 --> 02:01:23,159 separately and basically 2776 02:01:21,179 --> 02:01:25,619 this is the point that I want to make in 2777 02:01:23,159 --> 02:01:29,099 this video for the most part 2778 02:01:25,619 --> 02:01:31,380 there are multiple approaches available 2779 02:01:29,099 --> 02:01:33,119 so there is no right or wrong 2780 02:01:31,380 --> 02:01:35,580 if your preference is to set up the 2781 02:01:33,119 --> 02:01:38,760 objects and pass them in this way 2782 02:01:35,580 --> 02:01:41,159 go ahead and do that if not if you want 2783 02:01:38,760 --> 02:01:43,800 to pass them directly in the code that's 2784 02:01:41,159 --> 02:01:46,739 also an option as long as the result is 2785 02:01:43,800 --> 02:01:47,820 the same it really comes down to your 2786 02:01:46,739 --> 02:01:50,699 preference 2787 02:01:47,820 --> 02:01:53,040 now why we won't use the inline Styles 2788 02:01:50,699 --> 02:01:54,480 in this course because if you ever work 2789 02:01:53,040 --> 02:01:57,300 with inline Styles you know that they're 2790 02:01:54,480 --> 02:01:59,119 somewhat of a pain so yes it's a good 2791 02:01:57,300 --> 02:02:02,219 solution here and there but in general 2792 02:01:59,119 --> 02:02:04,679 you'll want to stick with external style 2793 02:02:02,219 --> 02:02:07,440 sheet maybe some CSS libraries and all 2794 02:02:04,679 --> 02:02:09,960 that because adding styles of course one 2795 02:02:07,440 --> 02:02:11,880 by one to each element is somewhat 2796 02:02:09,960 --> 02:02:15,239 painful but please be aware of this 2797 02:02:11,880 --> 02:02:18,119 option because you might see that in 2798 02:02:15,239 --> 02:02:21,179 someone else's code all right and up 2799 02:02:18,119 --> 02:02:24,599 next let's see how we can use JavaScript 2800 02:02:21,179 --> 02:02:26,900 in jsx so in the previous video we 2801 02:02:24,599 --> 02:02:30,360 covered CSS now let's talk about 2802 02:02:26,900 --> 02:02:33,420 JavaScript and before we continue I want 2803 02:02:30,360 --> 02:02:35,639 to refactor this to a single book 2804 02:02:33,420 --> 02:02:37,320 component and I'm not going to give you 2805 02:02:35,639 --> 02:02:42,000 the entire speech one more time 2806 02:02:37,320 --> 02:02:43,980 essentially my preference is to set up a 2807 02:02:42,000 --> 02:02:46,260 component with all of these elements so 2808 02:02:43,980 --> 02:02:47,400 this is exactly what I'm going to do but 2809 02:02:46,260 --> 02:02:51,000 yes 2810 02:02:47,400 --> 02:02:53,760 you can always split it up into more 2811 02:02:51,000 --> 02:02:56,219 components so I want to navigate back 2812 02:02:53,760 --> 02:02:59,040 and essentially where I have the book 2813 02:02:56,219 --> 02:03:01,199 I'll remove all of them and basically I 2814 02:02:59,040 --> 02:03:03,719 just want to grab the elements one by 2815 02:03:01,199 --> 02:03:06,239 one of course I'll remove the other ones 2816 02:03:03,719 --> 02:03:08,400 so let me take this one out 2817 02:03:06,239 --> 02:03:12,000 so this is going to be right after image 2818 02:03:08,400 --> 02:03:15,179 and I'm also not going to use this 2819 02:03:12,000 --> 02:03:17,940 inline setup essentially I'm going to 2820 02:03:15,179 --> 02:03:20,820 add that code in CSS 2821 02:03:17,940 --> 02:03:24,000 so let me take the heading 4. 2822 02:03:20,820 --> 02:03:25,980 I'll place right after heading 2. I'll 2823 02:03:24,000 --> 02:03:28,560 move the style 2824 02:03:25,980 --> 02:03:30,599 and effectively I just want to copy 2825 02:03:28,560 --> 02:03:34,139 these Styles and set it up in the index 2826 02:03:30,599 --> 02:03:36,239 CSS and in order to speed this up I'm 2827 02:03:34,139 --> 02:03:39,300 going to grab this code as you can see 2828 02:03:36,239 --> 02:03:41,820 pretty much the same properties and 2829 02:03:39,300 --> 02:03:45,420 values I just don't want to spend time 2830 02:03:41,820 --> 02:03:48,119 on typing them so let me navigate back 2831 02:03:45,420 --> 02:03:51,480 I'll move this one and copy and paste 2832 02:03:48,119 --> 02:03:54,780 and as far as the index.js well I want 2833 02:03:51,480 --> 02:03:56,040 to remove all of this so now I have book 2834 02:03:54,780 --> 02:03:58,920 list 2835 02:03:56,040 --> 02:04:00,719 with four book components and then I 2836 02:03:58,920 --> 02:04:03,239 have a book component 2837 02:04:00,719 --> 02:04:06,780 now the only thing is that I need to 2838 02:04:03,239 --> 02:04:08,940 remove this semicolon now let's talk 2839 02:04:06,780 --> 02:04:11,520 about jsx 2840 02:04:08,940 --> 02:04:12,960 and like we already discussed in the 2841 02:04:11,520 --> 02:04:15,960 previous video 2842 02:04:12,960 --> 02:04:18,060 if we add these curlies in jsx that 2843 02:04:15,960 --> 02:04:21,360 means we're going back to the JavaScript 2844 02:04:18,060 --> 02:04:24,659 land so essentially we can use our 2845 02:04:21,360 --> 02:04:28,520 vanilla.js logic inside of the curlers 2846 02:04:24,659 --> 02:04:32,040 now one big gotcha is that the value 2847 02:04:28,520 --> 02:04:33,659 must be an expression so essentially it 2848 02:04:32,040 --> 02:04:36,960 needs to return a value and of course 2849 02:04:33,659 --> 02:04:39,239 I'll show you the example and I'll also 2850 02:04:36,960 --> 02:04:40,920 show you what happens if we provide a 2851 02:04:39,239 --> 02:04:43,380 statement so there's a difference 2852 02:04:40,920 --> 02:04:46,199 expression returns a value statement 2853 02:04:43,380 --> 02:04:48,780 does not and let's just start by setting 2854 02:04:46,199 --> 02:04:50,219 up the variable somewhere here 2855 02:04:48,780 --> 02:04:52,380 and 2856 02:04:50,219 --> 02:04:54,300 I'm going to name this title and 2857 02:04:52,380 --> 02:04:56,880 effectively where we have the heading 2 2858 02:04:54,300 --> 02:04:59,099 with the title let's pass it in so 2859 02:04:56,880 --> 02:05:00,780 notice in here we go back 2860 02:04:59,099 --> 02:05:03,659 to the JavaScript so we set up the 2861 02:05:00,780 --> 02:05:06,780 curlies and since we have the variable 2862 02:05:03,659 --> 02:05:10,980 we can directly access it and you'll see 2863 02:05:06,780 --> 02:05:12,540 what are the benefits back in the index 2864 02:05:10,980 --> 02:05:15,719 .js 2865 02:05:12,540 --> 02:05:17,099 let's go over here and let's grab this 2866 02:05:15,719 --> 02:05:20,099 value 2867 02:05:17,099 --> 02:05:22,800 for the title so let's cut it out and 2868 02:05:20,099 --> 02:05:25,980 I'll set it up over here I'll say const 2869 02:05:22,800 --> 02:05:27,900 title and again you can set it up inside 2870 02:05:25,980 --> 02:05:30,360 of the component or you can set it up 2871 02:05:27,900 --> 02:05:31,679 outside of the component in the file or 2872 02:05:30,360 --> 02:05:33,420 you can set it up in a different file 2873 02:05:31,679 --> 02:05:36,719 and then import it we just haven't 2874 02:05:33,420 --> 02:05:38,940 covered the es6 modules yet so let's set 2875 02:05:36,719 --> 02:05:41,880 it equal to a string so that doesn't 2876 02:05:38,940 --> 02:05:44,580 change and now instead of 2877 02:05:41,880 --> 02:05:47,099 card coding here in the heading two 2878 02:05:44,580 --> 02:05:50,760 we're gonna go here with title and 2879 02:05:47,099 --> 02:05:52,500 effectively whatever changes we apply 2880 02:05:50,760 --> 02:05:55,199 right away will be displayed in the 2881 02:05:52,500 --> 02:05:57,360 browser so it's already somewhat Dynamic 2882 02:05:55,199 --> 02:05:59,280 now of course we're not getting the data 2883 02:05:57,360 --> 02:06:02,760 from somewhere else from the external 2884 02:05:59,280 --> 02:06:04,440 API for example and all that stuff but 2885 02:06:02,760 --> 02:06:05,880 we're already moving in the right 2886 02:06:04,440 --> 02:06:08,159 direction 2887 02:06:05,880 --> 02:06:10,619 so when you think of dynamic think of 2888 02:06:08,159 --> 02:06:13,679 less hard coding 2889 02:06:10,619 --> 02:06:16,320 so that's the first then let's take a 2890 02:06:13,679 --> 02:06:17,040 look how we can set up the author as 2891 02:06:16,320 --> 02:06:19,560 well 2892 02:06:17,040 --> 02:06:21,000 so now let me take this value and just 2893 02:06:19,560 --> 02:06:22,080 to Showcase that it's still going to 2894 02:06:21,000 --> 02:06:24,599 work 2895 02:06:22,080 --> 02:06:27,480 I'm gonna go here and let's come up with 2896 02:06:24,599 --> 02:06:29,820 a value author copy and paste and same 2897 02:06:27,480 --> 02:06:32,400 deal we go back 2898 02:06:29,820 --> 02:06:36,860 huge JavaScript land and we simply 2899 02:06:32,400 --> 02:06:36,860 provide the variable in this case 2900 02:06:38,040 --> 02:06:43,440 then let me show you some instances 2901 02:06:41,460 --> 02:06:46,320 where this is going to fail where 2902 02:06:43,440 --> 02:06:49,500 basically if we'll set up a statement 2903 02:06:46,320 --> 02:06:53,280 it's not going to work so first let me 2904 02:06:49,500 --> 02:06:55,260 go right after the heading four and 2905 02:06:53,280 --> 02:06:57,360 we're gonna go with paragraph So if 2906 02:06:55,260 --> 02:07:02,159 we'll write a statement and if we'll say 2907 02:06:57,360 --> 02:07:04,739 let X is let's say equal to 6. pretty 2908 02:07:02,159 --> 02:07:07,139 much right away you'll get an error and 2909 02:07:04,739 --> 02:07:09,719 you'll get these quickly lines again we 2910 02:07:07,139 --> 02:07:13,020 can only pass here an expression 2911 02:07:09,719 --> 02:07:15,480 and essentially the expression returns a 2912 02:07:13,020 --> 02:07:18,239 value now this is a side note but if we 2913 02:07:15,480 --> 02:07:20,760 want to comment something out in jsx we 2914 02:07:18,239 --> 02:07:23,159 need to go with this syntax and of 2915 02:07:20,760 --> 02:07:25,199 course there is a shortcut for that so 2916 02:07:23,159 --> 02:07:27,300 in my case that is command and forward 2917 02:07:25,199 --> 02:07:31,139 slash but if you want to see in your 2918 02:07:27,300 --> 02:07:34,560 operating system go to edit and notice 2919 02:07:31,139 --> 02:07:35,880 it's going to be toggle line comment so 2920 02:07:34,560 --> 02:07:37,380 that's how we can comment out and of 2921 02:07:35,880 --> 02:07:40,619 course once I do 2922 02:07:37,380 --> 02:07:42,599 notice I don't get the error and let me 2923 02:07:40,619 --> 02:07:45,719 showcase one more time with the new 2924 02:07:42,599 --> 02:07:48,360 expression so let's say if I have 6 plus 2925 02:07:45,719 --> 02:07:51,420 6 this is going to work because again 2926 02:07:48,360 --> 02:07:54,780 this returns some kind of value 2927 02:07:51,420 --> 02:07:58,139 hopefully that is clear so every time we 2928 02:07:54,780 --> 02:08:00,780 want to go back to a JavaScript 2929 02:07:58,139 --> 02:08:04,380 we just set up the curlies and we can 2930 02:08:00,780 --> 02:08:07,320 start applying our logic and lastly let 2931 02:08:04,380 --> 02:08:09,000 me just showcase that we can do more 2932 02:08:07,320 --> 02:08:11,639 than just accessing the variables of 2933 02:08:09,000 --> 02:08:14,280 course when it comes to Strings we have 2934 02:08:11,639 --> 02:08:17,099 a cool method by the name of two 2935 02:08:14,280 --> 02:08:19,679 uppercase so we can simply provide that 2936 02:08:17,099 --> 02:08:23,340 we can invoke it and as you can see we 2937 02:08:19,679 --> 02:08:26,780 don't get the narrow the author is now 2938 02:08:23,340 --> 02:08:29,340 in uppercase now I'm going to clean up 2939 02:08:26,780 --> 02:08:30,960 basically I'll just leave these 2940 02:08:29,340 --> 02:08:33,360 variables over here 2941 02:08:30,960 --> 02:08:35,580 and remove these ones just keep in mind 2942 02:08:33,360 --> 02:08:39,119 that if you ever need a reference you do 2943 02:08:35,580 --> 02:08:41,580 have a readme so let me remove this also 2944 02:08:39,119 --> 02:08:43,139 this one as well I also want to clean up 2945 02:08:41,580 --> 02:08:46,380 this gibberish 2946 02:08:43,139 --> 02:08:49,199 and up next let's talk about props in 2947 02:08:46,380 --> 02:08:52,860 react all right and up next let's talk 2948 02:08:49,199 --> 02:08:55,739 about props and why they're so useful 2949 02:08:52,860 --> 02:08:58,679 and also so powerful 2950 02:08:55,739 --> 02:09:01,679 first let's start by doing a little bit 2951 02:08:58,679 --> 02:09:03,719 of spring cleanup or essentially I just 2952 02:09:01,679 --> 02:09:05,940 want to set up three variables with all 2953 02:09:03,719 --> 02:09:09,080 of the values so for author title and 2954 02:09:05,940 --> 02:09:12,480 image and where I have the book 2955 02:09:09,080 --> 02:09:14,820 component I want to access them so 2956 02:09:12,480 --> 02:09:16,139 pretty much the same thing again we're 2957 02:09:14,820 --> 02:09:18,179 just not 2958 02:09:16,139 --> 02:09:20,460 hard coding anymore and yes I'll place 2959 02:09:18,179 --> 02:09:22,980 it above the book list and you'll see in 2960 02:09:20,460 --> 02:09:25,860 a few videos why so let me move the 2961 02:09:22,980 --> 02:09:28,199 sticker up first then I want to do the 2962 02:09:25,860 --> 02:09:31,080 same thing with the author and now let's 2963 02:09:28,199 --> 02:09:34,380 set up that image one so const 2964 02:09:31,080 --> 02:09:37,679 const time G is equal to 2965 02:09:34,380 --> 02:09:39,480 um let's just grab this string now copy 2966 02:09:37,679 --> 02:09:41,880 and paste and now where I have the 2967 02:09:39,480 --> 02:09:44,040 source what do we do we set up the 2968 02:09:41,880 --> 02:09:46,679 curlies so now we're back in JavaScript 2969 02:09:44,040 --> 02:09:49,800 plan and we provide the variable name 2970 02:09:46,679 --> 02:09:52,020 awesome then the same we're gonna do 2971 02:09:49,800 --> 02:09:54,719 with alternative 2972 02:09:52,020 --> 02:09:57,239 so let's just provide here title and 2973 02:09:54,719 --> 02:10:00,659 before we go any further let's discuss 2974 02:09:57,239 --> 02:10:02,760 parameters and arguments and as a note 2975 02:10:00,659 --> 02:10:05,940 you can actually take this code 2976 02:10:02,760 --> 02:10:07,440 set it up anywhere in the index.js 2977 02:10:05,940 --> 02:10:09,900 and 2978 02:10:07,440 --> 02:10:12,360 effectively is going to run so if I go 2979 02:10:09,900 --> 02:10:15,239 and inspect the console I should see job 2980 02:10:12,360 --> 02:10:18,000 and developer so in vanilla Jazz for 2981 02:10:15,239 --> 02:10:20,099 every function we can provide parameters 2982 02:10:18,000 --> 02:10:22,320 when we're setting up the function and 2983 02:10:20,099 --> 02:10:24,199 then when we invoke the function we 2984 02:10:22,320 --> 02:10:26,820 provide the arguments and then of course 2985 02:10:24,199 --> 02:10:28,280 whatever logic we have in this case is 2986 02:10:26,820 --> 02:10:31,920 just a log 2987 02:10:28,280 --> 02:10:35,580 gets executed okay hopefully we're clear 2988 02:10:31,920 --> 02:10:37,500 on that and now let me remove this code 2989 02:10:35,580 --> 02:10:38,639 so what does it have to do with react 2990 02:10:37,500 --> 02:10:40,500 well 2991 02:10:38,639 --> 02:10:43,980 but it also has to be some kind of way 2992 02:10:40,500 --> 02:10:46,080 how we can pass the data down to our 2993 02:10:43,980 --> 02:10:48,179 component because at the moment yes it's 2994 02:10:46,080 --> 02:10:51,540 nice yes we're using the variables and 2995 02:10:48,179 --> 02:10:54,000 all that but ideally of course you would 2996 02:10:51,540 --> 02:10:58,139 want different 2997 02:10:54,000 --> 02:10:59,099 books rendered in the browser correct so 2998 02:10:58,139 --> 02:11:00,780 at the moment we're pretty much 2999 02:10:59,099 --> 02:11:04,080 rendering the same but we need to come 3000 02:11:00,780 --> 02:11:05,820 up with a way how we can pass data 3001 02:11:04,080 --> 02:11:08,760 different data 3002 02:11:05,820 --> 02:11:10,980 to individual components so we have the 3003 02:11:08,760 --> 02:11:13,380 same structure but the data is going to 3004 02:11:10,980 --> 02:11:17,219 be different and the way we do that is 3005 02:11:13,380 --> 02:11:20,360 by using of the props object so every 3006 02:11:17,219 --> 02:11:24,840 component we create gets automatically 3007 02:11:20,360 --> 02:11:28,800 there's props object which essentially 3008 02:11:24,840 --> 02:11:31,020 is the parameter now we can call this a 3009 02:11:28,800 --> 02:11:33,480 shake and bake it's an awesome choice 3010 02:11:31,020 --> 02:11:34,980 but a common convention is calling this 3011 02:11:33,480 --> 02:11:37,860 props 3012 02:11:34,980 --> 02:11:39,300 so again it's provided right away by 3013 02:11:37,860 --> 02:11:42,239 default so we don't need to do anything 3014 02:11:39,300 --> 02:11:44,760 so instead of creating a component and 3015 02:11:42,239 --> 02:11:46,920 then saying yeah there's going to be a 3016 02:11:44,760 --> 02:11:50,400 param it's right away provided 3017 02:11:46,920 --> 02:11:53,460 so react effectively injects that 3018 02:11:50,400 --> 02:11:56,099 so if I type here props I know that this 3019 02:11:53,460 --> 02:11:58,920 props will automatically be passed down 3020 02:11:56,099 --> 02:12:00,719 to me and now let's go with log and we 3021 02:11:58,920 --> 02:12:02,699 have a few places where we can log that 3022 02:12:00,719 --> 02:12:05,699 we can log it here in the function body 3023 02:12:02,699 --> 02:12:08,639 again above the return if you place 3024 02:12:05,699 --> 02:12:12,179 anything after returns not going to run 3025 02:12:08,639 --> 02:12:15,780 or we can do that directly here in the 3026 02:12:12,179 --> 02:12:18,119 jsx so we can go with console and log 3027 02:12:15,780 --> 02:12:19,080 now I'll actually remove this one right 3028 02:12:18,119 --> 02:12:21,659 away 3029 02:12:19,080 --> 02:12:23,099 but I just want to showcase and you know 3030 02:12:21,659 --> 02:12:25,440 there's going to be too many console 3031 02:12:23,099 --> 02:12:26,340 logs so I'll go back to two books for 3032 02:12:25,440 --> 02:12:28,500 now 3033 02:12:26,340 --> 02:12:30,420 and then eventually we'll add more so if 3034 02:12:28,500 --> 02:12:32,520 you take a look at the console you'll 3035 02:12:30,420 --> 02:12:33,840 see four objects again the reason why we 3036 02:12:32,520 --> 02:12:36,480 have four because we have two console 3037 02:12:33,840 --> 02:12:39,480 logs and we have two components so if we 3038 02:12:36,480 --> 02:12:41,580 do two times two yes we have four and 3039 02:12:39,480 --> 02:12:44,460 check it out now for now it's an empty 3040 02:12:41,580 --> 02:12:46,380 object but that's already a good start 3041 02:12:44,460 --> 02:12:49,800 that means that we'll be able to access 3042 02:12:46,380 --> 02:12:51,000 something now we have not provided that 3043 02:12:49,800 --> 02:12:54,360 something 3044 02:12:51,000 --> 02:12:56,699 so we have an empty object but it is a 3045 02:12:54,360 --> 02:12:58,980 awesome start and the way we provide 3046 02:12:56,699 --> 02:13:01,020 those values is here 3047 02:12:58,980 --> 02:13:04,079 so again 3048 02:13:01,020 --> 02:13:08,300 props here when we set up the component 3049 02:13:04,079 --> 02:13:11,579 but then when we render the component 3050 02:13:08,300 --> 02:13:13,380 that's where we provide eprops and again 3051 02:13:11,579 --> 02:13:16,380 we'll start with the manual way where I 3052 02:13:13,380 --> 02:13:18,239 can go here with job and then equals and 3053 02:13:16,380 --> 02:13:21,360 then if it's string then we need to use 3054 02:13:18,239 --> 02:13:23,400 the quotation mark if it's a number or 3055 02:13:21,360 --> 02:13:25,500 variable whatever then we need to 3056 02:13:23,400 --> 02:13:28,860 provide the curlish so 3057 02:13:25,500 --> 02:13:31,980 in here I can go job developer 3058 02:13:28,860 --> 02:13:33,780 and as far as the second book why don't 3059 02:13:31,980 --> 02:13:35,400 we change things around I'm going to say 3060 02:13:33,780 --> 02:13:38,040 here random 3061 02:13:35,400 --> 02:13:39,599 title and then the second one will be 3062 02:13:38,040 --> 02:13:41,579 number like I said 3063 02:13:39,599 --> 02:13:43,139 when it comes to numbers we need to use 3064 02:13:41,579 --> 02:13:44,880 the curlies 3065 02:13:43,139 --> 02:13:47,880 if we have a string then we just use 3066 02:13:44,880 --> 02:13:50,040 quotation marks and if let's say we want 3067 02:13:47,880 --> 02:13:52,500 to provide here the variable same deal 3068 02:13:50,040 --> 02:13:54,659 curlies and then we can access it for 3069 02:13:52,500 --> 02:13:57,239 now let's just use the number I'm going 3070 02:13:54,659 --> 02:13:59,219 to say 22. and the moment I say like I 3071 02:13:57,239 --> 02:14:01,800 said I will remove this log there's no 3072 02:13:59,219 --> 02:14:05,159 need to over complicate things 3073 02:14:01,800 --> 02:14:07,440 now what I see in the console job 3074 02:14:05,159 --> 02:14:09,900 developer so this is an object 3075 02:14:07,440 --> 02:14:12,360 that's the props object 3076 02:14:09,900 --> 02:14:17,099 and then for the second one I have title 3077 02:14:12,360 --> 02:14:19,920 and number okay so this is clear now how 3078 02:14:17,099 --> 02:14:21,179 we can access those values 3079 02:14:19,920 --> 02:14:23,699 well 3080 02:14:21,179 --> 02:14:25,440 just like any regular object it's an 3081 02:14:23,699 --> 02:14:27,239 object these are the properties and 3082 02:14:25,440 --> 02:14:30,420 these are the values what do we normally 3083 02:14:27,239 --> 02:14:33,719 do in JavaScript we go with object dot 3084 02:14:30,420 --> 02:14:37,560 property correct same deal over here 3085 02:14:33,719 --> 02:14:39,719 now since these are extra props 3086 02:14:37,560 --> 02:14:41,699 basically since we're just testing I'm 3087 02:14:39,719 --> 02:14:44,099 going to add more paragraphs and I'll 3088 02:14:41,699 --> 02:14:47,460 copy and paste essentially the most 3089 02:14:44,099 --> 02:14:49,860 basic way we go here with the curlies so 3090 02:14:47,460 --> 02:14:52,679 we go back to the JavaScript plan and we 3091 02:14:49,860 --> 02:14:55,800 go with props job 3092 02:14:52,679 --> 02:14:59,280 then we want to set up props again we're 3093 02:14:55,800 --> 02:15:04,260 referencing the same object go title and 3094 02:14:59,280 --> 02:15:07,199 then we go with props and a number let's 3095 02:15:04,260 --> 02:15:09,900 save it and don't care about the console 3096 02:15:07,199 --> 02:15:12,360 right now but you'll notice that 3097 02:15:09,900 --> 02:15:14,760 basically 3098 02:15:12,360 --> 02:15:15,840 our components now have some extra 3099 02:15:14,760 --> 02:15:17,579 values 3100 02:15:15,840 --> 02:15:19,500 now one important thing if you don't 3101 02:15:17,579 --> 02:15:21,360 provide the prop 3102 02:15:19,500 --> 02:15:22,619 even though in your structure you're 3103 02:15:21,360 --> 02:15:25,380 accessing it 3104 02:15:22,619 --> 02:15:27,960 you have nothing there so notice how the 3105 02:15:25,380 --> 02:15:30,420 first component has the job prop with 3106 02:15:27,960 --> 02:15:32,880 this value I don't see it anywhere in my 3107 02:15:30,420 --> 02:15:35,099 second component why well because it's 3108 02:15:32,880 --> 02:15:36,540 not over here it's not present in the 3109 02:15:35,099 --> 02:15:37,800 second component so that's very 3110 02:15:36,540 --> 02:15:39,420 important 3111 02:15:37,800 --> 02:15:42,300 to remember 3112 02:15:39,420 --> 02:15:44,880 the prop will be only displayed if it's 3113 02:15:42,300 --> 02:15:47,159 actually provided 3114 02:15:44,880 --> 02:15:50,280 so the first one doesn't get the title 3115 02:15:47,159 --> 02:15:51,900 or number notice nothing is rendered in 3116 02:15:50,280 --> 02:15:53,159 the browser why well because we don't 3117 02:15:51,900 --> 02:15:54,420 provide them 3118 02:15:53,159 --> 02:15:56,520 so again 3119 02:15:54,420 --> 02:15:58,380 you'll have the structure 3120 02:15:56,520 --> 02:16:00,900 you'll provide the props and then you'll 3121 02:15:58,380 --> 02:16:02,219 be able to display them if the prop is 3122 02:16:00,900 --> 02:16:03,840 not provided 3123 02:16:02,219 --> 02:16:05,760 either you're not going to display 3124 02:16:03,840 --> 02:16:07,440 anything or you'll get an error let's 3125 02:16:05,760 --> 02:16:10,260 say if you're trying to access some kind 3126 02:16:07,440 --> 02:16:11,639 of value that doesn't exist please keep 3127 02:16:10,260 --> 02:16:14,219 that in mind 3128 02:16:11,639 --> 02:16:16,560 so that should do it for 3129 02:16:14,219 --> 02:16:19,199 the generic setup again 3130 02:16:16,560 --> 02:16:22,800 we provide here the prop name then we 3131 02:16:19,199 --> 02:16:25,199 provide the value now let's use the 3132 02:16:22,800 --> 02:16:28,440 author title and image 3133 02:16:25,199 --> 02:16:31,020 so instead of the testing ones now let's 3134 02:16:28,440 --> 02:16:33,179 actually provide those values so I'm 3135 02:16:31,020 --> 02:16:36,179 going to remove all these paragraphs 3136 02:16:33,179 --> 02:16:37,439 let's save and then one by one let's 3137 02:16:36,179 --> 02:16:40,679 provide them 3138 02:16:37,439 --> 02:16:42,359 so what is the first value author now as 3139 02:16:40,679 --> 02:16:44,460 far as the prop name does it have to 3140 02:16:42,359 --> 02:16:47,519 match here no 3141 02:16:44,460 --> 02:16:49,979 you can write banana but when you're 3142 02:16:47,519 --> 02:16:51,780 accessing that's when you have to go 3143 02:16:49,979 --> 02:16:53,820 with props.banana 3144 02:16:51,780 --> 02:16:55,500 so this is the name this is the value 3145 02:16:53,820 --> 02:16:58,080 and yes 3146 02:16:55,500 --> 02:16:59,760 a common of course convention is to use 3147 02:16:58,080 --> 02:17:01,679 the same one but it's not always 3148 02:16:59,760 --> 02:17:04,260 possible so we just go with author 3149 02:17:01,679 --> 02:17:06,540 author curlies we go back to JavaScript 3150 02:17:04,260 --> 02:17:09,120 and then we provide the variable and the 3151 02:17:06,540 --> 02:17:11,760 same thing over here title is equal to 3152 02:17:09,120 --> 02:17:14,399 battle and then lastly what do we have 3153 02:17:11,760 --> 02:17:16,380 we have the image same deal we provide 3154 02:17:14,399 --> 02:17:20,460 all of them and in order to speed this 3155 02:17:16,380 --> 02:17:23,580 up I'll grab this and I'll copy and 3156 02:17:20,460 --> 02:17:26,099 paste and once we have successfully set 3157 02:17:23,580 --> 02:17:29,580 up all the props now of course instead 3158 02:17:26,099 --> 02:17:33,300 of accessing these variables directly we 3159 02:17:29,580 --> 02:17:35,040 need to go with props dot image props 3160 02:17:33,300 --> 02:17:36,899 dot title and yes of course in the 3161 02:17:35,040 --> 02:17:38,939 following videos we'll take a look how 3162 02:17:36,899 --> 02:17:41,939 we can shorten this up 3163 02:17:38,939 --> 02:17:44,820 but for now object property object 3164 02:17:41,939 --> 02:17:46,800 property and effectively it matches 3165 02:17:44,820 --> 02:17:49,979 whatever we're passing here 3166 02:17:46,800 --> 02:17:51,899 so we want to go here with props dot 3167 02:17:49,979 --> 02:17:55,620 title and then we want to go with props 3168 02:17:51,899 --> 02:17:58,979 dot author once we save if everything is 3169 02:17:55,620 --> 02:18:00,599 correct we'll have the same list 3170 02:17:58,979 --> 02:18:02,639 but the difference right now of course 3171 02:18:00,599 --> 02:18:05,639 that we are providing these values 3172 02:18:02,639 --> 02:18:07,260 dynamically which is really cool but we 3173 02:18:05,639 --> 02:18:10,800 also need to keep in mind that if we 3174 02:18:07,260 --> 02:18:12,960 don't provide some specific prop well 3175 02:18:10,800 --> 02:18:15,479 we're not going to render anything on 3176 02:18:12,960 --> 02:18:19,200 the screen check it out since the second 3177 02:18:15,479 --> 02:18:21,420 book component doesn't get the image we 3178 02:18:19,200 --> 02:18:23,280 just see the alternative hopefully that 3179 02:18:21,420 --> 02:18:24,840 is clear and in the next video we'll 3180 02:18:23,280 --> 02:18:28,500 take a look how we can 3181 02:18:24,840 --> 02:18:31,500 make our setup more Dynamic all right 3182 02:18:28,500 --> 02:18:33,179 and once we're clear on the most 3183 02:18:31,500 --> 02:18:36,300 straightforward setup 3184 02:18:33,179 --> 02:18:40,019 now let's make things more interesting 3185 02:18:36,300 --> 02:18:43,500 and create two of those objects yes 3186 02:18:40,019 --> 02:18:45,420 we won't have totally Dynamic setup yet 3187 02:18:43,500 --> 02:18:47,399 that is still coming where basically 3188 02:18:45,420 --> 02:18:49,019 we'll just iterate over the list grab 3189 02:18:47,399 --> 02:18:51,599 the values and render everything on the 3190 02:18:49,019 --> 02:18:53,820 screen but at least we'll be moving in 3191 02:18:51,599 --> 02:18:57,660 the right direction so here's what I 3192 02:18:53,820 --> 02:18:59,700 want you to do first create a object 3193 02:18:57,660 --> 02:19:02,099 with 3194 02:18:59,700 --> 02:19:04,679 the same three variables we already used 3195 02:19:02,099 --> 02:19:08,099 in the previous one so author title and 3196 02:19:04,679 --> 02:19:10,080 image now create a object in my case I'm 3197 02:19:08,099 --> 02:19:12,120 going to call this first book and set 3198 02:19:10,080 --> 02:19:13,439 them up as properties 3199 02:19:12,120 --> 02:19:16,260 then 3200 02:19:13,439 --> 02:19:20,040 create a second object and essentially 3201 02:19:16,260 --> 02:19:22,139 just go to Amazon and get those values 3202 02:19:20,040 --> 02:19:24,179 and don't worry about the local image 3203 02:19:22,139 --> 02:19:26,340 just use the external one it's going to 3204 02:19:24,179 --> 02:19:28,260 be faster unless you really want to just 3205 02:19:26,340 --> 02:19:31,200 keep in mind eventually we'll still 3206 02:19:28,260 --> 02:19:34,439 switch back to a local 3207 02:19:31,200 --> 02:19:37,019 image option using source so it doesn't 3208 02:19:34,439 --> 02:19:39,559 really matter and then we'll see how we 3209 02:19:37,019 --> 02:19:43,979 can provide unique values 3210 02:19:39,559 --> 02:19:47,040 paste on our data so let's start working 3211 02:19:43,979 --> 02:19:49,080 on that where first in the index.js I 3212 02:19:47,040 --> 02:19:51,479 want to create two 3213 02:19:49,080 --> 02:19:54,780 objects I'm going to go here with const 3214 02:19:51,479 --> 02:19:57,300 and then first book that's the object 3215 02:19:54,780 --> 02:19:59,939 and then we want to grab all of those 3216 02:19:57,300 --> 02:20:03,060 values I'm not sure whether I'm going to 3217 02:19:59,939 --> 02:20:06,359 be able to just copy and paste so I'm 3218 02:20:03,060 --> 02:20:08,640 gonna do it this way so author then we 3219 02:20:06,359 --> 02:20:12,060 want to grab your title 3220 02:20:08,640 --> 02:20:15,540 that is equal to this one 3221 02:20:12,060 --> 02:20:17,640 and then lastly I have IMG 3222 02:20:15,540 --> 02:20:20,580 and that is equal 3223 02:20:17,640 --> 02:20:21,780 to list string value we can remove the 3224 02:20:20,580 --> 02:20:24,180 properties 3225 02:20:21,780 --> 02:20:26,160 and let's just add for sure that image 3226 02:20:24,180 --> 02:20:28,920 hopefully it's clear 3227 02:20:26,160 --> 02:20:31,560 uh we need to provide all of the props 3228 02:20:28,920 --> 02:20:33,120 okay that's good as you can see we get 3229 02:20:31,560 --> 02:20:34,859 an error since there are no properties 3230 02:20:33,120 --> 02:20:35,880 anymore don't worry we'll fix that in a 3231 02:20:34,859 --> 02:20:38,640 second 3232 02:20:35,880 --> 02:20:40,439 so now we want to copy and paste and 3233 02:20:38,640 --> 02:20:42,359 let's come up with a different name of 3234 02:20:40,439 --> 02:20:43,920 course we can have two variables with 3235 02:20:42,359 --> 02:20:46,260 the same name 3236 02:20:43,920 --> 02:20:48,479 then let's navigate 3237 02:20:46,260 --> 02:20:50,460 to our list 3238 02:20:48,479 --> 02:20:54,000 and I'm just going to look for some 3239 02:20:50,460 --> 02:20:56,520 other random book and as I said this one 3240 02:20:54,000 --> 02:20:57,660 actually is a very good book again this 3241 02:20:56,520 --> 02:21:00,000 is not 3242 02:20:57,660 --> 02:21:03,540 some kind of Amazon promotion I'm just 3243 02:21:00,000 --> 02:21:06,479 saying it is a good book so let me grab 3244 02:21:03,540 --> 02:21:09,960 the title I guess first 3245 02:21:06,479 --> 02:21:12,300 then we want to grab the 3246 02:21:09,960 --> 02:21:14,100 author by the way 3247 02:21:12,300 --> 02:21:17,880 collect something 3248 02:21:14,100 --> 02:21:20,760 we want to take the title here 3249 02:21:17,880 --> 02:21:23,520 provide so that's the author and then 3250 02:21:20,760 --> 02:21:26,280 lastly we want to get that image so I'm 3251 02:21:23,520 --> 02:21:29,399 gonna go with copy image address and we 3252 02:21:26,280 --> 02:21:32,520 just want to provide it here so copy and 3253 02:21:29,399 --> 02:21:35,220 paste and now instead of accessing the 3254 02:21:32,520 --> 02:21:37,680 variables we're gonna go with first book 3255 02:21:35,220 --> 02:21:40,620 or the second book so either of these 3256 02:21:37,680 --> 02:21:43,080 objects and we can probably put already 3257 02:21:40,620 --> 02:21:44,640 two and two together we'll use basically 3258 02:21:43,080 --> 02:21:47,880 one object 3259 02:21:44,640 --> 02:21:48,960 per component and essentially we just 3260 02:21:47,880 --> 02:21:51,600 want to access 3261 02:21:48,960 --> 02:21:54,240 those properties so instead of author 3262 02:21:51,600 --> 02:21:56,399 I'm going to go here with the first book 3263 02:21:54,240 --> 02:21:58,680 and in order to speed this up I'll just 3264 02:21:56,399 --> 02:22:02,640 set up multiple cursors in my case that 3265 02:21:58,680 --> 02:22:05,040 is with option and the mouse click and 3266 02:22:02,640 --> 02:22:08,399 I'll just say first 3267 02:22:05,040 --> 02:22:10,680 book Dot and then whatever property and 3268 02:22:08,399 --> 02:22:13,800 essentially we want to repeat 3269 02:22:10,680 --> 02:22:15,359 for a second component as well so again 3270 02:22:13,800 --> 02:22:18,000 same deal 3271 02:22:15,359 --> 02:22:21,899 we use multiple cursors 3272 02:22:18,000 --> 02:22:25,140 second book and then dot once we save 3273 02:22:21,899 --> 02:22:26,580 check it out now we have already a 3274 02:22:25,140 --> 02:22:29,880 dynamic list 3275 02:22:26,580 --> 02:22:32,640 again it's not totally Dynamic but we're 3276 02:22:29,880 --> 02:22:35,640 moving in the right direction and if you 3277 02:22:32,640 --> 02:22:37,979 take a look at our console log you'll 3278 02:22:35,640 --> 02:22:40,680 see the values that we're getting 3279 02:22:37,979 --> 02:22:42,899 so these are already different values 3280 02:22:40,680 --> 02:22:45,420 the structure is the same but since 3281 02:22:42,899 --> 02:22:47,700 we're providing different values to our 3282 02:22:45,420 --> 02:22:50,580 props the result that we see on the 3283 02:22:47,700 --> 02:22:53,040 screen is also different 3284 02:22:50,580 --> 02:22:55,740 structure is the same the result is 3285 02:22:53,040 --> 02:22:58,800 different since the values the data that 3286 02:22:55,740 --> 02:23:01,140 we're providing is different right and 3287 02:22:58,800 --> 02:23:04,020 up next let's take a breather and let's 3288 02:23:01,140 --> 02:23:07,439 discuss multiple approaches how we can 3289 02:23:04,020 --> 02:23:08,760 access props in the component and before 3290 02:23:07,439 --> 02:23:11,399 we continue 3291 02:23:08,760 --> 02:23:14,280 let me just make something extremely 3292 02:23:11,399 --> 02:23:16,439 clear there is no right or wrong 3293 02:23:14,280 --> 02:23:18,479 again I know you're probably sick of me 3294 02:23:16,439 --> 02:23:19,680 saying this but it really comes down to 3295 02:23:18,479 --> 02:23:21,359 your preference the reason why I'm 3296 02:23:19,680 --> 02:23:23,520 showing multiple approaches because I 3297 02:23:21,359 --> 02:23:25,560 want you to be aware 3298 02:23:23,520 --> 02:23:27,120 just in case you see that in someone 3299 02:23:25,560 --> 02:23:30,660 else's code 3300 02:23:27,120 --> 02:23:33,600 also in this video we will highly highly 3301 02:23:30,660 --> 02:23:36,420 highly lean on the the structuring 3302 02:23:33,600 --> 02:23:38,939 concept in vanilla Jazz and if you're 3303 02:23:36,420 --> 02:23:39,780 not familiar with the concept or if you 3304 02:23:38,939 --> 02:23:43,020 see 3305 02:23:39,780 --> 02:23:45,960 a feature during the video that maybe is 3306 02:23:43,020 --> 02:23:49,020 new to you I suggest watching this video 3307 02:23:45,960 --> 02:23:52,380 the JS nuggets playlist on my YouTube 3308 02:23:49,020 --> 02:23:54,960 channel and the link is over here and in 3309 02:23:52,380 --> 02:23:58,920 there we cover everything in vanilla.js 3310 02:23:54,960 --> 02:24:02,220 setup and in more detail and ensure the 3311 02:23:58,920 --> 02:24:04,920 structuring in vanilla.js just allows us 3312 02:24:02,220 --> 02:24:07,979 to pull out the properties 3313 02:24:04,920 --> 02:24:10,620 so we don't need to reference the object 3314 02:24:07,979 --> 02:24:12,899 anymore and in the long run it just 3315 02:24:10,620 --> 02:24:15,720 saves us time as far as the example 3316 02:24:12,899 --> 02:24:18,060 again if you want to copy and paste you 3317 02:24:15,720 --> 02:24:21,359 can definitely do so you can set it up 3318 02:24:18,060 --> 02:24:23,399 in the index.js but I'm just gonna 3319 02:24:21,359 --> 02:24:24,720 show you here in the readme that if we 3320 02:24:23,399 --> 02:24:27,660 have an object with some kind of 3321 02:24:24,720 --> 02:24:29,340 properties instead of doing this so the 3322 02:24:27,660 --> 02:24:31,319 object name and then the property we can 3323 02:24:29,340 --> 02:24:32,819 just pull them out so that's the the 3324 02:24:31,319 --> 02:24:35,939 structuring we need to reference of 3325 02:24:32,819 --> 02:24:37,800 course the main object and here we go 3326 02:24:35,939 --> 02:24:41,399 with whatever properties we want to pull 3327 02:24:37,800 --> 02:24:44,040 out and then in rest of the code 3328 02:24:41,399 --> 02:24:46,800 we just need to reference these two so 3329 02:24:44,040 --> 02:24:51,240 we don't have to go anymore with some 3330 02:24:46,800 --> 02:24:54,300 object and then dot name and in our case 3331 02:24:51,240 --> 02:24:57,420 it just allows us to do this 3332 02:24:54,300 --> 02:24:59,939 where we have the perhaps object and 3333 02:24:57,420 --> 02:25:03,060 instead of going props.props dot props 3334 02:24:59,939 --> 02:25:05,700 dot we can simply go with props pull out 3335 02:25:03,060 --> 02:25:07,979 the properties that we know exist over 3336 02:25:05,700 --> 02:25:10,200 there again 3337 02:25:07,979 --> 02:25:12,420 let me repeat one more time you cannot 3338 02:25:10,200 --> 02:25:14,819 magically access the prop if it's not 3339 02:25:12,420 --> 02:25:16,680 there so if you don't provide when you 3340 02:25:14,819 --> 02:25:19,200 set up the component 3341 02:25:16,680 --> 02:25:21,600 it's not going to be there 3342 02:25:19,200 --> 02:25:24,479 hopefully that is clear and essentially 3343 02:25:21,600 --> 02:25:26,700 then we can just go with the property 3344 02:25:24,479 --> 02:25:27,960 name so let's try it out let me navigate 3345 02:25:26,700 --> 02:25:31,200 here 3346 02:25:27,960 --> 02:25:32,460 I know that I'll access over there 3347 02:25:31,200 --> 02:25:33,660 three 3348 02:25:32,460 --> 02:25:36,500 props 3349 02:25:33,660 --> 02:25:39,660 so object with three properties image 3350 02:25:36,500 --> 02:25:43,560 title and author 3351 02:25:39,660 --> 02:25:46,380 and that is equal to a propped object so 3352 02:25:43,560 --> 02:25:48,000 what do we need to do well now we just 3353 02:25:46,380 --> 02:25:51,180 need to remove 3354 02:25:48,000 --> 02:25:53,040 all the prop instances that's it let's 3355 02:25:51,180 --> 02:25:54,720 save and if everything is correct the 3356 02:25:53,040 --> 02:25:57,840 result is going to be exactly the same 3357 02:25:54,720 --> 02:26:00,420 the difference well we need to type less 3358 02:25:57,840 --> 02:26:04,200 we just reference these properties once 3359 02:26:00,420 --> 02:26:06,540 and we're good to go now alternatively 3360 02:26:04,200 --> 02:26:08,580 and this is something that I do discuss 3361 02:26:06,540 --> 02:26:10,800 in the video so again if this is new to 3362 02:26:08,580 --> 02:26:14,640 you I strongly suggest referencing that 3363 02:26:10,800 --> 02:26:18,060 video we can also destructure 3364 02:26:14,640 --> 02:26:21,660 in function parameters so in our case 3365 02:26:18,060 --> 02:26:23,939 that's the props object now keep in mind 3366 02:26:21,660 --> 02:26:26,340 though once we do that if you'll try to 3367 02:26:23,939 --> 02:26:29,340 cancel log props it's not going to be 3368 02:26:26,340 --> 02:26:30,960 defined so what am I talking about well 3369 02:26:29,340 --> 02:26:32,040 this is a function 3370 02:26:30,960 --> 02:26:34,500 right 3371 02:26:32,040 --> 02:26:38,220 and at the moment we have props 3372 02:26:34,500 --> 02:26:41,939 parameter now since it's an object we 3373 02:26:38,220 --> 02:26:44,280 destructured inside of the function body 3374 02:26:41,939 --> 02:26:46,979 instead I'm going to say all right I 3375 02:26:44,280 --> 02:26:49,380 know that the object is going to be 3376 02:26:46,979 --> 02:26:51,420 there and I right away want to pull 3377 02:26:49,380 --> 02:26:53,819 these properties out 3378 02:26:51,420 --> 02:26:55,200 Again Properties need to match exactly 3379 02:26:53,819 --> 02:26:57,540 and everything 3380 02:26:55,200 --> 02:26:59,880 but essentially we don't need to add 3381 02:26:57,540 --> 02:27:03,359 this line of code we can do directly 3382 02:26:59,880 --> 02:27:04,500 here in the function parameters let's 3383 02:27:03,359 --> 02:27:07,439 remove this 3384 02:27:04,500 --> 02:27:10,200 save and check it out again everything 3385 02:27:07,439 --> 02:27:12,600 still works and like I mentioned 3386 02:27:10,200 --> 02:27:14,580 in the beginning of the video it really 3387 02:27:12,600 --> 02:27:17,520 comes down to your preference you'll see 3388 02:27:14,580 --> 02:27:20,340 some people who use this type of setup 3389 02:27:17,520 --> 02:27:23,880 and you'll see some that implement this 3390 02:27:20,340 --> 02:27:26,880 one and it really comes down to 3391 02:27:23,880 --> 02:27:28,920 what is your preference 3392 02:27:26,880 --> 02:27:32,100 which code makes the most sense to you 3393 02:27:28,920 --> 02:27:35,780 at the end of the day they deliver the 3394 02:27:32,100 --> 02:27:38,700 exact same result instead of using 3395 02:27:35,780 --> 02:27:39,780 props.props.props Dot we just grab the 3396 02:27:38,700 --> 02:27:44,580 prop 3397 02:27:39,780 --> 02:27:46,740 and set it up in our jsx okay and up 3398 02:27:44,580 --> 02:27:48,540 next let's discuss special prop in react 3399 02:27:46,740 --> 02:27:50,939 called children 3400 02:27:48,540 --> 02:27:51,899 which provides access to everything we 3401 02:27:50,939 --> 02:27:54,960 render 3402 02:27:51,899 --> 02:27:57,359 between component tags as I say note the 3403 02:27:54,960 --> 02:28:00,899 goal of this video is just to showcase 3404 02:27:57,359 --> 02:28:03,240 the general concept of children prop 3405 02:28:00,899 --> 02:28:06,240 since honestly at least during the 3406 02:28:03,240 --> 02:28:09,000 course we'll only use it when we need to 3407 02:28:06,240 --> 02:28:12,180 set up context API 3408 02:28:09,000 --> 02:28:15,359 so if by the end of the video you're 3409 02:28:12,180 --> 02:28:18,240 still iffy on some stuff or just don't 3410 02:28:15,359 --> 02:28:20,819 see bunch of use cases for children prop 3411 02:28:18,240 --> 02:28:23,180 it's totally okay 3412 02:28:20,819 --> 02:28:27,899 we'll return to this concept 3413 02:28:23,180 --> 02:28:30,359 when we cover context API 3414 02:28:27,899 --> 02:28:33,840 and essentially let's imagine this 3415 02:28:30,359 --> 02:28:38,160 scenario what if for some reason 3416 02:28:33,840 --> 02:28:40,979 I want to render paragraph and a button 3417 02:28:38,160 --> 02:28:44,520 in one of the components it doesn't 3418 02:28:40,979 --> 02:28:47,520 really matter which one just one of them 3419 02:28:44,520 --> 02:28:51,300 well you can say we can go to a book 3420 02:28:47,520 --> 02:28:54,479 component let's add some dummy text 3421 02:28:51,300 --> 02:28:58,200 and let's also add a button 3422 02:28:54,479 --> 02:29:01,020 and let's just say click me let's save 3423 02:28:58,200 --> 02:29:03,120 and everything is awesome but of course 3424 02:29:01,020 --> 02:29:07,020 since we're adding it 3425 02:29:03,120 --> 02:29:10,140 to the component all the instances get 3426 02:29:07,020 --> 02:29:13,020 the paragraph as well as the button 3427 02:29:10,140 --> 02:29:14,520 so what's the solution well we could 3428 02:29:13,020 --> 02:29:16,620 render it 3429 02:29:14,520 --> 02:29:20,700 in between the tags we just need to pick 3430 02:29:16,620 --> 02:29:23,100 the component so in my case I'm gonna go 3431 02:29:20,700 --> 02:29:24,780 with the second one or I'm sorry I'm 3432 02:29:23,100 --> 02:29:26,460 working with the first one I'm telling 3433 02:29:24,780 --> 02:29:28,380 you the second one so I want to do that 3434 02:29:26,460 --> 02:29:31,020 with the first one but again it doesn't 3435 02:29:28,380 --> 02:29:33,960 really matter which one we just want to 3436 02:29:31,020 --> 02:29:36,899 create a opening and closing tag 3437 02:29:33,960 --> 02:29:39,180 then and as I know this is the incorrect 3438 02:29:36,899 --> 02:29:41,939 syntax we want to do it this way and 3439 02:29:39,180 --> 02:29:44,520 then place the paragraph and the button 3440 02:29:41,939 --> 02:29:45,540 in between those Stacks so let's get it 3441 02:29:44,520 --> 02:29:48,359 out here 3442 02:29:45,540 --> 02:29:51,479 and let's set it up and the moment I 3443 02:29:48,359 --> 02:29:53,939 save I actually don't see them in any of 3444 02:29:51,479 --> 02:29:55,859 the components so what's up with that 3445 02:29:53,939 --> 02:29:59,040 well you see 3446 02:29:55,859 --> 02:30:02,640 if we want to render something that is 3447 02:29:59,040 --> 02:30:05,160 between the component tags we need to 3448 02:30:02,640 --> 02:30:08,220 use a special prop 3449 02:30:05,160 --> 02:30:11,040 and the name is children 3450 02:30:08,220 --> 02:30:13,439 now before we access the children let me 3451 02:30:11,040 --> 02:30:15,780 just make something really clear 3452 02:30:13,439 --> 02:30:18,000 since we're already destructuring in the 3453 02:30:15,780 --> 02:30:20,660 component since we're accessing the 3454 02:30:18,000 --> 02:30:22,859 object we'll need to provide the prop 3455 02:30:20,660 --> 02:30:24,960 over here 3456 02:30:22,859 --> 02:30:26,819 now if you still have this setup where 3457 02:30:24,960 --> 02:30:28,859 you have props you can log it and you'll 3458 02:30:26,819 --> 02:30:30,600 see the children and that's why I will 3459 02:30:28,859 --> 02:30:33,420 switch to this one to the second 3460 02:30:30,600 --> 02:30:34,560 approach just so you can see that one 3461 02:30:33,420 --> 02:30:36,960 component 3462 02:30:34,560 --> 02:30:39,479 has the props and the other one doesn't 3463 02:30:36,960 --> 02:30:41,640 again you can use either of these 3464 02:30:39,479 --> 02:30:43,319 approaches just remember that children 3465 02:30:41,640 --> 02:30:45,720 is going to be over here 3466 02:30:43,319 --> 02:30:48,060 if you're destructuring right away in 3467 02:30:45,720 --> 02:30:49,859 the parameters so let me just showcase 3468 02:30:48,060 --> 02:30:53,760 that so we go here with children prop 3469 02:30:49,859 --> 02:30:56,040 and the name must be exact so don't type 3470 02:30:53,760 --> 02:30:58,859 here children like this don't say 3471 02:30:56,040 --> 02:31:00,540 children's or whatever no it has to be 3472 02:30:58,859 --> 02:31:02,880 children's so this is an extremely 3473 02:31:00,540 --> 02:31:05,160 important point it's a special prop 3474 02:31:02,880 --> 02:31:07,620 provided by react so we cannot use 3475 02:31:05,160 --> 02:31:10,080 whatever name we like we have to use 3476 02:31:07,620 --> 02:31:12,780 children and if we want to render the 3477 02:31:10,080 --> 02:31:13,800 children we simply go with curlies and 3478 02:31:12,780 --> 02:31:17,460 then 3479 02:31:13,800 --> 02:31:19,920 access the prop let's save it and now 3480 02:31:17,460 --> 02:31:21,979 you'll notice that the first one has the 3481 02:31:19,920 --> 02:31:25,800 paragraph as well as the button 3482 02:31:21,979 --> 02:31:27,960 now we can move this up and down as we 3483 02:31:25,800 --> 02:31:30,840 like so let's say if I want to place it 3484 02:31:27,960 --> 02:31:34,439 on top just render Children First 3485 02:31:30,840 --> 02:31:37,920 again there's really no rule for it 3486 02:31:34,439 --> 02:31:39,840 really depends on your setup and now let 3487 02:31:37,920 --> 02:31:43,500 me refactor it just so we can see 3488 02:31:39,840 --> 02:31:45,960 basically a props and that way I can 3489 02:31:43,500 --> 02:31:49,260 showcase the children so let me cut this 3490 02:31:45,960 --> 02:31:52,740 one out I'll say Here Comes copy and 3491 02:31:49,260 --> 02:31:55,620 paste let's set it equal to props 3492 02:31:52,740 --> 02:31:57,720 props over here and let's log it 3493 02:31:55,620 --> 02:31:59,700 so let's go over here with log and let's 3494 02:31:57,720 --> 02:32:01,920 say props 3495 02:31:59,700 --> 02:32:04,380 and once I open up the developer tools 3496 02:32:01,920 --> 02:32:07,200 and console notice over here 3497 02:32:04,380 --> 02:32:09,600 so I have two objects but the first one 3498 02:32:07,200 --> 02:32:13,680 actually has this children 3499 02:32:09,600 --> 02:32:16,439 so notice special prop and essentially 3500 02:32:13,680 --> 02:32:18,060 we just need to set it up in our jsx now 3501 02:32:16,439 --> 02:32:19,500 the second one doesn't have that 3502 02:32:18,060 --> 02:32:21,899 children prop 3503 02:32:19,500 --> 02:32:23,700 so as a result it doesn't render 3504 02:32:21,899 --> 02:32:25,439 anything 3505 02:32:23,700 --> 02:32:28,080 wait let me just showcase how it's going 3506 02:32:25,439 --> 02:32:30,540 to look like on a big screen so 3507 02:32:28,080 --> 02:32:31,859 basically we have these two cards and 3508 02:32:30,540 --> 02:32:34,920 optionally 3509 02:32:31,859 --> 02:32:38,160 if you want to 3510 02:32:34,920 --> 02:32:39,960 set up your cards in a way that they're 3511 02:32:38,160 --> 02:32:43,140 not stretching we just need to add a 3512 02:32:39,960 --> 02:32:46,200 little bit of CSS logic so 3513 02:32:43,140 --> 02:32:48,300 let's navigate to index CSS 3514 02:32:46,200 --> 02:32:50,880 we have book 3515 02:32:48,300 --> 02:32:53,340 heading four again I'm not going to keep 3516 02:32:50,880 --> 02:32:54,240 that paragraph and a button but just in 3517 02:32:53,340 --> 02:32:56,040 case 3518 02:32:54,240 --> 02:32:58,439 you want to keep it for your reference 3519 02:32:56,040 --> 02:33:02,700 we can add here margin 3520 02:32:58,439 --> 02:33:05,280 1 RM 0 and 0.5 3521 02:33:02,700 --> 02:33:09,660 so that's for the paragraph and then 3522 02:33:05,280 --> 02:33:12,540 if you want all your cards to 3523 02:33:09,660 --> 02:33:14,819 basically start at one point and just 3524 02:33:12,540 --> 02:33:18,000 end where the content ends you can go 3525 02:33:14,819 --> 02:33:20,160 here with align items and start and 3526 02:33:18,000 --> 02:33:23,340 essentially or at least it should work 3527 02:33:20,160 --> 02:33:25,439 not the line itself sorry align 3528 02:33:23,340 --> 02:33:28,319 and items and then start and once you 3529 02:33:25,439 --> 02:33:30,240 say notice so essentially your cards are 3530 02:33:28,319 --> 02:33:32,939 just going to be spanning as much as the 3531 02:33:30,240 --> 02:33:34,380 content instead of stretching so by 3532 02:33:32,939 --> 02:33:37,020 default 3533 02:33:34,380 --> 02:33:38,880 when it comes to CSS grid it stretches 3534 02:33:37,020 --> 02:33:40,800 all the items so if this one has a bunch 3535 02:33:38,880 --> 02:33:43,140 of content then this one will also 3536 02:33:40,800 --> 02:33:45,120 stretch like I said in my case I'm not 3537 02:33:43,140 --> 02:33:46,740 going to keep it if you ever need a 3538 02:33:45,120 --> 02:33:50,640 reference you can always find it in the 3539 02:33:46,740 --> 02:33:53,280 readme so I'll leave I guess the props 3540 02:33:50,640 --> 02:33:54,540 in such a way but I'll remove the 3541 02:33:53,280 --> 02:33:57,780 children 3542 02:33:54,540 --> 02:33:59,460 I'll move children over here I'll keep 3543 02:33:57,780 --> 02:34:01,319 the log because 3544 02:33:59,460 --> 02:34:03,359 pretty often we use it and now let me 3545 02:34:01,319 --> 02:34:06,240 remove the paragraph as well as the 3546 02:34:03,359 --> 02:34:08,520 button so that's the general concept 3547 02:34:06,240 --> 02:34:11,340 behind children which again essentially 3548 02:34:08,520 --> 02:34:13,620 allows us to access everything we are 3549 02:34:11,340 --> 02:34:16,979 rendering between the component tags 3550 02:34:13,620 --> 02:34:21,120 okay and now let's finally set up a 3551 02:34:16,979 --> 02:34:24,120 proper list and iterate over it which 3552 02:34:21,120 --> 02:34:25,620 will be our actual setup in all the 3553 02:34:24,120 --> 02:34:28,500 course projects 3554 02:34:25,620 --> 02:34:32,280 and most likely in all your other 3555 02:34:28,500 --> 02:34:36,000 projects as well so yes finally we have 3556 02:34:32,280 --> 02:34:39,000 arrived at the list which is going to be 3557 02:34:36,000 --> 02:34:40,500 our typical approach as they said now 3558 02:34:39,000 --> 02:34:43,560 during this video we'll utilize the rain 3559 02:34:40,500 --> 02:34:46,500 method called map and if you're not 3560 02:34:43,560 --> 02:34:49,680 familiar with it please reference this 3561 02:34:46,500 --> 02:34:52,800 JavaScript nuggets video where I cover 3562 02:34:49,680 --> 02:34:55,140 it in great detail 3563 02:34:52,800 --> 02:34:56,640 and essentially we'll start with 3564 02:34:55,140 --> 02:34:58,020 refactoring 3565 02:34:56,640 --> 02:35:00,780 first 3566 02:34:58,020 --> 02:35:02,640 I want to create an array in my case I'm 3567 02:35:00,780 --> 02:35:05,220 going to call it books and then let's 3568 02:35:02,640 --> 02:35:07,220 remove everything from the book list 3569 02:35:05,220 --> 02:35:11,460 and after that I'll show you an error 3570 02:35:07,220 --> 02:35:14,760 that we're gonna get if we try to render 3571 02:35:11,460 --> 02:35:16,140 objects so let's get cracking I'm going 3572 02:35:14,760 --> 02:35:18,240 to navigate back 3573 02:35:16,140 --> 02:35:20,399 I have two objects now I want to place 3574 02:35:18,240 --> 02:35:23,819 them in the array 3575 02:35:20,399 --> 02:35:26,340 so I'm going to say const and books so 3576 02:35:23,819 --> 02:35:28,260 that's my array and now let's Place both 3577 02:35:26,340 --> 02:35:30,479 of these objects in there so we don't 3578 02:35:28,260 --> 02:35:31,740 need the names anymore basically we can 3579 02:35:30,479 --> 02:35:34,859 just remove this 3580 02:35:31,740 --> 02:35:38,460 and this we can add right away comma 3581 02:35:34,859 --> 02:35:41,720 and we can remove this part as well as 3582 02:35:38,460 --> 02:35:44,760 the semicolon so now I have my structure 3583 02:35:41,720 --> 02:35:48,240 effectively I have an array 3584 02:35:44,760 --> 02:35:52,260 of items and each item is that object 3585 02:35:48,240 --> 02:35:54,000 now also we right away want to remove 3586 02:35:52,260 --> 02:35:56,399 everything that we have in the book list 3587 02:35:54,000 --> 02:35:58,920 otherwise of course we're going to get 3588 02:35:56,399 --> 02:36:02,880 the error and now let me showcase what 3589 02:35:58,920 --> 02:36:06,300 happens if we try to render objects in 3590 02:36:02,880 --> 02:36:08,460 react so if I go with the correlation so 3591 02:36:06,300 --> 02:36:10,560 I'm going back to JavaScript and I'm 3592 02:36:08,460 --> 02:36:11,819 going to say box so I want to access the 3593 02:36:10,560 --> 02:36:15,180 books variable 3594 02:36:11,819 --> 02:36:18,660 and then if we take a look at the 3595 02:36:15,180 --> 02:36:21,000 console we'll see these giant error 3596 02:36:18,660 --> 02:36:24,960 messages and essentially here's the deal 3597 02:36:21,000 --> 02:36:28,380 in react we cannot render 3598 02:36:24,960 --> 02:36:31,140 object directly in the jsx and I'm 3599 02:36:28,380 --> 02:36:34,439 purposely showing you this error because 3600 02:36:31,140 --> 02:36:36,899 throughout your react Journey 3601 02:36:34,439 --> 02:36:38,880 when you work with data here and there 3602 02:36:36,899 --> 02:36:41,399 you might see this error so the first 3603 02:36:38,880 --> 02:36:43,080 thing you want to do is double check the 3604 02:36:41,399 --> 02:36:45,300 data that you're rendering 3605 02:36:43,080 --> 02:36:48,120 if you see objects are not valid as 3606 02:36:45,300 --> 02:36:49,020 react child blah blah blah blah that 3607 02:36:48,120 --> 02:36:51,479 means 3608 02:36:49,020 --> 02:36:54,120 you need to double check your data 3609 02:36:51,479 --> 02:36:56,580 essentially somewhere in your data you 3610 02:36:54,120 --> 02:37:00,240 have an object and we cannot directly 3611 02:36:56,580 --> 02:37:02,040 display it in jsx now what's the 3612 02:37:00,240 --> 02:37:04,859 solution well eventually we'll use map 3613 02:37:02,040 --> 02:37:05,819 method we'll iterate over pull out the 3614 02:37:04,859 --> 02:37:09,600 properties 3615 02:37:05,819 --> 02:37:12,960 and one by one basically pass them to a 3616 02:37:09,600 --> 02:37:15,240 book component but before we do that 3617 02:37:12,960 --> 02:37:18,720 let me just show you step by step how we 3618 02:37:15,240 --> 02:37:21,780 can work with arrays in react so first 3619 02:37:18,720 --> 02:37:23,280 let me just remove this error again for 3620 02:37:21,780 --> 02:37:24,180 time being we won't see anything on 3621 02:37:23,280 --> 02:37:27,720 screen 3622 02:37:24,180 --> 02:37:32,280 and let's just create a basic array 3623 02:37:27,720 --> 02:37:34,740 let's say const names noun is equal to I 3624 02:37:32,280 --> 02:37:37,560 don't know John Peter and Susan why not 3625 02:37:34,740 --> 02:37:41,520 I'm not being particularly original but 3626 02:37:37,560 --> 02:37:43,380 hey it's Thursday and I think that 3627 02:37:41,520 --> 02:37:45,420 regular names are good enough if you 3628 02:37:43,380 --> 02:37:49,680 want to go on shake and bake Bobby Lee 3629 02:37:45,420 --> 02:37:51,899 of course you can do that and if I'm 3630 02:37:49,680 --> 02:37:54,720 gonna go back to my book list and if I'm 3631 02:37:51,899 --> 02:37:56,640 gonna change and I'll say names once I 3632 02:37:54,720 --> 02:37:58,680 save I can see that everything is nicely 3633 02:37:56,640 --> 02:38:01,680 displayed on the screen 3634 02:37:58,680 --> 02:38:03,899 so we can definitely render array 3635 02:38:01,680 --> 02:38:04,800 but most likely you will agree with me 3636 02:38:03,899 --> 02:38:08,760 that 3637 02:38:04,800 --> 02:38:11,880 it would be nicer if we could wrap these 3638 02:38:08,760 --> 02:38:14,100 values in some kind of HTML because 3639 02:38:11,880 --> 02:38:17,280 essentially at the moment I just have 3640 02:38:14,100 --> 02:38:19,979 here this string so again yes I'm going 3641 02:38:17,280 --> 02:38:22,200 to go back to the root I'll take a look 3642 02:38:19,979 --> 02:38:24,720 at the book list and notice effectively 3643 02:38:22,200 --> 02:38:27,960 I just have these string values 3644 02:38:24,720 --> 02:38:29,220 so how we can wrap this in some kind of 3645 02:38:27,960 --> 02:38:31,800 element 3646 02:38:29,220 --> 02:38:35,340 so instead of displaying these strings 3647 02:38:31,800 --> 02:38:38,760 essentially we'll display elements well 3648 02:38:35,340 --> 02:38:40,200 this is where the map method is going to 3649 02:38:38,760 --> 02:38:43,859 come into play 3650 02:38:40,200 --> 02:38:46,979 so let me show you first in the readme 3651 02:38:43,859 --> 02:38:50,520 basically react method creates a new 3652 02:38:46,979 --> 02:38:52,920 array from calling a function for every 3653 02:38:50,520 --> 02:38:56,280 array element so 3654 02:38:52,920 --> 02:38:59,760 I have names array I can use the map 3655 02:38:56,280 --> 02:39:03,479 method and I'm going to call a callback 3656 02:38:59,760 --> 02:39:05,960 function for every item in the array and 3657 02:39:03,479 --> 02:39:08,640 this is going to return a new array 3658 02:39:05,960 --> 02:39:10,140 and eventually 3659 02:39:08,640 --> 02:39:12,420 we'll wrap 3660 02:39:10,140 --> 02:39:14,220 our items 3661 02:39:12,420 --> 02:39:16,859 the moment these strings 3662 02:39:14,220 --> 02:39:19,920 in the heading one so let's try this one 3663 02:39:16,859 --> 02:39:22,979 out back in the index.js I'm going to go 3664 02:39:19,920 --> 02:39:26,340 here with const and then new 3665 02:39:22,979 --> 02:39:30,300 names then let's set it equal to names 3666 02:39:26,340 --> 02:39:32,760 map so again this returns a new array we 3667 02:39:30,300 --> 02:39:35,359 pass in the Callback function and 3668 02:39:32,760 --> 02:39:38,040 essentially the first parameter 3669 02:39:35,359 --> 02:39:39,720 references the item so if the item is 3670 02:39:38,040 --> 02:39:42,300 going to be an object it will reference 3671 02:39:39,720 --> 02:39:44,880 the object if the item is string 3672 02:39:42,300 --> 02:39:47,640 it's going to reference that string and 3673 02:39:44,880 --> 02:39:49,380 hopefully we are on the same page and 3674 02:39:47,640 --> 02:39:50,880 since it's a parameter we can name it 3675 02:39:49,380 --> 02:39:53,520 whatever we want in my case I'm going to 3676 02:39:50,880 --> 02:39:56,520 call this name and yes this is the case 3677 02:39:53,520 --> 02:39:57,899 where you can call this a vegetable as 3678 02:39:56,520 --> 02:40:00,000 long as you reference it correctly 3679 02:39:57,899 --> 02:40:02,760 you'll be good to go before we do 3680 02:40:00,000 --> 02:40:05,939 anything let's just see what are those 3681 02:40:02,760 --> 02:40:09,540 values so console log what do you know I 3682 02:40:05,939 --> 02:40:11,340 have John Peter and Susan so those are 3683 02:40:09,540 --> 02:40:13,560 the string values 3684 02:40:11,340 --> 02:40:17,340 and what's really cool that we can 3685 02:40:13,560 --> 02:40:21,020 effectively just set up a return 3686 02:40:17,340 --> 02:40:24,060 and we want to return a HTML element 3687 02:40:21,020 --> 02:40:26,819 wrapping that specific 3688 02:40:24,060 --> 02:40:28,680 string of course with objects it's going 3689 02:40:26,819 --> 02:40:31,260 to be a little bit different but for now 3690 02:40:28,680 --> 02:40:33,899 we simply want to go here with return 3691 02:40:31,260 --> 02:40:35,280 and then whatever element you want in my 3692 02:40:33,899 --> 02:40:38,160 case I'm going to go with heading one 3693 02:40:35,280 --> 02:40:40,979 and now I want to access it so now I 3694 02:40:38,160 --> 02:40:43,920 want to say you know what grab me this 3695 02:40:40,979 --> 02:40:46,319 name parameter and before we render 3696 02:40:43,920 --> 02:40:49,080 anything let me just showcase what is 3697 02:40:46,319 --> 02:40:50,819 the value for new names let's save it 3698 02:40:49,080 --> 02:40:53,520 and in the console you'll see a new 3699 02:40:50,819 --> 02:40:54,899 array which is of course awesome and 3700 02:40:53,520 --> 02:40:57,300 then check it out 3701 02:40:54,899 --> 02:40:59,280 now I have those react elements 3702 02:40:57,300 --> 02:41:00,359 so essentially instead of doing this 3703 02:40:59,280 --> 02:41:02,460 manually 3704 02:41:00,359 --> 02:41:05,160 we iterate over the list 3705 02:41:02,460 --> 02:41:08,040 we wrap it in some kind of element and 3706 02:41:05,160 --> 02:41:11,220 we're good to go and as a result 3707 02:41:08,040 --> 02:41:12,600 basically we can just replace names with 3708 02:41:11,220 --> 02:41:15,780 new names 3709 02:41:12,600 --> 02:41:18,240 save it and what do you know now I 3710 02:41:15,780 --> 02:41:20,580 actually have three heading ones with 3711 02:41:18,240 --> 02:41:21,960 these values and as you can see this 3712 02:41:20,580 --> 02:41:24,720 right away 3713 02:41:21,960 --> 02:41:27,600 opens up a lot of doors 3714 02:41:24,720 --> 02:41:30,240 because this truly is a dynamic setup I 3715 02:41:27,600 --> 02:41:33,300 have some kind of data which is a list 3716 02:41:30,240 --> 02:41:35,220 and I can iterate over that list 3717 02:41:33,300 --> 02:41:39,479 and return 3718 02:41:35,220 --> 02:41:42,120 whatever HTML structure I want to see on 3719 02:41:39,479 --> 02:41:43,380 a screen now don't worry about this key 3720 02:41:42,120 --> 02:41:46,200 prop 3721 02:41:43,380 --> 02:41:48,120 erly we'll fix this warning in few 3722 02:41:46,200 --> 02:41:50,520 videos also I want to mention that since 3723 02:41:48,120 --> 02:41:54,240 we have this option to go back 3724 02:41:50,520 --> 02:41:57,060 to JavaScript land essentially you can 3725 02:41:54,240 --> 02:41:58,859 also take this code and right away write 3726 02:41:57,060 --> 02:42:00,720 it over here remember we are looking for 3727 02:41:58,859 --> 02:42:02,939 expression so we want to get some kind 3728 02:42:00,720 --> 02:42:05,340 of value back and essentially with map 3729 02:42:02,939 --> 02:42:07,680 this is what we're getting so if you 3730 02:42:05,340 --> 02:42:10,620 don't like setting up the new variable 3731 02:42:07,680 --> 02:42:13,680 and then accessing it you can do this 3732 02:42:10,620 --> 02:42:15,720 directly so essentially we're still 3733 02:42:13,680 --> 02:42:18,479 doing the same thing the difference 3734 02:42:15,720 --> 02:42:21,240 right now is that we're writing 3735 02:42:18,479 --> 02:42:24,120 everything here inside of the component 3736 02:42:21,240 --> 02:42:25,080 again two flavors there's no right or 3737 02:42:24,120 --> 02:42:28,319 wrong 3738 02:42:25,080 --> 02:42:30,479 you can use either of these options the 3739 02:42:28,319 --> 02:42:33,420 result is going to be exactly the same 3740 02:42:30,479 --> 02:42:36,479 again we iterate over the list and we 3741 02:42:33,420 --> 02:42:39,780 just wrap the elements 3742 02:42:36,479 --> 02:42:42,899 in some kind of HTML structure 3743 02:42:39,780 --> 02:42:45,120 and once we are familiar with the basic 3744 02:42:42,899 --> 02:42:48,240 setup up next we'll take a look how we 3745 02:42:45,120 --> 02:42:51,359 can do the same thing with our books 3746 02:42:48,240 --> 02:42:54,060 array okay and up next let's set up a 3747 02:42:51,359 --> 02:42:55,800 proper list and essentially what I mean 3748 02:42:54,060 --> 02:42:58,859 by that is 3749 02:42:55,800 --> 02:43:03,120 iterating over the bookstore and then 3750 02:42:58,859 --> 02:43:04,500 for every item eventually we'll return a 3751 02:43:03,120 --> 02:43:06,720 component 3752 02:43:04,500 --> 02:43:08,700 so let's start by just navigating back 3753 02:43:06,720 --> 02:43:11,760 to index.js 3754 02:43:08,700 --> 02:43:13,319 and we want to remove everything that 3755 02:43:11,760 --> 02:43:14,700 has to do with names 3756 02:43:13,319 --> 02:43:16,439 of course you can keep it for your 3757 02:43:14,700 --> 02:43:18,720 reference just keep in mind that it's 3758 02:43:16,439 --> 02:43:20,880 already available in readme so it 3759 02:43:18,720 --> 02:43:24,840 doesn't really matter so again let me go 3760 02:43:20,880 --> 02:43:26,700 back to empty book list and like I 3761 02:43:24,840 --> 02:43:28,800 mentioned in the previous video if you 3762 02:43:26,700 --> 02:43:31,200 want to set up a new variable and then 3763 02:43:28,800 --> 02:43:32,580 render it in the book list 3764 02:43:31,200 --> 02:43:34,560 totally 3765 02:43:32,580 --> 02:43:36,180 okay option in my case I'm going to do 3766 02:43:34,560 --> 02:43:38,819 it right away in the book list 3767 02:43:36,180 --> 02:43:40,800 effectively I'll iterate over and return 3768 02:43:38,819 --> 02:43:44,280 that new array so we want to start here 3769 02:43:40,800 --> 02:43:46,800 with box then map and again we're 3770 02:43:44,280 --> 02:43:49,920 passing the Callback function so yes 3771 02:43:46,800 --> 02:43:52,319 you'll see the syntax over and over and 3772 02:43:49,920 --> 02:43:54,140 over and over again and the honestly 3773 02:43:52,319 --> 02:43:57,840 fetching data 3774 02:43:54,140 --> 02:44:00,300 and rendering it on a screen 3775 02:43:57,840 --> 02:44:03,720 is going to be your bread and butter 3776 02:44:00,300 --> 02:44:07,020 when it comes to react applications it's 3777 02:44:03,720 --> 02:44:10,260 hard for me to imagine a project where 3778 02:44:07,020 --> 02:44:11,460 you won't be doing at least one of them 3779 02:44:10,260 --> 02:44:13,380 so 3780 02:44:11,460 --> 02:44:15,600 yes this is the syntax you'll see over 3781 02:44:13,380 --> 02:44:18,000 and over and over again and don't worry 3782 02:44:15,600 --> 02:44:21,120 the more times you'll write this 3783 02:44:18,000 --> 02:44:22,859 the more familiar you'll be with the 3784 02:44:21,120 --> 02:44:25,020 entire setup so again we have this 3785 02:44:22,859 --> 02:44:26,939 callback function here and we just need 3786 02:44:25,020 --> 02:44:29,939 to decide what we want to return because 3787 02:44:26,939 --> 02:44:31,439 remember when it comes to functions by 3788 02:44:29,939 --> 02:44:35,100 default they return 3789 02:44:31,439 --> 02:44:37,620 undefined all right so yes explicitly we 3790 02:44:35,100 --> 02:44:40,020 need to set up a return and for now 3791 02:44:37,620 --> 02:44:43,140 let's just go here return and then div 3792 02:44:40,020 --> 02:44:45,660 and just so you can see that pretty much 3793 02:44:43,140 --> 02:44:47,880 everything works even if we don't access 3794 02:44:45,660 --> 02:44:50,760 values from the array let me just go 3795 02:44:47,880 --> 02:44:54,840 here with heading one we're heading two 3796 02:44:50,760 --> 02:44:56,819 whatever doesn't really matter and let's 3797 02:44:54,840 --> 02:44:58,680 just say item here 3798 02:44:56,819 --> 02:45:00,180 and you'll notice that actually 3799 02:44:58,680 --> 02:45:02,399 everything works 3800 02:45:00,180 --> 02:45:05,280 so I have those items on the screen why 3801 02:45:02,399 --> 02:45:08,460 well because I have an array with two 3802 02:45:05,280 --> 02:45:10,140 items we are iterating over here and 3803 02:45:08,460 --> 02:45:12,720 then for every item this is what we 3804 02:45:10,140 --> 02:45:14,819 return so technically we don't need to 3805 02:45:12,720 --> 02:45:16,020 even access it now it kind of makes 3806 02:45:14,819 --> 02:45:17,819 sense right 3807 02:45:16,020 --> 02:45:19,319 since we're iterating over the list we 3808 02:45:17,819 --> 02:45:22,080 want to access those values but 3809 02:45:19,319 --> 02:45:24,540 technically we don't have to and now 3810 02:45:22,080 --> 02:45:26,640 let's grab here this book 3811 02:45:24,540 --> 02:45:28,979 so this is what I'm going through 3812 02:45:26,640 --> 02:45:31,740 do with my parameter I'm going to name 3813 02:45:28,979 --> 02:45:34,560 this a book just please keep in mind we 3814 02:45:31,740 --> 02:45:36,660 cannot pass it directly so if I go here 3815 02:45:34,560 --> 02:45:39,479 with book and I'll do the same thing 3816 02:45:36,660 --> 02:45:43,920 I'll say book I'll get the same error 3817 02:45:39,479 --> 02:45:47,280 again object are not valid as a react 3818 02:45:43,920 --> 02:45:49,859 child so what do we need to do well 3819 02:45:47,280 --> 02:45:52,979 we need to pull out the properties 3820 02:45:49,859 --> 02:45:55,859 so we can either destructure them 3821 02:45:52,979 --> 02:45:57,720 or we can simply go to book that title 3822 02:45:55,859 --> 02:45:59,640 and the reason why I logged it because I 3823 02:45:57,720 --> 02:46:02,580 want you to see that we're iterating 3824 02:45:59,640 --> 02:46:05,939 over and pretty much we're getting 3825 02:46:02,580 --> 02:46:06,720 each object so yes if you have 200 of 3826 02:46:05,939 --> 02:46:09,660 them 3827 02:46:06,720 --> 02:46:12,300 you'll have 200 logs you'll access 200 3828 02:46:09,660 --> 02:46:13,380 items and now effectively we simply 3829 02:46:12,300 --> 02:46:16,319 wanna 3830 02:46:13,380 --> 02:46:19,500 like I said access the property now 3831 02:46:16,319 --> 02:46:20,760 eventually of course I will structure it 3832 02:46:19,500 --> 02:46:24,000 and all that for now I just want to 3833 02:46:20,760 --> 02:46:26,760 showcase that if I go here with title 3834 02:46:24,000 --> 02:46:29,220 I'll have title in my heading twos if 3835 02:46:26,760 --> 02:46:31,859 I'll go with author I'll get an author 3836 02:46:29,220 --> 02:46:33,420 now for the image of course it's going 3837 02:46:31,859 --> 02:46:35,040 to make sense if I 3838 02:46:33,420 --> 02:46:37,140 set it up 3839 02:46:35,040 --> 02:46:39,720 in different element but hopefully you 3840 02:46:37,140 --> 02:46:42,899 see where I'm going with this what's 3841 02:46:39,720 --> 02:46:47,520 next well even though we can nicely 3842 02:46:42,899 --> 02:46:48,899 return HTML elements directly in our 3843 02:46:47,520 --> 02:46:51,540 callback function 3844 02:46:48,899 --> 02:46:54,000 let's also remember that we still have 3845 02:46:51,540 --> 02:46:57,000 this book component 3846 02:46:54,000 --> 02:47:00,899 so why don't we put two and two together 3847 02:46:57,000 --> 02:47:03,420 so we have a book component that already 3848 02:47:00,899 --> 02:47:05,460 has the HTML 3849 02:47:03,420 --> 02:47:06,420 and it's simply looking for these three 3850 02:47:05,460 --> 02:47:10,140 things 3851 02:47:06,420 --> 02:47:12,780 image title and author 3852 02:47:10,140 --> 02:47:14,819 so if we go back 3853 02:47:12,780 --> 02:47:18,840 to our callback function 3854 02:47:14,819 --> 02:47:21,120 and if we return book and pass in the 3855 02:47:18,840 --> 02:47:24,000 values it should work 3856 02:47:21,120 --> 02:47:26,700 correct so let's try it out I'm going to 3857 02:47:24,000 --> 02:47:30,000 go back to my return I'm going to write 3858 02:47:26,700 --> 02:47:31,560 here book so I'm rendering now the book 3859 02:47:30,000 --> 02:47:33,660 component 3860 02:47:31,560 --> 02:47:34,500 and for now let's just pass them one by 3861 02:47:33,660 --> 02:47:36,120 one 3862 02:47:34,500 --> 02:47:38,340 in the following videos I'll show you 3863 02:47:36,120 --> 02:47:39,720 how we can pass the entire object as 3864 02:47:38,340 --> 02:47:41,760 well as how we can spread out the 3865 02:47:39,720 --> 02:47:44,580 properties which is my favorite way of 3866 02:47:41,760 --> 02:47:47,100 passing in the props but for now let's 3867 02:47:44,580 --> 02:47:50,520 just go one by one and let's say over 3868 02:47:47,100 --> 02:47:53,040 here book and then remember all these 3869 02:47:50,520 --> 02:47:55,740 properties are over here so above the 3870 02:47:53,040 --> 02:47:59,399 return we want to go with const then set 3871 02:47:55,740 --> 02:48:01,620 it equal to a book so essentially we are 3872 02:47:59,399 --> 02:48:03,780 destructuring something that we covered 3873 02:48:01,620 --> 02:48:07,200 already before and we want to go with 3874 02:48:03,780 --> 02:48:09,899 image then title and author and yes I 3875 02:48:07,200 --> 02:48:13,439 know that those properties are in there 3876 02:48:09,899 --> 02:48:16,979 so as far as the return since I know 3877 02:48:13,439 --> 02:48:20,100 that I need to pass them as props what 3878 02:48:16,979 --> 02:48:22,200 do we need to do we just go here image 3879 02:48:20,100 --> 02:48:25,680 is equal to image 3880 02:48:22,200 --> 02:48:27,540 title is equal to title and hopefully 3881 02:48:25,680 --> 02:48:30,000 you see where I'm going with this 3882 02:48:27,540 --> 02:48:33,180 one by one essentially I just passed 3883 02:48:30,000 --> 02:48:36,660 them in and as a result you'll see this 3884 02:48:33,180 --> 02:48:38,640 cool setup where again we have structure 3885 02:48:36,660 --> 02:48:41,040 in one place 3886 02:48:38,640 --> 02:48:42,540 we have a structure for the book 3887 02:48:41,040 --> 02:48:44,399 component 3888 02:48:42,540 --> 02:48:46,800 and essentially 3889 02:48:44,399 --> 02:48:49,439 we just iterate over 3890 02:48:46,800 --> 02:48:51,600 some data some list 3891 02:48:49,439 --> 02:48:52,500 we pull out the properties out of the 3892 02:48:51,600 --> 02:48:55,680 object 3893 02:48:52,500 --> 02:48:56,880 and then for every item we return this 3894 02:48:55,680 --> 02:48:59,399 component 3895 02:48:56,880 --> 02:49:00,660 and essentially we get this awesome 3896 02:48:59,399 --> 02:49:03,660 setup 3897 02:49:00,660 --> 02:49:07,260 where now all our logic is here 3898 02:49:03,660 --> 02:49:10,319 in order to render stuff on a screen we 3899 02:49:07,260 --> 02:49:12,960 simply need to iterate over and for 3900 02:49:10,319 --> 02:49:14,340 every item we just need to return the 3901 02:49:12,960 --> 02:49:16,920 component 3902 02:49:14,340 --> 02:49:19,500 all right next let's get rid of that key 3903 02:49:16,920 --> 02:49:21,180 prop warning in the console and 3904 02:49:19,500 --> 02:49:23,720 basically every time 3905 02:49:21,180 --> 02:49:27,540 you'll render a list of items in react 3906 02:49:23,720 --> 02:49:31,560 react wants to keep track of those items 3907 02:49:27,540 --> 02:49:34,500 so we need to provide a unique value and 3908 02:49:31,560 --> 02:49:37,740 set it equal to a key prop 3909 02:49:34,500 --> 02:49:40,380 and typically it's going to be an ID and 3910 02:49:37,740 --> 02:49:43,260 since we're creating this list ourselves 3911 02:49:40,380 --> 02:49:45,600 essentially we'll go back and just add 3912 02:49:43,260 --> 02:49:48,359 these two properties keep in mind that 3913 02:49:45,600 --> 02:49:51,899 the property name doesn't have to be ID 3914 02:49:48,359 --> 02:49:54,180 just these values need to be unique so 3915 02:49:51,899 --> 02:49:56,100 if you call this vegan food truck it's 3916 02:49:54,180 --> 02:49:58,680 totally okay as long as you provide 3917 02:49:56,100 --> 02:50:00,000 unique values and they don't have to be 3918 02:49:58,680 --> 02:50:02,460 numbers 3919 02:50:00,000 --> 02:50:04,859 essentially you can turn this into a 3920 02:50:02,460 --> 02:50:07,380 string of one and string of two and 3921 02:50:04,859 --> 02:50:10,380 hopefully you see where I'm going this 3922 02:50:07,380 --> 02:50:13,020 and lastly normally 3923 02:50:10,380 --> 02:50:16,319 will be fetching data from some kind of 3924 02:50:13,020 --> 02:50:19,439 external resource anyway so 3925 02:50:16,319 --> 02:50:21,240 those values again most likely IDs will 3926 02:50:19,439 --> 02:50:24,540 be right away provided 3927 02:50:21,240 --> 02:50:26,580 and I'll talk about the indexes 3928 02:50:24,540 --> 02:50:27,479 at the end of the video and why we 3929 02:50:26,580 --> 02:50:30,240 should 3930 02:50:27,479 --> 02:50:32,880 avoid that so first let's just go back 3931 02:50:30,240 --> 02:50:34,800 to index.js and we want to add those 3932 02:50:32,880 --> 02:50:36,479 unique values again not going to be 3933 02:50:34,800 --> 02:50:39,359 particularly original I'm just going to 3934 02:50:36,479 --> 02:50:40,800 go with id1 and id2 and yes if you have 3935 02:50:39,359 --> 02:50:44,460 10 of them then 3936 02:50:40,800 --> 02:50:46,920 basically manual you add this one id1 3937 02:50:44,460 --> 02:50:49,680 and id2 in my case 3938 02:50:46,920 --> 02:50:53,520 then we want to navigate and we want to 3939 02:50:49,680 --> 02:50:56,580 set this key prop on the main return 3940 02:50:53,520 --> 02:50:59,280 so it's not going to work if we go here 3941 02:50:56,580 --> 02:51:00,359 and set it equal to key and then the 3942 02:50:59,280 --> 02:51:02,460 Eddy 3943 02:51:00,359 --> 02:51:04,620 because this is the element that I'm 3944 02:51:02,460 --> 02:51:05,399 returning when I'm iterating over the 3945 02:51:04,620 --> 02:51:07,859 list 3946 02:51:05,399 --> 02:51:10,200 keep that in mind and 3947 02:51:07,859 --> 02:51:12,060 the reason why I'm saying that because I 3948 02:51:10,200 --> 02:51:15,660 get these questions in course q and I 3949 02:51:12,060 --> 02:51:17,640 where in some project it's confusing 3950 02:51:15,660 --> 02:51:19,200 where we should set the key so always 3951 02:51:17,640 --> 02:51:20,760 always always 3952 02:51:19,200 --> 02:51:22,380 look for the place where you're 3953 02:51:20,760 --> 02:51:25,920 iterating over 3954 02:51:22,380 --> 02:51:29,160 the data and then you want to set on the 3955 02:51:25,920 --> 02:51:31,080 item that you're returning basically if 3956 02:51:29,160 --> 02:51:33,359 there's going to be this type of setup 3957 02:51:31,080 --> 02:51:36,240 so let me go back here and show that ID 3958 02:51:33,359 --> 02:51:39,540 if you have this type of setup then add 3959 02:51:36,240 --> 02:51:42,120 it on a div not on a heading 2. 3960 02:51:39,540 --> 02:51:43,979 hopefully that is clear so let's go back 3961 02:51:42,120 --> 02:51:47,040 over here now we know that in the book 3962 02:51:43,979 --> 02:51:49,500 we actually have Daddy correct so we 3963 02:51:47,040 --> 02:51:51,359 pull it out and then just like they 3964 02:51:49,500 --> 02:51:54,720 suggest in console we just go with key 3965 02:51:51,359 --> 02:51:56,760 and set it equal to an ID and what do 3966 02:51:54,720 --> 02:51:58,140 you know the moment I save it everything 3967 02:51:56,760 --> 02:52:00,180 is fine 3968 02:51:58,140 --> 02:52:03,359 now 3969 02:52:00,180 --> 02:52:05,460 as you're looking at someone else's code 3970 02:52:03,359 --> 02:52:07,319 you'll see this approach as well 3971 02:52:05,460 --> 02:52:08,460 essentially this just saves a little bit 3972 02:52:07,319 --> 02:52:11,220 of time where 3973 02:52:08,460 --> 02:52:12,420 in the map the second parameter is the 3974 02:52:11,220 --> 02:52:13,740 index 3975 02:52:12,420 --> 02:52:15,779 so 3976 02:52:13,740 --> 02:52:18,359 you know that in JavaScript 3977 02:52:15,779 --> 02:52:20,580 arrays are zero index based the first 3978 02:52:18,359 --> 02:52:23,220 item is going to have the value of zero 3979 02:52:20,580 --> 02:52:24,899 then one and if you have 100 items that 3980 02:52:23,220 --> 02:52:30,840 means that you'll have those values from 3981 02:52:24,899 --> 02:52:35,160 0 to 99 and technically you can get away 3982 02:52:30,840 --> 02:52:37,680 with passing this index into a key yes 3983 02:52:35,160 --> 02:52:40,560 you'll see this type of approach in 3984 02:52:37,680 --> 02:52:42,420 general I don't advise doing that even 3985 02:52:40,560 --> 02:52:46,080 though you can get away with it 3986 02:52:42,420 --> 02:52:48,779 because it only works for the list that 3987 02:52:46,080 --> 02:52:51,300 you know will never change so basically 3988 02:52:48,779 --> 02:52:53,700 if we'll have some kind of option to 3989 02:52:51,300 --> 02:52:55,800 remove items from the list then this is 3990 02:52:53,700 --> 02:52:57,960 not going to work effectively there's a 3991 02:52:55,800 --> 02:52:58,979 high chance you'll get some bugs down 3992 02:52:57,960 --> 02:53:01,859 the road 3993 02:52:58,979 --> 02:53:04,859 now if you know 100 that the list is not 3994 02:53:01,859 --> 02:53:09,000 going to change yes you can cheat a 3995 02:53:04,859 --> 02:53:11,340 little bit but in general I don't advise 3996 02:53:09,000 --> 02:53:14,100 using such an approach so that's how we 3997 02:53:11,340 --> 02:53:16,979 can set up the key prop we just need to 3998 02:53:14,100 --> 02:53:19,200 set it equal to some kind of 3999 02:53:16,979 --> 02:53:22,859 unique value 4000 02:53:19,200 --> 02:53:25,500 that is in our data okay and before we 4001 02:53:22,859 --> 02:53:27,600 discuss events in react 4002 02:53:25,500 --> 02:53:31,500 let's take a look at two options how we 4003 02:53:27,600 --> 02:53:34,260 can pass the entire object as props 4004 02:53:31,500 --> 02:53:37,380 so effectively if we take a look at the 4005 02:53:34,260 --> 02:53:40,500 index.js Everything Is Awesome 4006 02:53:37,380 --> 02:53:43,740 but what if I don't know we have 20 4007 02:53:40,500 --> 02:53:45,960 items in this particular object 4008 02:53:43,740 --> 02:53:47,460 we can see that I think we're doing 4009 02:53:45,960 --> 02:53:50,279 double the work 4010 02:53:47,460 --> 02:53:52,859 I pull out all the items over here 4011 02:53:50,279 --> 02:53:56,340 and then I need to repeat the same steps 4012 02:53:52,859 --> 02:53:58,080 or if you go with props.name same deal 4013 02:53:56,340 --> 02:54:00,899 in the component 4014 02:53:58,080 --> 02:54:02,760 so is there any way how we can 4015 02:54:00,899 --> 02:54:05,399 simplify our life 4016 02:54:02,760 --> 02:54:07,859 here when we're iterating over the list 4017 02:54:05,399 --> 02:54:12,000 and essentially we have two options 4018 02:54:07,859 --> 02:54:15,060 we can pass the entire object as a prop 4019 02:54:12,000 --> 02:54:17,700 or we can use the spread operator which 4020 02:54:15,060 --> 02:54:18,840 essentially is the method that I prefer 4021 02:54:17,700 --> 02:54:21,420 the most 4022 02:54:18,840 --> 02:54:25,220 but let's start by passing the entire 4023 02:54:21,420 --> 02:54:27,899 object so inside of the map method 4024 02:54:25,220 --> 02:54:31,859 instead of pulling them out one by one 4025 02:54:27,899 --> 02:54:35,040 here's alternative I can simply go to my 4026 02:54:31,859 --> 02:54:37,200 book component and I can say book prop 4027 02:54:35,040 --> 02:54:39,660 so I need to come up with a name and 4028 02:54:37,200 --> 02:54:42,479 I'll name this exactly the same as my 4029 02:54:39,660 --> 02:54:44,160 parameter and I'll set it equal to my 4030 02:54:42,479 --> 02:54:46,439 book object 4031 02:54:44,160 --> 02:54:49,260 however once we save 4032 02:54:46,439 --> 02:54:50,399 we'll see nothing on screen why well 4033 02:54:49,260 --> 02:54:52,380 because remember 4034 02:54:50,399 --> 02:54:55,260 now props 4035 02:54:52,380 --> 02:54:58,500 is an object but inside of it I have 4036 02:54:55,260 --> 02:55:01,319 another object I have a book object 4037 02:54:58,500 --> 02:55:04,080 so in order to pull these properties out 4038 02:55:01,319 --> 02:55:09,300 I have two options I can go here with 4039 02:55:04,080 --> 02:55:11,939 props DOT book or another one if you're 4040 02:55:09,300 --> 02:55:14,819 destructuring right away in the function 4041 02:55:11,939 --> 02:55:17,160 parameter this is the syntax 4042 02:55:14,819 --> 02:55:20,100 again if you're not familiar with it 4043 02:55:17,160 --> 02:55:23,279 please watch the the structuring video 4044 02:55:20,100 --> 02:55:26,040 that I shared previously in the readme 4045 02:55:23,279 --> 02:55:28,260 so essentially I know that there is a 4046 02:55:26,040 --> 02:55:31,319 object and I just pull these properties 4047 02:55:28,260 --> 02:55:32,939 out that's the syntax again those are 4048 02:55:31,319 --> 02:55:35,700 our two options 4049 02:55:32,939 --> 02:55:37,800 either we just go with props Dot and 4050 02:55:35,700 --> 02:55:40,560 then whatever is the object 4051 02:55:37,800 --> 02:55:41,939 or we can restructure it right here in 4052 02:55:40,560 --> 02:55:44,580 the function parameters again 4053 02:55:41,939 --> 02:55:46,800 essentially it just saves us here on 4054 02:55:44,580 --> 02:55:48,420 getting these properties now we'll still 4055 02:55:46,800 --> 02:55:50,220 need to get an ID 4056 02:55:48,420 --> 02:55:52,140 so this is the case where you can either 4057 02:55:50,220 --> 02:55:54,479 pull it out or you can just say book and 4058 02:55:52,140 --> 02:55:56,700 then whatever is that property 4059 02:55:54,479 --> 02:55:59,160 and then of course in that case you can 4060 02:55:56,700 --> 02:56:00,500 just remove all of them this is totally 4061 02:55:59,160 --> 02:56:03,960 up to you 4062 02:56:00,500 --> 02:56:05,580 and the method that I prefer the most is 4063 02:56:03,960 --> 02:56:08,520 actually a little bit different 4064 02:56:05,580 --> 02:56:10,020 where we can use the spread operator and 4065 02:56:08,520 --> 02:56:12,720 again if you're not familiar with the 4066 02:56:10,020 --> 02:56:14,340 spread operator I will give you the 4067 02:56:12,720 --> 02:56:16,740 general overview but if you want to find 4068 02:56:14,340 --> 02:56:18,899 out more info just please follow this 4069 02:56:16,740 --> 02:56:21,120 link where I cover everything in great 4070 02:56:18,899 --> 02:56:24,300 detail and essentially the spread 4071 02:56:21,120 --> 02:56:25,439 operator so these three dots allow us to 4072 02:56:24,300 --> 02:56:29,640 copy 4073 02:56:25,439 --> 02:56:31,319 from the arrays or from the object so if 4074 02:56:29,640 --> 02:56:33,060 you have friends and then if you want to 4075 02:56:31,319 --> 02:56:34,920 create a new array you go here with data 4076 02:56:33,060 --> 02:56:37,680 dot friends and then let's say you want 4077 02:56:34,920 --> 02:56:39,479 to add a friend and if you log you'll 4078 02:56:37,680 --> 02:56:41,880 see that effectively you have two 4079 02:56:39,479 --> 02:56:44,399 arrange and again this is copying so 4080 02:56:41,880 --> 02:56:46,920 this is not going to be passed as a 4081 02:56:44,399 --> 02:56:50,040 reference and the same goes for the 4082 02:56:46,920 --> 02:56:51,479 object if you have some object and then 4083 02:56:50,040 --> 02:56:54,600 if you want to create a new one you go 4084 02:56:51,479 --> 02:56:57,420 with dot dot and you copy the properties 4085 02:56:54,600 --> 02:56:59,520 from some object and then let's say you 4086 02:56:57,420 --> 02:57:02,040 add another property and if you log 4087 02:56:59,520 --> 02:57:04,500 you'll see that essentially you have a 4088 02:57:02,040 --> 02:57:07,500 new object with all of these properties 4089 02:57:04,500 --> 02:57:08,340 now in react here's what we can do if I 4090 02:57:07,500 --> 02:57:10,140 have 4091 02:57:08,340 --> 02:57:12,899 a object 4092 02:57:10,140 --> 02:57:14,040 and if I want to pass all of those 4093 02:57:12,899 --> 02:57:18,540 properties 4094 02:57:14,040 --> 02:57:21,060 as props one by one I simply set up the 4095 02:57:18,540 --> 02:57:22,500 curlies here and I go with dot dot dot 4096 02:57:21,060 --> 02:57:25,140 and then that object 4097 02:57:22,500 --> 02:57:28,260 and once you log you'll see that all of 4098 02:57:25,140 --> 02:57:30,600 them are effectively passed down as 4099 02:57:28,260 --> 02:57:32,460 props so let's try this out and let me 4100 02:57:30,600 --> 02:57:34,500 know right away tell you that it's 4101 02:57:32,460 --> 02:57:36,359 not going to access anything from the 4102 02:57:34,500 --> 02:57:38,160 book so this line is going to 4103 02:57:36,359 --> 02:57:40,680 effectively fail 4104 02:57:38,160 --> 02:57:42,720 so let's go back to the book list and 4105 02:57:40,680 --> 02:57:44,700 instead of setting this up as book 4106 02:57:42,720 --> 02:57:46,800 equals the book 4107 02:57:44,700 --> 02:57:49,620 simply want to go with curlies 4108 02:57:46,800 --> 02:57:52,920 we want to go dot dot and when we want 4109 02:57:49,620 --> 02:57:55,260 to pass in the object again this line is 4110 02:57:52,920 --> 02:57:57,300 going to fail right now because we are 4111 02:57:55,260 --> 02:57:59,520 trying to access properties that don't 4112 02:57:57,300 --> 02:58:03,000 exist there is no book anymore 4113 02:57:59,520 --> 02:58:05,939 so now we need to go back and let's just 4114 02:58:03,000 --> 02:58:08,939 log and check it out so now one by one 4115 02:58:05,939 --> 02:58:11,160 we pass those properties down including 4116 02:58:08,939 --> 02:58:12,180 the ID of course now we don't use it in 4117 02:58:11,160 --> 02:58:14,760 a book 4118 02:58:12,180 --> 02:58:16,200 but it's not a big deal 4119 02:58:14,760 --> 02:58:18,300 so again 4120 02:58:16,200 --> 02:58:20,340 this is totally up to you 4121 02:58:18,300 --> 02:58:23,040 if you want to pass the entire object 4122 02:58:20,340 --> 02:58:25,500 down as props you can definitely do so 4123 02:58:23,040 --> 02:58:28,620 just come up with the property or you 4124 02:58:25,500 --> 02:58:31,500 can spread out all the values the end 4125 02:58:28,620 --> 02:58:33,540 result is going to be exactly the same 4126 02:58:31,500 --> 02:58:34,439 and it really comes down to your 4127 02:58:33,540 --> 02:58:37,560 preference 4128 02:58:34,439 --> 02:58:40,319 if you prefer the first wave 4129 02:58:37,560 --> 02:58:42,540 use that one if you prefer the second 4130 02:58:40,319 --> 02:58:44,640 one the spread operator 4131 02:58:42,540 --> 02:58:46,380 then of course go with that one now 4132 02:58:44,640 --> 02:58:48,420 since 4133 02:58:46,380 --> 02:58:50,340 I prefer the second option 4134 02:58:48,420 --> 02:58:53,340 that's why in the course you'll most 4135 02:58:50,340 --> 02:58:56,819 likely see me doing this 4136 02:58:53,340 --> 02:58:59,460 the spread operator approach rendering a 4137 02:58:56,819 --> 02:59:00,779 list with the help of components is 4138 02:58:59,460 --> 02:59:03,840 awesome 4139 02:59:00,779 --> 02:59:08,399 but of course it would be nicer if the 4140 02:59:03,840 --> 02:59:12,060 user could interact with our application 4141 02:59:08,399 --> 02:59:13,620 otherwise our app is somewhat boring so 4142 02:59:12,060 --> 02:59:17,640 long story short 4143 02:59:13,620 --> 02:59:20,160 our app needs to respond to events 4144 02:59:17,640 --> 02:59:23,340 for example when the user clicks the 4145 02:59:20,160 --> 02:59:24,660 button submits the form and rest of the 4146 02:59:23,340 --> 02:59:27,420 good stuff 4147 02:59:24,660 --> 02:59:29,760 so up next let's talk about how we can 4148 02:59:27,420 --> 02:59:32,520 handle events in react 4149 02:59:29,760 --> 02:59:35,279 now let me right away bring the cold 4150 02:59:32,520 --> 02:59:37,920 bucket of water and just say that all 4151 02:59:35,279 --> 02:59:40,800 our examples will focus on logging stuff 4152 02:59:37,920 --> 02:59:43,740 yes that's going to be the case simply 4153 02:59:40,800 --> 02:59:47,760 because we have not covered what is 4154 02:59:43,740 --> 02:59:49,319 State and react and what hook we use to 4155 02:59:47,760 --> 02:59:51,899 control the state 4156 02:59:49,319 --> 02:59:54,600 and therefore we really can't change 4157 02:59:51,899 --> 02:59:55,380 what we currently see on a screen don't 4158 02:59:54,600 --> 02:59:58,140 worry 4159 02:59:55,380 --> 03:00:01,200 all of that is coming up now let's just 4160 02:59:58,140 --> 03:00:02,819 focus on the main points 4161 03:00:01,200 --> 03:00:04,740 and essentially 4162 03:00:02,819 --> 03:00:07,500 if you're a little bit familiar with 4163 03:00:04,740 --> 03:00:10,080 vanilla.js you know that 4164 03:00:07,500 --> 03:00:12,479 effectively the setup is following 4165 03:00:10,080 --> 03:00:15,720 we have some kind of button form input 4166 03:00:12,479 --> 03:00:19,380 whatever then we select that element and 4167 03:00:15,720 --> 03:00:21,899 we can add event listener and I guess 4168 03:00:19,380 --> 03:00:24,359 the most common or basic or however you 4169 03:00:21,899 --> 03:00:27,180 want to say it is the click event 4170 03:00:24,359 --> 03:00:29,819 so we have the event we pass the name 4171 03:00:27,180 --> 03:00:32,100 and then we have the Callback function 4172 03:00:29,819 --> 03:00:34,859 in the Callback function we can access 4173 03:00:32,100 --> 03:00:37,800 the event object which 4174 03:00:34,859 --> 03:00:40,380 contains bunch of cool stuff for example 4175 03:00:37,800 --> 03:00:43,859 the element we're selecting 4176 03:00:40,380 --> 03:00:46,979 what value is inside of it and of course 4177 03:00:43,859 --> 03:00:49,680 bunch of other things and also we can do 4178 03:00:46,979 --> 03:00:52,260 something when that event takes place 4179 03:00:49,680 --> 03:00:56,040 and when it comes to react essentially 4180 03:00:52,260 --> 03:00:59,399 the setup is extremely similar 4181 03:00:56,040 --> 03:01:03,060 we have an element and of course in our 4182 03:00:59,399 --> 03:01:07,260 case that element is located in the jsx 4183 03:01:03,060 --> 03:01:09,899 in the component we have an event 4184 03:01:07,260 --> 03:01:12,300 and remember when we talked about Lee 4185 03:01:09,899 --> 03:01:16,319 kamokesh this is the time when we will 4186 03:01:12,300 --> 03:01:18,779 write on click instead of click 4187 03:01:16,319 --> 03:01:22,140 like we have been doing in vanilla.js 4188 03:01:18,779 --> 03:01:24,359 and then the Callback function 4189 03:01:22,140 --> 03:01:27,540 basically 4190 03:01:24,359 --> 03:01:29,100 a functionality we want to invoke 4191 03:01:27,540 --> 03:01:33,359 when that 4192 03:01:29,100 --> 03:01:35,899 event fires now if you want to see all 4193 03:01:33,359 --> 03:01:40,680 of the available 4194 03:01:35,899 --> 03:01:42,899 events in react you can follow this link 4195 03:01:40,680 --> 03:01:46,979 so let me copy this one just to Showcase 4196 03:01:42,899 --> 03:01:50,040 but I strongly suggest not memorizing 4197 03:01:46,979 --> 03:01:54,120 all of them because list is quite long 4198 03:01:50,040 --> 03:01:57,960 and essentially if you'll ever need to 4199 03:01:54,120 --> 03:02:01,200 use a specific event you can always go 4200 03:01:57,960 --> 03:02:04,260 there and get the exact syntax 4201 03:02:01,200 --> 03:02:05,580 the idea for all of them is the same 4202 03:02:04,260 --> 03:02:10,140 again 4203 03:02:05,580 --> 03:02:12,060 element event and a function and notice 4204 03:02:10,140 --> 03:02:13,740 over here how basically we're passing in 4205 03:02:12,060 --> 03:02:15,359 the reference so we go back to a 4206 03:02:13,740 --> 03:02:19,260 JavaScript land we set up the curlies 4207 03:02:15,359 --> 03:02:22,319 and same how we reference the variable 4208 03:02:19,260 --> 03:02:24,359 now we're referencing the function and 4209 03:02:22,319 --> 03:02:27,300 like I mentioned before 4210 03:02:24,359 --> 03:02:30,060 in all our examples we'll invoke alert 4211 03:02:27,300 --> 03:02:33,060 or log just because we haven't covered 4212 03:02:30,060 --> 03:02:35,100 how we can control what we can see on a 4213 03:02:33,060 --> 03:02:37,979 screen all of that is coming up 4214 03:02:35,100 --> 03:02:39,000 and I can also say that during the 4215 03:02:37,979 --> 03:02:42,720 course 4216 03:02:39,000 --> 03:02:46,200 mostly we'll use these three so on click 4217 03:02:42,720 --> 03:02:49,319 which responds to click events on submit 4218 03:02:46,200 --> 03:02:52,020 which fires off when the user submits 4219 03:02:49,319 --> 03:02:56,700 the form and on change which fires off 4220 03:02:52,020 --> 03:03:00,359 when the user is providing value in the 4221 03:02:56,700 --> 03:03:02,939 input again I strongly strongly 4222 03:03:00,359 --> 03:03:04,200 so just not memorizing all of them over 4223 03:03:02,939 --> 03:03:06,720 here 4224 03:03:04,200 --> 03:03:09,420 I mean in my opinion it's a waste of 4225 03:03:06,720 --> 03:03:11,700 your time if you'll ever have some kind 4226 03:03:09,420 --> 03:03:14,100 of specific use case 4227 03:03:11,700 --> 03:03:16,680 and come back over here you can get the 4228 03:03:14,100 --> 03:03:19,260 exact name for that event and you're 4229 03:03:16,680 --> 03:03:20,880 good to go because again the idea for 4230 03:03:19,260 --> 03:03:21,779 all of them is going to be exactly the 4231 03:03:20,880 --> 03:03:23,880 same 4232 03:03:21,779 --> 03:03:26,460 and once we have covered the journal 4233 03:03:23,880 --> 03:03:28,140 setup now let's create event examples 4234 03:03:26,460 --> 03:03:29,520 component which I'm going to render 4235 03:03:28,140 --> 03:03:31,859 above 4236 03:03:29,520 --> 03:03:33,720 the books one 4237 03:03:31,859 --> 03:03:36,000 and the reason why I'm doing that is 4238 03:03:33,720 --> 03:03:38,100 because eventually we'll just have the 4239 03:03:36,000 --> 03:03:39,060 books so this one will stay for your 4240 03:03:38,100 --> 03:03:41,580 reference 4241 03:03:39,060 --> 03:03:44,640 and of course since you have the readme 4242 03:03:41,580 --> 03:03:48,359 I'll be able to remove the event 4243 03:03:44,640 --> 03:03:51,120 examples so in the event examples 4244 03:03:48,359 --> 03:03:52,920 we want to set up a section form 4245 03:03:51,120 --> 03:03:56,279 with heading 2. 4246 03:03:52,920 --> 03:03:59,880 so some kind of title I guess input 4247 03:03:56,279 --> 03:04:03,000 and we want to go with a button with the 4248 03:03:59,880 --> 03:04:05,880 on click now let's not worry about these 4249 03:04:03,000 --> 03:04:07,680 functions for now as well as the name of 4250 03:04:05,880 --> 03:04:09,540 the event over here let's just set up 4251 03:04:07,680 --> 03:04:12,060 the basic return 4252 03:04:09,540 --> 03:04:14,460 so essentially let's just worry about 4253 03:04:12,060 --> 03:04:17,520 the jsx we're returning so let me 4254 03:04:14,460 --> 03:04:18,960 navigate to index.js and I think I'm 4255 03:04:17,520 --> 03:04:20,819 going to do right after the book list 4256 03:04:18,960 --> 03:04:23,279 because like I just said I am going to 4257 03:04:20,819 --> 03:04:25,859 eventually remove it anyway so why not 4258 03:04:23,279 --> 03:04:28,580 I'm gonna go with const and then let's 4259 03:04:25,859 --> 03:04:31,800 name this event 4260 03:04:28,580 --> 03:04:33,120 examples so that is going to be my 4261 03:04:31,800 --> 03:04:35,040 component 4262 03:04:33,120 --> 03:04:36,180 inside of it for now let's just go to 4263 03:04:35,040 --> 03:04:38,880 return 4264 03:04:36,180 --> 03:04:40,740 just so we can see that everything has 4265 03:04:38,880 --> 03:04:42,779 been set up correctly 4266 03:04:40,740 --> 03:04:46,020 so let's go with heading one and then 4267 03:04:42,779 --> 03:04:48,540 I'll say events let's save that and then 4268 03:04:46,020 --> 03:04:50,460 we want to go back to the book list 4269 03:04:48,540 --> 03:04:53,100 and effectively you want to do that 4270 03:04:50,460 --> 03:04:54,600 before the books so this is really up to 4271 03:04:53,100 --> 03:04:55,500 you if you want to set it up here in the 4272 03:04:54,600 --> 03:04:58,200 bottom 4273 03:04:55,500 --> 03:05:00,120 that's also an option but in my case I'm 4274 03:04:58,200 --> 03:05:04,200 going to go above and I'm just going to 4275 03:05:00,120 --> 03:05:05,939 access the event examples now if you 4276 03:05:04,200 --> 03:05:08,160 want to place them side by side in the 4277 03:05:05,939 --> 03:05:10,560 book list then of course just remember 4278 03:05:08,160 --> 03:05:13,680 that we need to use that react fragment 4279 03:05:10,560 --> 03:05:16,979 or div or whatever because there has to 4280 03:05:13,680 --> 03:05:19,200 be one element that we're returning and 4281 03:05:16,979 --> 03:05:20,580 also keep in mind that I'm placing this 4282 03:05:19,200 --> 03:05:23,279 in a section because there is already 4283 03:05:20,580 --> 03:05:25,920 some CSS so if you won't place it inside 4284 03:05:23,279 --> 03:05:29,100 of the section then effectively it's 4285 03:05:25,920 --> 03:05:31,920 just going to be hanging out here on the 4286 03:05:29,100 --> 03:05:34,979 left top corner so let me save it I have 4287 03:05:31,920 --> 03:05:37,200 your events so I'm good to go and as a 4288 03:05:34,979 --> 03:05:39,240 quick sign up this is usually what I do 4289 03:05:37,200 --> 03:05:41,700 I mean I'm not returning and hitting one 4290 03:05:39,240 --> 03:05:43,740 each and every time but I always like to 4291 03:05:41,700 --> 03:05:45,779 set up my component so I know that 4292 03:05:43,740 --> 03:05:47,760 definitely it's being rendered on a 4293 03:05:45,779 --> 03:05:49,920 screen there's no point to set up the 4294 03:05:47,760 --> 03:05:51,420 entire logic and then start chasing some 4295 03:05:49,920 --> 03:05:56,580 weird bugs 4296 03:05:51,420 --> 03:05:58,319 if let's say the initial setup was wrong 4297 03:05:56,580 --> 03:06:01,200 so in order to 4298 03:05:58,319 --> 03:06:02,819 avoid chasing my own tail I usually 4299 03:06:01,200 --> 03:06:06,060 start by rendering something on screen 4300 03:06:02,819 --> 03:06:08,279 and then once I can clearly see that I 4301 03:06:06,060 --> 03:06:09,899 mean the component setup is correct then 4302 03:06:08,279 --> 03:06:11,880 we can move on and start adding the 4303 03:06:09,899 --> 03:06:15,000 logic over here hopefully this wasn't 4304 03:06:11,880 --> 03:06:17,100 too long of the explanation and now 4305 03:06:15,000 --> 03:06:19,620 let's just set up that return 4306 03:06:17,100 --> 03:06:22,260 where basically we want to go 4307 03:06:19,620 --> 03:06:24,420 with another section 4308 03:06:22,260 --> 03:06:28,319 then inside of the section let's right 4309 03:06:24,420 --> 03:06:30,120 away go with form now we don't need the 4310 03:06:28,319 --> 03:06:32,399 action here 4311 03:06:30,120 --> 03:06:34,460 usually if you go damage right away it 4312 03:06:32,399 --> 03:06:37,439 gives you an action just as a side note 4313 03:06:34,460 --> 03:06:39,600 as far as the heading tools say typical 4314 03:06:37,439 --> 03:06:42,060 form 4315 03:06:39,600 --> 03:06:45,779 and then we want to go with inputs for 4316 03:06:42,060 --> 03:06:47,760 now it's going to be a text input but I 4317 03:06:45,779 --> 03:06:50,880 just want to add a little bit of 4318 03:06:47,760 --> 03:06:53,399 inline styles so let's go with style 4319 03:06:50,880 --> 03:06:56,580 it's an object and then let's say margin 4320 03:06:53,399 --> 03:06:59,160 one Rems usually we won't do that we 4321 03:06:56,580 --> 03:07:01,140 won't add the inline Styles but this is 4322 03:06:59,160 --> 03:07:04,020 the case where I thought 4323 03:07:01,140 --> 03:07:06,240 it's a great example of using such 4324 03:07:04,020 --> 03:07:08,939 approach and then let's also go with 4325 03:07:06,240 --> 03:07:10,560 name and we'll set up the example just 4326 03:07:08,939 --> 03:07:12,359 because I want to showcase something in 4327 03:07:10,560 --> 03:07:14,460 the following videos 4328 03:07:12,359 --> 03:07:17,340 and as far as the value I'm going to go 4329 03:07:14,460 --> 03:07:19,920 with examples so we know that in HTML we 4330 03:07:17,340 --> 03:07:23,460 can add name attributes 4331 03:07:19,920 --> 03:07:24,899 to the inputs and in the following 4332 03:07:23,460 --> 03:07:28,800 videos I'll show you how we can access 4333 03:07:24,899 --> 03:07:31,080 them in react and then right outside of 4334 03:07:28,800 --> 03:07:32,580 the form let's set up that button 4335 03:07:31,080 --> 03:07:33,960 as far as the value I'm just going to go 4336 03:07:32,580 --> 03:07:35,880 to click me 4337 03:07:33,960 --> 03:07:39,479 so we have our setup 4338 03:07:35,880 --> 03:07:42,420 and now let's just reiterate when I 4339 03:07:39,479 --> 03:07:45,840 covered I guess five minutes ago 4340 03:07:42,420 --> 03:07:49,560 where basically we have these events 4341 03:07:45,840 --> 03:07:52,080 so if we want to respond to user typing 4342 03:07:49,560 --> 03:07:55,080 something in the input the event name is 4343 03:07:52,080 --> 03:07:56,580 on change this is the case where we kind 4344 03:07:55,080 --> 03:07:59,279 of just simply come up with our own 4345 03:07:56,580 --> 03:08:02,460 names yes they need to match exactly 4346 03:07:59,279 --> 03:08:06,479 and if we want to respond to click 4347 03:08:02,460 --> 03:08:08,279 events we simply go here with on click 4348 03:08:06,479 --> 03:08:10,319 and then we need to pass in the 4349 03:08:08,279 --> 03:08:12,060 reference so we need to set up those 4350 03:08:10,319 --> 03:08:13,680 functions again they can be inside of 4351 03:08:12,060 --> 03:08:15,060 the component they can be outside of the 4352 03:08:13,680 --> 03:08:18,240 component they can be coming from 4353 03:08:15,060 --> 03:08:20,700 another file that part is irrelevant we 4354 03:08:18,240 --> 03:08:23,580 just need to pass those functions as a 4355 03:08:20,700 --> 03:08:25,260 reference and then every time that event 4356 03:08:23,580 --> 03:08:28,979 is going to fire 4357 03:08:25,260 --> 03:08:31,859 guess what react is going to execute the 4358 03:08:28,979 --> 03:08:33,540 functionality we have inside of the 4359 03:08:31,859 --> 03:08:35,279 reference function 4360 03:08:33,540 --> 03:08:37,380 I'm not going to be particularly 4361 03:08:35,279 --> 03:08:41,340 original I'm just going to say handle 4362 03:08:37,380 --> 03:08:44,760 form input handle button click yes in 4363 03:08:41,340 --> 03:08:47,819 react World this is somewhat typical 4364 03:08:44,760 --> 03:08:51,120 essentially to start the name with 4365 03:08:47,819 --> 03:08:54,120 handle but again it's not a rule so 4366 03:08:51,120 --> 03:08:57,240 let's go back to index.js let's go above 4367 03:08:54,120 --> 03:08:59,760 the return and let's come up with those 4368 03:08:57,240 --> 03:09:02,580 two functions now of course we can 4369 03:08:59,760 --> 03:09:04,020 pass them interchangeably 4370 03:09:02,580 --> 03:09:05,100 as long as you have the reference 4371 03:09:04,020 --> 03:09:07,500 function 4372 03:09:05,100 --> 03:09:10,080 you can pass the same one in the input 4373 03:09:07,500 --> 03:09:12,120 as well as the button click but 4374 03:09:10,080 --> 03:09:14,760 most likely you'll have some specific 4375 03:09:12,120 --> 03:09:17,520 Logic for the input or for the button 4376 03:09:14,760 --> 03:09:21,960 click so let's go here with handle form 4377 03:09:17,520 --> 03:09:25,140 sorry at four but form input for now 4378 03:09:21,960 --> 03:09:27,960 we're gonna go with pretty basic Arrow 4379 03:09:25,140 --> 03:09:31,260 function and we'll simply go log and 4380 03:09:27,960 --> 03:09:33,000 we'll say handle form input then we'll 4381 03:09:31,260 --> 03:09:35,160 copy and paste and we'll just change 4382 03:09:33,000 --> 03:09:37,080 some stuff around so it's not going to 4383 03:09:35,160 --> 03:09:41,220 be handle form input 4384 03:09:37,080 --> 03:09:43,080 we'll say handle button click and 4385 03:09:41,220 --> 03:09:45,240 instead of log why don't we go with 4386 03:09:43,080 --> 03:09:48,600 alert 4387 03:09:45,240 --> 03:09:50,340 let's save that and then like I said the 4388 03:09:48,600 --> 03:09:51,960 event 4389 03:09:50,340 --> 03:09:54,840 to handle 4390 03:09:51,960 --> 03:09:57,600 form inputs is on change 4391 03:09:54,840 --> 03:10:00,779 we go to any input in our case this one 4392 03:09:57,600 --> 03:10:02,819 the text one and we go with on change 4393 03:10:00,779 --> 03:10:06,060 so every time user is going to type 4394 03:10:02,819 --> 03:10:06,960 something in the input the event will 4395 03:10:06,060 --> 03:10:10,080 fire 4396 03:10:06,960 --> 03:10:12,660 and yes this functionality 4397 03:10:10,080 --> 03:10:15,420 will be executed so we go here with 4398 03:10:12,660 --> 03:10:17,760 handle form input so we pass the 4399 03:10:15,420 --> 03:10:19,920 reference and we want to do same thing 4400 03:10:17,760 --> 03:10:22,740 here with the button in this case though 4401 03:10:19,920 --> 03:10:25,859 the event name is on click 4402 03:10:22,740 --> 03:10:30,359 what do we do we pass here the reference 4403 03:10:25,859 --> 03:10:33,720 handle button and click I just make sure 4404 03:10:30,359 --> 03:10:35,580 that the name is exact and let me open 4405 03:10:33,720 --> 03:10:38,580 up the dev tool since there's going to 4406 03:10:35,580 --> 03:10:41,340 be some console logs as I said you know 4407 03:10:38,580 --> 03:10:43,020 it I think I can remove this sucker for 4408 03:10:41,340 --> 03:10:47,160 now the props one 4409 03:10:43,020 --> 03:10:50,040 if only to use it later of course I will 4410 03:10:47,160 --> 03:10:53,040 uncomment it so let's try that out with 4411 03:10:50,040 --> 03:10:55,800 a button check it out the moment I press 4412 03:10:53,040 --> 03:10:57,359 F handle form input then of course it's 4413 03:10:55,800 --> 03:10:58,319 because I didn't change the name my 4414 03:10:57,359 --> 03:11:01,560 apologies 4415 03:10:58,319 --> 03:11:03,180 so in here we want to go with handle and 4416 03:11:01,560 --> 03:11:04,859 then button click 4417 03:11:03,180 --> 03:11:06,779 of course you can leave the same one but 4418 03:11:04,859 --> 03:11:09,000 just so you can see that I'm not messing 4419 03:11:06,779 --> 03:11:10,620 with you I'm going to go click the 4420 03:11:09,000 --> 03:11:13,560 correct one 4421 03:11:10,620 --> 03:11:15,600 and of course we want to press OK OK 4422 03:11:13,560 --> 03:11:17,819 and then let's click one more time 4423 03:11:15,600 --> 03:11:19,800 handle button click awesome 4424 03:11:17,819 --> 03:11:21,899 so we can see that this functionality 4425 03:11:19,800 --> 03:11:24,000 works and the same is going to be with 4426 03:11:21,899 --> 03:11:25,200 our input so I'm going to go here and 4427 03:11:24,000 --> 03:11:27,899 start typing 4428 03:11:25,200 --> 03:11:30,560 notice in the console I have these logs 4429 03:11:27,899 --> 03:11:33,479 which means that again we're responding 4430 03:11:30,560 --> 03:11:36,000 to the events 4431 03:11:33,479 --> 03:11:39,000 and as a result of course our 4432 03:11:36,000 --> 03:11:41,460 application is more Dynamic and again 4433 03:11:39,000 --> 03:11:44,540 you'll need three things you'll need an 4434 03:11:41,460 --> 03:11:47,700 element you'll need an event 4435 03:11:44,540 --> 03:11:49,020 and you can always find all of the 4436 03:11:47,700 --> 03:11:52,800 events 4437 03:11:49,020 --> 03:11:55,859 in react docs and lastly you need a 4438 03:11:52,800 --> 03:11:58,640 reference to a function or a callback 4439 03:11:55,859 --> 03:12:03,840 function that you're going to invoke 4440 03:11:58,640 --> 03:12:06,120 once the event takes place or fires off 4441 03:12:03,840 --> 03:12:08,760 remember in the last video when we 4442 03:12:06,120 --> 03:12:10,979 discussed the events in vanilla address 4443 03:12:08,760 --> 03:12:13,680 we covered that 4444 03:12:10,979 --> 03:12:16,680 when we pass the Callback function we 4445 03:12:13,680 --> 03:12:18,660 have access to an event object 4446 03:12:16,680 --> 03:12:22,200 and in that object we can 4447 03:12:18,660 --> 03:12:25,620 find bunch of useful info and guess what 4448 03:12:22,200 --> 03:12:28,140 we can do the same thing in react 4449 03:12:25,620 --> 03:12:31,080 so every 4450 03:12:28,140 --> 03:12:32,580 callback function so every function that 4451 03:12:31,080 --> 03:12:34,260 we're referencing 4452 03:12:32,580 --> 03:12:37,560 is going to have 4453 03:12:34,260 --> 03:12:40,740 access to that event and since it's a 4454 03:12:37,560 --> 03:12:43,740 parameter of course we can give it 4455 03:12:40,740 --> 03:12:46,620 whatever name we want so you can go with 4456 03:12:43,740 --> 03:12:50,580 e which is pretty popular you can go 4457 03:12:46,620 --> 03:12:51,479 with event and always bananas is also an 4458 03:12:50,580 --> 03:12:55,380 option 4459 03:12:51,479 --> 03:12:56,399 and let's just log it in the input so we 4460 03:12:55,380 --> 03:12:58,859 can see 4461 03:12:56,399 --> 03:13:01,500 what values we're getting back and then 4462 03:12:58,859 --> 03:13:03,420 we'll talk about the form submissions so 4463 03:13:01,500 --> 03:13:05,819 as far as the annual form input I'm 4464 03:13:03,420 --> 03:13:08,520 going to go here and 4465 03:13:05,819 --> 03:13:11,700 we'll do few logs in this video so let's 4466 03:13:08,520 --> 03:13:14,279 start with an event and check it out and 4467 03:13:11,700 --> 03:13:18,020 here we'll find bunch of useful info now 4468 03:13:14,279 --> 03:13:20,939 the ones that we'll use the most are 4469 03:13:18,020 --> 03:13:23,700 event.target.name so Target effectively 4470 03:13:20,939 --> 03:13:25,920 is going to point back to an element in 4471 03:13:23,700 --> 03:13:28,500 our case since we're logging it in the 4472 03:13:25,920 --> 03:13:30,300 input yes it's pointing back to an input 4473 03:13:28,500 --> 03:13:34,080 and then remember in the previous video 4474 03:13:30,300 --> 03:13:37,260 we set up name is equal to an example 4475 03:13:34,080 --> 03:13:38,660 and if we want to access that name we go 4476 03:13:37,260 --> 03:13:40,800 to 4477 03:13:38,660 --> 03:13:45,420 event.target.name and if you want to get 4478 03:13:40,800 --> 03:13:45,420 the value we go with event.target.value 4479 03:13:45,540 --> 03:13:48,540 and essentially this is going to allow 4480 03:13:47,880 --> 03:13:50,100 us 4481 03:13:48,540 --> 03:13:53,640 to collect 4482 03:13:50,100 --> 03:13:55,560 the data from the input and again we're 4483 03:13:53,640 --> 03:13:58,920 not going to cover the entire example 4484 03:13:55,560 --> 03:14:02,160 because we haven't covered the state yet 4485 03:13:58,920 --> 03:14:05,359 but just to Showcase that it's actually 4486 03:14:02,160 --> 03:14:07,740 a case let me go here with log and then 4487 03:14:05,359 --> 03:14:10,560 event.target essentially is going to be 4488 03:14:07,740 --> 03:14:12,479 our input and then let's copy and paste 4489 03:14:10,560 --> 03:14:14,640 and if you want of course you can write 4490 03:14:12,479 --> 03:14:16,200 some additional text over here 4491 03:14:14,640 --> 03:14:17,700 just so you can see which one is which 4492 03:14:16,200 --> 03:14:19,500 but in my case I'm going to go the first 4493 03:14:17,700 --> 03:14:22,920 one name and then the second one value 4494 03:14:19,500 --> 03:14:25,080 and again it's going to log once we 4495 03:14:22,920 --> 03:14:27,240 provide some value in input and notice 4496 03:14:25,080 --> 03:14:29,640 so the first one is my element the 4497 03:14:27,240 --> 03:14:31,979 second one is the value that we provide 4498 03:14:29,640 --> 03:14:34,680 for the name so of course if I'll change 4499 03:14:31,979 --> 03:14:37,560 this around and if I'll say 4500 03:14:34,680 --> 03:14:40,200 product and if I'm going to go back to 4501 03:14:37,560 --> 03:14:42,660 an input any file type some kind of 4502 03:14:40,200 --> 03:14:45,000 letter now of course 4503 03:14:42,660 --> 03:14:48,600 in the console I'll see the product 4504 03:14:45,000 --> 03:14:51,840 and the third one is the actual value 4505 03:14:48,600 --> 03:14:53,819 in the input and last one is the 4506 03:14:51,840 --> 03:14:57,420 original 4507 03:14:53,819 --> 03:14:59,700 log that we set up in the previous video 4508 03:14:57,420 --> 03:15:03,779 so these two will use quite often 4509 03:14:59,700 --> 03:15:06,600 throughout the course and also we'll use 4510 03:15:03,779 --> 03:15:08,279 prevent default which is very useful 4511 03:15:06,600 --> 03:15:10,740 when we have it formed 4512 03:15:08,279 --> 03:15:13,380 so if we want to respond to form 4513 03:15:10,740 --> 03:15:15,720 submissions we just need to set up an 4514 03:15:13,380 --> 03:15:18,660 event so we're going to go here with on 4515 03:15:15,720 --> 03:15:21,479 submit and let's 4516 03:15:18,660 --> 03:15:23,819 create that function as well so let me 4517 03:15:21,479 --> 03:15:26,220 copy and paste and I'm going to rename 4518 03:15:23,819 --> 03:15:29,160 it to handle form submission 4519 03:15:26,220 --> 03:15:31,740 hopefully I'm spelling this correctly 4520 03:15:29,160 --> 03:15:34,620 and for now let's just log something at 4521 03:15:31,740 --> 03:15:36,840 least try to actually will fail 4522 03:15:34,620 --> 03:15:39,960 that's the whole purpose why we covered 4523 03:15:36,840 --> 03:15:43,859 the event object so let's go with form 4524 03:15:39,960 --> 03:15:45,180 sub method and then let's pass in the 4525 03:15:43,859 --> 03:15:46,680 handle 4526 03:15:45,180 --> 03:15:50,100 form submission 4527 03:15:46,680 --> 03:15:51,960 and once I save I mean it doesn't really 4528 03:15:50,100 --> 03:15:55,100 matter whether we type something in the 4529 03:15:51,960 --> 03:15:58,260 input or no if we'll press enter 4530 03:15:55,100 --> 03:16:01,740 essentially your turn we should see 4531 03:15:58,260 --> 03:16:03,359 something in the browser but we don't 4532 03:16:01,740 --> 03:16:06,120 so we can clearly see that something 4533 03:16:03,359 --> 03:16:08,100 happened notice over here we have this 4534 03:16:06,120 --> 03:16:09,840 product but we didn't see anything in 4535 03:16:08,100 --> 03:16:12,960 the browser and the reason for that is 4536 03:16:09,840 --> 03:16:16,080 because the default behavior of the 4537 03:16:12,960 --> 03:16:18,540 forms is to collect those values 4538 03:16:16,080 --> 03:16:21,060 and essentially 4539 03:16:18,540 --> 03:16:23,640 send it to 4540 03:16:21,060 --> 03:16:26,460 some URL now in this case we don't want 4541 03:16:23,640 --> 03:16:27,420 to do that we want to handle the forms 4542 03:16:26,460 --> 03:16:29,399 ourselves 4543 03:16:27,420 --> 03:16:31,020 and that's why we want to access the 4544 03:16:29,399 --> 03:16:33,899 event again 4545 03:16:31,020 --> 03:16:36,000 same as with handle form input we have 4546 03:16:33,899 --> 03:16:38,700 right away access to it and in there 4547 03:16:36,000 --> 03:16:41,580 there is a function called 4548 03:16:38,700 --> 03:16:44,100 event Dot and then prevent 4549 03:16:41,580 --> 03:16:46,620 default so this is also something that 4550 03:16:44,100 --> 03:16:49,140 we use quite often and effectively this 4551 03:16:46,620 --> 03:16:51,720 just means that we will handle those 4552 03:16:49,140 --> 03:16:55,740 form submissions ourselves and as you 4553 03:16:51,720 --> 03:16:57,840 can see now we can clearly see this log 4554 03:16:55,740 --> 03:17:00,180 in the console 4555 03:16:57,840 --> 03:17:02,220 so each and every 4556 03:17:00,180 --> 03:17:05,460 reference function is going to have 4557 03:17:02,220 --> 03:17:09,000 access to event in there we can find 4558 03:17:05,460 --> 03:17:12,200 bunch of useful things but mostly will 4559 03:17:09,000 --> 03:17:15,660 work with Target target.name 4560 03:17:12,200 --> 03:17:19,740 target.value as well as event prevent 4561 03:17:15,660 --> 03:17:22,380 default now if for some weird reason you 4562 03:17:19,740 --> 03:17:26,580 still don't see the log once you submit 4563 03:17:22,380 --> 03:17:29,640 the form don't panic and just try this 4564 03:17:26,580 --> 03:17:33,420 Mouse click on the input and then press 4565 03:17:29,640 --> 03:17:36,300 enter or return on the keyboard 4566 03:17:33,420 --> 03:17:37,200 basically we want to set focus on the 4567 03:17:36,300 --> 03:17:40,920 input 4568 03:17:37,200 --> 03:17:43,979 and don't worry in the following videos 4569 03:17:40,920 --> 03:17:46,920 we will add a button to submit the form 4570 03:17:43,979 --> 03:17:50,580 which actually is going to be our setup 4571 03:17:46,920 --> 03:17:53,880 for the rest of the course and in that 4572 03:17:50,580 --> 03:17:57,359 case we won't have to do all of these 4573 03:17:53,880 --> 03:18:00,060 acrobatics and before we continue let's 4574 03:17:57,359 --> 03:18:02,040 just take a small break and let's 4575 03:18:00,060 --> 03:18:06,720 discuss something that 4576 03:18:02,040 --> 03:18:09,960 pops up quite often in the course q a 4577 03:18:06,720 --> 03:18:13,800 and that essentially is how we can 4578 03:18:09,960 --> 03:18:15,660 handle form submissions in HTML again 4579 03:18:13,800 --> 03:18:17,340 please keep in mind that at the end of 4580 03:18:15,660 --> 03:18:21,359 the day we're still writing HTML we're 4581 03:18:17,340 --> 03:18:24,000 still writing CSS and yes JavaScript and 4582 03:18:21,359 --> 03:18:25,740 essentially we have two options 4583 03:18:24,000 --> 03:18:28,859 if we set up 4584 03:18:25,740 --> 03:18:30,600 a button with type submit 4585 03:18:28,859 --> 03:18:34,439 in the form 4586 03:18:30,600 --> 03:18:38,220 we can also utilize that button 4587 03:18:34,439 --> 03:18:40,380 but keep in mind that the value has to 4588 03:18:38,220 --> 03:18:43,260 be submit 4589 03:18:40,380 --> 03:18:45,779 so if it's just going to be a button 4590 03:18:43,260 --> 03:18:47,580 then it's not going to work so let me 4591 03:18:45,779 --> 03:18:50,220 just showcase what I'm talking about I'm 4592 03:18:47,580 --> 03:18:53,040 going to go back to index.js 4593 03:18:50,220 --> 03:18:56,340 and essentially where I have the form 4594 03:18:53,040 --> 03:19:00,180 first let me move this sucker up over 4595 03:18:56,340 --> 03:19:01,560 here let me add type and then equals to 4596 03:19:00,180 --> 03:19:04,200 A Button 4597 03:19:01,560 --> 03:19:05,520 and then let's copy and paste and we're 4598 03:19:04,200 --> 03:19:06,300 not going to have any on clicks over 4599 03:19:05,520 --> 03:19:09,300 here 4600 03:19:06,300 --> 03:19:10,680 and instead of type button we're gonna 4601 03:19:09,300 --> 03:19:13,560 go here with 4602 03:19:10,680 --> 03:19:16,200 submit and then instead of Click me just 4603 03:19:13,560 --> 03:19:17,760 so we can see which one is a which we're 4604 03:19:16,200 --> 03:19:18,540 going to go with submit and I'll move it 4605 03:19:17,760 --> 03:19:22,680 up 4606 03:19:18,540 --> 03:19:24,060 so let's save I have this submit one 4607 03:19:22,680 --> 03:19:26,460 and you know what 4608 03:19:24,060 --> 03:19:29,760 just so it's not all over the place I'll 4609 03:19:26,460 --> 03:19:32,819 add a div and just wrap the button the 4610 03:19:29,760 --> 03:19:34,859 second one the quick one and check it 4611 03:19:32,819 --> 03:19:37,319 out now of course we can click and 4612 03:19:34,859 --> 03:19:40,380 notice we nicely submitted the form 4613 03:19:37,319 --> 03:19:41,700 why well because we have on submit on 4614 03:19:40,380 --> 03:19:44,700 the form 4615 03:19:41,700 --> 03:19:47,100 and we have the Callback function and of 4616 03:19:44,700 --> 03:19:48,899 course we prevent the default and you 4617 03:19:47,100 --> 03:19:51,300 already know rest of the stuff 4618 03:19:48,899 --> 03:19:53,279 now if I'll click this one 4619 03:19:51,300 --> 03:19:56,040 I'll have the alert 4620 03:19:53,279 --> 03:19:58,080 so if we have just type button it's not 4621 03:19:56,040 --> 03:20:00,899 going to submit the form then please 4622 03:19:58,080 --> 03:20:02,160 keep in mind this is straight up HTML 4623 03:20:00,899 --> 03:20:04,560 setup 4624 03:20:02,160 --> 03:20:09,180 that's effectively how it works 4625 03:20:04,560 --> 03:20:12,120 now if you do want to set up an event 4626 03:20:09,180 --> 03:20:14,399 over here on this button let's say you 4627 03:20:12,120 --> 03:20:17,880 just don't like this unsubmit here in 4628 03:20:14,399 --> 03:20:20,220 the form you can definitely do so 4629 03:20:17,880 --> 03:20:21,779 and the logic is going to work exactly 4630 03:20:20,220 --> 03:20:23,760 the same but 4631 03:20:21,779 --> 03:20:26,640 the event you want to set up over here 4632 03:20:23,760 --> 03:20:27,479 is on click so here's what I'm talking 4633 03:20:26,640 --> 03:20:29,220 about 4634 03:20:27,479 --> 03:20:31,620 if for some reason you don't like this 4635 03:20:29,220 --> 03:20:33,240 setup you can remove 4636 03:20:31,620 --> 03:20:34,439 but in here you're not going to write on 4637 03:20:33,240 --> 03:20:36,180 submit 4638 03:20:34,439 --> 03:20:37,380 so you're not going to go here on submit 4639 03:20:36,180 --> 03:20:39,359 and then 4640 03:20:37,380 --> 03:20:42,660 let's say pass in the value let me try 4641 03:20:39,359 --> 03:20:44,760 that one notice over here up in paste 4642 03:20:42,660 --> 03:20:49,080 okay good 4643 03:20:44,760 --> 03:20:51,960 notice right so it's not going to work 4644 03:20:49,080 --> 03:20:55,920 you're going to get an error 4645 03:20:51,960 --> 03:20:58,380 in order to properly submit this form 4646 03:20:55,920 --> 03:21:00,479 we'll have to go here with on click 4647 03:20:58,380 --> 03:21:02,819 because it's a button 4648 03:21:00,479 --> 03:21:05,939 so you need to provide here on click and 4649 03:21:02,819 --> 03:21:09,000 then again both of them work so we can 4650 03:21:05,939 --> 03:21:13,439 submit the form with enter if I just go 4651 03:21:09,000 --> 03:21:16,080 to an input and press return key or if I 4652 03:21:13,439 --> 03:21:19,500 click the button and again both of them 4653 03:21:16,080 --> 03:21:21,600 are in your readme so you can clearly 4654 03:21:19,500 --> 03:21:26,580 see if you want to use the button code 4655 03:21:21,600 --> 03:21:29,220 on click if you're using the form just 4656 03:21:26,580 --> 03:21:31,380 make sure this one is type submit 4657 03:21:29,220 --> 03:21:33,600 because type button 4658 03:21:31,380 --> 03:21:36,240 it's not going to work hopefully that is 4659 03:21:33,600 --> 03:21:39,300 clear and now we can move on to the next 4660 03:21:36,240 --> 03:21:43,140 topic and while we're still on the event 4661 03:21:39,300 --> 03:21:46,560 topic let me throw you a mind grenade 4662 03:21:43,140 --> 03:21:50,279 remember I believe two videos ago when 4663 03:21:46,560 --> 03:21:52,319 we discussed the basic setup 4664 03:21:50,279 --> 03:21:54,540 in vanilla address 4665 03:21:52,319 --> 03:21:58,080 we use this example 4666 03:21:54,540 --> 03:22:00,359 now what is happening over here 4667 03:21:58,080 --> 03:22:01,319 I have here Anonymous callback function 4668 03:22:00,359 --> 03:22:03,660 right 4669 03:22:01,319 --> 03:22:06,779 now of course you can set up here the 4670 03:22:03,660 --> 03:22:09,120 variable set it equal to a function or 4671 03:22:06,779 --> 03:22:11,520 you can pass here directly 4672 03:22:09,120 --> 03:22:13,500 Arrow function which by default is 4673 03:22:11,520 --> 03:22:16,859 anonymous 4674 03:22:13,500 --> 03:22:19,500 the main point is that we can directly 4675 03:22:16,859 --> 03:22:21,479 pass in Anonymous function 4676 03:22:19,500 --> 03:22:23,279 so 4677 03:22:21,479 --> 03:22:26,700 do you think we can do that you know 4678 03:22:23,279 --> 03:22:29,040 react and of course the answer is yes 4679 03:22:26,700 --> 03:22:31,500 so you'll also see 4680 03:22:29,040 --> 03:22:33,359 this alternative approach 4681 03:22:31,500 --> 03:22:36,540 where instead of setting up those 4682 03:22:33,359 --> 03:22:37,859 functions here or in the file or in a 4683 03:22:36,540 --> 03:22:42,120 separate file 4684 03:22:37,859 --> 03:22:44,040 we pass the anonymous function directly 4685 03:22:42,120 --> 03:22:46,740 so in my case of course I'm passing here 4686 03:22:44,040 --> 03:22:50,100 the arrow function now please keep in 4687 03:22:46,740 --> 03:22:52,020 mind we're not invoking it over here 4688 03:22:50,100 --> 03:22:55,380 we just pass 4689 03:22:52,020 --> 03:22:58,319 the function so of course this logic is 4690 03:22:55,380 --> 03:23:00,479 going to run when well when we click on 4691 03:22:58,319 --> 03:23:03,779 a button in this case so let's try it 4692 03:23:00,479 --> 03:23:05,760 out I'm going to navigate back of course 4693 03:23:03,779 --> 03:23:07,680 I keep repeating this but I just want to 4694 03:23:05,760 --> 03:23:09,840 make it clear all of this code is 4695 03:23:07,680 --> 03:23:13,859 available and read me for your reference 4696 03:23:09,840 --> 03:23:15,600 so I'll simply do this I'll remove and I 4697 03:23:13,859 --> 03:23:17,100 don't think I'm gonna worry about the on 4698 03:23:15,600 --> 03:23:19,680 submit 4699 03:23:17,100 --> 03:23:22,260 so let me remove it keep in mind that 4700 03:23:19,680 --> 03:23:25,140 yes essentially it works the same way 4701 03:23:22,260 --> 03:23:27,479 but instead of using reference 4702 03:23:25,140 --> 03:23:30,060 I'm going to go with my arrow function 4703 03:23:27,479 --> 03:23:31,920 and then whatever logic we want to do 4704 03:23:30,060 --> 03:23:34,080 in our case what do we want to do we 4705 03:23:31,920 --> 03:23:37,319 simply want to log yes for now we'll 4706 03:23:34,080 --> 03:23:39,660 just be a little bit conservative so 4707 03:23:37,319 --> 03:23:42,899 let's go here with console log and then 4708 03:23:39,660 --> 03:23:45,300 say click me and of course we'll have an 4709 03:23:42,899 --> 03:23:47,819 error because we don't have here the 4710 03:23:45,300 --> 03:23:50,640 reference so let's do the same thing and 4711 03:23:47,819 --> 03:23:52,680 I guess one of the major gotchas here is 4712 03:23:50,640 --> 03:23:54,239 that we still have access to this event 4713 03:23:52,680 --> 03:23:58,080 object 4714 03:23:54,239 --> 03:23:59,160 so that doesn't change if I want to go 4715 03:23:58,080 --> 03:24:02,540 log 4716 03:23:59,160 --> 03:24:04,200 and inside of it I want to go with 4717 03:24:02,540 --> 03:24:07,439 event.target.value I'll still access 4718 03:24:04,200 --> 03:24:09,239 whatever is in the input and yes it's 4719 03:24:07,439 --> 03:24:11,760 still going to fire every time the user 4720 03:24:09,239 --> 03:24:14,100 types something in input again those 4721 03:24:11,760 --> 03:24:15,960 things don't change but you'll see this 4722 03:24:14,100 --> 03:24:17,880 shorter syntax 4723 03:24:15,960 --> 03:24:19,979 is there a rule no 4724 03:24:17,880 --> 03:24:22,140 if you want to use this one use this one 4725 03:24:19,979 --> 03:24:24,359 if you want to set up the reference use 4726 03:24:22,140 --> 03:24:26,760 that one mostly it depends on how much 4727 03:24:24,359 --> 03:24:28,140 logic you have if let's say you have 4728 03:24:26,760 --> 03:24:29,760 this input 4729 03:24:28,140 --> 03:24:32,580 and you're just accessing the value 4730 03:24:29,760 --> 03:24:34,680 quite often you'll see this approach if 4731 03:24:32,580 --> 03:24:36,660 you have some kind of button click and 4732 03:24:34,680 --> 03:24:39,180 then there's 20 lines of code 4733 03:24:36,660 --> 03:24:41,460 theoretically you can set it up over 4734 03:24:39,180 --> 03:24:44,220 here just keep in mind that you'll have 4735 03:24:41,460 --> 03:24:46,439 to set up the curlies and all that but I 4736 03:24:44,220 --> 03:24:48,600 mean most realistically then you'll set 4737 03:24:46,439 --> 03:24:49,500 up a reference function and now check it 4738 03:24:48,600 --> 03:24:52,380 out 4739 03:24:49,500 --> 03:24:56,100 both of them work exactly the same 4740 03:24:52,380 --> 03:24:58,859 but effectively we just have less lines 4741 03:24:56,100 --> 03:25:01,080 of code and since I'm enjoying mind 4742 03:24:58,859 --> 03:25:02,819 grenades so much I'm effectively 4743 03:25:01,080 --> 03:25:05,100 throwing them at you 4744 03:25:02,819 --> 03:25:08,580 let me throw another one 4745 03:25:05,100 --> 03:25:11,939 and that is the simple fact that 4746 03:25:08,580 --> 03:25:14,340 components are independent by default 4747 03:25:11,939 --> 03:25:16,680 and if this at the moment sounds like 4748 03:25:14,340 --> 03:25:19,080 okay well what's the big deal let me 4749 03:25:16,680 --> 03:25:21,479 show you an example 4750 03:25:19,080 --> 03:25:23,939 um after that I'll probably spend five 4751 03:25:21,479 --> 03:25:25,739 minutes just raving about it and let's 4752 03:25:23,939 --> 03:25:27,660 start I guess by removing the events 4753 03:25:25,739 --> 03:25:30,060 again we cover the basics we'll return 4754 03:25:27,660 --> 03:25:34,140 to events many times throughout the 4755 03:25:30,060 --> 03:25:36,660 course it's one of the main features to 4756 03:25:34,140 --> 03:25:38,520 any application if it's Dynamic so 4757 03:25:36,660 --> 03:25:41,160 there's no need to keep that component 4758 03:25:38,520 --> 03:25:43,800 here it's always available in readme and 4759 03:25:41,160 --> 03:25:45,540 therefore I'm going to go to index and 4760 03:25:43,800 --> 03:25:47,520 first I'll remove 4761 03:25:45,540 --> 03:25:50,640 the reference 4762 03:25:47,520 --> 03:25:51,960 and then I'll actually remove the 4763 03:25:50,640 --> 03:25:55,439 component 4764 03:25:51,960 --> 03:25:59,399 and now let's try to set up an event in 4765 03:25:55,439 --> 03:26:02,340 the book component and it's going to be 4766 03:25:59,399 --> 03:26:04,439 somewhat straightforward where I want to 4767 03:26:02,340 --> 03:26:06,540 set up a function I'll call this display 4768 03:26:04,439 --> 03:26:09,000 title if you want to practice 4769 03:26:06,540 --> 03:26:10,680 on setting up Anonymous callback 4770 03:26:09,000 --> 03:26:12,840 functions you can definitely do so in my 4771 03:26:10,680 --> 03:26:15,720 case I'll have a reference and I simply 4772 03:26:12,840 --> 03:26:18,180 want to log the title that's it 4773 03:26:15,720 --> 03:26:20,700 not much and of course we do need to set 4774 03:26:18,180 --> 03:26:21,720 up the event we need to reference the 4775 03:26:20,700 --> 03:26:23,040 function 4776 03:26:21,720 --> 03:26:25,380 and 4777 03:26:23,040 --> 03:26:28,080 it kind of makes sense to have some kind 4778 03:26:25,380 --> 03:26:30,239 of value in there as far as the value 4779 03:26:28,080 --> 03:26:32,279 inside of the button so let's start here 4780 03:26:30,239 --> 03:26:34,800 doesn't really matter where you place it 4781 03:26:32,279 --> 03:26:36,840 I think I'm gonna do it in between just 4782 03:26:34,800 --> 03:26:39,300 because I have some margins 4783 03:26:36,840 --> 03:26:41,279 so let's go here with on click for now 4784 03:26:39,300 --> 03:26:44,399 we don't have the function 4785 03:26:41,279 --> 03:26:47,160 so let me first just type display 4786 03:26:44,399 --> 03:26:48,960 title and then above it let's go with 4787 03:26:47,160 --> 03:26:52,439 that function const 4788 03:26:48,960 --> 03:26:55,140 display title and 4789 03:26:52,439 --> 03:26:57,779 let's go here and log that 4790 03:26:55,140 --> 03:27:00,120 so I already have the log so just so I 4791 03:26:57,779 --> 03:27:02,640 can speed this up or copy and paste oh 4792 03:27:00,120 --> 03:27:05,580 uncommon this one and we're gonna go 4793 03:27:02,640 --> 03:27:08,640 with title so what am I logging here 4794 03:27:05,580 --> 03:27:11,399 a title prop correct 4795 03:27:08,640 --> 03:27:13,739 so let's go here let's say display title 4796 03:27:11,399 --> 03:27:15,420 let's save it let's find the button 4797 03:27:13,739 --> 03:27:17,640 somewhere here 4798 03:27:15,420 --> 03:27:21,120 and check it out right away in the 4799 03:27:17,640 --> 03:27:23,040 console I see the same exact title that 4800 03:27:21,120 --> 03:27:25,140 is displayed in component 4801 03:27:23,040 --> 03:27:27,660 and guess what 4802 03:27:25,140 --> 03:27:30,899 if I click 4803 03:27:27,660 --> 03:27:34,680 the same button in the second component 4804 03:27:30,899 --> 03:27:37,319 it does the same only this time 4805 03:27:34,680 --> 03:27:40,680 it references the title that is 4806 03:27:37,319 --> 03:27:43,620 associated with the second component 4807 03:27:40,680 --> 03:27:46,979 and if you have done 4808 03:27:43,620 --> 03:27:49,319 pretty much any work in vanilla.js 4809 03:27:46,979 --> 03:27:52,500 you know that you need to do quite a bit 4810 03:27:49,319 --> 03:27:55,140 of acrobatics to get to that point 4811 03:27:52,500 --> 03:27:58,279 to essentially have some functionality 4812 03:27:55,140 --> 03:28:01,800 directly point to that one specific item 4813 03:27:58,279 --> 03:28:03,660 and just to give you an example let me 4814 03:28:01,800 --> 03:28:06,000 show you one of the JavaScript projects 4815 03:28:03,660 --> 03:28:08,460 that we're working on my course 4816 03:28:06,000 --> 03:28:11,040 I think it was this one either the 4817 03:28:08,460 --> 03:28:12,120 questions or the tabs I think it was 4818 03:28:11,040 --> 03:28:14,279 tabs 4819 03:28:12,120 --> 03:28:16,439 no sorry what was the questions my bad 4820 03:28:14,279 --> 03:28:19,439 my bad I'm gonna go back we're going for 4821 03:28:16,439 --> 03:28:22,439 questions and notice I mean it's not 4822 03:28:19,439 --> 03:28:24,000 earth shattering functionality basically 4823 03:28:22,439 --> 03:28:27,120 we're just displaying some kind of 4824 03:28:24,000 --> 03:28:28,020 content once we click on a tab but trust 4825 03:28:27,120 --> 03:28:30,540 me 4826 03:28:28,020 --> 03:28:32,939 in order to get to that point 4827 03:28:30,540 --> 03:28:35,939 where each of these buttons only 4828 03:28:32,939 --> 03:28:37,620 reference the specific item it's not as 4829 03:28:35,939 --> 03:28:40,979 easy as just setting up here to function 4830 03:28:37,620 --> 03:28:43,920 and the button so if you open up the 4831 03:28:40,979 --> 03:28:46,680 developer tools the sources tab 4832 03:28:43,920 --> 03:28:49,920 and more specifically MJS 4833 03:28:46,680 --> 03:28:52,260 you'll see that we're selecting all of 4834 03:28:49,920 --> 03:28:55,140 the questions we iterate over the 4835 03:28:52,260 --> 03:28:56,399 questions as we're iterating we're 4836 03:28:55,140 --> 03:29:00,180 selecting 4837 03:28:56,399 --> 03:29:02,460 the button and then essentially we 4838 03:29:00,180 --> 03:29:05,100 toggle the text inside of the function 4839 03:29:02,460 --> 03:29:06,899 now again there's multiple ways how you 4840 03:29:05,100 --> 03:29:09,899 can set up the logic 4841 03:29:06,899 --> 03:29:11,520 we also cover the alternative 4842 03:29:09,899 --> 03:29:14,220 that's not the point 4843 03:29:11,520 --> 03:29:15,960 the point is that in vanilla.js it 4844 03:29:14,220 --> 03:29:19,500 doesn't come by default 4845 03:29:15,960 --> 03:29:22,080 you cannot just right away go to a 4846 03:29:19,500 --> 03:29:25,200 specific element and say that 4847 03:29:22,080 --> 03:29:27,239 the functionality is going to apply only 4848 03:29:25,200 --> 03:29:30,300 to that element 4849 03:29:27,239 --> 03:29:32,939 you need to do more work and that is not 4850 03:29:30,300 --> 03:29:35,100 the case in react 4851 03:29:32,939 --> 03:29:38,939 so this is 4852 03:29:35,100 --> 03:29:41,760 an extremely powerful concept where 4853 03:29:38,939 --> 03:29:44,340 essentially whatever functionality will 4854 03:29:41,760 --> 03:29:47,220 have inside of the component 4855 03:29:44,340 --> 03:29:52,080 it's narrowed down to that component 4856 03:29:47,220 --> 03:29:55,260 where in vanilla.js in order to 4857 03:29:52,080 --> 03:29:58,080 have this type of functionality you need 4858 03:29:55,260 --> 03:29:59,460 to jump through the Hoops you need to do 4859 03:29:58,080 --> 03:30:01,739 some acrobatics 4860 03:29:59,460 --> 03:30:04,319 in react 4861 03:30:01,739 --> 03:30:06,960 it comes by default we don't need to do 4862 03:30:04,319 --> 03:30:10,460 anything okay and up next let's talk 4863 03:30:06,960 --> 03:30:14,640 about react data flow and in the process 4864 03:30:10,460 --> 03:30:16,319 I will cover what is prop Drilling and 4865 03:30:14,640 --> 03:30:18,420 the first thing we need to keep in mind 4866 03:30:16,319 --> 03:30:22,620 that in react 4867 03:30:18,420 --> 03:30:26,279 we can only pass the data down 4868 03:30:22,620 --> 03:30:30,180 so in our case we can only pass data 4869 03:30:26,279 --> 03:30:33,359 from book list to a book component 4870 03:30:30,180 --> 03:30:35,340 not other way around and yes if you have 4871 03:30:33,359 --> 03:30:38,939 a component 4872 03:30:35,340 --> 03:30:40,680 nested inside of the book component 4873 03:30:38,939 --> 03:30:43,260 and if you want to pass something from 4874 03:30:40,680 --> 03:30:46,560 the book list all the way down to that 4875 03:30:43,260 --> 03:30:49,200 component you'll have to go through the 4876 03:30:46,560 --> 03:30:52,200 book component of course there are 4877 03:30:49,200 --> 03:30:54,479 alternatives so later on 4878 03:30:52,200 --> 03:30:56,700 will cover context API we'll talk about 4879 03:30:54,479 --> 03:30:58,620 Redux and 4880 03:30:56,700 --> 03:31:01,560 of course you can use other state 4881 03:30:58,620 --> 03:31:02,460 libraries for that as well but by 4882 03:31:01,560 --> 03:31:06,060 default 4883 03:31:02,460 --> 03:31:08,340 we can only pass the props down 4884 03:31:06,060 --> 03:31:11,340 and essentially in a process we're doing 4885 03:31:08,340 --> 03:31:14,520 something called prop drilling so let's 4886 03:31:11,340 --> 03:31:16,500 just take a look at the example and 4887 03:31:14,520 --> 03:31:19,560 you'll see what I'm talking about first 4888 03:31:16,500 --> 03:31:21,479 let's navigate to index.js 4889 03:31:19,560 --> 03:31:24,720 I don't need the title 4890 03:31:21,479 --> 03:31:27,120 but I will keep the button because we'll 4891 03:31:24,720 --> 03:31:30,300 implement it in this example as well 4892 03:31:27,120 --> 03:31:32,580 it's not going to be display title we'll 4893 03:31:30,300 --> 03:31:34,739 just say click me doesn't really matter 4894 03:31:32,580 --> 03:31:36,720 what you write over there and then as 4895 03:31:34,739 --> 03:31:41,100 far as the book list let me come up with 4896 03:31:36,720 --> 03:31:43,080 first variable so const some value and 4897 03:31:41,100 --> 03:31:46,439 I'll go with Shake 4898 03:31:43,080 --> 03:31:47,460 and bake and up next let's set up a 4899 03:31:46,439 --> 03:31:49,920 function 4900 03:31:47,460 --> 03:31:51,840 so hopefully it's clear that when it 4901 03:31:49,920 --> 03:31:53,580 comes to this type of variable 4902 03:31:51,840 --> 03:31:55,500 effectively 4903 03:31:53,580 --> 03:31:57,899 we would just set up 4904 03:31:55,500 --> 03:31:59,640 the prop correct now in this case of 4905 03:31:57,899 --> 03:32:01,979 course we're iterating over the list and 4906 03:31:59,640 --> 03:32:04,859 we're passing into a book but let's say 4907 03:32:01,979 --> 03:32:08,399 if I have I don't know some component 4908 03:32:04,859 --> 03:32:11,939 over here so some component in order to 4909 03:32:08,399 --> 03:32:14,220 pass it down what would I do well I will 4910 03:32:11,939 --> 03:32:16,739 just pass it here as a prop right so I 4911 03:32:14,220 --> 03:32:20,279 would say some value is equal to some 4912 03:32:16,739 --> 03:32:21,060 value name is as always up to you but I 4913 03:32:20,279 --> 03:32:22,920 mean 4914 03:32:21,060 --> 03:32:24,960 in my case I'm just going to go with the 4915 03:32:22,920 --> 03:32:27,479 same name so some value equals the sum 4916 03:32:24,960 --> 03:32:31,439 value hopefully that is clear 4917 03:32:27,479 --> 03:32:34,260 but what about if we have a function so 4918 03:32:31,439 --> 03:32:37,800 let's say what if I have a function that 4919 03:32:34,260 --> 03:32:41,340 has a name of display value and in here 4920 03:32:37,800 --> 03:32:42,540 I'm accessing that variable in the 4921 03:32:41,340 --> 03:32:45,660 component 4922 03:32:42,540 --> 03:32:47,819 so I'm accessing the variable that is in 4923 03:32:45,660 --> 03:32:49,260 the book list component again we're just 4924 03:32:47,819 --> 03:32:51,239 going to be logging 4925 03:32:49,260 --> 03:32:55,560 we don't know how to control the state 4926 03:32:51,239 --> 03:32:58,319 yet so how do we pass this function down 4927 03:32:55,560 --> 03:33:00,420 well effectively it's the same thing we 4928 03:32:58,319 --> 03:33:03,300 need to come up with a prop name and we 4929 03:33:00,420 --> 03:33:05,939 need to pass it as a value 4930 03:33:03,300 --> 03:33:08,160 same deal we're iterating over the book 4931 03:33:05,939 --> 03:33:10,560 and we have actually this book component 4932 03:33:08,160 --> 03:33:14,220 so I'll pass it here just keep in mind 4933 03:33:10,560 --> 03:33:15,540 the same works with any component that 4934 03:33:14,220 --> 03:33:19,200 you have 4935 03:33:15,540 --> 03:33:21,120 in the jsx so let me remove this one 4936 03:33:19,200 --> 03:33:23,220 because otherwise I'll get an error of 4937 03:33:21,120 --> 03:33:25,080 course there is no sum component and let 4938 03:33:23,220 --> 03:33:26,580 me just go here with display value 4939 03:33:25,080 --> 03:33:29,460 equals 4940 03:33:26,580 --> 03:33:32,819 to display value 4941 03:33:29,460 --> 03:33:34,380 and how do we access it and as I say not 4942 03:33:32,819 --> 03:33:38,160 the first I'll have the error over here 4943 03:33:34,380 --> 03:33:39,899 because there's nothing in the on click 4944 03:33:38,160 --> 03:33:42,420 so for now 4945 03:33:39,899 --> 03:33:44,580 let's just deal with display value 4946 03:33:42,420 --> 03:33:46,620 and in order to access it 4947 03:33:44,580 --> 03:33:47,939 it's in the props again if you want you 4948 03:33:46,620 --> 03:33:48,960 can log it 4949 03:33:47,939 --> 03:33:50,520 sure 4950 03:33:48,960 --> 03:33:53,100 in my case I already know that it's 4951 03:33:50,520 --> 03:33:55,920 there so I'm gonna go display value so 4952 03:33:53,100 --> 03:33:59,040 we grab the function and again we just 4953 03:33:55,920 --> 03:34:01,680 pass it as a reference that's it that's 4954 03:33:59,040 --> 03:34:03,420 all we have to do and now if we open up 4955 03:34:01,680 --> 03:34:05,340 the devtools 4956 03:34:03,420 --> 03:34:08,460 we'll nicely see 4957 03:34:05,340 --> 03:34:10,920 that we log shake and bake and it's 4958 03:34:08,460 --> 03:34:13,380 going to work the same in all of the 4959 03:34:10,920 --> 03:34:17,460 components and again the main points are 4960 03:34:13,380 --> 03:34:21,080 we can only pass props down 4961 03:34:17,460 --> 03:34:24,479 so from the parent to the child 4962 03:34:21,080 --> 03:34:27,840 and effectively it means that if you 4963 03:34:24,479 --> 03:34:30,420 have really nested structure 4964 03:34:27,840 --> 03:34:32,279 you'll have to go through all of those 4965 03:34:30,420 --> 03:34:34,080 components which of course 4966 03:34:32,279 --> 03:34:38,520 can get annoying 4967 03:34:34,080 --> 03:34:41,220 and that's what prop drilling stands for 4968 03:34:38,520 --> 03:34:43,200 the annoying part of passing perhaps 4969 03:34:41,220 --> 03:34:45,420 literally down I don't know 10 4970 03:34:43,200 --> 03:34:48,080 components or five components or 4971 03:34:45,420 --> 03:34:51,300 whatever now at least in my opinion 4972 03:34:48,080 --> 03:34:54,660 passing the props 4973 03:34:51,300 --> 03:34:56,520 from parent to child is not a prop 4974 03:34:54,660 --> 03:34:58,500 drilling again that comes down to 4975 03:34:56,520 --> 03:35:00,239 preference but in my opinion that's 4976 03:34:58,500 --> 03:35:02,399 totally okay if you have a parent and 4977 03:35:00,239 --> 03:35:05,340 you pass it down to a child however 4978 03:35:02,399 --> 03:35:07,500 if there is a grandchild basically if 4979 03:35:05,340 --> 03:35:09,000 there is a child component inside of the 4980 03:35:07,500 --> 03:35:10,859 book and then you have to do the same 4981 03:35:09,000 --> 03:35:12,600 thing essentially you don't use it in 4982 03:35:10,859 --> 03:35:17,160 the book component but you pass it down 4983 03:35:12,600 --> 03:35:19,859 to whatever component in jsx yes in that 4984 03:35:17,160 --> 03:35:22,319 case at least in my opinion it becomes 4985 03:35:19,859 --> 03:35:23,460 prop Drilling and we'll come back to 4986 03:35:22,319 --> 03:35:25,620 this topic 4987 03:35:23,460 --> 03:35:28,160 a little bit later when we discuss the 4988 03:35:25,620 --> 03:35:30,840 Alternatives when we discuss context API 4989 03:35:28,160 --> 03:35:33,420 Redux and all that stuff again this is 4990 03:35:30,840 --> 03:35:36,060 just to give you a general infill 4991 03:35:33,420 --> 03:35:38,160 and one last time let me just repeat 4992 03:35:36,060 --> 03:35:41,819 that in react 4993 03:35:38,160 --> 03:35:45,300 as far as data flow we can only pass 4994 03:35:41,819 --> 03:35:48,200 props down from the parent to the child 4995 03:35:45,300 --> 03:35:51,840 okay and before we discuss es6 modules 4996 03:35:48,200 --> 03:35:55,439 in react applications let's also cover 4997 03:35:51,840 --> 03:35:58,500 more complex example when 4998 03:35:55,439 --> 03:36:01,500 we have a function in a component and we 4999 03:35:58,500 --> 03:36:04,040 need to invoke it in the child and as I 5000 03:36:01,500 --> 03:36:08,100 said this is something that keeps 5001 03:36:04,040 --> 03:36:11,460 tripping people up later in the course 5002 03:36:08,100 --> 03:36:13,200 so please pay a close attention 5003 03:36:11,460 --> 03:36:16,439 and in here we'll start with a little 5004 03:36:13,200 --> 03:36:19,260 challenge where essentially in the book 5005 03:36:16,439 --> 03:36:21,660 list component I want you to create a 5006 03:36:19,260 --> 03:36:24,420 get book function 5007 03:36:21,660 --> 03:36:26,880 now that get book function 5008 03:36:24,420 --> 03:36:29,340 accept ID as an argument 5009 03:36:26,880 --> 03:36:31,620 and it finds a book 5010 03:36:29,340 --> 03:36:33,359 I can give you a hint I'm gonna use the 5011 03:36:31,620 --> 03:36:35,460 find method and if you're not familiar 5012 03:36:33,359 --> 03:36:38,160 with that you can reference this video 5013 03:36:35,460 --> 03:36:41,279 in JavaScript nuggets 5014 03:36:38,160 --> 03:36:44,160 and we're not going to do anything 5015 03:36:41,279 --> 03:36:46,800 basically we want to grab the books all 5016 03:36:44,160 --> 03:36:50,520 right we're going to find the book that 5017 03:36:46,800 --> 03:36:52,680 matches the ID that we're passing in and 5018 03:36:50,520 --> 03:36:55,200 we just want to log it we want to log 5019 03:36:52,680 --> 03:36:57,120 our result again we don't know how to 5020 03:36:55,200 --> 03:37:00,960 control the state yet 5021 03:36:57,120 --> 03:37:03,540 so don't worry about that part just set 5022 03:37:00,960 --> 03:37:07,140 up the function log the book 5023 03:37:03,540 --> 03:37:10,260 that matches the ID that you passed in 5024 03:37:07,140 --> 03:37:13,260 and then pass that function down to a 5025 03:37:10,260 --> 03:37:14,819 book component and try to invoke it on a 5026 03:37:13,260 --> 03:37:17,100 button click 5027 03:37:14,819 --> 03:37:18,180 so essentially just like in the last 5028 03:37:17,100 --> 03:37:20,100 video 5029 03:37:18,180 --> 03:37:22,859 pass the function down come up with 5030 03:37:20,100 --> 03:37:25,319 whatever prop to structure it grab the 5031 03:37:22,859 --> 03:37:26,640 ID and try to invoke it 5032 03:37:25,319 --> 03:37:30,120 and 5033 03:37:26,640 --> 03:37:32,100 most likely you'll hit a bug and that 5034 03:37:30,120 --> 03:37:34,500 essentially is the entire reason for 5035 03:37:32,100 --> 03:37:35,760 this video so first try to set it up 5036 03:37:34,500 --> 03:37:37,800 yourself 5037 03:37:35,760 --> 03:37:41,580 and the next video I'll show you my 5038 03:37:37,800 --> 03:37:45,540 solution for the get book function and 5039 03:37:41,580 --> 03:37:48,899 also two possible solutions for the bug 5040 03:37:45,540 --> 03:37:51,899 that you might encounter okay so let's 5041 03:37:48,899 --> 03:37:55,080 get cracking I'm going to navigate back 5042 03:37:51,899 --> 03:37:56,819 for now I have this display value I 5043 03:37:55,080 --> 03:37:58,439 still know that I'm going to use the 5044 03:37:56,819 --> 03:38:01,680 button so I'll just 5045 03:37:58,439 --> 03:38:03,540 remove the reference as well as display 5046 03:38:01,680 --> 03:38:06,899 value 5047 03:38:03,540 --> 03:38:10,140 the prop is also not going to be here 5048 03:38:06,899 --> 03:38:14,040 I don't need these values and let's 5049 03:38:10,140 --> 03:38:15,720 first come up with the function so const 5050 03:38:14,040 --> 03:38:18,300 get book 5051 03:38:15,720 --> 03:38:19,920 then it's going to be looking for the ID 5052 03:38:18,300 --> 03:38:22,140 and 5053 03:38:19,920 --> 03:38:23,520 as far as the logic I want to find a 5054 03:38:22,140 --> 03:38:26,760 book so I'm going to go with constant 5055 03:38:23,520 --> 03:38:29,279 book is equal to books dot find 5056 03:38:26,760 --> 03:38:32,880 so I'll use the find method and in here 5057 03:38:29,279 --> 03:38:35,220 we pass in the Callback function and 5058 03:38:32,880 --> 03:38:37,920 we need to come up with the parameter in 5059 03:38:35,220 --> 03:38:42,720 my case I'm going to go with bulk and I 5060 03:38:37,920 --> 03:38:46,439 wanna search in the array for the book 5061 03:38:42,720 --> 03:38:48,840 whose ID matches this one how can we do 5062 03:38:46,439 --> 03:38:52,080 that well we can go with book.id is 5063 03:38:48,840 --> 03:38:54,300 equal to the ID that we're passing in 5064 03:38:52,080 --> 03:38:56,399 and then like I said we're not going to 5065 03:38:54,300 --> 03:38:59,580 be controlling anything what we can see 5066 03:38:56,399 --> 03:39:00,600 on the screen but we do want to log the 5067 03:38:59,580 --> 03:39:02,819 book 5068 03:39:00,600 --> 03:39:04,800 again I'm going to get this error and 5069 03:39:02,819 --> 03:39:06,600 you know what it's probably annoying to 5070 03:39:04,800 --> 03:39:08,399 some people so I'll just remove it and 5071 03:39:06,600 --> 03:39:10,920 I'll set it up from the scratch so let's 5072 03:39:08,399 --> 03:39:13,560 save it yep everything works all that is 5073 03:39:10,920 --> 03:39:17,040 nice now we need to pass it down okay 5074 03:39:13,560 --> 03:39:21,239 same deal just go get book is equal to 5075 03:39:17,040 --> 03:39:22,920 our function get book awesome then this 5076 03:39:21,239 --> 03:39:24,960 time let's log it just so we can 5077 03:39:22,920 --> 03:39:28,800 definitely see that it's there so both 5078 03:39:24,960 --> 03:39:32,220 of them get this function get book all 5079 03:39:28,800 --> 03:39:34,979 right awesome so let's grab it get book 5080 03:39:32,220 --> 03:39:36,660 it's coming this one out for now we 5081 03:39:34,979 --> 03:39:39,000 already have the button so we simply 5082 03:39:36,660 --> 03:39:41,460 want to go on click 5083 03:39:39,000 --> 03:39:44,880 and here comes the gotcha 5084 03:39:41,460 --> 03:39:46,140 so how are we going to invoke it 5085 03:39:44,880 --> 03:39:48,899 I mean 5086 03:39:46,140 --> 03:39:52,080 you can try to do this way 5087 03:39:48,899 --> 03:39:53,640 so get book here as a reference click me 5088 03:39:52,080 --> 03:39:55,859 undefined 5089 03:39:53,640 --> 03:39:57,239 why it's undefined well because it's 5090 03:39:55,859 --> 03:39:58,439 looking for ID 5091 03:39:57,239 --> 03:40:00,899 right 5092 03:39:58,439 --> 03:40:03,060 okay let's pass in the array 5093 03:40:00,899 --> 03:40:05,580 let's go here with get book Let's invoke 5094 03:40:03,060 --> 03:40:08,220 it and let's pass it in 5095 03:40:05,580 --> 03:40:09,180 so technically what's missing here is 5096 03:40:08,220 --> 03:40:11,160 the ID 5097 03:40:09,180 --> 03:40:13,260 all right so I know it's there I know 5098 03:40:11,160 --> 03:40:14,340 it's in the props object so let me just 5099 03:40:13,260 --> 03:40:17,700 grab it 5100 03:40:14,340 --> 03:40:19,080 but now I have a different issue so I 5101 03:40:17,700 --> 03:40:20,640 have the logs 5102 03:40:19,080 --> 03:40:22,920 all this is nice 5103 03:40:20,640 --> 03:40:24,840 but it's not happening on a button click 5104 03:40:22,920 --> 03:40:26,580 essentially it happens when the 5105 03:40:24,840 --> 03:40:29,399 application loads why 5106 03:40:26,580 --> 03:40:31,260 because we have parentheses here 5107 03:40:29,399 --> 03:40:34,859 and in JavaScript 5108 03:40:31,260 --> 03:40:37,080 essentially if we have a logic like this 5109 03:40:34,859 --> 03:40:38,460 where we have a function and then we 5110 03:40:37,080 --> 03:40:40,500 invoke it 5111 03:40:38,460 --> 03:40:42,540 what happens well we run the logic right 5112 03:40:40,500 --> 03:40:44,160 so it's the same thing if I would go 5113 03:40:42,540 --> 03:40:46,200 over here and if I would say I don't 5114 03:40:44,160 --> 03:40:48,660 know two for example right so in here 5115 03:40:46,200 --> 03:40:49,680 I'll pass in some kind of ID I will get 5116 03:40:48,660 --> 03:40:51,600 that book 5117 03:40:49,680 --> 03:40:54,300 so this is correct but we need to keep 5118 03:40:51,600 --> 03:40:56,520 in mind that this runs instantly and the 5119 03:40:54,300 --> 03:40:58,920 same thing happens over here 5120 03:40:56,520 --> 03:41:01,140 we're not running this when we invoke 5121 03:40:58,920 --> 03:41:02,640 this is not a reference anymore and the 5122 03:41:01,140 --> 03:41:04,680 reason why I'm being so annoying and I 5123 03:41:02,640 --> 03:41:07,560 keep repeating this because again later 5124 03:41:04,680 --> 03:41:10,080 in the course as our applications get 5125 03:41:07,560 --> 03:41:11,460 more complex this seems to be tripping a 5126 03:41:10,080 --> 03:41:13,920 lot of people up 5127 03:41:11,460 --> 03:41:15,720 yes in JavaScript once we set up the 5128 03:41:13,920 --> 03:41:18,600 function if we don't have it as a 5129 03:41:15,720 --> 03:41:20,460 reference if we have parentheses 5130 03:41:18,600 --> 03:41:22,380 we'll invoke it right away 5131 03:41:20,460 --> 03:41:24,600 and that's not what we want to do in 5132 03:41:22,380 --> 03:41:27,899 this case in this case we don't want to 5133 03:41:24,600 --> 03:41:31,680 do that so in the following video I'll 5134 03:41:27,899 --> 03:41:35,399 show you two possible solutions okay so 5135 03:41:31,680 --> 03:41:39,300 we have tiny bug in the code and the 5136 03:41:35,399 --> 03:41:41,340 first option is to set up wrapper inside 5137 03:41:39,300 --> 03:41:44,340 of the child component 5138 03:41:41,340 --> 03:41:46,080 so again our main issue is that we are 5139 03:41:44,340 --> 03:41:48,239 invoking this 5140 03:41:46,080 --> 03:41:51,239 where we have the on click 5141 03:41:48,239 --> 03:41:53,700 so in that case it runs instantly what's 5142 03:41:51,239 --> 03:41:54,720 the solution well in the child we can 5143 03:41:53,700 --> 03:41:57,060 come up with 5144 03:41:54,720 --> 03:41:59,279 a different function in my case I'm 5145 03:41:57,060 --> 03:42:00,779 going to call this get single book 5146 03:41:59,279 --> 03:42:03,600 and 5147 03:42:00,779 --> 03:42:04,620 will invoke the get book inside of the 5148 03:42:03,600 --> 03:42:06,540 function 5149 03:42:04,620 --> 03:42:09,120 so now we can pass the get single book 5150 03:42:06,540 --> 03:42:11,700 and we're good to go you'll see that 5151 03:42:09,120 --> 03:42:15,300 everything is actually correct so let me 5152 03:42:11,700 --> 03:42:17,040 just I guess comment this one out 5153 03:42:15,300 --> 03:42:19,439 just in case you ever need it for your 5154 03:42:17,040 --> 03:42:22,680 reference so get book get book all that 5155 03:42:19,439 --> 03:42:25,380 is good but instead of invoking it we're 5156 03:42:22,680 --> 03:42:26,760 gonna go with const get single book 5157 03:42:25,380 --> 03:42:28,560 again 5158 03:42:26,760 --> 03:42:31,620 if you want to name this get my 5159 03:42:28,560 --> 03:42:34,500 groceries that's also going to work 5160 03:42:31,620 --> 03:42:38,340 the main point is that we want to invoke 5161 03:42:34,500 --> 03:42:40,500 get book and grab the ID again this will 5162 03:42:38,340 --> 03:42:43,380 get us the ID 5163 03:42:40,500 --> 03:42:45,720 that belongs to that component as we're 5164 03:42:43,380 --> 03:42:47,520 iterating over list again in the second 5165 03:42:45,720 --> 03:42:49,680 component it's not going to get the ID 5166 03:42:47,520 --> 03:42:52,800 from the first one that's the beauty of 5167 03:42:49,680 --> 03:42:56,520 react and instead of going with get book 5168 03:42:52,800 --> 03:42:58,140 we're gonna go with get single book and 5169 03:42:56,520 --> 03:43:01,500 what do you know now of course 5170 03:42:58,140 --> 03:43:05,340 everything is going to work so I'll 5171 03:43:01,500 --> 03:43:07,380 save and once I click check it out this 5172 03:43:05,340 --> 03:43:08,819 actually gets me the book with an idea 5173 03:43:07,380 --> 03:43:11,160 of one 5174 03:43:08,819 --> 03:43:13,800 and it references 5175 03:43:11,160 --> 03:43:15,540 the book that is inside of the component 5176 03:43:13,800 --> 03:43:17,399 which is just awesome and of course I 5177 03:43:15,540 --> 03:43:19,920 can keep clicking and I'm going to be 5178 03:43:17,399 --> 03:43:22,439 getting this book so let me I guess 5179 03:43:19,920 --> 03:43:24,540 clean out the console and if I'll click 5180 03:43:22,439 --> 03:43:27,120 on a second one hopefully you see where 5181 03:43:24,540 --> 03:43:28,800 I'm going with this and yes 5182 03:43:27,120 --> 03:43:30,899 eventually this is how we're going to 5183 03:43:28,800 --> 03:43:31,800 remove items from the list now the logic 5184 03:43:30,899 --> 03:43:33,359 is going to be a little bit different 5185 03:43:31,800 --> 03:43:34,560 but the idea is going to be the same 5186 03:43:33,359 --> 03:43:37,080 again 5187 03:43:34,560 --> 03:43:40,439 the main thing that I'm trying to 5188 03:43:37,080 --> 03:43:43,380 Showcase over here is that 5189 03:43:40,439 --> 03:43:46,260 we need to pass a reference we cannot 5190 03:43:43,380 --> 03:43:48,600 invoke the function right away and as 5191 03:43:46,260 --> 03:43:51,779 far as the second option 5192 03:43:48,600 --> 03:43:54,899 it's a little bit more challenging I 5193 03:43:51,779 --> 03:43:58,020 guess as far as the concept where 5194 03:43:54,899 --> 03:44:01,620 I can pass the anonymous function 5195 03:43:58,020 --> 03:44:04,020 so think of it this way the general idea 5196 03:44:01,620 --> 03:44:07,140 doesn't change I still pass the 5197 03:44:04,020 --> 03:44:09,420 reference I'm just not naming it here 5198 03:44:07,140 --> 03:44:13,739 I pass in the anatoms function and then 5199 03:44:09,420 --> 03:44:15,239 I invoke it so let's go back to index 5200 03:44:13,739 --> 03:44:18,000 and 5201 03:44:15,239 --> 03:44:20,279 instead of get single book which is a 5202 03:44:18,000 --> 03:44:22,380 valid approach again this really comes 5203 03:44:20,279 --> 03:44:23,520 down to your preference the reason why 5204 03:44:22,380 --> 03:44:24,600 I'm showing you both of them because 5205 03:44:23,520 --> 03:44:26,220 again 5206 03:44:24,600 --> 03:44:27,600 as you're going to be looking at someone 5207 03:44:26,220 --> 03:44:30,180 else's project 5208 03:44:27,600 --> 03:44:32,700 you most likely see 5209 03:44:30,180 --> 03:44:34,979 one of those approaches so I just want 5210 03:44:32,700 --> 03:44:37,560 you to be aware of them where you can go 5211 03:44:34,979 --> 03:44:41,640 to get book and then pass the ID same 5212 03:44:37,560 --> 03:44:44,100 deal here is a reference and inside of 5213 03:44:41,640 --> 03:44:46,920 the function that's when we invoke the 5214 03:44:44,100 --> 03:44:51,120 book and check it out once I click again 5215 03:44:46,920 --> 03:44:53,479 I get the correct value in the console 5216 03:44:51,120 --> 03:44:55,640 all right and up next let's talk about 5217 03:44:53,479 --> 03:44:58,979 es6 modules 5218 03:44:55,640 --> 03:45:03,120 or a way for us to split up our 5219 03:44:58,979 --> 03:45:04,500 application into multiple files and of 5220 03:45:03,120 --> 03:45:08,100 course folders 5221 03:45:04,500 --> 03:45:09,899 and have the ability to Import and 5222 03:45:08,100 --> 03:45:11,939 Export the code 5223 03:45:09,899 --> 03:45:15,000 which essentially 5224 03:45:11,939 --> 03:45:16,979 helps us to organize our application 5225 03:45:15,000 --> 03:45:19,500 because if we take a look at the 5226 03:45:16,979 --> 03:45:22,920 index.js I mean everything is awesome 5227 03:45:19,500 --> 03:45:25,319 we have the list we iterate over we have 5228 03:45:22,920 --> 03:45:27,960 the book component and all of that is 5229 03:45:25,319 --> 03:45:31,020 great but you can probably imagine that 5230 03:45:27,960 --> 03:45:33,479 as our application grows this is not 5231 03:45:31,020 --> 03:45:35,819 sustainable I mean we need to have some 5232 03:45:33,479 --> 03:45:38,160 kind of way where we can split up the 5233 03:45:35,819 --> 03:45:39,300 code correct where we have separate 5234 03:45:38,160 --> 03:45:42,600 functionalities 5235 03:45:39,300 --> 03:45:45,120 in different places and then if we need 5236 03:45:42,600 --> 03:45:45,899 that if we need the component or data or 5237 03:45:45,120 --> 03:45:48,840 whatever 5238 03:45:45,899 --> 03:45:51,899 we just access it and the way we do that 5239 03:45:48,840 --> 03:45:54,720 is by using es6 modules which again are 5240 03:45:51,899 --> 03:45:57,540 not unique to react react just uses them 5241 03:45:54,720 --> 03:46:00,120 and if you have ever used node before in 5242 03:45:57,540 --> 03:46:03,000 the previous versions of node they used 5243 03:46:00,120 --> 03:46:06,180 to use only common JS now they also use 5244 03:46:03,000 --> 03:46:08,580 es6 modules and again it's the same 5245 03:46:06,180 --> 03:46:10,920 principle just the syntax is a little 5246 03:46:08,580 --> 03:46:13,800 bit different and you'll see it's 5247 03:46:10,920 --> 03:46:16,140 extremely straightforward so first I 5248 03:46:13,800 --> 03:46:17,520 just want to remove all the get book 5249 03:46:16,140 --> 03:46:20,520 logic as always 5250 03:46:17,520 --> 03:46:22,020 it's available in the readme if you ever 5251 03:46:20,520 --> 03:46:24,960 need a reference 5252 03:46:22,020 --> 03:46:27,600 so I don't need to get book and I'm 5253 03:46:24,960 --> 03:46:30,180 pretty sure I'm not gonna need the ID my 5254 03:46:27,600 --> 03:46:31,140 apologies if I will and if I'll have to 5255 03:46:30,180 --> 03:46:33,540 rewrite 5256 03:46:31,140 --> 03:46:35,700 hopefully you can forgive me and I don't 5257 03:46:33,540 --> 03:46:36,899 need the button okay good let's remove 5258 03:46:35,700 --> 03:46:38,279 that 5259 03:46:36,899 --> 03:46:40,620 and now 5260 03:46:38,279 --> 03:46:44,220 essentially I want to come up with a 5261 03:46:40,620 --> 03:46:46,979 file name we have two flavors when it 5262 03:46:44,220 --> 03:46:50,640 comes to imports and exports we have a 5263 03:46:46,979 --> 03:46:53,580 named one and we have a default one 5264 03:46:50,640 --> 03:46:57,060 as always you can use whichever you 5265 03:46:53,580 --> 03:46:59,580 prefer in most cases what you'll see if 5266 03:46:57,060 --> 03:47:02,399 you have let's say a one component in 5267 03:46:59,580 --> 03:47:04,500 the file you'll do that as a default one 5268 03:47:02,399 --> 03:47:07,140 and if you have multiples they will be 5269 03:47:04,500 --> 03:47:09,180 named now is it the only way how you can 5270 03:47:07,140 --> 03:47:09,960 do that no but if I'll show you pretty 5271 03:47:09,180 --> 03:47:12,779 much 5272 03:47:09,960 --> 03:47:14,520 every flavor of Import and Export this 5273 03:47:12,779 --> 03:47:17,279 video is going to be two hours long so 5274 03:47:14,520 --> 03:47:19,439 we'll focus on two main ones name the 5275 03:47:17,279 --> 03:47:21,180 export and default export and the only 5276 03:47:19,439 --> 03:47:24,120 thing we need to understand 5277 03:47:21,180 --> 03:47:26,760 is that when we import with the named 5278 03:47:24,120 --> 03:47:28,979 one we need to use the same name so name 5279 03:47:26,760 --> 03:47:30,779 must must must must must must be the 5280 03:47:28,979 --> 03:47:32,460 same and with the default one we can 5281 03:47:30,779 --> 03:47:35,700 come up with whatever value we want 5282 03:47:32,460 --> 03:47:38,399 literally that's the main difference 5283 03:47:35,700 --> 03:47:41,939 so first let's go to source and let's 5284 03:47:38,399 --> 03:47:43,979 create box.js and book JS so this is 5285 03:47:41,939 --> 03:47:46,800 going to be for our component and this 5286 03:47:43,979 --> 03:47:48,960 is going to be for our data and then I 5287 03:47:46,800 --> 03:47:53,340 want to remove books array from index.js 5288 03:47:48,960 --> 03:47:57,540 and I want to add it to books j s 5289 03:47:53,340 --> 03:48:00,120 so let's try this one out new file I'm 5290 03:47:57,540 --> 03:48:03,479 going to go with books JS that's where 5291 03:48:00,120 --> 03:48:06,180 I'll have my data and yes of course I 5292 03:48:03,479 --> 03:48:09,180 can name this data as well if I want and 5293 03:48:06,180 --> 03:48:10,800 I'm going to go with book JS let's start 5294 03:48:09,180 --> 03:48:14,640 with the books array 5295 03:48:10,800 --> 03:48:17,460 so let's get the data away from the 5296 03:48:14,640 --> 03:48:19,319 index so let's clean up this file so I 5297 03:48:17,460 --> 03:48:21,660 want to cut it out and I want to place 5298 03:48:19,319 --> 03:48:24,540 it in the box of course the moment I 5299 03:48:21,660 --> 03:48:27,060 save I'll have big fat error why 5300 03:48:24,540 --> 03:48:30,120 because books is not defined 5301 03:48:27,060 --> 03:48:32,640 of course I'm accessing it over here but 5302 03:48:30,120 --> 03:48:35,160 there is no books anymore 5303 03:48:32,640 --> 03:48:37,979 so in here we have this array and like I 5304 03:48:35,160 --> 03:48:40,020 said we have two flavors and essentially 5305 03:48:37,979 --> 03:48:41,939 with books I'll show you both and then 5306 03:48:40,020 --> 03:48:44,040 with the book one we'll just use the 5307 03:48:41,939 --> 03:48:46,620 default one so in here I can go with 5308 03:48:44,040 --> 03:48:49,620 export notice I simply want to go with 5309 03:48:46,620 --> 03:48:52,979 export and then whatever 5310 03:48:49,620 --> 03:48:54,720 variable I'm exporting or function or 5311 03:48:52,979 --> 03:48:58,439 hopefully you see where I'm going with 5312 03:48:54,720 --> 03:49:00,600 this now again there are a few different 5313 03:48:58,439 --> 03:49:03,660 flavors to this but we'll just stick 5314 03:49:00,600 --> 03:49:06,779 with the most common ones so we'll save 5315 03:49:03,660 --> 03:49:09,540 over here and then back in the index we 5316 03:49:06,779 --> 03:49:11,760 need to repeat these steps however this 5317 03:49:09,540 --> 03:49:14,279 is our own file so this is where that 5318 03:49:11,760 --> 03:49:16,859 path comes into play when we go to 5319 03:49:14,279 --> 03:49:19,200 import and this is extremely extremely 5320 03:49:16,859 --> 03:49:20,880 important we need to go with curlish 5321 03:49:19,200 --> 03:49:24,359 which means that 5322 03:49:20,880 --> 03:49:27,840 we'll be getting a specific item meaning 5323 03:49:24,359 --> 03:49:30,960 the item you're exporting is exported as 5324 03:49:27,840 --> 03:49:33,660 named export so this needs to match 5325 03:49:30,960 --> 03:49:35,939 exactly what I have in the file learn 5326 03:49:33,660 --> 03:49:39,720 from and what do you know I'm looking 5327 03:49:35,939 --> 03:49:42,600 for the books and remember I told you 5328 03:49:39,720 --> 03:49:44,580 when we covered CSS JavaScript files we 5329 03:49:42,600 --> 03:49:47,160 don't need to do that and the moment we 5330 03:49:44,580 --> 03:49:49,680 save check it out everything works now 5331 03:49:47,160 --> 03:49:53,880 if I'm gonna go here with carrot banana 5332 03:49:49,680 --> 03:49:57,120 or whatever Bob it's not going to work 5333 03:49:53,880 --> 03:49:59,460 so this is exported as books so anywhere 5334 03:49:57,120 --> 03:50:02,460 anywhere in your application you need to 5335 03:49:59,460 --> 03:50:04,080 access books that's a must you cannot 5336 03:50:02,460 --> 03:50:07,319 just willingly come up with a different 5337 03:50:04,080 --> 03:50:10,680 name so that's the first option we have 5338 03:50:07,319 --> 03:50:12,120 let me go here with books and like I 5339 03:50:10,680 --> 03:50:14,279 said I'll show you right away both of 5340 03:50:12,120 --> 03:50:16,739 them but we'll stick with the named one 5341 03:50:14,279 --> 03:50:19,560 now alternatively here's what we can do 5342 03:50:16,739 --> 03:50:23,279 I can remove this export for now 5343 03:50:19,560 --> 03:50:25,680 and I could say export default 5344 03:50:23,279 --> 03:50:27,960 and then of course I need to reference 5345 03:50:25,680 --> 03:50:29,580 the books 5346 03:50:27,960 --> 03:50:32,760 one thing we need to keep in mind that 5347 03:50:29,580 --> 03:50:34,500 we can only have one default export per 5348 03:50:32,760 --> 03:50:37,080 file 5349 03:50:34,500 --> 03:50:39,720 so you cannot have like 10 default 5350 03:50:37,080 --> 03:50:41,939 exports that's the whole point that you 5351 03:50:39,720 --> 03:50:45,840 have one default export and the biggest 5352 03:50:41,939 --> 03:50:48,120 difference is that in the index.js we 5353 03:50:45,840 --> 03:50:50,220 don't have any more this syntax we 5354 03:50:48,120 --> 03:50:51,840 simply need to go with import and pretty 5355 03:50:50,220 --> 03:50:55,399 much we're saying we're importing 5356 03:50:51,840 --> 03:50:58,200 anything and this could be banana 5357 03:50:55,399 --> 03:51:00,660 and I just need to change it here of 5358 03:50:58,200 --> 03:51:04,080 course right so I need to go here with 5359 03:51:00,660 --> 03:51:06,239 banana and of course everything works so 5360 03:51:04,080 --> 03:51:07,380 hopefully this gives you a good idea 5361 03:51:06,239 --> 03:51:09,779 again 5362 03:51:07,380 --> 03:51:12,120 named and default and the biggest 5363 03:51:09,779 --> 03:51:13,260 difference is that with default you 5364 03:51:12,120 --> 03:51:16,560 actually can 5365 03:51:13,260 --> 03:51:18,180 name it however we want in the index.js 5366 03:51:16,560 --> 03:51:20,939 this is something that we talked about 5367 03:51:18,180 --> 03:51:23,040 it before where you can technically set 5368 03:51:20,939 --> 03:51:25,200 up a component with lowercase and Export 5369 03:51:23,040 --> 03:51:27,300 it but when you import you will still 5370 03:51:25,200 --> 03:51:29,880 have to use the application so first let 5371 03:51:27,300 --> 03:51:33,000 me just go back to the name one 5372 03:51:29,880 --> 03:51:34,680 export we use like I said the default 5373 03:51:33,000 --> 03:51:36,660 one 5374 03:51:34,680 --> 03:51:39,479 in the book component 5375 03:51:36,660 --> 03:51:42,239 let's remove this and then in the 5376 03:51:39,479 --> 03:51:44,160 index.js that's when we want to go back 5377 03:51:42,239 --> 03:51:45,840 to the books 5378 03:51:44,160 --> 03:51:49,319 little detour 5379 03:51:45,840 --> 03:51:51,000 my apologies and now let's go with the 5380 03:51:49,319 --> 03:51:52,680 component the reason why I showed you 5381 03:51:51,000 --> 03:51:56,279 both I don't want you to think that you 5382 03:51:52,680 --> 03:52:00,060 can only do default with a component 5383 03:51:56,279 --> 03:52:03,300 you can do with any data so up next 5384 03:52:00,060 --> 03:52:04,800 let's take this sucker out let's remove 5385 03:52:03,300 --> 03:52:07,680 and 5386 03:52:04,800 --> 03:52:09,000 I have book JS copy and paste and 5387 03:52:07,680 --> 03:52:10,439 remember the extension that I used 5388 03:52:09,000 --> 03:52:12,180 before 5389 03:52:10,439 --> 03:52:13,920 so 5390 03:52:12,180 --> 03:52:15,720 the Snippets one 5391 03:52:13,920 --> 03:52:17,939 check it out I can of course create the 5392 03:52:15,720 --> 03:52:20,160 component this way as well and notice 5393 03:52:17,939 --> 03:52:22,800 how I right away have that export 5394 03:52:20,160 --> 03:52:25,859 default book so now you know what that 5395 03:52:22,800 --> 03:52:27,960 line of code is doing so let me copy and 5396 03:52:25,859 --> 03:52:29,540 paste again and we're going to go with 5397 03:52:27,960 --> 03:52:32,580 export 5398 03:52:29,540 --> 03:52:36,479 export default 5399 03:52:32,580 --> 03:52:39,300 and we're looking for the book component 5400 03:52:36,479 --> 03:52:42,300 then we're going to navigate back 5401 03:52:39,300 --> 03:52:45,359 and we want to go with import 5402 03:52:42,300 --> 03:52:48,500 and book from the book 5403 03:52:45,359 --> 03:52:52,680 also I want to right away mention that 5404 03:52:48,500 --> 03:52:54,960 technically vs code tries to provide the 5405 03:52:52,680 --> 03:52:57,479 Auto Import and when I say technically 5406 03:52:54,960 --> 03:52:58,979 because I mean unfortunately sometimes 5407 03:52:57,479 --> 03:53:03,479 it doesn't work but don't be surprised 5408 03:52:58,979 --> 03:53:05,340 if for example you see me or some other 5409 03:53:03,479 --> 03:53:08,100 instructor doing something like this 5410 03:53:05,340 --> 03:53:10,500 where I can go with book and then notice 5411 03:53:08,100 --> 03:53:12,779 out right away sets up that component 5412 03:53:10,500 --> 03:53:14,760 now of course I'm not passing anything 5413 03:53:12,779 --> 03:53:18,479 in here and it's looking for those props 5414 03:53:14,760 --> 03:53:22,260 and all that but hopefully you see that 5415 03:53:18,479 --> 03:53:24,779 yes I can type the component and visual 5416 03:53:22,260 --> 03:53:27,239 studio code is going to try to help me 5417 03:53:24,779 --> 03:53:31,319 and the reason why I say tries to help 5418 03:53:27,239 --> 03:53:34,200 you because here and there it just fails 5419 03:53:31,319 --> 03:53:36,120 so let me remove this one 5420 03:53:34,200 --> 03:53:36,899 the import is in place and as you can 5421 03:53:36,120 --> 03:53:39,300 see 5422 03:53:36,899 --> 03:53:42,420 the logic still works 5423 03:53:39,300 --> 03:53:45,420 I still have my list and everything is 5424 03:53:42,420 --> 03:53:49,200 correct the difference right now is that 5425 03:53:45,420 --> 03:53:51,660 I've nicely split up my code where the 5426 03:53:49,200 --> 03:53:54,420 component is in one place so if I ever 5427 03:53:51,660 --> 03:53:57,120 need to come back I can just navigate to 5428 03:53:54,420 --> 03:53:58,979 a book.js this is all the logic for this 5429 03:53:57,120 --> 03:54:02,399 one and you already know that we can add 5430 03:53:58,979 --> 03:54:04,500 code remove code and right away all the 5431 03:54:02,399 --> 03:54:06,899 components are going to be affected and 5432 03:54:04,500 --> 03:54:09,359 the same goes with book shares in few 5433 03:54:06,899 --> 03:54:12,720 videos we will cover 5434 03:54:09,359 --> 03:54:15,239 how to set up local images in the source 5435 03:54:12,720 --> 03:54:17,520 folder which essentially 5436 03:54:15,239 --> 03:54:19,680 is a better approach because 5437 03:54:17,520 --> 03:54:22,380 there's some optimizations 5438 03:54:19,680 --> 03:54:26,040 happening under the hood so the result 5439 03:54:22,380 --> 03:54:28,500 is better and yes all the work we're 5440 03:54:26,040 --> 03:54:31,979 only going to do in books.js 5441 03:54:28,500 --> 03:54:34,319 so we won't have to run back to index.js 5442 03:54:31,979 --> 03:54:37,800 as long as the import is correct 5443 03:54:34,319 --> 03:54:40,080 we're good to go again these are es6 5444 03:54:37,800 --> 03:54:42,359 modules they're not specific to react 5445 03:54:40,080 --> 03:54:43,739 you can use them in vanilla.js you can 5446 03:54:42,359 --> 03:54:48,060 use them in node 5447 03:54:43,739 --> 03:54:50,640 and essentially they help us to organize 5448 03:54:48,060 --> 03:54:52,500 our application all right and now let's 5449 03:54:50,640 --> 03:54:55,560 talk about how we can set up local 5450 03:54:52,500 --> 03:54:58,140 images in the source folder which 5451 03:54:55,560 --> 03:55:00,180 essentially is a better option for 5452 03:54:58,140 --> 03:55:02,699 setting up the assets because like I 5453 03:55:00,180 --> 03:55:05,880 keep saying when the react builds our 5454 03:55:02,699 --> 03:55:08,160 production application effectively it 5455 03:55:05,880 --> 03:55:11,340 optimizes those assets 5456 03:55:08,160 --> 03:55:13,439 so unlike the public one where there's 5457 03:55:11,340 --> 03:55:17,220 no optimization we just dump the asset 5458 03:55:13,439 --> 03:55:20,120 and that's it in this case react is 5459 03:55:17,220 --> 03:55:22,319 trying to help us and essentially by 5460 03:55:20,120 --> 03:55:25,439 optimizing those assets 5461 03:55:22,319 --> 03:55:27,300 the result is that our application is 5462 03:55:25,439 --> 03:55:30,239 going to be faster 5463 03:55:27,300 --> 03:55:33,319 and in order to Showcase how that works 5464 03:55:30,239 --> 03:55:36,779 I decided to add one more book 5465 03:55:33,319 --> 03:55:39,540 to our array and essentially we're just 5466 03:55:36,779 --> 03:55:43,140 going to utilize the es6 modules we 5467 03:55:39,540 --> 03:55:46,439 covered already in the previous video 5468 03:55:43,140 --> 03:55:50,399 so first I want you to go back 5469 03:55:46,439 --> 03:55:52,560 to Amazon so find that link or get your 5470 03:55:50,399 --> 03:55:56,220 own images it doesn't really matter 5471 03:55:52,560 --> 03:55:57,420 and just get for the book one and two 5472 03:55:56,220 --> 03:55:59,660 so 5473 03:55:57,420 --> 03:56:03,120 my guys have these two 5474 03:55:59,660 --> 03:56:04,560 so I'm going to go and save images I 5475 03:56:03,120 --> 03:56:07,380 think I'm going to place it on desktop 5476 03:56:04,560 --> 03:56:11,540 and I'm just going to go with book one 5477 03:56:07,380 --> 03:56:11,540 then let me find the second one 5478 03:56:11,699 --> 03:56:16,260 I'll do the same thing 5479 03:56:14,220 --> 03:56:18,300 and now I just need to come up with the 5480 03:56:16,260 --> 03:56:20,520 third one 5481 03:56:18,300 --> 03:56:22,500 this is probably going to be 5482 03:56:20,520 --> 03:56:24,540 challenge but I'm gonna go with this one 5483 03:56:22,500 --> 03:56:27,120 bad jokes why not 5484 03:56:24,540 --> 03:56:28,140 so let's go here and I'm gonna go with 5485 03:56:27,120 --> 03:56:31,560 book 5486 03:56:28,140 --> 03:56:34,739 three and before we navigate to a 5487 03:56:31,560 --> 03:56:36,000 desktop let me also get the data for 5488 03:56:34,739 --> 03:56:38,399 this book 5489 03:56:36,000 --> 03:56:40,560 so let me open this one up 5490 03:56:38,399 --> 03:56:44,279 and basically this is going to be the 5491 03:56:40,560 --> 03:56:46,500 title and this is going to be an author 5492 03:56:44,279 --> 03:56:48,840 so let's navigate to the box and like I 5493 03:56:46,500 --> 03:56:51,840 said we want to add the third one 5494 03:56:48,840 --> 03:56:54,239 so let me copy and paste and let's just 5495 03:56:51,840 --> 03:56:57,479 change the values around so these ones 5496 03:56:54,239 --> 03:56:59,100 I'm going to get from the Amazon 5497 03:56:57,479 --> 03:57:00,960 for now I just want to change the idea 5498 03:56:59,100 --> 03:57:02,520 which is going to be three 5499 03:57:00,960 --> 03:57:03,960 then we wanna 5500 03:57:02,520 --> 03:57:06,060 get the 5501 03:57:03,960 --> 03:57:08,279 title and I think I'm just going to go 5502 03:57:06,060 --> 03:57:09,540 with the short one here 5503 03:57:08,279 --> 03:57:12,000 title 5504 03:57:09,540 --> 03:57:13,920 and let's get 5505 03:57:12,000 --> 03:57:16,979 the author 5506 03:57:13,920 --> 03:57:19,199 UPS keeps running away from me 5507 03:57:16,979 --> 03:57:21,420 let me set it up over here 5508 03:57:19,199 --> 03:57:23,939 and now let's deal with those images 5509 03:57:21,420 --> 03:57:24,899 so I have these three images on my 5510 03:57:23,939 --> 03:57:29,040 desktop 5511 03:57:24,899 --> 03:57:31,260 the folder structure is definitely up to 5512 03:57:29,040 --> 03:57:34,380 you for the bigger project you'll mostly 5513 03:57:31,260 --> 03:57:37,020 have like assets and then images folder 5514 03:57:34,380 --> 03:57:38,760 the CSS folder and hopefully you see 5515 03:57:37,020 --> 03:57:41,399 where I'm going with this for now I'll 5516 03:57:38,760 --> 03:57:43,739 just create a images and place it in the 5517 03:57:41,399 --> 03:57:46,439 source but it's a must it has to be in 5518 03:57:43,739 --> 03:57:48,479 the source so even if the structure is 5519 03:57:46,439 --> 03:57:50,760 more nested make sure that the parent 5520 03:57:48,479 --> 03:57:52,380 the main directory is still within the 5521 03:57:50,760 --> 03:57:54,120 source please don't place it in the 5522 03:57:52,380 --> 03:57:56,279 public it's not going to work 5523 03:57:54,120 --> 03:57:58,680 so let's grab 5524 03:57:56,279 --> 03:58:01,620 the folder and then we just want to move 5525 03:57:58,680 --> 03:58:03,720 all of these images over here once 5526 03:58:01,620 --> 03:58:05,819 they're there let's move them to the 5527 03:58:03,720 --> 03:58:08,640 source like I just said 5528 03:58:05,819 --> 03:58:11,160 and here's the deal we simply want to 5529 03:58:08,640 --> 03:58:13,260 import those images one by one and yes 5530 03:58:11,160 --> 03:58:15,479 this is a downside if you have 100 5531 03:58:13,260 --> 03:58:17,220 images you'll have to do 5532 03:58:15,479 --> 03:58:21,060 this one by one 5533 03:58:17,220 --> 03:58:23,580 for example if you have 5534 03:58:21,060 --> 03:58:25,620 images in the public then of course you 5535 03:58:23,580 --> 03:58:28,560 can just reference it as dot forward 5536 03:58:25,620 --> 03:58:30,420 slash and then whatever is the path 5537 03:58:28,560 --> 03:58:32,220 so keep in mind yes 5538 03:58:30,420 --> 03:58:33,960 it is a little bit more annoying 5539 03:58:32,220 --> 03:58:36,479 technically because we need to do this 5540 03:58:33,960 --> 03:58:38,580 one by one but the results are going to 5541 03:58:36,479 --> 03:58:41,340 be better and essentially we want to 5542 03:58:38,580 --> 03:58:43,500 import we want to come up with some kind 5543 03:58:41,340 --> 03:58:46,380 of name and we need to look for images 5544 03:58:43,500 --> 03:58:49,620 and then the file name and notice again 5545 03:58:46,380 --> 03:58:52,800 full path basically extension included 5546 03:58:49,620 --> 03:58:55,800 and then notice over here instead of the 5547 03:58:52,800 --> 03:58:59,340 local one or the external one for the 5548 03:58:55,800 --> 03:59:02,640 image what do we do we simply pass in 5549 03:58:59,340 --> 03:59:06,060 that variable so one two and three in my 5550 03:59:02,640 --> 03:59:08,100 case so let me save this I'm going to go 5551 03:59:06,060 --> 03:59:10,739 to books again like I said we're not 5552 03:59:08,100 --> 03:59:13,800 working anymore in index.js we only need 5553 03:59:10,739 --> 03:59:18,060 to focus on box.js and one by one let's 5554 03:59:13,800 --> 03:59:21,720 grab them let's go here and say import 5555 03:59:18,060 --> 03:59:25,979 and IMG one from and now we're looking 5556 03:59:21,720 --> 03:59:29,040 in the images and then book I believe it 5557 03:59:25,979 --> 03:59:30,899 was a one and this was the extension so 5558 03:59:29,040 --> 03:59:33,180 now we want to copy and paste and then I 5559 03:59:30,899 --> 03:59:34,680 want to set up multiple cursors and 5560 03:59:33,180 --> 03:59:37,319 let's just change these values so this 5561 03:59:34,680 --> 03:59:39,359 is going to be book two and book three 5562 03:59:37,319 --> 03:59:45,140 and like I said one by one 5563 03:59:39,359 --> 03:59:48,779 or just change it we'll say IMG one 5564 03:59:45,140 --> 03:59:51,660 img2 and img3 5565 03:59:48,779 --> 03:59:56,580 img2 5566 03:59:51,660 --> 04:00:00,779 and last one is going to be img3 5567 03:59:56,580 --> 04:00:02,100 once we save we should have a list 5568 04:00:00,779 --> 04:00:03,660 of 5569 04:00:02,100 --> 04:00:05,760 three books 5570 04:00:03,660 --> 04:00:09,420 and as you can see 5571 04:00:05,760 --> 04:00:12,180 images work as expected again the main 5572 04:00:09,420 --> 04:00:14,399 benefit of such approach not just for 5573 04:00:12,180 --> 04:00:17,640 images again we're covering images keep 5574 04:00:14,399 --> 04:00:21,840 in mind it goes for all the assets is 5575 04:00:17,640 --> 04:00:24,120 that they get optimized when react or 5576 04:00:21,840 --> 04:00:26,100 more specifically create react app they 5577 04:00:24,120 --> 04:00:29,460 told that we're using build out our 5578 04:00:26,100 --> 04:00:32,580 production ready application and as a 5579 04:00:29,460 --> 04:00:35,580 result our applications are faster okay 5580 04:00:32,580 --> 04:00:38,120 and before we set up a production or the 5581 04:00:35,580 --> 04:00:40,859 application and deploy it online so 5582 04:00:38,120 --> 04:00:42,779 everyone can see our awesome project 5583 04:00:40,859 --> 04:00:45,300 let's work on two 5584 04:00:42,779 --> 04:00:47,340 challenges and the first one is 5585 04:00:45,300 --> 04:00:50,819 following I want to set up 5586 04:00:47,340 --> 04:00:54,180 numbers just like they have in Amazon 5587 04:00:50,819 --> 04:00:57,540 now please don't worry about the CSS 5588 04:00:54,180 --> 04:00:58,859 the main goal is to figure out how we 5589 04:00:57,540 --> 04:01:01,260 can get 5590 04:00:58,859 --> 04:01:02,880 the correct number 5591 04:01:01,260 --> 04:01:06,239 for the book 5592 04:01:02,880 --> 04:01:08,880 so if I have a list 5593 04:01:06,239 --> 04:01:10,560 each value represents whether it's the 5594 04:01:08,880 --> 04:01:12,899 first one second one and hopefully you 5595 04:01:10,560 --> 04:01:15,239 see where I'm going with this and I just 5596 04:01:12,899 --> 04:01:17,220 want to display those numbers again I'll 5597 04:01:15,239 --> 04:01:19,979 show you the CSS it's actually very 5598 04:01:17,220 --> 04:01:21,300 simple and all that but for now just 5599 04:01:19,979 --> 04:01:23,399 worry about getting that number 5600 04:01:21,300 --> 04:01:26,340 somewhere here in a component 5601 04:01:23,399 --> 04:01:29,819 and also we'll set up the title that's 5602 04:01:26,340 --> 04:01:32,460 going to be our Second Challenge and for 5603 04:01:29,819 --> 04:01:33,899 both of them pretty much 5604 04:01:32,460 --> 04:01:36,300 if you want to work on that 5605 04:01:33,899 --> 04:01:39,000 independently once I'm done explaining 5606 04:01:36,300 --> 04:01:41,279 the challenge just stop the video try to 5607 04:01:39,000 --> 04:01:43,199 accomplish it yourself and once you're 5608 04:01:41,279 --> 04:01:46,199 ready to compare the results just resume 5609 04:01:43,199 --> 04:01:49,380 the video now you don't have to you can 5610 04:01:46,199 --> 04:01:50,939 simply just watch and code along with me 5611 04:01:49,380 --> 04:01:53,640 and basically like I said I want to set 5612 04:01:50,939 --> 04:01:54,840 up those numbers so I can see 5613 04:01:53,640 --> 04:01:57,060 which 5614 04:01:54,840 --> 04:01:58,800 item this is in the list whether it's 5615 04:01:57,060 --> 04:02:01,680 the first one second one 5616 04:01:58,800 --> 04:02:04,920 or third one don't worry about the CSS 5617 04:02:01,680 --> 04:02:06,720 and I'll give you a tiny tiny hint I'm 5618 04:02:04,920 --> 04:02:09,600 going to use index 5619 04:02:06,720 --> 04:02:14,460 for this again I'm not using index for 5620 04:02:09,600 --> 04:02:16,979 the key I'm using it to get the value 5621 04:02:14,460 --> 04:02:19,920 so I can clearly display whether it's 5622 04:02:16,979 --> 04:02:21,300 the first book second or third again if 5623 04:02:19,920 --> 04:02:24,000 you want to work on this independently 5624 04:02:21,300 --> 04:02:26,340 please stop the video right now and 5625 04:02:24,000 --> 04:02:27,420 resume once you're ready to compare the 5626 04:02:26,340 --> 04:02:31,020 results 5627 04:02:27,420 --> 04:02:34,739 I'm going to go to index.js and then the 5628 04:02:31,020 --> 04:02:37,199 second parameter in map method is the 5629 04:02:34,739 --> 04:02:39,180 index so 5630 04:02:37,199 --> 04:02:42,180 we can reference it here I'm going to go 5631 04:02:39,180 --> 04:02:44,279 with index and I'm going to come up with 5632 04:02:42,180 --> 04:02:46,319 a different prop name 5633 04:02:44,279 --> 04:02:48,720 of course I can go with index index but 5634 04:02:46,319 --> 04:02:52,680 I'm just going to go with number and 5635 04:02:48,720 --> 04:02:56,819 then index all right good then let's go 5636 04:02:52,680 --> 04:02:59,220 to a book JS so now we Navigate to a 5637 04:02:56,819 --> 04:03:01,020 book component and we want to grab that 5638 04:02:59,220 --> 04:03:03,140 number correct we're going to go here 5639 04:03:01,020 --> 04:03:06,960 and remember 5640 04:03:03,140 --> 04:03:09,540 arrays in JavaScript are zero indexed 5641 04:03:06,960 --> 04:03:11,640 paste what does that mean that means 5642 04:03:09,540 --> 04:03:12,779 that if I'm gonna go here with log and 5643 04:03:11,640 --> 04:03:14,580 number 5644 04:03:12,779 --> 04:03:19,080 I'll nicely see 5645 04:03:14,580 --> 04:03:22,020 zero one and two which is awesome but it 5646 04:03:19,080 --> 04:03:24,060 doesn't really match exactly correct so 5647 04:03:22,020 --> 04:03:26,899 it should be one two three what's the 5648 04:03:24,060 --> 04:03:29,399 solution well we can just add plus one 5649 04:03:26,899 --> 04:03:32,460 and in my case I'm going to place that 5650 04:03:29,399 --> 04:03:36,060 in a span so right after the author I'm 5651 04:03:32,460 --> 04:03:38,040 going to go with span allari class just 5652 04:03:36,060 --> 04:03:42,000 because I'll add the styles 5653 04:03:38,040 --> 04:03:44,520 in a second and then we can go here like 5654 04:03:42,000 --> 04:03:48,300 this number 5655 04:03:44,520 --> 04:03:50,220 plus one which effectively will get us 5656 04:03:48,300 --> 04:03:51,899 the right number and the reason why you 5657 04:03:50,220 --> 04:03:53,699 see everything displayed because of 5658 04:03:51,899 --> 04:03:57,180 course I already added the CSS and my 5659 04:03:53,699 --> 04:04:00,420 apologies let me just remove this one 5660 04:03:57,180 --> 04:04:02,939 for now I was already working on that 5661 04:04:00,420 --> 04:04:04,920 we'll set that one up in a second if you 5662 04:04:02,939 --> 04:04:10,500 have the same setup you should see 5663 04:04:04,920 --> 04:04:13,739 number all the way after the author 5664 04:04:10,500 --> 04:04:16,500 and also I want to showcase that once we 5665 04:04:13,739 --> 04:04:18,960 go back to a JavaScript land of course 5666 04:04:16,500 --> 04:04:20,880 we can use the template literals as well 5667 04:04:18,960 --> 04:04:23,699 so let's say in my case I want to add 5668 04:04:20,880 --> 04:04:26,760 that hashtag in front of it 5669 04:04:23,699 --> 04:04:29,279 so in order to do that I'm gonna go here 5670 04:04:26,760 --> 04:04:31,439 with the template literals and then 5671 04:04:29,279 --> 04:04:32,720 remember in a template literal in order 5672 04:04:31,439 --> 04:04:36,600 to access 5673 04:04:32,720 --> 04:04:38,939 the variable we need to go with dollar 5674 04:04:36,600 --> 04:04:41,580 sign and then the curlies keep in mind 5675 04:04:38,939 --> 04:04:44,460 that is a JavaScript syntax 5676 04:04:41,580 --> 04:04:46,560 we are back here in JavaScript plan 5677 04:04:44,460 --> 04:04:49,500 so we just need to follow the same rules 5678 04:04:46,560 --> 04:04:53,220 I'll start here with the hashtag 5679 04:04:49,500 --> 04:04:55,319 and then I'll go to dollar sign and then 5680 04:04:53,220 --> 04:04:57,359 the curlies so again I'm accessing the 5681 04:04:55,319 --> 04:04:59,460 same number I'm still adding plus one 5682 04:04:57,359 --> 04:05:02,520 the difference is that I just nicely 5683 04:04:59,460 --> 04:05:05,520 added this hashtag that's it and then as 5684 04:05:02,520 --> 04:05:08,279 far as the CSS we want to go to index 5685 04:05:05,520 --> 04:05:10,199 CSS I want to make book relative because 5686 04:05:08,279 --> 04:05:12,840 of the span 5687 04:05:10,199 --> 04:05:15,420 with the value of number 5688 04:05:12,840 --> 04:05:17,520 is going to be absolute 5689 04:05:15,420 --> 04:05:19,279 so in order to control the absolute we 5690 04:05:17,520 --> 04:05:22,380 need to set up the parent as relative 5691 04:05:19,279 --> 04:05:24,500 then let's keep moving and we're going 5692 04:05:22,380 --> 04:05:28,680 to go with number like I said position 5693 04:05:24,500 --> 04:05:30,960 absolute then I want to go with top 5694 04:05:28,680 --> 04:05:33,600 zero left 5695 04:05:30,960 --> 04:05:37,140 is also going to be zero 5696 04:05:33,600 --> 04:05:39,060 and we have font size padding and I'm 5697 04:05:37,140 --> 04:05:41,520 just going to add those border radiuses 5698 04:05:39,060 --> 04:05:44,100 since I'm using the poor radius for 5699 04:05:41,520 --> 04:05:46,140 entire card I just decided that 5700 04:05:44,100 --> 04:05:48,300 it's a good idea to do the same thing 5701 04:05:46,140 --> 04:05:49,620 for the sponge so padding is going to be 5702 04:05:48,300 --> 04:05:54,960 zero point 5703 04:05:49,620 --> 04:05:57,479 75 Rems than font size one 5704 04:05:54,960 --> 04:05:59,460 after that we have Porter and I don't 5705 04:05:57,479 --> 04:06:02,160 want to go with border radius because 5706 04:05:59,460 --> 04:06:04,739 essentially this is going to apply all 5707 04:06:02,160 --> 04:06:06,359 around and it's hard to see because we 5708 04:06:04,739 --> 04:06:09,359 don't have any background so let's say 5709 04:06:06,359 --> 04:06:12,120 here red of course I mean it doesn't 5710 04:06:09,359 --> 04:06:15,540 look as good as 5711 04:06:12,120 --> 04:06:18,180 when we go with border top and we're 5712 04:06:15,540 --> 04:06:20,460 gonna go with left radius so only the 5713 04:06:18,180 --> 04:06:22,439 left side and I also decided to do the 5714 04:06:20,460 --> 04:06:24,779 right side now you don't have to so 5715 04:06:22,439 --> 04:06:26,220 let's copy and paste this is going to be 5716 04:06:24,779 --> 04:06:29,120 bottom 5717 04:06:26,220 --> 04:06:29,120 and then right 5718 04:06:30,960 --> 04:06:36,000 let's save that okay awesome and now 5719 04:06:34,020 --> 04:06:37,680 let's just add some background colors 5720 04:06:36,000 --> 04:06:39,600 and this one I actually got from the 5721 04:06:37,680 --> 04:06:41,939 Amazon so 5722 04:06:39,600 --> 04:06:44,100 it's not really a secret you can just go 5723 04:06:41,939 --> 04:06:46,020 there pick the color 5724 04:06:44,100 --> 04:06:47,580 and it's going to spit back this one 5725 04:06:46,020 --> 04:06:50,720 most likely 5726 04:06:47,580 --> 04:06:50,720 um let's just go with color 5727 04:06:50,819 --> 04:06:55,140 and set it equal to a white 5728 04:06:53,640 --> 04:06:57,899 and with that we're done with this 5729 04:06:55,140 --> 04:07:00,180 challenge hopefully you enjoyed it and 5730 04:06:57,899 --> 04:07:01,739 in next video work on the title 5731 04:07:00,180 --> 04:07:04,800 challenge all right and up next let's 5732 04:07:01,739 --> 04:07:06,840 work on the title challenge just like 5733 04:07:04,800 --> 04:07:09,060 the previous challenge CSS is optional 5734 04:07:06,840 --> 04:07:12,840 don't worry about the CSS the main goal 5735 04:07:09,060 --> 04:07:15,899 is to set up an element with some kind 5736 04:07:12,840 --> 04:07:20,100 of title and that element needs to sit 5737 04:07:15,899 --> 04:07:22,199 on top of our books same deal if you 5738 04:07:20,100 --> 04:07:24,239 want to work on it independently please 5739 04:07:22,199 --> 04:07:27,180 stop the video and resume once you're 5740 04:07:24,239 --> 04:07:28,880 ready to compare the results and 5741 04:07:27,180 --> 04:07:31,680 essentially I'm gonna go back to 5742 04:07:28,880 --> 04:07:34,680 index.js and here I guess the biggest 5743 04:07:31,680 --> 04:07:36,660 gotcha is that we cannot return two 5744 04:07:34,680 --> 04:07:39,239 adjacent elements correct so if I'm 5745 04:07:36,660 --> 04:07:41,220 gonna do something like this it's not 5746 04:07:39,239 --> 04:07:43,080 going to work what's the solution well 5747 04:07:41,220 --> 04:07:45,540 we need to come up with a parent 5748 04:07:43,080 --> 04:07:49,500 one option for parent is what it is 5749 04:07:45,540 --> 04:07:53,580 react dot fragment or we can have here 5750 04:07:49,500 --> 04:07:54,720 shortcut like a show so let me move this 5751 04:07:53,580 --> 04:07:56,399 sucker down 5752 04:07:54,720 --> 04:07:59,640 and then let's come up with that value 5753 04:07:56,399 --> 04:08:01,699 so I'm going to call this um Zone 5754 04:07:59,640 --> 04:08:05,160 and best 5755 04:08:01,699 --> 04:08:06,239 Sellers and like I said CSS is optional 5756 04:08:05,160 --> 04:08:08,819 but 5757 04:08:06,239 --> 04:08:11,520 I'm still at it somewhere there doesn't 5758 04:08:08,819 --> 04:08:13,380 really matter where normally I 5759 04:08:11,520 --> 04:08:15,300 place it 5760 04:08:13,380 --> 04:08:17,880 as I'm adding the item but in this case 5761 04:08:15,300 --> 04:08:19,920 I'll somewhere in the bottom I'm just 5762 04:08:17,880 --> 04:08:22,920 gonna go with heading one 5763 04:08:19,920 --> 04:08:24,960 and text align 5764 04:08:22,920 --> 04:08:26,819 text line Center 5765 04:08:24,960 --> 04:08:29,520 then margin top 5766 04:08:26,819 --> 04:08:31,680 I'm gonna go to four Rems and then 5767 04:08:29,520 --> 04:08:35,220 lastly let's go with capitalize so text 5768 04:08:31,680 --> 04:08:38,279 transform and let's set it equal to a 5769 04:08:35,220 --> 04:08:40,680 capitalized and then also 5770 04:08:38,279 --> 04:08:42,420 I want you to change the title 5771 04:08:40,680 --> 04:08:44,040 in the public one 5772 04:08:42,420 --> 04:08:46,560 remember 5773 04:08:44,040 --> 04:08:47,699 there is this index HTML where we can 5774 04:08:46,560 --> 04:08:50,100 change the title 5775 04:08:47,699 --> 04:08:52,800 come up with the value and just change 5776 04:08:50,100 --> 04:08:55,380 it in my case I have react tutorial at 5777 04:08:52,800 --> 04:08:57,300 the end I want best sellers or something 5778 04:08:55,380 --> 04:08:59,399 along those lines let me open this one 5779 04:08:57,300 --> 04:09:01,500 up public 5780 04:08:59,399 --> 04:09:04,220 access HTML remember 5781 04:09:01,500 --> 04:09:07,620 this is where our entire application 5782 04:09:04,220 --> 04:09:09,600 lives then we want to find the title 5783 04:09:07,620 --> 04:09:10,880 instead of react tutorial we'll go with 5784 04:09:09,600 --> 04:09:14,520 best 5785 04:09:10,880 --> 04:09:15,239 Sellers and with this in place we are 5786 04:09:14,520 --> 04:09:17,340 done 5787 04:09:15,239 --> 04:09:19,920 with the challenge hopefully everyone 5788 04:09:17,340 --> 04:09:22,020 enjoyed it and in the following videos 5789 04:09:19,920 --> 04:09:25,680 we'll build a production ready 5790 04:09:22,020 --> 04:09:29,100 application and deploy it on netlify 5791 04:09:25,680 --> 04:09:31,859 okay and up next let's work on the 5792 04:09:29,100 --> 04:09:35,220 production application you see this is 5793 04:09:31,859 --> 04:09:37,439 our development environment 5794 04:09:35,220 --> 04:09:39,120 which works really awesome we can right 5795 04:09:37,439 --> 04:09:40,080 away see the results in the browser and 5796 04:09:39,120 --> 04:09:42,000 all that 5797 04:09:40,080 --> 04:09:43,260 but when we want to deploy the 5798 04:09:42,000 --> 04:09:44,880 application 5799 04:09:43,260 --> 04:09:47,100 the setup 5800 04:09:44,880 --> 04:09:49,140 needs to be different 5801 04:09:47,100 --> 04:09:51,540 essentially we're not going to have the 5802 04:09:49,140 --> 04:09:53,399 source and then public and all that 5803 04:09:51,540 --> 04:09:57,239 and in order to set up that production 5804 04:09:53,399 --> 04:10:01,680 application so we can deploy it on any 5805 04:09:57,239 --> 04:10:03,840 provider we need to stop the server 5806 04:10:01,680 --> 04:10:06,960 and also we just need to take a peek in 5807 04:10:03,840 --> 04:10:09,840 the package.json remember 5808 04:10:06,960 --> 04:10:11,720 initially I showed you the script 5809 04:10:09,840 --> 04:10:14,880 so these are our commands so start 5810 04:10:11,720 --> 04:10:15,779 here's the development server a build 5811 04:10:14,880 --> 04:10:17,699 one 5812 04:10:15,779 --> 04:10:21,300 is the one that builds that production 5813 04:10:17,699 --> 04:10:23,699 ready application and in order to run it 5814 04:10:21,300 --> 04:10:26,760 I'm going to clear the terminal and the 5815 04:10:23,699 --> 04:10:28,380 command is clear we go with npm and then 5816 04:10:26,760 --> 04:10:30,540 we add this run 5817 04:10:28,380 --> 04:10:34,020 and build 5818 04:10:30,540 --> 04:10:36,060 npm run build and what you'll notice 5819 04:10:34,020 --> 04:10:37,979 once we run the command 5820 04:10:36,060 --> 04:10:41,520 create react app 5821 04:10:37,979 --> 04:10:44,340 is going to be busy building our 5822 04:10:41,520 --> 04:10:47,760 application and once everything is done 5823 04:10:44,340 --> 04:10:50,460 there's going to be an extra folder a 5824 04:10:47,760 --> 04:10:53,220 build folder this is where our 5825 04:10:50,460 --> 04:10:55,199 production ready application is going to 5826 04:10:53,220 --> 04:10:57,779 live now I don't know how long it's 5827 04:10:55,199 --> 04:11:00,300 going to take most likely oh no as I was 5828 04:10:57,779 --> 04:11:02,040 saying it actually finished it so notice 5829 04:11:00,300 --> 04:11:04,859 this build folder right now 5830 04:11:02,040 --> 04:11:06,779 and check it out this is our production 5831 04:11:04,859 --> 04:11:09,239 ready application 5832 04:11:06,779 --> 04:11:11,760 and if you'll take a look at it you'll 5833 04:11:09,239 --> 04:11:15,720 notice that it has a bunch of static 5834 04:11:11,760 --> 04:11:17,880 assets so a bunch of HTML CSS JavaScript 5835 04:11:15,720 --> 04:11:19,979 and all that so again we don't have any 5836 04:11:17,880 --> 04:11:22,859 more of The Source One 5837 04:11:19,979 --> 04:11:25,680 this is only while we're developing once 5838 04:11:22,859 --> 04:11:27,960 we run npm run build this is the 5839 04:11:25,680 --> 04:11:31,380 production ready application and 5840 04:11:27,960 --> 04:11:35,399 effectively we can use this build folder 5841 04:11:31,380 --> 04:11:37,020 with pretty much any provider that you 5842 04:11:35,399 --> 04:11:39,420 pick in our case we're going to go with 5843 04:11:37,020 --> 04:11:41,520 metal fiber you don't have to again if 5844 04:11:39,420 --> 04:11:43,439 you use some other provider it's going 5845 04:11:41,520 --> 04:11:47,279 to work the same way just like you 5846 04:11:43,439 --> 04:11:49,800 normally would with the HTML project or 5847 04:11:47,279 --> 04:11:51,540 vanilla.js project and hopefully you see 5848 04:11:49,800 --> 04:11:53,399 where I'm going with this once we run 5849 04:11:51,540 --> 04:11:56,160 npm build 5850 04:11:53,399 --> 04:12:00,239 we can get this build folder that we can 5851 04:11:56,160 --> 04:12:03,000 use with any of the hosting providers as 5852 04:12:00,239 --> 04:12:05,279 far as the hosting like I said already 5853 04:12:03,000 --> 04:12:08,699 50 000 times in the previous video you 5854 04:12:05,279 --> 04:12:10,199 can use your provider but since I use 5855 04:12:08,699 --> 04:12:12,540 nettlefive 5856 04:12:10,199 --> 04:12:14,819 during the course we'll use this one 5857 04:12:12,540 --> 04:12:16,260 now let me be very clear they're not 5858 04:12:14,819 --> 04:12:18,540 sponsoring this video they're not 5859 04:12:16,260 --> 04:12:21,420 sponsoring the course I'm not paid to 5860 04:12:18,540 --> 04:12:23,220 say this I just enjoy the service they 5861 04:12:21,420 --> 04:12:24,540 do offer generous free tier so 5862 04:12:23,220 --> 04:12:27,540 essentially the only thing you need to 5863 04:12:24,540 --> 04:12:30,120 do is sign up that's it you'll have to 5864 04:12:27,540 --> 04:12:32,939 provide your email and you're good to go 5865 04:12:30,120 --> 04:12:35,939 and once you sign up for account 5866 04:12:32,939 --> 04:12:38,699 just log in and this is going to be your 5867 04:12:35,939 --> 04:12:39,960 dashboard then you want to go for a new 5868 04:12:38,699 --> 04:12:41,699 site and of course 5869 04:12:39,960 --> 04:12:43,979 if you just opened up you're not going 5870 04:12:41,699 --> 04:12:46,680 to see the sites but since I'm using 5871 04:12:43,979 --> 04:12:48,540 this already for quite a while I have a 5872 04:12:46,680 --> 04:12:50,939 bunch of sites in here 5873 04:12:48,540 --> 04:12:52,859 you want to look for add new site and 5874 04:12:50,939 --> 04:12:55,560 we'll start with deploy manually yes 5875 04:12:52,859 --> 04:12:56,819 with a later project we will take a look 5876 04:12:55,560 --> 04:12:59,460 at the 5877 04:12:56,819 --> 04:13:01,100 GitHub option but for now let's just go 5878 04:12:59,460 --> 04:13:03,600 deploy manually 5879 04:13:01,100 --> 04:13:05,819 and essentially we just want to look for 5880 04:13:03,600 --> 04:13:10,380 that build one so let's go to tutorial 5881 04:13:05,819 --> 04:13:12,960 and let's grab this build folder 5882 04:13:10,380 --> 04:13:14,340 yep we want to upload 5883 04:13:12,960 --> 04:13:15,600 the 5884 04:13:14,340 --> 04:13:18,899 process 5885 04:13:15,600 --> 04:13:22,319 it's going to be pretty fast and once 5886 04:13:18,899 --> 04:13:25,560 netlify is done processing the files we 5887 04:13:22,319 --> 04:13:26,640 will have our site live and if you click 5888 04:13:25,560 --> 04:13:29,939 on this link 5889 04:13:26,640 --> 04:13:31,199 you'll see our awesome project one last 5890 04:13:29,939 --> 04:13:34,260 thing that I want to mention in this 5891 04:13:31,199 --> 04:13:35,399 video if you want to change the site 5892 04:13:34,260 --> 04:13:39,120 name 5893 04:13:35,399 --> 04:13:41,399 you just need to navigate here to a site 5894 04:13:39,120 --> 04:13:45,319 settings let's say you're not happy with 5895 04:13:41,399 --> 04:13:48,540 this generic name go to change site name 5896 04:13:45,319 --> 04:13:51,180 and you need to provide whatever value 5897 04:13:48,540 --> 04:13:53,340 you want now keep in mind unless you add 5898 04:13:51,180 --> 04:13:55,260 the custom domain it's always going to 5899 04:13:53,340 --> 04:13:56,939 have this notify that app which in my 5900 04:13:55,260 --> 04:13:59,220 opinion is not a big deal but just in 5901 04:13:56,939 --> 04:14:00,960 case you don't want to so yes you'll 5902 04:13:59,220 --> 04:14:04,140 effectively need to use the search 5903 04:14:00,960 --> 04:14:06,120 engine to figure out how you can set up 5904 04:14:04,140 --> 04:14:08,520 a custom domain which by the way is not 5905 04:14:06,120 --> 04:14:10,620 hard but we're not going to cover in 5906 04:14:08,520 --> 04:14:13,439 this course because this is a react 5907 04:14:10,620 --> 04:14:14,939 course and if the name is already taken 5908 04:14:13,439 --> 04:14:17,040 let's say if I'm going to go with react 5909 04:14:14,939 --> 04:14:19,680 of course I'm going to have this error 5910 04:14:17,040 --> 04:14:21,300 so just keep that in mind okay so I'm 5911 04:14:19,680 --> 04:14:24,840 going to go with the react 5912 04:14:21,300 --> 04:14:27,540 horse and fundamentals hopefully that is 5913 04:14:24,840 --> 04:14:30,420 not taken but you never know yep 5914 04:14:27,540 --> 04:14:32,520 everything works like I said this is the 5915 04:14:30,420 --> 04:14:35,460 link now that we can share 5916 04:14:32,520 --> 04:14:38,699 and anyone who visits this link is going 5917 04:14:35,460 --> 04:14:41,580 to see our awesome application so this 5918 04:14:38,699 --> 04:14:44,819 is it for react fundamentals hopefully 5919 04:14:41,580 --> 04:14:46,979 everyone enjoy this section and I'll see 5920 04:14:44,819 --> 04:14:50,100 you in the next one alright we're pretty 5921 04:14:46,979 --> 04:14:52,739 much done with react fundamentals and 5922 04:14:50,100 --> 04:14:56,160 before we start discussing Advanced 5923 04:14:52,739 --> 04:14:58,380 react topics let's quickly cover all the 5924 04:14:56,160 --> 04:15:01,380 files in the source folder 5925 04:14:58,380 --> 04:15:02,399 that create react app provides by 5926 04:15:01,380 --> 04:15:05,640 default 5927 04:15:02,399 --> 04:15:09,300 and I'll also show you which files 5928 04:15:05,640 --> 04:15:12,660 I typically remove when I start a new 5929 04:15:09,300 --> 04:15:16,199 react project with create react app 5930 04:15:12,660 --> 04:15:17,819 technically this video is optional so if 5931 04:15:16,199 --> 04:15:20,760 you don't feel like 5932 04:15:17,819 --> 04:15:21,779 following along feel free to move on to 5933 04:15:20,760 --> 04:15:25,439 the next video 5934 04:15:21,779 --> 04:15:28,739 if you however do want to follow along I 5935 04:15:25,439 --> 04:15:31,380 suggest installing new create react app 5936 04:15:28,739 --> 04:15:33,180 instance and remember the command was 5937 04:15:31,380 --> 04:15:36,060 following first 5938 04:15:33,180 --> 04:15:39,359 we want to navigate to 5939 04:15:36,060 --> 04:15:41,939 where we will set up the project so in 5940 04:15:39,359 --> 04:15:45,060 my case that is going to be desktop and 5941 04:15:41,939 --> 04:15:49,500 then we want to type npx create react 5942 04:15:45,060 --> 04:15:52,640 app at latest and then the app name so 5943 04:15:49,500 --> 04:15:55,800 type this command and resume the video 5944 04:15:52,640 --> 04:15:57,000 once all the dependencies have been 5945 04:15:55,800 --> 04:16:00,060 installed 5946 04:15:57,000 --> 04:16:03,420 and once the install is complete 5947 04:16:00,060 --> 04:16:04,620 just open up the project in your text 5948 04:16:03,420 --> 04:16:07,920 error 5949 04:16:04,620 --> 04:16:09,359 and you don't have to bother with npm 5950 04:16:07,920 --> 04:16:11,279 start 5951 04:16:09,359 --> 04:16:14,160 since essentially we'll spend most of 5952 04:16:11,279 --> 04:16:17,160 this video in the source folder so just 5953 04:16:14,160 --> 04:16:19,920 open up the project and text error and 5954 04:16:17,160 --> 04:16:23,160 then crack open the search folder if we 5955 04:16:19,920 --> 04:16:26,640 navigate to index.js you'll see that the 5956 04:16:23,160 --> 04:16:27,420 setup is almost almost the same so we 5957 04:16:26,640 --> 04:16:31,319 have 5958 04:16:27,420 --> 04:16:34,319 Imports for react and react Dom we also 5959 04:16:31,319 --> 04:16:36,420 have here import for index CSS now of 5960 04:16:34,319 --> 04:16:38,279 course if we navigate there we can see 5961 04:16:36,420 --> 04:16:40,979 that the code is a little bit different 5962 04:16:38,279 --> 04:16:43,439 and again the general idea is that 5963 04:16:40,979 --> 04:16:46,620 essentially this is where you set up 5964 04:16:43,439 --> 04:16:49,080 your Global Styles or maybe all the 5965 04:16:46,620 --> 04:16:50,220 styles again it really depends on your 5966 04:16:49,080 --> 04:16:53,760 application 5967 04:16:50,220 --> 04:16:55,859 then we have app component which I'm 5968 04:16:53,760 --> 04:16:57,359 going to talk about in a second we also 5969 04:16:55,859 --> 04:17:01,080 have this one 5970 04:16:57,359 --> 04:17:03,060 so the report web vitals and essentially 5971 04:17:01,080 --> 04:17:04,859 if you're interested learning more they 5972 04:17:03,060 --> 04:17:07,199 provide detailed info in the comments 5973 04:17:04,859 --> 04:17:09,660 now in our case we're not going to 5974 04:17:07,199 --> 04:17:12,239 implement that so here's what we can do 5975 04:17:09,660 --> 04:17:16,560 we can simplify we can remove this code 5976 04:17:12,239 --> 04:17:19,020 and we can also remove this import 5977 04:17:16,560 --> 04:17:22,739 so that's done then what's happening 5978 04:17:19,020 --> 04:17:25,680 here with app.js well you see unlike the 5979 04:17:22,739 --> 04:17:27,120 react fundamentals where if I remember 5980 04:17:25,680 --> 04:17:29,880 correctly 5981 04:17:27,120 --> 04:17:33,239 we ended up with a book list component 5982 04:17:29,880 --> 04:17:36,600 which was in the index.js 5983 04:17:33,239 --> 04:17:40,260 a common convention is to 5984 04:17:36,600 --> 04:17:42,720 set up that root component as a app 5985 04:17:40,260 --> 04:17:46,319 and set it up in a separate file 5986 04:17:42,720 --> 04:17:49,500 basically we have app.js so this is our 5987 04:17:46,319 --> 04:17:52,859 root component this is where all our 5988 04:17:49,500 --> 04:17:54,840 components are going to meet 5989 04:17:52,859 --> 04:17:57,840 um it's sitting in a separate file and 5990 04:17:54,840 --> 04:18:00,239 Convention is to call this app and most 5991 04:17:57,840 --> 04:18:02,100 likely I don't have to say that you can 5992 04:18:00,239 --> 04:18:05,640 call this orange and everything is still 5993 04:18:02,100 --> 04:18:06,660 going to work as long as the Imports are 5994 04:18:05,640 --> 04:18:09,300 correct 5995 04:18:06,660 --> 04:18:11,340 so again it's just a convention and 5996 04:18:09,300 --> 04:18:14,640 basically if we navigate here in the 5997 04:18:11,340 --> 04:18:17,160 app.js you'll see that the import logo 5998 04:18:14,640 --> 04:18:18,120 so this is where we can see over here on 5999 04:18:17,160 --> 04:18:20,220 the screen 6000 04:18:18,120 --> 04:18:22,319 and since we're already familiar how the 6001 04:18:20,220 --> 04:18:24,899 es6 modules work of course we're 6002 04:18:22,319 --> 04:18:27,840 familiar with this code and then rest of 6003 04:18:24,899 --> 04:18:29,699 the return now we'll start from the very 6004 04:18:27,840 --> 04:18:32,640 scratch so 6005 04:18:29,699 --> 04:18:34,620 I'll remove the logo actually I'll 6006 04:18:32,640 --> 04:18:36,779 delete the file in a second as well 6007 04:18:34,620 --> 04:18:40,080 and as far as the return well let's just 6008 04:18:36,779 --> 04:18:43,199 go here with simple heading one 6009 04:18:40,080 --> 04:18:45,899 and let's say Backroads app back roads 6010 04:18:43,199 --> 04:18:49,080 and app let's save it because I still 6011 04:18:45,899 --> 04:18:51,960 want to talk about the app CSS but while 6012 04:18:49,080 --> 04:18:53,460 we're still in the index.js one last 6013 04:18:51,960 --> 04:18:55,680 thing that I want to mention notice here 6014 04:18:53,460 --> 04:18:58,439 how in the render 6015 04:18:55,680 --> 04:18:59,880 not only we have the app so that's the 6016 04:18:58,439 --> 04:19:03,000 root component again 6017 04:18:59,880 --> 04:19:05,520 the exact same setup how we had in react 6018 04:19:03,000 --> 04:19:08,040 fundamentals the difference of course is 6019 04:19:05,520 --> 04:19:09,359 the component instead of book list now 6020 04:19:08,040 --> 04:19:11,760 we have app 6021 04:19:09,359 --> 04:19:12,960 but it's also wrapped here in this 6022 04:19:11,760 --> 04:19:14,880 strict mode 6023 04:19:12,960 --> 04:19:17,160 and essentially Street mode is a tool 6024 04:19:14,880 --> 04:19:18,600 for highlighting potential problems in 6025 04:19:17,160 --> 04:19:20,699 application 6026 04:19:18,600 --> 04:19:21,720 it activates additional checks and 6027 04:19:20,699 --> 04:19:25,199 warnings 6028 04:19:21,720 --> 04:19:29,520 for its descendants Now it only runs in 6029 04:19:25,199 --> 04:19:30,899 development so it's not going to impact 6030 04:19:29,520 --> 04:19:32,939 your production build 6031 04:19:30,899 --> 04:19:36,120 and one gotcha 6032 04:19:32,939 --> 04:19:38,040 is that in development it renders twice 6033 04:19:36,120 --> 04:19:40,620 and I'll showcase that in a second don't 6034 04:19:38,040 --> 04:19:43,260 worry now if you want you can remove 6035 04:19:40,620 --> 04:19:45,479 please keep that in mind it is an option 6036 04:19:43,260 --> 04:19:47,340 if for some reason you don't like the 6037 04:19:45,479 --> 04:19:49,920 warnings you don't like the fact that it 6038 04:19:47,340 --> 04:19:51,840 renders twice you can always remove this 6039 04:19:49,920 --> 04:19:55,020 component and just to Showcase that if I 6040 04:19:51,840 --> 04:19:58,680 go back to app.js somewhere here 6041 04:19:55,020 --> 04:20:00,300 if I go with console.log and if I'll say 6042 04:19:58,680 --> 04:20:01,500 hello you'll notice that the moment we 6043 04:20:00,300 --> 04:20:04,020 save 6044 04:20:01,500 --> 04:20:05,520 basically it runs twice that's something 6045 04:20:04,020 --> 04:20:08,340 that we need to keep in mind so again 6046 04:20:05,520 --> 04:20:11,399 once we refresh notice essentially we 6047 04:20:08,340 --> 04:20:14,040 have two hellos in the console and again 6048 04:20:11,399 --> 04:20:17,160 it's because we have strict mode 6049 04:20:14,040 --> 04:20:20,399 wrapping our app component our root 6050 04:20:17,160 --> 04:20:23,040 component however it is not going to 6051 04:20:20,399 --> 04:20:26,399 affect the production 6052 04:20:23,040 --> 04:20:29,100 so once we ship this to production the 6053 04:20:26,399 --> 04:20:30,239 strict mode is not going to affect our 6054 04:20:29,100 --> 04:20:33,720 application 6055 04:20:30,239 --> 04:20:36,840 now notice here how we have import for 6056 04:20:33,720 --> 04:20:40,260 app CSS so essentially we have two files 6057 04:20:36,840 --> 04:20:42,479 we have index CSS and we have app CSS 6058 04:20:40,260 --> 04:20:46,140 and you can already see that 6059 04:20:42,479 --> 04:20:49,199 essentially we can split up our CSS code 6060 04:20:46,140 --> 04:20:52,800 so instead of jamming 6061 04:20:49,199 --> 04:20:55,080 our entire CSS logic in one file we can 6062 04:20:52,800 --> 04:20:58,199 split it up 6063 04:20:55,080 --> 04:21:00,120 for example you create a component and 6064 04:20:58,199 --> 04:21:02,819 you right away set up a 6065 04:21:00,120 --> 04:21:06,660 CSS file with the corresponding logic 6066 04:21:02,819 --> 04:21:09,779 now one gotcha we need to be aware of 6067 04:21:06,660 --> 04:21:12,359 all of this is still 6068 04:21:09,779 --> 04:21:13,439 going in the same pile now what do I 6069 04:21:12,359 --> 04:21:16,920 mean by that 6070 04:21:13,439 --> 04:21:19,260 so let's say if in the index CSS I have 6071 04:21:16,920 --> 04:21:21,359 a code for heading 1. and I'm not going 6072 04:21:19,260 --> 04:21:24,239 to be particularly original I'm just 6073 04:21:21,359 --> 04:21:26,100 going to say here color red 6074 04:21:24,239 --> 04:21:28,260 if we navigate back of course everything 6075 04:21:26,100 --> 04:21:31,439 works now 6076 04:21:28,260 --> 04:21:33,239 if we go to app CSS 6077 04:21:31,439 --> 04:21:37,140 the thing is 6078 04:21:33,239 --> 04:21:40,260 it's not going to be isolated by default 6079 04:21:37,140 --> 04:21:43,560 basically it's not going to be scoped 6080 04:21:40,260 --> 04:21:45,779 to just this app Js 6081 04:21:43,560 --> 04:21:49,199 essentially whatever logic we're gonna 6082 04:21:45,779 --> 04:21:52,979 add here in the app CSS it's gonna get 6083 04:21:49,199 --> 04:21:56,100 added to whatever logic we have in the 6084 04:21:52,979 --> 04:21:57,840 index file or any other CSS file and 6085 04:21:56,100 --> 04:21:59,819 again just showcase that 6086 04:21:57,840 --> 04:22:01,979 effectively we'll remove this component 6087 04:21:59,819 --> 04:22:04,260 altogether in a second but for now let 6088 04:22:01,979 --> 04:22:06,960 me just go here with heading 1 and again 6089 04:22:04,260 --> 04:22:10,020 we need to go with color 6090 04:22:06,960 --> 04:22:12,779 I'm gonna go with blue and once we go 6091 04:22:10,020 --> 04:22:16,319 there actually this overrides wherever I 6092 04:22:12,779 --> 04:22:17,460 have in the the index CSS so yes you can 6093 04:22:16,319 --> 04:22:21,120 definitely 6094 04:22:17,460 --> 04:22:23,520 set up the files and split up the logic 6095 04:22:21,120 --> 04:22:25,739 however keep in mind one thing 6096 04:22:23,520 --> 04:22:27,840 you'll still need to be careful as 6097 04:22:25,739 --> 04:22:29,640 you're setting up the CSS now of course 6098 04:22:27,840 --> 04:22:31,020 there are solutions for that something 6099 04:22:29,640 --> 04:22:34,979 that we're going to cover in this course 6100 04:22:31,020 --> 04:22:37,260 for example I am a huge fan of CSS and 6101 04:22:34,979 --> 04:22:38,520 JS more specifically styled components 6102 04:22:37,260 --> 04:22:41,340 and that's why I will cover in this 6103 04:22:38,520 --> 04:22:44,040 course that solved this problem but 6104 04:22:41,340 --> 04:22:47,040 right out of the box yes 6105 04:22:44,040 --> 04:22:50,279 you can nicely split up your CSS code 6106 04:22:47,040 --> 04:22:51,720 but please keep in mind that you'll have 6107 04:22:50,279 --> 04:22:55,319 to be careful 6108 04:22:51,720 --> 04:22:56,160 because all that CSS code ends up in one 6109 04:22:55,319 --> 04:22:59,220 place 6110 04:22:56,160 --> 04:23:04,500 so hopefully that is clear it's not 6111 04:22:59,220 --> 04:23:06,960 scoped by default to a certain component 6112 04:23:04,500 --> 04:23:09,779 all right I think we covered everything 6113 04:23:06,960 --> 04:23:12,180 so now let me just remove this log 6114 04:23:09,779 --> 04:23:14,160 let me also remove the import we won't 6115 04:23:12,180 --> 04:23:17,399 need that essentially we'll just delete 6116 04:23:14,160 --> 04:23:20,160 bunch of files we won't need them so 6117 04:23:17,399 --> 04:23:22,739 let's start here with app CSS we'll also 6118 04:23:20,160 --> 04:23:24,600 remove test so this is if you want to 6119 04:23:22,739 --> 04:23:27,899 set up tests which we're now going to do 6120 04:23:24,600 --> 04:23:30,600 then we also want to select the logo we 6121 04:23:27,899 --> 04:23:34,260 don't need the logo this one as well as 6122 04:23:30,600 --> 04:23:36,540 setup best so you should only see 6123 04:23:34,260 --> 04:23:39,540 once you're done with delete app.js 6124 04:23:36,540 --> 04:23:40,800 index CSS and index 6125 04:23:39,540 --> 04:23:43,080 .js 6126 04:23:40,800 --> 04:23:47,340 so let me remove 6127 04:23:43,080 --> 04:23:49,560 and yep now I have again index.js 6128 04:23:47,340 --> 04:23:52,939 this is where we inject 6129 04:23:49,560 --> 04:23:56,460 our JavaScript logic into 6130 04:23:52,939 --> 04:24:01,140 index.html in the public one and the 6131 04:23:56,460 --> 04:24:03,899 app.js is our root component okay and up 6132 04:24:01,140 --> 04:24:06,600 next let's switch to beat basically 6133 04:24:03,899 --> 04:24:09,239 instead of create react app we're going 6134 04:24:06,600 --> 04:24:12,600 to use different tool to scaffold and 6135 04:24:09,239 --> 04:24:14,340 develop our react project now I really 6136 04:24:12,600 --> 04:24:17,279 don't want to spend too much time on 6137 04:24:14,340 --> 04:24:19,859 discussing benefits of beat if you're 6138 04:24:17,279 --> 04:24:22,199 interested in learning more there are 6139 04:24:19,859 --> 04:24:23,699 plenty of YouTube videos and blog posts 6140 04:24:22,199 --> 04:24:27,840 that cover that 6141 04:24:23,699 --> 04:24:32,279 and as a side note they also cover that 6142 04:24:27,840 --> 04:24:35,520 in a docs just click on why veed tab in 6143 04:24:32,279 --> 04:24:38,760 short at this point vid is much faster 6144 04:24:35,520 --> 04:24:41,279 than create react app and right out of 6145 04:24:38,760 --> 04:24:42,600 the box it provides tons of great 6146 04:24:41,279 --> 04:24:46,500 features 6147 04:24:42,600 --> 04:24:47,880 and as a result we get way better than 6148 04:24:46,500 --> 04:24:50,880 experience 6149 04:24:47,880 --> 04:24:53,239 now if you're wondering why we covered 6150 04:24:50,880 --> 04:24:56,580 create react app in the first place 6151 04:24:53,239 --> 04:24:59,220 effectively why we did not start with 6152 04:24:56,580 --> 04:25:01,080 Veet right away well there are two 6153 04:24:59,220 --> 04:25:03,180 reasons first 6154 04:25:01,080 --> 04:25:06,180 there's still plenty of 6155 04:25:03,180 --> 04:25:09,120 code examples that use create react app 6156 04:25:06,180 --> 04:25:12,479 so in my opinion it's useful to know the 6157 04:25:09,120 --> 04:25:14,880 general layout and commands second more 6158 04:25:12,479 --> 04:25:17,699 importantly I really want you to get 6159 04:25:14,880 --> 04:25:20,100 comfortable writing react code in any 6160 04:25:17,699 --> 04:25:22,680 environment you see 6161 04:25:20,100 --> 04:25:24,899 once you start working for a company or 6162 04:25:22,680 --> 04:25:28,500 switch to one of the react Frameworks 6163 04:25:24,899 --> 04:25:31,560 for example nexjs the dev environment 6164 04:25:28,500 --> 04:25:33,960 will be different yes you'll still write 6165 04:25:31,560 --> 04:25:36,720 the same react code but folders and 6166 04:25:33,960 --> 04:25:39,420 commands they will be different in each 6167 04:25:36,720 --> 04:25:41,939 environment and therefore it's important 6168 04:25:39,420 --> 04:25:44,699 for us to get comfortable working 6169 04:25:41,939 --> 04:25:47,159 in different setups with that said once 6170 04:25:44,699 --> 04:25:48,659 we cover beat setup you'll see that the 6171 04:25:47,159 --> 04:25:51,120 general idea is pretty much the same 6172 04:25:48,659 --> 04:25:53,640 it's just here and there the 6173 04:25:51,120 --> 04:25:56,040 implementation is tiny bit different 6174 04:25:53,640 --> 04:25:57,960 okay and in order to get started with 6175 04:25:56,040 --> 04:26:01,699 beat project we first want to navigate 6176 04:25:57,960 --> 04:26:01,699 to the Dachshund the URL is 6177 04:26:01,939 --> 04:26:11,220 dot Dev again URL is 6178 04:26:06,620 --> 04:26:12,659 dot dev then we want to go with get 6179 04:26:11,220 --> 04:26:15,540 started 6180 04:26:12,659 --> 04:26:18,180 continue scrolling and we're gonna go 6181 04:26:15,540 --> 04:26:22,680 with template now I'm assuming you're 6182 04:26:18,180 --> 04:26:25,439 using the latest node version so if you 6183 04:26:22,680 --> 04:26:26,760 don't if your npm version is less than 6184 04:26:25,439 --> 04:26:28,620 seven then of course 6185 04:26:26,760 --> 04:26:30,540 use this command 6186 04:26:28,620 --> 04:26:34,140 but since I'm assuming that most people 6187 04:26:30,540 --> 04:26:36,600 who are watching have the npm greater 6188 04:26:34,140 --> 04:26:37,739 than seven that's why I'm gonna go with 6189 04:26:36,600 --> 04:26:40,739 this command 6190 04:26:37,739 --> 04:26:43,739 so we're looking for npm create Veet at 6191 04:26:40,739 --> 04:26:46,439 latest then we need to provide the name 6192 04:26:43,739 --> 04:26:48,899 and then we have two sets of dashes 6193 04:26:46,439 --> 04:26:51,120 template and as you can see they provide 6194 04:26:48,899 --> 04:26:53,520 bunch of templates we have for 6195 04:26:51,120 --> 04:26:56,939 vanilla.js for view spelled and all that 6196 04:26:53,520 --> 04:26:59,100 now we're looking for the react one so 6197 04:26:56,939 --> 04:27:01,439 essentially we just need to 6198 04:26:59,100 --> 04:27:02,640 structure this command so let me 6199 04:27:01,439 --> 04:27:05,460 navigate back 6200 04:27:02,640 --> 04:27:07,020 I'm going to massively zoom in again in 6201 04:27:05,460 --> 04:27:09,000 my case I'm going to create that project 6202 04:27:07,020 --> 04:27:10,500 in the desktop 6203 04:27:09,000 --> 04:27:12,779 of course it's really up to you where 6204 04:27:10,500 --> 04:27:14,040 you want to do that and now let me 6205 04:27:12,779 --> 04:27:16,800 carefully 6206 04:27:14,040 --> 04:27:18,720 grab this command so I'll take 6207 04:27:16,800 --> 04:27:20,279 everything up to a template first I'm 6208 04:27:18,720 --> 04:27:22,080 going to change the name it's not going 6209 04:27:20,279 --> 04:27:24,720 to be my view app 6210 04:27:22,080 --> 04:27:26,880 it's actually going to be a react app 6211 04:27:24,720 --> 04:27:29,939 and then as far as the template we want 6212 04:27:26,880 --> 04:27:32,040 to go with space and then type react or 6213 04:27:29,939 --> 04:27:34,859 copy and paste react that's really up to 6214 04:27:32,040 --> 04:27:37,140 you as you can see 6215 04:27:34,859 --> 04:27:38,939 you'll right away get the project so 6216 04:27:37,140 --> 04:27:41,040 that's the cool thing again I did not 6217 04:27:38,939 --> 04:27:43,199 stop the video or anything like that you 6218 04:27:41,040 --> 04:27:45,899 right away get the project and now we 6219 04:27:43,199 --> 04:27:48,540 just want to spin it up so first let's 6220 04:27:45,899 --> 04:27:50,399 just open up the text editor then I'm 6221 04:27:48,540 --> 04:27:53,460 going to drag and drop 6222 04:27:50,399 --> 04:27:57,479 and you'll see that the setup is very 6223 04:27:53,460 --> 04:28:00,000 very similar to the create react app and 6224 04:27:57,479 --> 04:28:02,279 first let's just navigate to the package 6225 04:28:00,000 --> 04:28:05,580 Json and you'll notice that the command 6226 04:28:02,279 --> 04:28:09,300 we're looking for is npm run Dev so this 6227 04:28:05,580 --> 04:28:10,800 is going to spin up the dev server and 6228 04:28:09,300 --> 04:28:13,739 before we can do that we want to install 6229 04:28:10,800 --> 04:28:16,260 the dependencies so we go here with npm 6230 04:28:13,739 --> 04:28:18,659 install so 6231 04:28:16,260 --> 04:28:21,060 this might take a little bit but trust 6232 04:28:18,659 --> 04:28:24,300 me it's definitely going to be much 6233 04:28:21,060 --> 04:28:26,580 faster than create react app and then we 6234 04:28:24,300 --> 04:28:31,199 want to go with npm run Dev again 6235 04:28:26,580 --> 04:28:32,220 instead of npm start we go with npm run 6236 04:28:31,199 --> 04:28:35,340 Dev 6237 04:28:32,220 --> 04:28:37,439 and also the localhost is going to be 6238 04:28:35,340 --> 04:28:39,420 different so yes we'll still have the 6239 04:28:37,439 --> 04:28:41,580 react application and we'll take a look 6240 04:28:39,420 --> 04:28:45,180 at the code in a second and all that 6241 04:28:41,580 --> 04:28:48,720 but the Local Host is going to be 5173 6242 04:28:45,180 --> 04:28:52,460 so up until now we have been using 6243 04:28:48,720 --> 04:28:55,620 localhost 3000 but now it's going to be 6244 04:28:52,460 --> 04:28:57,060 5173 and before we take a look at the 6245 04:28:55,620 --> 04:28:58,140 application I just want to quickly 6246 04:28:57,060 --> 04:28:59,880 mention 6247 04:28:58,140 --> 04:29:02,520 if you're interested 6248 04:28:59,880 --> 04:29:05,279 in basically the info that I'm providing 6249 04:29:02,520 --> 04:29:08,460 if you want to keep that as a note it's 6250 04:29:05,279 --> 04:29:10,739 actually located in the fundamentals all 6251 04:29:08,460 --> 04:29:12,239 the way at the end of the readme 6252 04:29:10,739 --> 04:29:14,819 so after 6253 04:29:12,239 --> 04:29:17,640 all the fundamental stuff 6254 04:29:14,819 --> 04:29:21,239 over here you'll find info on create 6255 04:29:17,640 --> 04:29:24,479 react app as well as beat 6256 04:29:21,239 --> 04:29:27,779 so I guess our project is up and running 6257 04:29:24,479 --> 04:29:30,180 at localhost 5173 so why don't we 6258 04:29:27,779 --> 04:29:31,020 navigate over there just to see what we 6259 04:29:30,180 --> 04:29:34,140 have 6260 04:29:31,020 --> 04:29:35,699 and yes probably during this video it's 6261 04:29:34,140 --> 04:29:38,100 going to be annoying to some people that 6262 04:29:35,699 --> 04:29:39,840 I'll just keep repeating that as you can 6263 04:29:38,100 --> 04:29:42,600 see the setup is extremely similar 6264 04:29:39,840 --> 04:29:45,000 because that's the main point that I'm 6265 04:29:42,600 --> 04:29:46,739 trying to get across 6266 04:29:45,000 --> 04:29:48,840 even though we're switching to a 6267 04:29:46,739 --> 04:29:51,540 different tool the setup is going to be 6268 04:29:48,840 --> 04:29:53,159 extremely extremely similar so we 6269 04:29:51,540 --> 04:29:55,319 installed dependencies 6270 04:29:53,159 --> 04:29:58,140 if we want to run the project we go with 6271 04:29:55,319 --> 04:30:00,420 npm around Dev as you can see here at 6272 04:29:58,140 --> 04:30:02,640 the top we have node modules what do we 6273 04:30:00,420 --> 04:30:06,199 have over there those are dependencies 6274 04:30:02,640 --> 04:30:08,540 as far as the differences we must must 6275 04:30:06,199 --> 04:30:13,260 name our files 6276 04:30:08,540 --> 04:30:14,279 jsx so in create react app we can name 6277 04:30:13,260 --> 04:30:16,979 the file 6278 04:30:14,279 --> 04:30:20,120 JS or jsx 6279 04:30:16,979 --> 04:30:23,640 in wheat the rule is 6280 04:30:20,120 --> 04:30:26,220 jsx so if you'll name your file JS and 6281 04:30:23,640 --> 04:30:28,859 then start setting up the component yes 6282 04:30:26,220 --> 04:30:31,680 wheat will complain 6283 04:30:28,859 --> 04:30:35,880 so that's the first difference we must 6284 04:30:31,680 --> 04:30:37,500 use jsx extension in the read now second 6285 04:30:35,880 --> 04:30:39,720 you'll notice that 6286 04:30:37,500 --> 04:30:42,540 even though the public folder is there 6287 04:30:39,720 --> 04:30:44,939 and the idea is exactly the same 6288 04:30:42,540 --> 04:30:46,020 whatever we place over here is publicly 6289 04:30:44,939 --> 04:30:48,540 available 6290 04:30:46,020 --> 04:30:49,800 there is no index HTML 6291 04:30:48,540 --> 04:30:52,739 so 6292 04:30:49,800 --> 04:30:55,319 in here the index.html is sitting in a 6293 04:30:52,739 --> 04:30:58,080 road again the idea is exactly the same 6294 04:30:55,319 --> 04:31:00,659 notice we still have the div with an 6295 04:30:58,080 --> 04:31:03,720 idea root so this is where our 6296 04:31:00,659 --> 04:31:05,819 application effectively is going to live 6297 04:31:03,720 --> 04:31:10,260 we can still change the title over here 6298 04:31:05,819 --> 04:31:12,359 so for example I can say react project 6299 04:31:10,260 --> 04:31:15,359 and now of course once I save 6300 04:31:12,359 --> 04:31:17,880 this is what I'll have here in a tab so 6301 04:31:15,359 --> 04:31:18,960 that part doesn't change only the 6302 04:31:17,880 --> 04:31:21,180 location 6303 04:31:18,960 --> 04:31:25,199 and once we're done with index.html now 6304 04:31:21,180 --> 04:31:29,399 let's discuss main jsx so unlike create 6305 04:31:25,199 --> 04:31:33,420 react app where we used index.js 6306 04:31:29,399 --> 04:31:35,159 in beat project we need to use Main jsx 6307 04:31:33,420 --> 04:31:37,439 and yes 6308 04:31:35,159 --> 04:31:39,120 as you can see the code is extremely 6309 04:31:37,439 --> 04:31:43,620 similar to 6310 04:31:39,120 --> 04:31:46,979 the index.js in create react app 6311 04:31:43,620 --> 04:31:51,420 so the idea is exactly the same we just 6312 04:31:46,979 --> 04:31:54,659 need to use Main jsx and as you can see 6313 04:31:51,420 --> 04:31:58,020 we're importing the app so this is still 6314 04:31:54,659 --> 04:32:00,060 going to be our root component now the 6315 04:31:58,020 --> 04:32:02,760 only thing that we haven't seen 6316 04:32:00,060 --> 04:32:05,939 is the use State something that we're 6317 04:32:02,760 --> 04:32:07,560 going to cover in a few videos but if 6318 04:32:05,939 --> 04:32:12,359 for example 6319 04:32:07,560 --> 04:32:14,760 I go here and change the jsx you'll see 6320 04:32:12,359 --> 04:32:17,640 that I'll have the same result in the 6321 04:32:14,760 --> 04:32:19,800 browser so let me go here I'll remove 6322 04:32:17,640 --> 04:32:21,720 everything I'm gonna go with heading 1 6323 04:32:19,800 --> 04:32:24,239 and I'll say 6324 04:32:21,720 --> 04:32:27,180 our first 6325 04:32:24,239 --> 04:32:30,120 beat project 6326 04:32:27,180 --> 04:32:31,800 and check it out once we save course 6327 04:32:30,120 --> 04:32:34,439 this is what we'll have on the screen so 6328 04:32:31,800 --> 04:32:36,420 again the idea doesn't change 6329 04:32:34,439 --> 04:32:40,020 we still import 6330 04:32:36,420 --> 04:32:42,659 the logo the same way because notice now 6331 04:32:40,020 --> 04:32:44,939 the assets is sitting in the source so 6332 04:32:42,659 --> 04:32:46,800 this is the public one this is where we 6333 04:32:44,939 --> 04:32:48,600 have all the assets that are publicly 6334 04:32:46,800 --> 04:32:50,819 available however 6335 04:32:48,600 --> 04:32:52,739 within the project when we're going to 6336 04:32:50,819 --> 04:32:55,439 be importing those assets they are 6337 04:32:52,739 --> 04:32:57,239 nicely tucked away in this assets folder 6338 04:32:55,439 --> 04:33:00,420 in the source and this should look 6339 04:32:57,239 --> 04:33:02,880 already familiar since we did Cover the 6340 04:33:00,420 --> 04:33:05,279 es6 modules and all that 6341 04:33:02,880 --> 04:33:07,619 and the rest of it is pretty much the 6342 04:33:05,279 --> 04:33:10,080 same again don't focus on this one we'll 6343 04:33:07,619 --> 04:33:12,719 cover this hook in pretty much few 6344 04:33:10,080 --> 04:33:15,719 videos so don't worry about that we also 6345 04:33:12,719 --> 04:33:18,840 have app CSS where basically we have all 6346 04:33:15,719 --> 04:33:21,600 the Styles and also the index CSS and we 6347 04:33:18,840 --> 04:33:24,719 did Cover differences when we discussed 6348 04:33:21,600 --> 04:33:28,080 the folder structure in create react app 6349 04:33:24,719 --> 04:33:30,600 so I'm not going to repeat that and 6350 04:33:28,080 --> 04:33:33,420 lastly I just want to mention that 6351 04:33:30,600 --> 04:33:34,500 if we want to build the application we 6352 04:33:33,420 --> 04:33:38,580 go with 6353 04:33:34,500 --> 04:33:40,260 and PM run build which again is going to 6354 04:33:38,580 --> 04:33:42,719 create that production or the 6355 04:33:40,260 --> 04:33:46,439 application and let's try it out and PM 6356 04:33:42,719 --> 04:33:49,980 run and build and in this case is going 6357 04:33:46,439 --> 04:33:52,260 to be sitting here in the dist folder so 6358 04:33:49,980 --> 04:33:55,439 unlike create react app which creates 6359 04:33:52,260 --> 04:33:58,199 build folder we have this folder 6360 04:33:55,439 --> 04:34:00,119 same deal we can take this folder drag 6361 04:33:58,199 --> 04:34:01,141 and drop and notify and we're good to go 6362 04:34:00,119 --> 04:34:03,660 and yes 6363 04:34:01,141 --> 04:34:05,520 if you use continuous deployment 6364 04:34:03,660 --> 04:34:08,400 the idea is going to be exactly the same 6365 04:34:05,520 --> 04:34:10,500 and as a signal notify right away picks 6366 04:34:08,400 --> 04:34:13,439 it up so 6367 04:34:10,500 --> 04:34:15,061 if you go with Veet right away it will 6368 04:34:13,439 --> 04:34:17,580 say hey do you want to use the this 6369 04:34:15,061 --> 04:34:21,119 folder and do you want to use this 6370 04:34:17,580 --> 04:34:23,240 command so again 6371 04:34:21,119 --> 04:34:26,580 the general idea is exactly the same 6372 04:34:23,240 --> 04:34:29,459 whether it's the Imports whether it's 6373 04:34:26,580 --> 04:34:32,699 the root component the index.html and 6374 04:34:29,459 --> 04:34:35,879 all that so that is not going to change 6375 04:34:32,699 --> 04:34:38,760 only difference is that this tool is 6376 04:34:35,879 --> 04:34:41,039 much better so our Dev experience is 6377 04:34:38,760 --> 04:34:45,119 just going to be awesome compared to 6378 04:34:41,039 --> 04:34:47,939 create react app awesome and once we're 6379 04:34:45,119 --> 04:34:51,920 comfortable with react fundamentals as 6380 04:34:47,939 --> 04:34:54,480 well as Veet setup but finally discuss 6381 04:34:51,920 --> 04:34:57,480 Advanced react topics 6382 04:34:54,480 --> 04:35:00,439 so in the following videos we're going 6383 04:34:57,480 --> 04:35:03,561 to cover things like react hooks 6384 04:35:00,439 --> 04:35:07,561 conditional rendering fetching data 6385 04:35:03,561 --> 04:35:08,699 forms context API and bunch of other 6386 04:35:07,561 --> 04:35:11,600 cool things 6387 04:35:08,699 --> 04:35:14,459 I also prepared quite a few challenges 6388 04:35:11,600 --> 04:35:17,039 just so we can apply our knowledge right 6389 04:35:14,459 --> 04:35:18,840 away and hopefully that way by the time 6390 04:35:17,039 --> 04:35:21,119 you're done with the section 6391 04:35:18,840 --> 04:35:24,180 you'll be able to apply the concepts 6392 04:35:21,119 --> 04:35:26,580 while you work on your own applications 6393 04:35:24,180 --> 04:35:29,340 so if possible 6394 04:35:26,580 --> 04:35:33,260 try not to rush through the content 6395 04:35:29,340 --> 04:35:36,180 since such approach very rarely leads to 6396 04:35:33,260 --> 04:35:39,420 long-term results in order to follow 6397 04:35:36,180 --> 04:35:43,379 along you mean some assets and they're 6398 04:35:39,420 --> 04:35:45,500 located in the same GitHub repo so my 6399 04:35:43,379 --> 04:35:50,100 profile and then you're looking for 6400 04:35:45,500 --> 04:35:52,320 react course version 3. and even if you 6401 04:35:50,100 --> 04:35:55,920 already downloaded the repo 6402 04:35:52,320 --> 04:35:59,219 I suggest repeating that step since that 6403 04:35:55,920 --> 04:36:02,279 way you'll get the very latest version 6404 04:35:59,219 --> 04:36:06,119 you see as I'm recording I tend to 6405 04:36:02,279 --> 04:36:08,760 update some stuff for example read me 6406 04:36:06,119 --> 04:36:11,039 so once I'm here I'm gonna go for 6407 04:36:08,760 --> 04:36:13,439 download zip option 6408 04:36:11,039 --> 04:36:17,340 then I'm gonna navigate back 6409 04:36:13,439 --> 04:36:20,760 look for downloads crack it open 6410 04:36:17,340 --> 04:36:24,240 up the directory and then we want to 6411 04:36:20,760 --> 04:36:26,699 grab the advanced react folder 6412 04:36:24,240 --> 04:36:29,520 so it's up to you you can work in the 6413 04:36:26,699 --> 04:36:31,740 main folder but I always prefer to work 6414 04:36:29,520 --> 04:36:33,539 in a separate folder then I want to open 6415 04:36:31,740 --> 04:36:36,180 up the text editor 6416 04:36:33,539 --> 04:36:37,980 and right away I'll set it side by side 6417 04:36:36,180 --> 04:36:39,299 with the browser again this is going to 6418 04:36:37,980 --> 04:36:41,939 be the case where 6419 04:36:39,299 --> 04:36:45,480 I'm gonna choose this option 6420 04:36:41,939 --> 04:36:49,439 so let me set them side by side 6421 04:36:45,480 --> 04:36:52,980 and basically you'll notice that it's a 6422 04:36:49,439 --> 04:36:54,719 pretty typical beat project we just have 6423 04:36:52,980 --> 04:36:56,039 here some assets and of course I'll 6424 04:36:54,719 --> 04:36:57,840 discuss them 6425 04:36:56,039 --> 04:37:00,480 in more detail 6426 04:36:57,840 --> 04:37:03,439 in a second for now I simply want you to 6427 04:37:00,480 --> 04:37:06,000 navigate to the readme and in here 6428 04:37:03,439 --> 04:37:09,719 effectively you're looking for these 6429 04:37:06,000 --> 04:37:12,240 commands now if you can run them in your 6430 04:37:09,719 --> 04:37:15,119 operating system in such manner awesome 6431 04:37:12,240 --> 04:37:16,920 if not then just run them separately so 6432 04:37:15,119 --> 04:37:19,680 I want to open up the integrated 6433 04:37:16,920 --> 04:37:21,959 terminal I'll make this one bigger and 6434 04:37:19,680 --> 04:37:24,180 then we're gonna go with npm install 6435 04:37:21,959 --> 04:37:25,740 and you know what I'll just run them 6436 04:37:24,180 --> 04:37:27,840 separately 6437 04:37:25,740 --> 04:37:29,100 just so you can see that everything 6438 04:37:27,840 --> 04:37:30,959 works so 6439 04:37:29,100 --> 04:37:32,520 we have npm installed so we installed 6440 04:37:30,959 --> 04:37:36,119 dependencies and then we want to go with 6441 04:37:32,520 --> 04:37:37,980 npm run and Dev 6442 04:37:36,119 --> 04:37:42,719 and essentially this again is going to 6443 04:37:37,980 --> 04:37:46,199 open up the project on localhost 5173 6444 04:37:42,719 --> 04:37:49,760 and yep we should see a heading 2 with 6445 04:37:46,199 --> 04:37:52,439 Advanced react and rest of the info 6446 04:37:49,760 --> 04:37:55,320 essentially is going to be located in 6447 04:37:52,439 --> 04:37:57,359 tutorial marking on file so this is just 6448 04:37:55,320 --> 04:37:59,939 a general readme how we can spin up the 6449 04:37:57,359 --> 04:38:03,359 project and all that in tutorial you'll 6450 04:37:59,939 --> 04:38:05,900 find effectively all of the info we're 6451 04:38:03,359 --> 04:38:09,000 going to cover in the following videos 6452 04:38:05,900 --> 04:38:11,459 and as far as our workflow like I 6453 04:38:09,000 --> 04:38:14,699 mentioned before this is a traditional 6454 04:38:11,459 --> 04:38:17,340 Veet application I just removed some 6455 04:38:14,699 --> 04:38:20,219 boilerplate then provided some assets 6456 04:38:17,340 --> 04:38:23,160 like CSS and data and all that just so 6457 04:38:20,219 --> 04:38:26,340 we can work on more meaningful examples 6458 04:38:23,160 --> 04:38:29,100 and for that I do need some assets I 6459 04:38:26,340 --> 04:38:31,619 also removed the strict mode just so we 6460 04:38:29,100 --> 04:38:33,680 don't have a bunch of logs so remember 6461 04:38:31,619 --> 04:38:37,619 strict mode was adding that extra login 6462 04:38:33,680 --> 04:38:39,779 well working and essentially exploring 6463 04:38:37,619 --> 04:38:42,359 stuff I don't want to have too many 6464 04:38:39,779 --> 04:38:45,840 locks so if you'll take a look at the 6465 04:38:42,359 --> 04:38:47,580 main jsx you'll see that essentially 6466 04:38:45,840 --> 04:38:50,760 everything is the same 6467 04:38:47,580 --> 04:38:53,760 there's just no strict mode in the index 6468 04:38:50,760 --> 04:38:56,400 CSS you'll find all of the CSS we're 6469 04:38:53,760 --> 04:38:57,779 going to use data this is going to be 6470 04:38:56,400 --> 04:39:01,500 some data that we're going to use in 6471 04:38:57,779 --> 04:39:03,719 examples app jsx so this is going to be 6472 04:39:01,500 --> 04:39:07,020 our root component 6473 04:39:03,719 --> 04:39:10,619 then we also have some assets here 6474 04:39:07,020 --> 04:39:13,619 again something that we're going to use 6475 04:39:10,619 --> 04:39:15,719 during the examples and there's also 6476 04:39:13,619 --> 04:39:18,000 public which by the way we're not going 6477 04:39:15,719 --> 04:39:21,359 to use and of course there's no modules 6478 04:39:18,000 --> 04:39:23,459 which are our dependencies now if we 6479 04:39:21,359 --> 04:39:25,859 again navigate back to tutorial you'll 6480 04:39:23,459 --> 04:39:27,061 see that basically we have this tutorial 6481 04:39:25,859 --> 04:39:29,820 directory 6482 04:39:27,061 --> 04:39:32,459 and in here we have a bunch of folders 6483 04:39:29,820 --> 04:39:35,340 so it starts with your state all the way 6484 04:39:32,459 --> 04:39:37,920 to userducer and 6485 04:39:35,340 --> 04:39:40,260 I'm actually working on more topics 6486 04:39:37,920 --> 04:39:43,980 right now so probably by the time you 6487 04:39:40,260 --> 04:39:46,200 install this you'll see more folders and 6488 04:39:43,980 --> 04:39:49,680 essentially the idea is following each 6489 04:39:46,200 --> 04:39:53,340 folder is going to have final and a star 6490 04:39:49,680 --> 04:39:56,100 so we will do a work in a star folder so 6491 04:39:53,340 --> 04:39:58,200 there you'll find some files and it 6492 04:39:56,100 --> 04:40:00,540 really depends sometimes there's going 6493 04:39:58,200 --> 04:40:02,218 to be just a structure for component and 6494 04:40:00,540 --> 04:40:03,958 sometimes I'll provide already some 6495 04:40:02,218 --> 04:40:06,060 stuff so yes 6496 04:40:03,958 --> 04:40:08,218 sometimes we'll code pretty much 6497 04:40:06,060 --> 04:40:10,980 everything from scratch but if we 6498 04:40:08,218 --> 04:40:13,500 already have covered that topic and if I 6499 04:40:10,980 --> 04:40:16,140 just want to provide some extra info on 6500 04:40:13,500 --> 04:40:20,100 that then yes you'll find already some 6501 04:40:16,140 --> 04:40:22,200 code in the star and of course 6502 04:40:20,100 --> 04:40:25,320 if this doesn't make any sense right now 6503 04:40:22,200 --> 04:40:27,900 you'll see as we start working on the 6504 04:40:25,320 --> 04:40:31,140 examples and then in the final 6505 04:40:27,900 --> 04:40:33,260 you can find the complete source code so 6506 04:40:31,140 --> 04:40:36,660 this one you can use for two examples 6507 04:40:33,260 --> 04:40:38,218 let's say if you want to compare your 6508 04:40:36,660 --> 04:40:40,620 code to mine 6509 04:40:38,218 --> 04:40:42,780 or you simply want to see how the 6510 04:40:40,620 --> 04:40:45,298 feature looks like 6511 04:40:42,780 --> 04:40:47,400 once it's done since again I will be 6512 04:40:45,298 --> 04:40:48,718 setting up some challenges so if you 6513 04:40:47,400 --> 04:40:51,840 want to take a peek 6514 04:40:48,718 --> 04:40:54,480 out effectively runs in the browser then 6515 04:40:51,840 --> 04:40:57,120 just set up the file and the way we set 6516 04:40:54,480 --> 04:41:01,200 up the file is following 6517 04:40:57,120 --> 04:41:03,360 so in the app.js essentially we'll just 6518 04:41:01,200 --> 04:41:06,540 import that file 6519 04:41:03,360 --> 04:41:09,000 and then we'll render it over here again 6520 04:41:06,540 --> 04:41:11,878 you're gonna render both you can render 6521 04:41:09,000 --> 04:41:14,040 star and the final this is really up to 6522 04:41:11,878 --> 04:41:17,100 you but you'll definitely need to get 6523 04:41:14,040 --> 04:41:19,740 that starter so pretty much every 6524 04:41:17,100 --> 04:41:22,080 lecture is going to have its own star so 6525 04:41:19,740 --> 04:41:27,540 you'll have to import that and set it up 6526 04:41:22,080 --> 04:41:29,940 over here in the app jsx and then if you 6527 04:41:27,540 --> 04:41:32,040 want to see the final folder again it 6528 04:41:29,940 --> 04:41:34,378 could be any file just navigate to 6529 04:41:32,040 --> 04:41:37,440 tutorial in this case you're looking for 6530 04:41:34,378 --> 04:41:38,340 the final one and then whatever file you 6531 04:41:37,440 --> 04:41:41,160 want 6532 04:41:38,340 --> 04:41:43,378 and the reason why you have here this 6533 04:41:41,160 --> 04:41:45,540 container is simply for some basic 6534 04:41:43,378 --> 04:41:46,860 styling so notice how it's nicely 6535 04:41:45,540 --> 04:41:49,260 sitting in the center it has some 6536 04:41:46,860 --> 04:41:51,718 padding and all that technically if you 6537 04:41:49,260 --> 04:41:53,940 don't want to you can just remove it and 6538 04:41:51,718 --> 04:41:57,180 everything's still going to work and 6539 04:41:53,940 --> 04:42:01,260 just to Showcase let me just navigate 6540 04:41:57,180 --> 04:42:04,560 to let's say app jsx 6541 04:42:01,260 --> 04:42:06,840 let me move up now the component name is 6542 04:42:04,560 --> 04:42:09,120 really up to you I'm just gonna go with 6543 04:42:06,840 --> 04:42:10,860 star and final because it just makes a 6544 04:42:09,120 --> 04:42:13,020 little bit more sense so I'm gonna go 6545 04:42:10,860 --> 04:42:14,780 here with import starter so that's going 6546 04:42:13,020 --> 04:42:18,798 to be my component name and then from 6547 04:42:14,780 --> 04:42:21,900 and then for now let me just navigate to 6548 04:42:18,798 --> 04:42:25,920 tutorial then 6549 04:42:21,900 --> 04:42:28,040 just as a example I'm gonna go to let's 6550 04:42:25,920 --> 04:42:30,958 say use ref so that's going to be seven 6551 04:42:28,040 --> 04:42:33,540 and then starter like I said and in 6552 04:42:30,958 --> 04:42:35,940 there we only have one file so we grab 6553 04:42:33,540 --> 04:42:37,320 this one and for now let me just remove 6554 04:42:35,940 --> 04:42:39,958 that heading too 6555 04:42:37,320 --> 04:42:43,138 then let's go with starter 6556 04:42:39,958 --> 04:42:46,020 and then let me close the component so 6557 04:42:43,138 --> 04:42:48,600 once I save notice I'll have some code 6558 04:42:46,020 --> 04:42:51,480 already so essentially the idea is that 6559 04:42:48,600 --> 04:42:54,780 we're gonna go to that file so that is 6560 04:42:51,480 --> 04:42:57,900 zero seven user blah blah blah blah blah 6561 04:42:54,780 --> 04:43:00,780 your starter essentially this is where 6562 04:42:57,900 --> 04:43:02,820 we'll start setting up the logic and the 6563 04:43:00,780 --> 04:43:05,280 same is going to work for all of the 6564 04:43:02,820 --> 04:43:08,100 files and like I said if you want to see 6565 04:43:05,280 --> 04:43:10,560 let's say how the complete code looks 6566 04:43:08,100 --> 04:43:12,718 like you can simply take and you know 6567 04:43:10,560 --> 04:43:15,958 what let me close the sidebar I'll copy 6568 04:43:12,718 --> 04:43:17,760 and paste over here 6569 04:43:15,958 --> 04:43:19,680 and once I'm done with that I'm just 6570 04:43:17,760 --> 04:43:21,840 going to rename since we cannot use the 6571 04:43:19,680 --> 04:43:25,020 same name correct so I'm going to go 6572 04:43:21,840 --> 04:43:26,520 here with final and let's also look in 6573 04:43:25,020 --> 04:43:29,280 the final folder 6574 04:43:26,520 --> 04:43:31,080 so let's change things around and let's 6575 04:43:29,280 --> 04:43:32,940 look in a different folder just so you 6576 04:43:31,080 --> 04:43:35,760 don't think that I'm messing with you 6577 04:43:32,940 --> 04:43:38,458 I'm just gonna go to I don't know maybe 6578 04:43:35,760 --> 04:43:40,620 use effect or you know what let me go to 6579 04:43:38,458 --> 04:43:42,958 your state I'll show you a nice list so 6580 04:43:40,620 --> 04:43:44,400 let me go to use State again we're 6581 04:43:42,958 --> 04:43:47,280 looking for final 6582 04:43:44,400 --> 04:43:50,638 we're looking for use array so that's my 6583 04:43:47,280 --> 04:43:52,860 final and then as an example 6584 04:43:50,638 --> 04:43:55,260 I'll save it here and the notice will 6585 04:43:52,860 --> 04:43:56,760 have a list and we can remove some items 6586 04:43:55,260 --> 04:43:59,400 we can clear them and all that kind of 6587 04:43:56,760 --> 04:44:03,000 stuff so hopefully this is clear 6588 04:43:59,400 --> 04:44:06,480 effectively before each video I'm going 6589 04:44:03,000 --> 04:44:09,120 to show you which file we want to import 6590 04:44:06,480 --> 04:44:12,600 so let's say the first one is tutorial 6591 04:44:09,120 --> 04:44:15,540 use State star and then error example so 6592 04:44:12,600 --> 04:44:18,298 we want to import that in the app 6593 04:44:15,540 --> 04:44:20,340 jsx we want to render it and then we 6594 04:44:18,298 --> 04:44:23,100 want to navigate to that file and start 6595 04:44:20,340 --> 04:44:25,440 setting up the code together and again 6596 04:44:23,100 --> 04:44:26,458 if you want to look at any of the final 6597 04:44:25,440 --> 04:44:28,920 ones 6598 04:44:26,458 --> 04:44:32,180 just import the file and render it 6599 04:44:28,920 --> 04:44:34,680 somewhere here in the app jsxn 6600 04:44:32,180 --> 04:44:35,700 effectively my workflow is going to be 6601 04:44:34,680 --> 04:44:38,160 following 6602 04:44:35,700 --> 04:44:40,920 I'll simply 6603 04:44:38,160 --> 04:44:43,378 keep changing the file name so let's say 6604 04:44:40,920 --> 04:44:45,480 if I have this tutorial right now I just 6605 04:44:43,378 --> 04:44:47,520 go with forward slash your state and 6606 04:44:45,480 --> 04:44:49,138 again I know that all my examples are 6607 04:44:47,520 --> 04:44:51,180 going to start with starter and then I'm 6608 04:44:49,138 --> 04:44:53,638 just going to be changing files so 6609 04:44:51,180 --> 04:44:55,920 pretty much you can leave the setup here 6610 04:44:53,638 --> 04:44:57,600 as it is you're just changing the path 6611 04:44:55,920 --> 04:45:00,240 that's it that's all you have to do 6612 04:44:57,600 --> 04:45:02,160 unless you want to take a look at the 6613 04:45:00,240 --> 04:45:04,560 final one then of course I already 6614 04:45:02,160 --> 04:45:06,958 showed you what to do so let me go to 6615 04:45:04,560 --> 04:45:09,060 the arrow example that's the one where 6616 04:45:06,958 --> 04:45:11,040 we'll start and notice in this one we 6617 04:45:09,060 --> 04:45:13,620 just have a heading two so in here we 6618 04:45:11,040 --> 04:45:16,200 basically will code everything from 6619 04:45:13,620 --> 04:45:17,160 scratch now a few more things I want to 6620 04:45:16,200 --> 04:45:19,378 mention 6621 04:45:17,160 --> 04:45:22,138 there will be some challenges like I 6622 04:45:19,378 --> 04:45:24,060 said just so we can right away apply our 6623 04:45:22,138 --> 04:45:26,160 knowledge so if you keep scrolling 6624 04:45:24,060 --> 04:45:28,740 obviously over here you'll have the 6625 04:45:26,160 --> 04:45:30,718 first setup challenge so 6626 04:45:28,740 --> 04:45:32,940 in the beginning of each video where we 6627 04:45:30,718 --> 04:45:34,200 have the challenge effectively I'll just 6628 04:45:32,940 --> 04:45:37,320 read it to you 6629 04:45:34,200 --> 04:45:38,580 I'll try to explain as best as I can I 6630 04:45:37,320 --> 04:45:41,940 might show you 6631 04:45:38,580 --> 04:45:43,200 the final one just so you can see how it 6632 04:45:41,940 --> 04:45:45,138 looks like again I'm not going to show 6633 04:45:43,200 --> 04:45:48,240 you the code but I'm going to show you 6634 04:45:45,138 --> 04:45:51,718 how effectively the feature is supposed 6635 04:45:48,240 --> 04:45:55,020 to act and if you want you can work on a 6636 04:45:51,718 --> 04:45:58,920 challenge now some of them are purposely 6637 04:45:55,020 --> 04:46:00,420 essentially made to fail so you'll work 6638 04:45:58,920 --> 04:46:02,520 on a challenge and I'll right away tell 6639 04:46:00,420 --> 04:46:05,400 you hey you most likely hit this bug 6640 04:46:02,520 --> 04:46:08,520 because I want you to have a 6641 04:46:05,400 --> 04:46:11,160 Real World Experience what triggers the 6642 04:46:08,520 --> 04:46:13,260 bug and that way it's going to be easier 6643 04:46:11,160 --> 04:46:17,340 to remember 6644 04:46:13,260 --> 04:46:19,020 the possible solutions as well and one 6645 04:46:17,340 --> 04:46:21,240 last thing that I want to mention yes in 6646 04:46:19,020 --> 04:46:24,000 the beginning our examples are going to 6647 04:46:21,240 --> 04:46:25,620 be with numbers and buttons 6648 04:46:24,000 --> 04:46:27,660 just because we need to start with 6649 04:46:25,620 --> 04:46:30,900 something yes of course 6650 04:46:27,660 --> 04:46:34,100 as we progress with the topics 6651 04:46:30,900 --> 04:46:36,958 I purposely chose already more complex 6652 04:46:34,100 --> 04:46:39,298 examples just so it's not all the time 6653 04:46:36,958 --> 04:46:42,840 numbers and buttons but the first few 6654 04:46:39,298 --> 04:46:46,740 videos yes we'll have to utilize numbers 6655 04:46:42,840 --> 04:46:49,798 and buttons just so we can see the 6656 04:46:46,740 --> 04:46:53,580 workings of react hopefully everything 6657 04:46:49,798 --> 04:46:56,040 is clear if not utilize the course q a 6658 04:46:53,580 --> 04:46:58,740 and in the next video we're going to 6659 04:46:56,040 --> 04:47:00,420 start working on our first example the 6660 04:46:58,740 --> 04:47:04,920 error example 6661 04:47:00,420 --> 04:47:07,980 okay and let's start by taking a look 6662 04:47:04,920 --> 04:47:10,020 why we need a use State hook in the 6663 04:47:07,980 --> 04:47:10,860 first place which by the way we're going 6664 04:47:10,020 --> 04:47:13,680 to 6665 04:47:10,860 --> 04:47:15,540 cover in the next lecture 6666 04:47:13,680 --> 04:47:18,298 and essentially we'll run away start 6667 04:47:15,540 --> 04:47:21,780 with the challenge so I want you to 6668 04:47:18,298 --> 04:47:25,740 navigate to the file basically a 6669 04:47:21,780 --> 04:47:29,458 component and create a count variable 6670 04:47:25,740 --> 04:47:31,860 display the value in jsx add a button 6671 04:47:29,458 --> 04:47:33,780 and try to increase the value 6672 04:47:31,860 --> 04:47:36,958 now I can tell you right away that it's 6673 04:47:33,780 --> 04:47:39,480 not going to work basically you won't 6674 04:47:36,958 --> 04:47:40,798 see the latest changes in the browser so 6675 04:47:39,480 --> 04:47:44,638 I don't suggest 6676 04:47:40,798 --> 04:47:47,638 trying like 10 different options just 6677 04:47:44,638 --> 04:47:50,280 basically try the most obvious one and 6678 04:47:47,638 --> 04:47:52,920 then resume the video and yes 6679 04:47:50,280 --> 04:47:55,260 I'm not going to be extremely annoying 6680 04:47:52,920 --> 04:47:57,060 and I'm just gonna say this once if you 6681 04:47:55,260 --> 04:48:00,120 want to work on those challenges just 6682 04:47:57,060 --> 04:48:02,520 pause the video try to set it up and 6683 04:48:00,120 --> 04:48:04,080 then resume once you're ready to compare 6684 04:48:02,520 --> 04:48:07,200 the results 6685 04:48:04,080 --> 04:48:10,080 so let me just go to app jsx 6686 04:48:07,200 --> 04:48:12,120 let me check that I'm getting the right 6687 04:48:10,080 --> 04:48:15,298 component again we're looking in the 6688 04:48:12,120 --> 04:48:19,980 tutorial we'll start with zero on your 6689 04:48:15,298 --> 04:48:23,040 state star and then the arrow example so 6690 04:48:19,980 --> 04:48:24,718 let me navigate over here close and 6691 04:48:23,040 --> 04:48:26,878 somewhere here I'm gonna create my 6692 04:48:24,718 --> 04:48:30,540 account variable so I'm going to go let 6693 04:48:26,878 --> 04:48:32,280 count is equal to zero beautiful 6694 04:48:30,540 --> 04:48:34,860 then I want to display that value 6695 04:48:32,280 --> 04:48:36,718 somewhere and since at the moment I have 6696 04:48:34,860 --> 04:48:39,000 heading 2 and we know that we cannot 6697 04:48:36,718 --> 04:48:40,940 return adjacent elements I'm gonna turn 6698 04:48:39,000 --> 04:48:44,400 this into a div 6699 04:48:40,940 --> 04:48:47,700 then we're going to set up a heading 2 6700 04:48:44,400 --> 04:48:50,160 with the actual value so here I want to 6701 04:48:47,700 --> 04:48:52,260 display the count let me save and I see 6702 04:48:50,160 --> 04:48:54,480 the zero so that concludes this is 6703 04:48:52,260 --> 04:48:55,860 already a good start then we want to set 6704 04:48:54,480 --> 04:48:58,620 up the button 6705 04:48:55,860 --> 04:49:01,138 right away type is equal to a button 6706 04:48:58,620 --> 04:49:02,340 then we'll set up the unclick if you 6707 04:49:01,138 --> 04:49:04,798 want the arrow function you can 6708 04:49:02,340 --> 04:49:07,500 definitely do so in my case I'll set up 6709 04:49:04,798 --> 04:49:09,000 the reference and I'll just increase or 6710 04:49:07,500 --> 04:49:12,000 increment whatever 6711 04:49:09,000 --> 04:49:14,760 your heart desires and now let's set up 6712 04:49:12,000 --> 04:49:18,660 that function let's go over here let's 6713 04:49:14,760 --> 04:49:20,878 say const handle click 6714 04:49:18,660 --> 04:49:23,340 basically in here you want to go with 6715 04:49:20,878 --> 04:49:26,218 count count is equal 6716 04:49:23,340 --> 04:49:28,680 to count plus one correct and let me 6717 04:49:26,218 --> 04:49:30,180 right away even console log and you'll 6718 04:49:28,680 --> 04:49:33,120 see that technically everything works 6719 04:49:30,180 --> 04:49:35,700 but we won't say anything e in the 6720 04:49:33,120 --> 04:49:38,400 browser sorry for the spoiler so let's 6721 04:49:35,700 --> 04:49:39,958 go here let's save we have the button by 6722 04:49:38,400 --> 04:49:42,060 the way if you want to add 6723 04:49:39,958 --> 04:49:43,680 a little bit of styles you can go here 6724 04:49:42,060 --> 04:49:45,900 with class name 6725 04:49:43,680 --> 04:49:47,878 and you can go with button again this is 6726 04:49:45,900 --> 04:49:49,920 coming from Global stuff 6727 04:49:47,878 --> 04:49:52,260 and check it out everything is awesome 6728 04:49:49,920 --> 04:49:53,580 but as I'm clicking the button nothing 6729 04:49:52,260 --> 04:49:56,160 is happening 6730 04:49:53,580 --> 04:49:58,980 and actually if I take a look at the 6731 04:49:56,160 --> 04:50:01,440 console I can see that my logic works 6732 04:49:58,980 --> 04:50:03,060 so this is technically correct I am 6733 04:50:01,440 --> 04:50:05,160 updating this value 6734 04:50:03,060 --> 04:50:05,940 but nothing is happening why is that 6735 04:50:05,160 --> 04:50:08,400 well 6736 04:50:05,940 --> 04:50:09,660 because we're not triggering the 6737 04:50:08,400 --> 04:50:11,580 re-render 6738 04:50:09,660 --> 04:50:13,080 something we're going to do with use 6739 04:50:11,580 --> 04:50:14,638 statehook 6740 04:50:13,080 --> 04:50:16,620 so even though 6741 04:50:14,638 --> 04:50:19,560 this logic is awesome in vanilla 6742 04:50:16,620 --> 04:50:22,320 jazzland it's not going to work in react 6743 04:50:19,560 --> 04:50:24,958 if we want to see the latest changes 6744 04:50:22,320 --> 04:50:27,480 displayed in the browser in react we 6745 04:50:24,958 --> 04:50:31,560 need to set up a state value 6746 04:50:27,480 --> 04:50:33,900 and we need to trigger a re-render and 6747 04:50:31,560 --> 04:50:37,440 in the next video we're going to cover 6748 04:50:33,900 --> 04:50:41,520 how we can do that using use State hook 6749 04:50:37,440 --> 04:50:44,580 all right so how we can achieve our 6750 04:50:41,520 --> 04:50:47,520 previous functionality in react well we 6751 04:50:44,580 --> 04:50:50,580 need to use a hook basically a function 6752 04:50:47,520 --> 04:50:53,100 that react provides and the name is huge 6753 04:50:50,580 --> 04:50:55,620 State as a quick sign out during this 6754 04:50:53,100 --> 04:50:59,580 video will heavily rely on 6755 04:50:55,620 --> 04:51:02,580 the structuring array principle and if 6756 04:50:59,580 --> 04:51:05,160 you need to jog your memory or if you're 6757 04:51:02,580 --> 04:51:07,560 not familiar at all with this concept I 6758 04:51:05,160 --> 04:51:10,798 cover this in great detail in this 6759 04:51:07,560 --> 04:51:13,620 JavaScript nugget video and essentially 6760 04:51:10,798 --> 04:51:15,540 I just want to double check that I'm 6761 04:51:13,620 --> 04:51:18,240 importing correct components so notice 6762 04:51:15,540 --> 04:51:20,100 now I switch to use State Basics so 6763 04:51:18,240 --> 04:51:22,340 that's the second file and if we 6764 04:51:20,100 --> 04:51:25,620 navigate here this is what we will see 6765 04:51:22,340 --> 04:51:29,280 and effectively we want to import from 6766 04:51:25,620 --> 04:51:31,680 react the use statehawk now we have 6767 04:51:29,280 --> 04:51:34,980 multiple flavors we can do it this way 6768 04:51:31,680 --> 04:51:38,638 so notice this is going to be a named 6769 04:51:34,980 --> 04:51:40,920 import so unlike let's say react this is 6770 04:51:38,638 --> 04:51:43,080 written like this now we're actually 6771 04:51:40,920 --> 04:51:46,440 looking for the curlies and we go here 6772 04:51:43,080 --> 04:51:48,718 with euge State and this is coming from 6773 04:51:46,440 --> 04:51:51,180 react now I believe 6774 04:51:48,718 --> 04:51:53,280 vs code is going to try to help us so if 6775 04:51:51,180 --> 04:51:55,020 you type here you state notice you'll 6776 04:51:53,280 --> 04:51:57,420 right away get the import now 6777 04:51:55,020 --> 04:51:59,280 alternatively whatever logic we're going 6778 04:51:57,420 --> 04:52:01,620 to set up in a component you can also 6779 04:51:59,280 --> 04:52:04,138 type it this way you can just import 6780 04:52:01,620 --> 04:52:07,080 react and then go with react Dot and 6781 04:52:04,138 --> 04:52:08,700 then use State just as I say no this is 6782 04:52:07,080 --> 04:52:11,218 also going to work 6783 04:52:08,700 --> 04:52:14,280 so once we have the use State hook like 6784 04:52:11,218 --> 04:52:16,440 I said it's a function we want to invoke 6785 04:52:14,280 --> 04:52:19,138 that function and we want to pass in the 6786 04:52:16,440 --> 04:52:21,120 default value and let's start from the 6787 04:52:19,138 --> 04:52:23,700 very scratch let's just invoke it in a 6788 04:52:21,120 --> 04:52:25,080 log just so you can see that whatever 6789 04:52:23,700 --> 04:52:28,378 I'm telling 6790 04:52:25,080 --> 04:52:30,540 actually is true so let me save it let 6791 04:52:28,378 --> 04:52:32,700 me invoke it and you know what I'll 6792 04:52:30,540 --> 04:52:34,798 clean the console and I'll just refresh 6793 04:52:32,700 --> 04:52:36,660 and notice over here 6794 04:52:34,798 --> 04:52:37,378 like I said we have function we invoke 6795 04:52:36,660 --> 04:52:39,000 it 6796 04:52:37,378 --> 04:52:40,020 we'll need to pass here the default 6797 04:52:39,000 --> 04:52:42,420 value 6798 04:52:40,020 --> 04:52:45,360 and we get back two things we get an 6799 04:52:42,420 --> 04:52:47,760 array with undefined and we also have 6800 04:52:45,360 --> 04:52:50,240 the function so this is going to be our 6801 04:52:47,760 --> 04:52:52,560 state value which we're going to change 6802 04:52:50,240 --> 04:52:55,138 with the second argument with the 6803 04:52:52,560 --> 04:52:58,620 function and this also is going to be 6804 04:52:55,138 --> 04:53:01,680 preserved between the renders and don't 6805 04:52:58,620 --> 04:53:04,440 worry if the render re-render and all 6806 04:53:01,680 --> 04:53:07,620 that sounds kind of fuzzy actually we'll 6807 04:53:04,440 --> 04:53:09,958 spend the next video discussing them in 6808 04:53:07,620 --> 04:53:11,580 Greater detail so we have used statehook 6809 04:53:09,958 --> 04:53:13,200 this is where we need to pass in that 6810 04:53:11,580 --> 04:53:15,180 default value that's why we have right 6811 04:53:13,200 --> 04:53:17,760 now undefined so if I'm going to type 6812 04:53:15,180 --> 04:53:20,580 here Bob Yep this is going to be my 6813 04:53:17,760 --> 04:53:22,200 value if I'm going to type here one then 6814 04:53:20,580 --> 04:53:24,480 of course you can already guess that 6815 04:53:22,200 --> 04:53:26,700 I'll have the value over here one so 6816 04:53:24,480 --> 04:53:28,980 this is going to be my default value and 6817 04:53:26,700 --> 04:53:31,440 of course you can pass here array the 6818 04:53:28,980 --> 04:53:33,600 object and all that kind of stuff so 6819 04:53:31,440 --> 04:53:35,638 hopefully that's clear now let's discuss 6820 04:53:33,600 --> 04:53:37,620 how we can access these values now the 6821 04:53:35,638 --> 04:53:40,200 fastest way is to use array the 6822 04:53:37,620 --> 04:53:42,000 structuring but just to 6823 04:53:40,200 --> 04:53:44,878 kind of proved the point that I'm not 6824 04:53:42,000 --> 04:53:47,340 making this up I'll access them one by 6825 04:53:44,878 --> 04:53:51,000 one so let me comment this one out let 6826 04:53:47,340 --> 04:53:54,060 me just copy and paste and let me assign 6827 04:53:51,000 --> 04:53:55,620 to some kind of value so in here I'm 6828 04:53:54,060 --> 04:53:57,660 going to say that this is equal to hello 6829 04:53:55,620 --> 04:54:00,180 again we can change these values as we 6830 04:53:57,660 --> 04:54:01,620 want there's really no difference and 6831 04:54:00,180 --> 04:54:03,840 you know what 6832 04:54:01,620 --> 04:54:07,378 I'll actually remove this log I don't 6833 04:54:03,840 --> 04:54:09,900 need it anymore I'll log the value so 6834 04:54:07,378 --> 04:54:12,180 let me go here with const and this is 6835 04:54:09,900 --> 04:54:14,400 equal to a value and then since we're 6836 04:54:12,180 --> 04:54:16,680 getting back the array we can do it this 6837 04:54:14,400 --> 04:54:19,138 way where I'm accessing the first item 6838 04:54:16,680 --> 04:54:20,520 hopefully that is clear so if I go here 6839 04:54:19,138 --> 04:54:22,620 with value 6840 04:54:20,520 --> 04:54:25,200 you'll see over here that it's actually 6841 04:54:22,620 --> 04:54:27,360 hello so whatever default value that I'm 6842 04:54:25,200 --> 04:54:30,240 passing and if I'll copy and paste in 6843 04:54:27,360 --> 04:54:32,218 here and if I'll say func which actually 6844 04:54:30,240 --> 04:54:33,298 stands for function at me copying paste 6845 04:54:32,218 --> 04:54:36,180 here as well 6846 04:54:33,298 --> 04:54:37,200 and let's just log the function so two 6847 04:54:36,180 --> 04:54:40,320 things 6848 04:54:37,200 --> 04:54:41,878 the default value and the function that 6849 04:54:40,320 --> 04:54:44,580 controls that value 6850 04:54:41,878 --> 04:54:46,920 now y array the structuring is 6851 04:54:44,580 --> 04:54:50,280 technically better well because we can 6852 04:54:46,920 --> 04:54:52,378 use a one-liner so let me 6853 04:54:50,280 --> 04:54:54,780 common this one out 6854 04:54:52,378 --> 04:54:58,560 and let's keep on moving and notice over 6855 04:54:54,780 --> 04:55:00,840 here I can simply go with const then I'm 6856 04:54:58,560 --> 04:55:02,760 destructuring right away out items so in 6857 04:55:00,840 --> 04:55:03,958 my case I'm going to go with count and 6858 04:55:02,760 --> 04:55:07,500 set count 6859 04:55:03,958 --> 04:55:10,500 now as far as naming it is a convention 6860 04:55:07,500 --> 04:55:13,080 to go with set count basically whatever 6861 04:55:10,500 --> 04:55:16,080 is the variable name so if you have name 6862 04:55:13,080 --> 04:55:18,240 here then convention is to go set name 6863 04:55:16,080 --> 04:55:20,580 but you don't have to and I'll talk 6864 04:55:18,240 --> 04:55:22,860 about some other conventions as well for 6865 04:55:20,580 --> 04:55:25,260 example for booleans and all that then 6866 04:55:22,860 --> 04:55:27,360 we want to go with use State and we want 6867 04:55:25,260 --> 04:55:29,218 to pass in that value like I said we can 6868 04:55:27,360 --> 04:55:31,080 pass in the string we can pass in Array 6869 04:55:29,218 --> 04:55:32,400 we can pass in number whatever you want 6870 04:55:31,080 --> 04:55:34,440 to pass in this is going to be the 6871 04:55:32,400 --> 04:55:36,958 default value and this is going to be 6872 04:55:34,440 --> 04:55:40,620 the function that controls it again the 6873 04:55:36,958 --> 04:55:41,580 reason why we went the long route is 6874 04:55:40,620 --> 04:55:43,980 because 6875 04:55:41,580 --> 04:55:45,840 maybe some people are not familiar with 6876 04:55:43,980 --> 04:55:47,700 the syntax which is totally okay 6877 04:55:45,840 --> 04:55:49,798 the first time I saw this so I was also 6878 04:55:47,700 --> 04:55:51,900 like hey uh what's happening over here 6879 04:55:49,798 --> 04:55:53,760 so basically we're just destructuring 6880 04:55:51,900 --> 04:55:56,218 these values if you don't like the 6881 04:55:53,760 --> 04:55:58,740 syntax you can always use this one 6882 04:55:56,218 --> 04:56:01,020 but my guess is you'll stick with this 6883 04:55:58,740 --> 04:56:03,298 one and then in here let's pass in that 6884 04:56:01,020 --> 04:56:06,000 initial value in our case what is that 6885 04:56:03,298 --> 04:56:07,820 well we want to go with zero correct 6886 04:56:06,000 --> 04:56:11,100 so this is where we're going to start 6887 04:56:07,820 --> 04:56:14,100 then let's quickly again set up that 6888 04:56:11,100 --> 04:56:17,340 button I know that this is a little bit 6889 04:56:14,100 --> 04:56:19,138 of repetition but since we just started 6890 04:56:17,340 --> 04:56:20,760 working in jsx I think it's going to be 6891 04:56:19,138 --> 04:56:22,980 a good practice so let's go here with 6892 04:56:20,760 --> 04:56:26,520 heading four then we're gonna go with 6893 04:56:22,980 --> 04:56:29,700 you clicked and then let's grab the 6894 04:56:26,520 --> 04:56:31,620 actual count so notice not accessing 6895 04:56:29,700 --> 04:56:33,718 some random variable I'm actually 6896 04:56:31,620 --> 04:56:35,700 looking for the count one 6897 04:56:33,718 --> 04:56:39,660 so the one that I'm setting up with you 6898 04:56:35,700 --> 04:56:42,180 state and I'll just say times here then 6899 04:56:39,660 --> 04:56:46,080 we want to set up our button 6900 04:56:42,180 --> 04:56:48,180 so the same deal type is equal to button 6901 04:56:46,080 --> 04:56:49,020 then class name just so we have a little 6902 04:56:48,180 --> 04:56:52,020 bit of 6903 04:56:49,020 --> 04:56:54,240 CSS and now let's also again set up that 6904 04:56:52,020 --> 04:56:55,260 on click and we want to set up the 6905 04:56:54,240 --> 04:56:58,500 function 6906 04:56:55,260 --> 04:57:01,680 so in here we want to go with const and 6907 04:56:58,500 --> 04:57:03,480 let's call this handle click 6908 04:57:01,680 --> 04:57:06,540 and inside of that function what do we 6909 04:57:03,480 --> 04:57:09,360 want to do well we wanna 6910 04:57:06,540 --> 04:57:12,660 invoke the set count right so we're 6911 04:57:09,360 --> 04:57:14,820 gonna go here with set count and before 6912 04:57:12,660 --> 04:57:17,040 we pass the value I just want to mention 6913 04:57:14,820 --> 04:57:20,458 something very important 6914 04:57:17,040 --> 04:57:22,020 when we invoke set count technically we 6915 04:57:20,458 --> 04:57:24,980 can pass here anything 6916 04:57:22,020 --> 04:57:28,378 so if I'll pass here string of Bob yes 6917 04:57:24,980 --> 04:57:30,660 instead of number this will turn into a 6918 04:57:28,378 --> 04:57:32,638 string above just something to be aware 6919 04:57:30,660 --> 04:57:34,440 of now of course in our case we will 6920 04:57:32,638 --> 04:57:35,700 increase the count 6921 04:57:34,440 --> 04:57:38,160 but 6922 04:57:35,700 --> 04:57:41,218 this is a very important gotcha and I 6923 04:57:38,160 --> 04:57:43,798 see this a lot in the course q a so I'll 6924 04:57:41,218 --> 04:57:45,298 reference the handle click over here and 6925 04:57:43,798 --> 04:57:47,700 then as far as the count what do we want 6926 04:57:45,298 --> 04:57:49,860 to do well we want to go with count and 6927 04:57:47,700 --> 04:57:53,100 then plus one that kind of makes sense 6928 04:57:49,860 --> 04:57:55,260 right so let's save it 6929 04:57:53,100 --> 04:57:58,560 and by the way I didn't add anything in 6930 04:57:55,260 --> 04:57:59,580 a button let's say click me and now of 6931 04:57:58,560 --> 04:58:02,160 course 6932 04:57:59,580 --> 04:58:04,560 we'll see that in the browser and also 6933 04:58:02,160 --> 04:58:06,900 if you log you'll see that count is 6934 04:58:04,560 --> 04:58:09,420 nicely increasing so again let me remove 6935 04:58:06,900 --> 04:58:11,040 everything here and check it out 6936 04:58:09,420 --> 04:58:13,820 it's really cool 6937 04:58:11,040 --> 04:58:17,540 that now we're triggering that re-render 6938 04:58:13,820 --> 04:58:20,958 so every time we'll update this value 6939 04:58:17,540 --> 04:58:24,540 using the function the Handler function 6940 04:58:20,958 --> 04:58:27,120 we will also trigger a re-render 6941 04:58:24,540 --> 04:58:28,620 so unlike the previous example where we 6942 04:58:27,120 --> 04:58:30,780 were just updating the value we didn't 6943 04:58:28,620 --> 04:58:33,360 see anything now you can clearly see 6944 04:58:30,780 --> 04:58:36,740 that everything works and 6945 04:58:33,360 --> 04:58:39,180 also what's really awesome you state 6946 04:58:36,740 --> 04:58:40,798 preserves this value between the renders 6947 04:58:39,180 --> 04:58:42,660 now I'm not going to do this right now 6948 04:58:40,798 --> 04:58:45,540 since we have tons of examples coming 6949 04:58:42,660 --> 04:58:47,160 out but let's say if you were to set up 6950 04:58:45,540 --> 04:58:49,798 another button 6951 04:58:47,160 --> 04:58:52,080 and set up another 6952 04:58:49,798 --> 04:58:53,280 callback function that increases that 6953 04:58:52,080 --> 04:58:55,980 value 6954 04:58:53,280 --> 04:58:57,540 you'll see that this one will stay the 6955 04:58:55,980 --> 04:59:00,000 same between the render so it's not 6956 04:58:57,540 --> 04:59:02,340 going to go back to zero and again let 6957 04:59:00,000 --> 04:59:04,200 me just emphasize one more time that we 6958 04:59:02,340 --> 04:59:08,040 can pass here anything so we need to be 6959 04:59:04,200 --> 04:59:11,520 very careful I could go here with let's 6960 04:59:08,040 --> 04:59:13,860 say set count and I'll pass in the pants 6961 04:59:11,520 --> 04:59:17,520 because why not 6962 04:59:13,860 --> 04:59:20,100 so let me set up my pants here and check 6963 04:59:17,520 --> 04:59:22,500 it out I start nicely here with zero 6964 04:59:20,100 --> 04:59:24,360 everything is beautiful and then I have 6965 04:59:22,500 --> 04:59:27,840 you clicked pants times 6966 04:59:24,360 --> 04:59:31,200 which is great but probably not 6967 04:59:27,840 --> 04:59:34,500 something you were expecting so those 6968 04:59:31,200 --> 04:59:38,340 are the basics of you state again it's a 6969 04:59:34,500 --> 04:59:40,458 function that we get back from react 6970 04:59:38,340 --> 04:59:43,378 it returns an array with two elements 6971 04:59:40,458 --> 04:59:45,780 the current state value and a function 6972 04:59:43,378 --> 04:59:48,840 that we can use to update the state 6973 04:59:45,780 --> 04:59:51,780 value and in the function we want to 6974 04:59:48,840 --> 04:59:52,980 pass in the default value basically 6975 04:59:51,780 --> 04:59:56,580 whatever 6976 04:59:52,980 --> 04:59:59,040 we will set up in the state whether that 6977 04:59:56,580 --> 05:00:01,798 is string number array and blah blah 6978 04:59:59,040 --> 05:00:04,378 blah blah blah and then every time we'll 6979 05:00:01,798 --> 05:00:05,760 update that we will trigger the 6980 05:00:04,378 --> 05:00:08,160 re-render 6981 05:00:05,760 --> 05:00:10,920 and the cool thing is we'll see the 6982 05:00:08,160 --> 05:00:13,620 latest changes in the browser and also 6983 05:00:10,920 --> 05:00:16,740 that value will be preserved between 6984 05:00:13,620 --> 05:00:18,840 those re-render so if you have six use 6985 05:00:16,740 --> 05:00:22,320 State values and if you update one of 6986 05:00:18,840 --> 05:00:24,540 them rest of them will stay as they are 6987 05:00:22,320 --> 05:00:27,480 so they're not going to go back to zero 6988 05:00:24,540 --> 05:00:28,378 so those are the basics of use State and 6989 05:00:27,480 --> 05:00:31,920 up next 6990 05:00:28,378 --> 05:00:33,840 let's talk about renders re-renders and 6991 05:00:31,920 --> 05:00:36,180 all that cool stuff all right and before 6992 05:00:33,840 --> 05:00:40,440 we continue let's discuss two important 6993 05:00:36,180 --> 05:00:43,020 buzzwords render and re-render 6994 05:00:40,440 --> 05:00:44,760 so essentially when it comes to initial 6995 05:00:43,020 --> 05:00:47,280 render 6996 05:00:44,760 --> 05:00:50,218 it happens the first time that the 6997 05:00:47,280 --> 05:00:52,080 component tree is rendered to the Dom 6998 05:00:50,218 --> 05:00:55,080 basically it happens when the 6999 05:00:52,080 --> 05:00:57,840 application loads so of this 7000 05:00:55,080 --> 05:01:01,500 now there's another 7001 05:00:57,840 --> 05:01:04,500 use case when we toggle the component 7002 05:01:01,500 --> 05:01:05,940 but we haven't covered conditional 7003 05:01:04,500 --> 05:01:08,580 rendering yet 7004 05:01:05,940 --> 05:01:10,980 so for now don't worry about it we'll 7005 05:01:08,580 --> 05:01:13,138 come back to it once we actually cover 7006 05:01:10,980 --> 05:01:14,638 conditional rendering for now just 7007 05:01:13,138 --> 05:01:18,000 remember that 7008 05:01:14,638 --> 05:01:20,820 essentially when our application loads 7009 05:01:18,000 --> 05:01:24,120 that's when the initial render of the 7010 05:01:20,820 --> 05:01:26,700 component takes place and this is also 7011 05:01:24,120 --> 05:01:29,700 called mounting so we Mount the 7012 05:01:26,700 --> 05:01:31,500 component initial render happens and yes 7013 05:01:29,700 --> 05:01:33,840 later we'll also learn how we can 7014 05:01:31,500 --> 05:01:37,080 unmount the component 7015 05:01:33,840 --> 05:01:40,980 now when it comes to re-renders 7016 05:01:37,080 --> 05:01:42,480 they happen when the components state or 7017 05:01:40,980 --> 05:01:45,298 props change 7018 05:01:42,480 --> 05:01:47,458 so remember our previous example what we 7019 05:01:45,298 --> 05:01:50,780 were doing we were changing the state 7020 05:01:47,458 --> 05:01:54,480 value correct and as a result we were 7021 05:01:50,780 --> 05:01:56,700 re-rendering our component and 7022 05:01:54,480 --> 05:02:00,298 essentially we can 7023 05:01:56,700 --> 05:02:02,940 re-render component also when the parent 7024 05:02:00,298 --> 05:02:06,360 element re-renders again 7025 05:02:02,940 --> 05:02:08,760 something that will run into 7026 05:02:06,360 --> 05:02:11,100 quite a few times during tutorials so 7027 05:02:08,760 --> 05:02:13,620 once that happens I'll just say hey 7028 05:02:11,100 --> 05:02:15,900 remember we talked about it for now 7029 05:02:13,620 --> 05:02:18,260 just remember if you'll change the state 7030 05:02:15,900 --> 05:02:22,200 or perhaps or the parent re-renders 7031 05:02:18,260 --> 05:02:24,840 that's when essentially re-render takes 7032 05:02:22,200 --> 05:02:28,138 place hopefully this is clear and before 7033 05:02:24,840 --> 05:02:29,100 we discuss the general rules of hooks I 7034 05:02:28,138 --> 05:02:31,920 just wanna 7035 05:02:29,100 --> 05:02:34,500 change it back to correct one so it's 7036 05:02:31,920 --> 05:02:37,200 not going to be pants and I want to 7037 05:02:34,500 --> 05:02:39,240 stress one more time that let's say if 7038 05:02:37,200 --> 05:02:40,560 I'm gonna come up with another state 7039 05:02:39,240 --> 05:02:43,560 value 7040 05:02:40,560 --> 05:02:46,620 I'm going to say set name if you don't 7041 05:02:43,560 --> 05:02:49,138 want to import your state and if you 7042 05:02:46,620 --> 05:02:52,860 already have react keep in mind you have 7043 05:02:49,138 --> 05:02:56,218 to import react here then you can go 7044 05:02:52,860 --> 05:03:00,298 this way now this used to be more 7045 05:02:56,218 --> 05:03:02,520 popular when we have to import react for 7046 05:03:00,298 --> 05:03:04,620 every component we don't have to do that 7047 05:03:02,520 --> 05:03:07,500 anymore so notice both of them 7048 05:03:04,620 --> 05:03:10,980 effectively do the same thing as just 7049 05:03:07,500 --> 05:03:12,420 previously we needed to import react 7050 05:03:10,980 --> 05:03:15,298 and that's why it was somewhat 7051 05:03:12,420 --> 05:03:16,798 convenient just to avoid importing your 7052 05:03:15,298 --> 05:03:19,440 state but 7053 05:03:16,798 --> 05:03:21,060 since we don't have to import react I 7054 05:03:19,440 --> 05:03:23,520 mean I don't see the difference right 7055 05:03:21,060 --> 05:03:25,740 now you can either use use state or you 7056 05:03:23,520 --> 05:03:27,840 can import react just keep in mind that 7057 05:03:25,740 --> 05:03:30,780 both of them pretty much do the same 7058 05:03:27,840 --> 05:03:33,958 thing and with this in place now let's 7059 05:03:30,780 --> 05:03:36,718 talk about the general rules of hooks 7060 05:03:33,958 --> 05:03:39,958 awesome and now let's quickly discuss 7061 05:03:36,718 --> 05:03:42,540 the general rules of hooks 7062 05:03:39,958 --> 05:03:44,760 so all the hooks 7063 05:03:42,540 --> 05:03:47,820 the ones we get from react as well as 7064 05:03:44,760 --> 05:03:50,878 our custom one yes later we'll build our 7065 05:03:47,820 --> 05:03:51,840 own custom hooks they need to start with 7066 05:03:50,878 --> 05:03:55,500 use 7067 05:03:51,840 --> 05:03:57,180 so every time you see use and then some 7068 05:03:55,500 --> 05:04:00,120 kind of name 7069 05:03:57,180 --> 05:04:03,980 just remember it's a hook 7070 05:04:00,120 --> 05:04:07,100 so effectively all the hooks rules apply 7071 05:04:03,980 --> 05:04:09,840 now in order to work with a hook 7072 05:04:07,100 --> 05:04:12,060 component must be uppercase and this is 7073 05:04:09,840 --> 05:04:14,580 something we already discussed at the 7074 05:04:12,060 --> 05:04:17,580 very very very very beginning when we 7075 05:04:14,580 --> 05:04:19,680 talked about react fundamentals now also 7076 05:04:17,580 --> 05:04:23,580 we need to invoke 7077 05:04:19,680 --> 05:04:27,900 the hook inside of the component body 7078 05:04:23,580 --> 05:04:31,020 so if I'll try to do this over here 7079 05:04:27,900 --> 05:04:32,878 use State and then I don't know provide 7080 05:04:31,020 --> 05:04:33,840 something again it doesn't really matter 7081 05:04:32,878 --> 05:04:36,298 what 7082 05:04:33,840 --> 05:04:39,120 I'm just going to say here hello blah 7083 05:04:36,298 --> 05:04:42,060 blah blah invalid hook call so hooks can 7084 05:04:39,120 --> 05:04:44,760 be only called inside blah blah blah 7085 05:04:42,060 --> 05:04:46,200 blah something that I just mentioned so 7086 05:04:44,760 --> 05:04:49,920 that's something important to keep in 7087 05:04:46,200 --> 05:04:52,740 mind also don't call hooks conditionally 7088 05:04:49,920 --> 05:04:55,020 and again this is the topic that I'll 7089 05:04:52,740 --> 05:04:56,638 showcase once we are familiar with 7090 05:04:55,020 --> 05:04:58,680 conditional rendering because at the 7091 05:04:56,638 --> 05:05:00,360 moment I fully understand if you're 7092 05:04:58,680 --> 05:05:02,520 looking at it you're like what do you 7093 05:05:00,360 --> 05:05:04,500 mean by that and essentially long story 7094 05:05:02,520 --> 05:05:07,260 short you don't want to place for 7095 05:05:04,500 --> 05:05:10,200 example a hook inside of the 7096 05:05:07,260 --> 05:05:13,378 if commission so don't do it like this 7097 05:05:10,200 --> 05:05:15,480 don't say if and again the same deal set 7098 05:05:13,378 --> 05:05:17,400 up the EU state for example this is not 7099 05:05:15,480 --> 05:05:19,260 going to work regardless whether this 7100 05:05:17,400 --> 05:05:21,900 condition is true or not 7101 05:05:19,260 --> 05:05:25,580 this is not allowed so hooks all the 7102 05:05:21,900 --> 05:05:28,320 time need to be called in the same order 7103 05:05:25,580 --> 05:05:31,620 and lastly 7104 05:05:28,320 --> 05:05:33,840 set functions don't update the state 7105 05:05:31,620 --> 05:05:37,020 immediately and again this is something 7106 05:05:33,840 --> 05:05:38,360 we'll discuss later essentially don't 7107 05:05:37,020 --> 05:05:42,958 expect 7108 05:05:38,360 --> 05:05:45,900 V synchronous Behavior so for example if 7109 05:05:42,958 --> 05:05:47,700 I go here and log the count you'll see 7110 05:05:45,900 --> 05:05:50,100 that actually I'm going to be accessing 7111 05:05:47,700 --> 05:05:53,100 the old value notice even though this is 7112 05:05:50,100 --> 05:05:55,080 one the value in the console is zero 7113 05:05:53,100 --> 05:05:56,520 again something we're going to discuss 7114 05:05:55,080 --> 05:06:00,120 for now 7115 05:05:56,520 --> 05:06:02,520 just focus on the main ones it has to 7116 05:06:00,120 --> 05:06:04,920 start with use component must be 7117 05:06:02,520 --> 05:06:08,340 uppercase and we want to invoke it 7118 05:06:04,920 --> 05:06:11,820 inside of the function basically 7119 05:06:08,340 --> 05:06:14,580 component body not bad not bad I think 7120 05:06:11,820 --> 05:06:16,920 we are somewhat familiar with you state 7121 05:06:14,580 --> 05:06:19,080 and hooks in general 7122 05:06:16,920 --> 05:06:20,458 so why don't we 7123 05:06:19,080 --> 05:06:23,760 set up 7124 05:06:20,458 --> 05:06:26,040 a state value with array and in the 7125 05:06:23,760 --> 05:06:27,120 process we'll work on the little 7126 05:06:26,040 --> 05:06:30,298 challenge 7127 05:06:27,120 --> 05:06:32,458 so you'll need to go to zero three use 7128 05:06:30,298 --> 05:06:34,860 data array basically import it of course 7129 05:06:32,458 --> 05:06:36,780 in app jsx 7130 05:06:34,860 --> 05:06:39,360 and then 7131 05:06:36,780 --> 05:06:42,120 navigate to the file in there I want you 7132 05:06:39,360 --> 05:06:44,160 to import data and don't worry I'll show 7133 05:06:42,120 --> 05:06:47,600 you where the data is located 7134 05:06:44,160 --> 05:06:51,780 then I want you to set up State value 7135 05:06:47,600 --> 05:06:53,160 and the default is going to be the data 7136 05:06:51,780 --> 05:06:55,620 you're importing 7137 05:06:53,160 --> 05:06:57,840 now it's going to be a list and I want 7138 05:06:55,620 --> 05:07:00,240 you to render that in the browser again 7139 05:06:57,840 --> 05:07:04,560 something we already covered how we can 7140 05:07:00,240 --> 05:07:07,500 access the value it right over and then 7141 05:07:04,560 --> 05:07:10,500 display the results in a browser now 7142 05:07:07,500 --> 05:07:13,440 don't worry about CSS again that is 7143 05:07:10,500 --> 05:07:15,840 totally irrelevant if you can set up the 7144 05:07:13,440 --> 05:07:16,920 logic it's going to be already an 7145 05:07:15,840 --> 05:07:19,320 awesome result 7146 05:07:16,920 --> 05:07:21,780 and then once you can render the list in 7147 05:07:19,320 --> 05:07:22,980 the browser this is an extra challenge I 7148 05:07:21,780 --> 05:07:27,540 want you to create 7149 05:07:22,980 --> 05:07:28,860 two functions one that clears the entire 7150 05:07:27,540 --> 05:07:31,020 list 7151 05:07:28,860 --> 05:07:35,160 and second one 7152 05:07:31,020 --> 05:07:36,718 which removes the single item from the 7153 05:07:35,160 --> 05:07:38,820 list and just to Showcase what we're 7154 05:07:36,718 --> 05:07:41,218 looking for and you know what by the way 7155 05:07:38,820 --> 05:07:43,080 before the data is sitting over here so 7156 05:07:41,218 --> 05:07:46,200 if you navigate to the route you'll see 7157 05:07:43,080 --> 05:07:49,020 data GIS and then here we have this 7158 05:07:46,200 --> 05:07:52,378 array so this is what I want you to 7159 05:07:49,020 --> 05:07:56,458 import in that component and then first 7160 05:07:52,378 --> 05:07:58,620 render it in the browser now as far as 7161 05:07:56,458 --> 05:08:01,798 our final result 7162 05:07:58,620 --> 05:08:03,780 let me just copy and paste here 7163 05:08:01,798 --> 05:08:05,700 I'm going to call this final 7164 05:08:03,780 --> 05:08:09,060 again make sure that you of course 7165 05:08:05,700 --> 05:08:10,980 import the correct component and in here 7166 05:08:09,060 --> 05:08:13,200 I just want to change this to 7167 05:08:10,980 --> 05:08:15,840 I know 7168 05:08:13,200 --> 05:08:17,940 and let's also take a look what we have 7169 05:08:15,840 --> 05:08:20,040 so essentially this should be your 7170 05:08:17,940 --> 05:08:22,860 result like I said don't worry about the 7171 05:08:20,040 --> 05:08:27,120 CSS effectively you just wanna 7172 05:08:22,860 --> 05:08:29,940 render the name the button and also a 7173 05:08:27,120 --> 05:08:32,760 button to clear all the items so if you 7174 05:08:29,940 --> 05:08:35,160 click you remove that specific 7175 05:08:32,760 --> 05:08:38,280 person from the list if you click on 7176 05:08:35,160 --> 05:08:40,020 clear items obviously we are removing 7177 05:08:38,280 --> 05:08:42,180 all of them 7178 05:08:40,020 --> 05:08:43,080 and once you're done resume with the 7179 05:08:42,180 --> 05:08:46,860 videos 7180 05:08:43,080 --> 05:08:49,920 so let me first start by doing a bit of 7181 05:08:46,860 --> 05:08:51,540 spring cleaning we won't need that and 7182 05:08:49,920 --> 05:08:53,458 then we're not 7183 05:08:51,540 --> 05:08:54,958 I will actually leave 7184 05:08:53,458 --> 05:08:57,120 so I'm not going to render that 7185 05:08:54,958 --> 05:08:58,320 component but I'll leave this one just 7186 05:08:57,120 --> 05:09:00,480 in case 7187 05:08:58,320 --> 05:09:03,000 next time I need to access some final 7188 05:09:00,480 --> 05:09:05,700 component I already have the code I just 7189 05:09:03,000 --> 05:09:11,040 need to change the path and then we want 7190 05:09:05,700 --> 05:09:14,100 to navigate to use State all right in 7191 05:09:11,040 --> 05:09:17,340 the star over here and essentially let's 7192 05:09:14,100 --> 05:09:20,100 start by getting the data let's 7193 05:09:17,340 --> 05:09:21,600 jog our memory how we can do that using 7194 05:09:20,100 --> 05:09:23,458 es6 7195 05:09:21,600 --> 05:09:26,100 modules so in here we want to go and 7196 05:09:23,458 --> 05:09:28,138 import now this is a named one so we go 7197 05:09:26,100 --> 05:09:30,718 with data I already know that's the name 7198 05:09:28,138 --> 05:09:32,580 and notice you right away have the 7199 05:09:30,718 --> 05:09:34,740 correct suggestion where it is located 7200 05:09:32,580 --> 05:09:37,440 and then second I know I'm going to be 7201 05:09:34,740 --> 05:09:39,360 setting up the state value and just to 7202 05:09:37,440 --> 05:09:42,958 hammer this home I will set up my state 7203 05:09:39,360 --> 05:09:46,620 value with react dot used it and then 7204 05:09:42,958 --> 05:09:47,820 pretty much for rest of the course I'm 7205 05:09:46,620 --> 05:09:51,120 pretty sure I'm just going to import 7206 05:09:47,820 --> 05:09:54,120 your state so first let's go here with 7207 05:09:51,120 --> 05:09:56,760 import and we want to look for react 7208 05:09:54,120 --> 05:09:59,700 okay that's awesome and then let's go 7209 05:09:56,760 --> 05:10:01,440 with the const and that is equal to 7210 05:09:59,700 --> 05:10:04,560 people so this is what I'm going to be 7211 05:10:01,440 --> 05:10:06,660 setting up and then set people so one is 7212 05:10:04,560 --> 05:10:09,900 the state's value and then the second 7213 05:10:06,660 --> 05:10:13,138 one is the function that controls it and 7214 05:10:09,900 --> 05:10:15,660 let's go over here with react Dot 7215 05:10:13,138 --> 05:10:17,520 so react that and then we're looking for 7216 05:10:15,660 --> 05:10:20,218 use state 7217 05:10:17,520 --> 05:10:21,298 and what is default value well passing 7218 05:10:20,218 --> 05:10:24,958 the data 7219 05:10:21,298 --> 05:10:26,520 and now we want to essentially 7220 05:10:24,958 --> 05:10:28,320 set up our return 7221 05:10:26,520 --> 05:10:32,400 so at the moment we have heading 2 7222 05:10:28,320 --> 05:10:34,500 instead we're gonna have a div and 7223 05:10:32,400 --> 05:10:36,120 we're going to iterate over so I'll say 7224 05:10:34,500 --> 05:10:39,718 people map 7225 05:10:36,120 --> 05:10:41,878 and just so you don't think 7226 05:10:39,718 --> 05:10:43,860 that I'm making this up I'm just going 7227 05:10:41,878 --> 05:10:45,840 to go with return and you know what for 7228 05:10:43,860 --> 05:10:48,060 now let's just go with heading four and 7229 05:10:45,840 --> 05:10:51,780 let's call this item what I'm interested 7230 05:10:48,060 --> 05:10:54,360 is the person who I well because that's 7231 05:10:51,780 --> 05:10:56,878 going to give me access to this object 7232 05:10:54,360 --> 05:10:58,260 so if I navigate to data 7233 05:10:56,878 --> 05:11:01,320 check it out 7234 05:10:58,260 --> 05:11:04,138 so I have here ID and name so this is 7235 05:11:01,320 --> 05:11:05,760 what I'm going to log over here 7236 05:11:04,138 --> 05:11:08,520 so log 7237 05:11:05,760 --> 05:11:11,700 and let's look for the person 7238 05:11:08,520 --> 05:11:13,260 let's save notice we have four items 7239 05:11:11,700 --> 05:11:17,160 okay that's awesome 7240 05:11:13,260 --> 05:11:20,400 and also I'll see you over here 7241 05:11:17,160 --> 05:11:21,780 the object now we do have the warning 7242 05:11:20,400 --> 05:11:24,180 each child 7243 05:11:21,780 --> 05:11:26,400 and all that stuff don't worry we'll fix 7244 05:11:24,180 --> 05:11:28,620 that in a second so why don't we pull 7245 05:11:26,400 --> 05:11:30,958 out both of those properties we'll say 7246 05:11:28,620 --> 05:11:34,798 over here ID 7247 05:11:30,958 --> 05:11:37,378 and the person and that is equal to our 7248 05:11:34,798 --> 05:11:39,660 person right or I'm sorry this is not 7249 05:11:37,378 --> 05:11:41,458 person this is his name my bad so we 7250 05:11:39,660 --> 05:11:44,878 pulled these ones out I don't think we 7251 05:11:41,458 --> 05:11:47,040 need a log anymore or let me just leave 7252 05:11:44,878 --> 05:11:48,840 it here for your reference and then 7253 05:11:47,040 --> 05:11:52,200 let's go with our div 7254 05:11:48,840 --> 05:11:55,378 and be right away want to set up the 7255 05:11:52,200 --> 05:11:58,320 wiki so let's set it up here over here 7256 05:11:55,378 --> 05:12:01,378 key and that is equal to my ID and then 7257 05:11:58,320 --> 05:12:05,940 inside of it we want to go with heading 7258 05:12:01,378 --> 05:12:07,440 four and let's just render the name and 7259 05:12:05,940 --> 05:12:11,520 if everything is correct we shouldn't 7260 05:12:07,440 --> 05:12:12,240 see any warnings in the browser and we 7261 05:12:11,520 --> 05:12:16,680 have 7262 05:12:12,240 --> 05:12:19,020 nicely rendered our list in the browser 7263 05:12:16,680 --> 05:12:20,580 so that's the first step and up next 7264 05:12:19,020 --> 05:12:23,400 we're gonna work on 7265 05:12:20,580 --> 05:12:25,798 removing the items from the list all 7266 05:12:23,400 --> 05:12:28,560 right so now let's complete the 7267 05:12:25,798 --> 05:12:30,780 functionality and as a quick signal 7268 05:12:28,560 --> 05:12:34,560 during this video at least 7269 05:12:30,780 --> 05:12:37,440 I will rely heavily on filter method and 7270 05:12:34,560 --> 05:12:40,020 if you want to find out more info 7271 05:12:37,440 --> 05:12:43,740 you can just follow this URL where I 7272 05:12:40,020 --> 05:12:47,400 cover everything in great detail so 7273 05:12:43,740 --> 05:12:49,378 let's navigate back to our example 7274 05:12:47,400 --> 05:12:51,840 and essentially I want to set up two 7275 05:12:49,378 --> 05:12:54,298 functions one is going to be 7276 05:12:51,840 --> 05:12:56,878 for removing all the items in the second 7277 05:12:54,298 --> 05:12:58,440 one is going to be for removing a single 7278 05:12:56,878 --> 05:13:01,080 item so let's start with that one so 7279 05:12:58,440 --> 05:13:01,860 remove item for now I'm not going to 7280 05:13:01,080 --> 05:13:04,020 pass 7281 05:13:01,860 --> 05:13:06,660 anything in even though eventually yes 7282 05:13:04,020 --> 05:13:09,420 will pass in the parameter 7283 05:13:06,660 --> 05:13:11,400 and I mean if you want you can set up 7284 05:13:09,420 --> 05:13:13,260 the log but 7285 05:13:11,400 --> 05:13:16,820 I think in order to speed this up I'll 7286 05:13:13,260 --> 05:13:20,878 just copy and paste and I'll say clear 7287 05:13:16,820 --> 05:13:23,820 all items so that's my second function 7288 05:13:20,878 --> 05:13:25,500 then we want to set up two buttons 7289 05:13:23,820 --> 05:13:29,340 so we wanna 7290 05:13:25,500 --> 05:13:31,860 set up a button right here in a div and 7291 05:13:29,340 --> 05:13:34,378 also if you want to set up a button that 7292 05:13:31,860 --> 05:13:37,500 clears all the values and that one is 7293 05:13:34,378 --> 05:13:39,718 going to be outside of the curlers since 7294 05:13:37,500 --> 05:13:42,780 I'm not going to set it in the list so 7295 05:13:39,718 --> 05:13:44,340 first I guess let's just start with some 7296 05:13:42,780 --> 05:13:47,760 HTML elements I'm going to go with 7297 05:13:44,340 --> 05:13:50,160 button type that is going to be equal to 7298 05:13:47,760 --> 05:13:52,620 a button I'm not going to add 7299 05:13:50,160 --> 05:13:53,760 any classes it's going to be pretty 7300 05:13:52,620 --> 05:13:55,740 basic 7301 05:13:53,760 --> 05:13:58,680 we can nicely see the buttons and now 7302 05:13:55,740 --> 05:14:02,100 let's also set up that clear all so 7303 05:13:58,680 --> 05:14:04,798 let's go here button same deal type is 7304 05:14:02,100 --> 05:14:08,700 equal to a button then let's add a 7305 05:14:04,798 --> 05:14:10,740 little bit of styles just because I want 7306 05:14:08,700 --> 05:14:15,060 a margin so I'm going to go with margin 7307 05:14:10,740 --> 05:14:17,700 top and that is equal to two Rems 7308 05:14:15,060 --> 05:14:19,260 on click is coming up and now let's go 7309 05:14:17,700 --> 05:14:22,260 with clear 7310 05:14:19,260 --> 05:14:25,920 items and now let's go back to our tasks 7311 05:14:22,260 --> 05:14:28,560 so essentially this one and as a note 7312 05:14:25,920 --> 05:14:31,620 I actually need to add here class name 7313 05:14:28,560 --> 05:14:33,480 and then that is equal to a BTN 7314 05:14:31,620 --> 05:14:36,120 so this one 7315 05:14:33,480 --> 05:14:39,060 we want to use to remove all the items 7316 05:14:36,120 --> 05:14:41,760 correct so what do we need to do well we 7317 05:14:39,060 --> 05:14:44,878 have people and set people currently 7318 05:14:41,760 --> 05:14:48,360 it's equal to what to our data array how 7319 05:14:44,878 --> 05:14:53,280 we can remove all items well we can set 7320 05:14:48,360 --> 05:14:56,160 people equal to an mtra by just using 7321 05:14:53,280 --> 05:14:59,340 set people so if I go with clear all 7322 05:14:56,160 --> 05:15:01,560 items and I say set people and set it 7323 05:14:59,340 --> 05:15:04,080 equal to an empty array I'll move 7324 05:15:01,560 --> 05:15:07,020 automatically all the items and since 7325 05:15:04,080 --> 05:15:09,900 we're updating the state value we will 7326 05:15:07,020 --> 05:15:11,040 trigger what will trigger re-render and 7327 05:15:09,900 --> 05:15:13,020 of course 7328 05:15:11,040 --> 05:15:15,420 as a result 7329 05:15:13,020 --> 05:15:17,760 we'll see the latest changes in the 7330 05:15:15,420 --> 05:15:20,820 browser so let's go over here let's say 7331 05:15:17,760 --> 05:15:23,340 on click that's the event and now we 7332 05:15:20,820 --> 05:15:24,718 just want to pass the reference so let's 7333 05:15:23,340 --> 05:15:27,900 say clear 7334 05:15:24,718 --> 05:15:29,400 all and items 7335 05:15:27,900 --> 05:15:32,760 let's save that 7336 05:15:29,400 --> 05:15:37,378 Let's test it out yep we remove all the 7337 05:15:32,760 --> 05:15:40,740 items from the list now just so we are 7338 05:15:37,378 --> 05:15:42,958 aware remember we can pass here the 7339 05:15:40,740 --> 05:15:45,840 arrow function as well 7340 05:15:42,958 --> 05:15:47,878 so just for your reference we can do it 7341 05:15:45,840 --> 05:15:51,600 this way you can go with arrow function 7342 05:15:47,878 --> 05:15:53,940 and then set people invoke and pass in 7343 05:15:51,600 --> 05:15:55,860 the value again this is very very 7344 05:15:53,940 --> 05:16:01,138 important concept and yes I'm getting 7345 05:15:55,860 --> 05:16:04,320 quite a few questions in the course q a 7346 05:16:01,138 --> 05:16:05,638 both of them achieve the same result so 7347 05:16:04,320 --> 05:16:09,120 keep in mind if you see this code 7348 05:16:05,638 --> 05:16:12,480 essentially it's just a shorter version 7349 05:16:09,120 --> 05:16:15,780 of this one and as far as the remove 7350 05:16:12,480 --> 05:16:19,378 item well let's think about it so we 7351 05:16:15,780 --> 05:16:22,080 have IDs correct so those are unique to 7352 05:16:19,378 --> 05:16:25,080 each item now how we can remove a 7353 05:16:22,080 --> 05:16:26,580 specific item from array well we can use 7354 05:16:25,080 --> 05:16:29,040 a filter method 7355 05:16:26,580 --> 05:16:31,798 where we're going to grab the ID 7356 05:16:29,040 --> 05:16:33,900 whatever it is one two three four and 7357 05:16:31,798 --> 05:16:37,798 essentially we'll just remove that one 7358 05:16:33,900 --> 05:16:42,000 item whose ID matches and then we'll set 7359 05:16:37,798 --> 05:16:43,920 the people equal to that new array so 7360 05:16:42,000 --> 05:16:46,440 let's try this one out 7361 05:16:43,920 --> 05:16:47,638 first again I just want to emphasize the 7362 05:16:46,440 --> 05:16:51,000 point that 7363 05:16:47,638 --> 05:16:54,900 each item right away have that unique 7364 05:16:51,000 --> 05:16:57,120 value when it invokes the remove item so 7365 05:16:54,900 --> 05:17:01,980 let's say here I'm going to go with ID 7366 05:16:57,120 --> 05:17:04,620 parameter and log an ID and if I go back 7367 05:17:01,980 --> 05:17:07,798 to the div that I'm returning notice 7368 05:17:04,620 --> 05:17:10,020 over here if I go with on click and 7369 05:17:07,798 --> 05:17:12,420 first of all if I want to invoke 7370 05:17:10,020 --> 05:17:14,940 remove item right away I do need to set 7371 05:17:12,420 --> 05:17:16,680 up the arrow function first and I'm not 7372 05:17:14,940 --> 05:17:18,840 going to go back to this topic we 7373 05:17:16,680 --> 05:17:22,638 literally spent like I believe three 7374 05:17:18,840 --> 05:17:25,138 videos on that in the fundamentals so if 7375 05:17:22,638 --> 05:17:27,420 you are a little bit confused please go 7376 05:17:25,138 --> 05:17:30,840 back and re-watch those videos so remove 7377 05:17:27,420 --> 05:17:32,520 item I'll pass in the Eddie and you'll 7378 05:17:30,840 --> 05:17:36,900 see nicely 7379 05:17:32,520 --> 05:17:39,958 once you click on remove that each item 7380 05:17:36,900 --> 05:17:42,180 basically has that unique ID so whatever 7381 05:17:39,958 --> 05:17:45,660 we'll pass over here 7382 05:17:42,180 --> 05:17:48,600 is going to be unique to that item so 7383 05:17:45,660 --> 05:17:53,700 like I said what can we do well we can 7384 05:17:48,600 --> 05:17:56,000 create new array so const new people 7385 05:17:53,700 --> 05:17:58,620 that is equal to what well filter 7386 05:17:56,000 --> 05:18:01,860 returns a new array right so we go with 7387 05:17:58,620 --> 05:18:04,560 State value then we go here with filter 7388 05:18:01,860 --> 05:18:06,060 and then we need to pass in the Callback 7389 05:18:04,560 --> 05:18:07,500 function 7390 05:18:06,060 --> 05:18:09,840 now in a callback function I'll 7391 05:18:07,500 --> 05:18:12,780 reference each item as a person and I'll 7392 05:18:09,840 --> 05:18:14,100 say if the person 7393 05:18:12,780 --> 05:18:17,100 ID 7394 05:18:14,100 --> 05:18:19,798 does not match then return basically 7395 05:18:17,100 --> 05:18:23,218 filter is looking for a flag 7396 05:18:19,798 --> 05:18:25,980 and if it matches then it's going to 7397 05:18:23,218 --> 05:18:28,080 return in this case if the ID doesn't 7398 05:18:25,980 --> 05:18:30,780 match then it's going to return 7399 05:18:28,080 --> 05:18:32,700 that person to a new people 7400 05:18:30,780 --> 05:18:34,798 if it does match 7401 05:18:32,700 --> 05:18:37,440 then essentially it's going to exclude 7402 05:18:34,798 --> 05:18:40,200 it so we go here with set people and we 7403 05:18:37,440 --> 05:18:42,540 set it equal to any new people 7404 05:18:40,200 --> 05:18:44,400 that's it that's all we have to do let's 7405 05:18:42,540 --> 05:18:48,060 come in this one out and notice how one 7406 05:18:44,400 --> 05:18:50,820 by one basically we can remove the items 7407 05:18:48,060 --> 05:18:53,400 from the array and 7408 05:18:50,820 --> 05:18:55,980 if you're wondering can we pass this 7409 05:18:53,400 --> 05:18:58,080 entire logic directly and set people 7410 05:18:55,980 --> 05:19:00,180 the answer is yes 7411 05:18:58,080 --> 05:19:01,860 so you can also do something like this 7412 05:19:00,180 --> 05:19:04,500 grab this line 7413 05:19:01,860 --> 05:19:06,958 don't set up the variable and right away 7414 05:19:04,500 --> 05:19:08,878 pass in the functionality remember this 7415 05:19:06,958 --> 05:19:09,840 returns a new array at the end of the 7416 05:19:08,878 --> 05:19:12,900 day 7417 05:19:09,840 --> 05:19:16,260 so yes this will also work notice I can 7418 05:19:12,900 --> 05:19:18,120 nicely remove John so that's how we can 7419 05:19:16,260 --> 05:19:21,540 set up 7420 05:19:18,120 --> 05:19:24,180 a state value as an array and right away 7421 05:19:21,540 --> 05:19:27,958 make it Dynamic by just adding a few 7422 05:19:24,180 --> 05:19:30,000 buttons as well as the functionality and 7423 05:19:27,958 --> 05:19:32,040 as you can see we are basically relying 7424 05:19:30,000 --> 05:19:33,780 on all the concepts we cover in 7425 05:19:32,040 --> 05:19:36,120 fundamentals 7426 05:19:33,780 --> 05:19:38,458 the difference well now we're using this 7427 05:19:36,120 --> 05:19:42,540 one line of code we're basically using 7428 05:19:38,458 --> 05:19:45,060 the use state which allows us to change 7429 05:19:42,540 --> 05:19:47,060 that value and every time we change the 7430 05:19:45,060 --> 05:19:50,580 value we trigger what we trigger 7431 05:19:47,060 --> 05:19:53,520 re-render and as a result we see the 7432 05:19:50,580 --> 05:19:56,280 latest changes in the browser all right 7433 05:19:53,520 --> 05:19:59,520 and before we start setting up the 7434 05:19:56,280 --> 05:20:01,980 object as a state value now let me 7435 05:19:59,520 --> 05:20:04,740 quickly answer the question which most 7436 05:20:01,980 --> 05:20:05,700 likely I'm going to get in the course q 7437 05:20:04,740 --> 05:20:08,160 a 7438 05:20:05,700 --> 05:20:11,878 and the question is following since now 7439 05:20:08,160 --> 05:20:14,520 we know you state should we go back to 7440 05:20:11,878 --> 05:20:19,980 let's say previous project the back 7441 05:20:14,520 --> 05:20:22,980 roads one and effectively add huge state 7442 05:20:19,980 --> 05:20:26,040 in each instance we're reiterating over 7443 05:20:22,980 --> 05:20:27,660 the list and honestly the answer really 7444 05:20:26,040 --> 05:20:29,280 depends on what you're going to be doing 7445 05:20:27,660 --> 05:20:33,060 with the list 7446 05:20:29,280 --> 05:20:35,458 so of course if you'll be manipulating 7447 05:20:33,060 --> 05:20:37,200 the list if you'll be removing the items 7448 05:20:35,458 --> 05:20:39,420 adding the atoms and all that kind of 7449 05:20:37,200 --> 05:20:40,980 stuff then of course you will definitely 7450 05:20:39,420 --> 05:20:42,840 need use state 7451 05:20:40,980 --> 05:20:44,580 if you know that data is not going to 7452 05:20:42,840 --> 05:20:47,638 change 7453 05:20:44,580 --> 05:20:51,180 then of course you can leave it the way 7454 05:20:47,638 --> 05:20:53,580 it is I mean nothing bad is going to 7455 05:20:51,180 --> 05:20:55,620 happen so what I'm trying to say there 7456 05:20:53,580 --> 05:20:57,660 is no rule that you definitely have to 7457 05:20:55,620 --> 05:21:00,120 use State however it is a common 7458 05:20:57,660 --> 05:21:02,458 practice even if let's say you're not 7459 05:21:00,120 --> 05:21:04,798 going to be doing anything with list 7460 05:21:02,458 --> 05:21:06,718 you get used State you set it up and 7461 05:21:04,798 --> 05:21:09,180 that way if you ever want to change 7462 05:21:06,718 --> 05:21:12,840 something about list well you already 7463 05:21:09,180 --> 05:21:14,280 have a state value so long story short I 7464 05:21:12,840 --> 05:21:16,620 wouldn't go back to the previous project 7465 05:21:14,280 --> 05:21:19,080 and change it I would leave it the way 7466 05:21:16,620 --> 05:21:21,480 it is but in the future when you are 7467 05:21:19,080 --> 05:21:25,200 iterating over the list yes setting up a 7468 05:21:21,480 --> 05:21:27,660 state value is a good practice okay and 7469 05:21:25,200 --> 05:21:30,798 up next let's work on 7470 05:21:27,660 --> 05:21:33,540 setting up the state value as an object 7471 05:21:30,798 --> 05:21:36,240 and before we do anything 7472 05:21:33,540 --> 05:21:38,820 let's just see why that would be useful 7473 05:21:36,240 --> 05:21:40,740 in the first place just like we did for 7474 05:21:38,820 --> 05:21:43,560 example with huge state 7475 05:21:40,740 --> 05:21:46,860 and also in the process 7476 05:21:43,560 --> 05:21:49,560 will right away utilize whatever we 7477 05:21:46,860 --> 05:21:52,580 learned in the previous lessons so 7478 05:21:49,560 --> 05:21:55,740 here's what I want you to do navigate to 7479 05:21:52,580 --> 05:21:58,980 starter and then you're looking for huge 7480 05:21:55,740 --> 05:22:01,860 State object and you know what let me 7481 05:21:58,980 --> 05:22:04,020 grab quickly that file here 7482 05:22:01,860 --> 05:22:06,180 just so you have it for your reference 7483 05:22:04,020 --> 05:22:09,240 in the readme 7484 05:22:06,180 --> 05:22:12,540 over here and you're looking for zero 7485 05:22:09,240 --> 05:22:15,000 four and I believe the name was use 7486 05:22:12,540 --> 05:22:16,798 State object over here 7487 05:22:15,000 --> 05:22:19,920 so you want to import that of course in 7488 05:22:16,798 --> 05:22:23,100 the app jsx you want to navigate to that 7489 05:22:19,920 --> 05:22:25,020 file and you want to set up three state 7490 05:22:23,100 --> 05:22:28,620 values not in my case it's going to be 7491 05:22:25,020 --> 05:22:31,440 name age and let's say a hobby 7492 05:22:28,620 --> 05:22:34,798 set up all of them with use State 7493 05:22:31,440 --> 05:22:36,900 basically one by one render them and 7494 05:22:34,798 --> 05:22:39,540 then create a button 7495 05:22:36,900 --> 05:22:42,840 and every time the user clicks the 7496 05:22:39,540 --> 05:22:45,600 button invoke a function which updates 7497 05:22:42,840 --> 05:22:48,600 all three state values 7498 05:22:45,600 --> 05:22:49,980 so as a result once you click the button 7499 05:22:48,600 --> 05:22:51,540 different person is going to be 7500 05:22:49,980 --> 05:22:54,840 displayed in the browser so let's say 7501 05:22:51,540 --> 05:22:58,680 these are my default values peer Pages 7502 05:22:54,840 --> 05:23:02,180 24 and Peter enjoys reading books now 7503 05:22:58,680 --> 05:23:05,820 once I click on show John I'll have John 7504 05:23:02,180 --> 05:23:06,840 28 and John enjoys screaming at the 7505 05:23:05,820 --> 05:23:08,638 computer 7506 05:23:06,840 --> 05:23:11,878 and once we have this one in place we'll 7507 05:23:08,638 --> 05:23:16,080 refactor to object just so you can see 7508 05:23:11,878 --> 05:23:18,718 the actual use case so let's navigate to 7509 05:23:16,080 --> 05:23:19,500 the object one in the star and you know 7510 05:23:18,718 --> 05:23:21,958 what 7511 05:23:19,500 --> 05:23:23,218 pretty sure I can just comment this one 7512 05:23:21,958 --> 05:23:27,540 out for now 7513 05:23:23,218 --> 05:23:30,180 and uh want to go to that file over here 7514 05:23:27,540 --> 05:23:33,120 and I already have the huge state so I 7515 05:23:30,180 --> 05:23:35,700 already imported for you and effectively 7516 05:23:33,120 --> 05:23:38,040 you want to set up those State values so 7517 05:23:35,700 --> 05:23:40,500 one by one so const 7518 05:23:38,040 --> 05:23:41,638 let's go in here with name 7519 05:23:40,500 --> 05:23:43,920 set 7520 05:23:41,638 --> 05:23:45,480 name and that is equal to use State and 7521 05:23:43,920 --> 05:23:47,878 whatever is the default and like I said 7522 05:23:45,480 --> 05:23:50,160 Mike is going to be Peter right away 7523 05:23:47,878 --> 05:23:52,740 copying paste and we want to change some 7524 05:23:50,160 --> 05:23:55,200 things around where this is not going to 7525 05:23:52,740 --> 05:23:57,480 be name it's going to be age 7526 05:23:55,200 --> 05:23:59,700 we'll go with set age and 7527 05:23:57,480 --> 05:24:01,798 this one is going to be number so let's 7528 05:23:59,700 --> 05:24:04,740 go with 24. 7529 05:24:01,798 --> 05:24:06,660 then we want to go with that Hobby 7530 05:24:04,740 --> 05:24:09,740 Lobby 7531 05:24:06,660 --> 05:24:09,740 and then set Hobby 7532 05:24:10,500 --> 05:24:17,520 and that one will be equal to 7533 05:24:14,040 --> 05:24:19,740 well I guess let's go with reading books 7534 05:24:17,520 --> 05:24:23,100 because that is really nice so 7535 05:24:19,740 --> 05:24:24,180 say read books awesome then let's render 7536 05:24:23,100 --> 05:24:25,860 that 7537 05:24:24,180 --> 05:24:27,718 and this is the case where I'm going to 7538 05:24:25,860 --> 05:24:29,878 use the fragment 7539 05:24:27,718 --> 05:24:31,920 sure so we can jog our memory how we can 7540 05:24:29,878 --> 05:24:33,600 do that so that's my fragment and then 7541 05:24:31,920 --> 05:24:37,500 heading three 7542 05:24:33,600 --> 05:24:39,180 let's go with name copy and paste and 7543 05:24:37,500 --> 05:24:42,360 effectively I want to set up rest of 7544 05:24:39,180 --> 05:24:44,040 them so one two and after that we'll 7545 05:24:42,360 --> 05:24:45,540 have the button so why don't we do this 7546 05:24:44,040 --> 05:24:49,020 why don't we go with heading 4 here 7547 05:24:45,540 --> 05:24:51,540 first one name second one page 7548 05:24:49,020 --> 05:24:53,638 and let's add enjoys 7549 05:24:51,540 --> 05:24:56,218 otherwise scream at the computer might 7550 05:24:53,638 --> 05:24:57,120 be a bit confusing so let's say here 7551 05:24:56,218 --> 05:24:59,340 Hobby 7552 05:24:57,120 --> 05:25:02,218 and let's right away set up that button 7553 05:24:59,340 --> 05:25:04,260 so let's save okay we have initial value 7554 05:25:02,218 --> 05:25:06,480 and basically we want to go here with 7555 05:25:04,260 --> 05:25:07,620 button and we want to go to class name 7556 05:25:06,480 --> 05:25:11,040 BTN 7557 05:25:07,620 --> 05:25:13,980 and we want to add on click and 7558 05:25:11,040 --> 05:25:18,060 let's set up a function 7559 05:25:13,980 --> 05:25:20,400 from here let's go with cons display 7560 05:25:18,060 --> 05:25:22,260 person 7561 05:25:20,400 --> 05:25:24,180 first let's just set up the structure 7562 05:25:22,260 --> 05:25:26,340 and then we'll worry about 7563 05:25:24,180 --> 05:25:30,360 the actual logic 7564 05:25:26,340 --> 05:25:33,360 so let's say here display portion and 7565 05:25:30,360 --> 05:25:35,458 I'm gonna write here show John since I 7566 05:25:33,360 --> 05:25:37,798 know that's what I'm going to be doing 7567 05:25:35,458 --> 05:25:38,820 so let's say okay we have the button and 7568 05:25:37,798 --> 05:25:41,400 then 7569 05:25:38,820 --> 05:25:43,500 it's pretty straightforward if I want to 7570 05:25:41,400 --> 05:25:45,718 change all of these three values yes 7571 05:25:43,500 --> 05:25:49,920 this is what I'll have to do one by one 7572 05:25:45,718 --> 05:25:51,900 I'll go with set and name what is the 7573 05:25:49,920 --> 05:25:54,420 name well that is going to be John and 7574 05:25:51,900 --> 05:25:58,138 you can probably or guess the other ones 7575 05:25:54,420 --> 05:26:01,020 so age as well as the hobby so we simply 7576 05:25:58,138 --> 05:26:05,700 need to change here page 7577 05:26:01,020 --> 05:26:08,878 and this one will be for example 28. and 7578 05:26:05,700 --> 05:26:10,980 then lastly we have set Hobby and this 7579 05:26:08,878 --> 05:26:13,378 is where I'll type scream at the 7580 05:26:10,980 --> 05:26:15,840 computer so scream 7581 05:26:13,378 --> 05:26:18,480 at the computer 7582 05:26:15,840 --> 05:26:20,520 okay good now we need to change this one 7583 05:26:18,480 --> 05:26:22,378 to a hobby of course and like I said 7584 05:26:20,520 --> 05:26:25,378 it's very straightforward 7585 05:26:22,378 --> 05:26:27,480 you have 15 values over here 7586 05:26:25,378 --> 05:26:32,280 if you have this type of setup where 7587 05:26:27,480 --> 05:26:34,920 each value has its own Handler function 7588 05:26:32,280 --> 05:26:35,878 essentially you'll have to call them one 7589 05:26:34,920 --> 05:26:39,540 by one 7590 05:26:35,878 --> 05:26:42,060 and therefore in the next video we'll 7591 05:26:39,540 --> 05:26:45,840 take a look at the object example okay 7592 05:26:42,060 --> 05:26:48,240 and before we refactor our example to 7593 05:26:45,840 --> 05:26:49,138 one state value which is going to be an 7594 05:26:48,240 --> 05:26:51,780 object 7595 05:26:49,138 --> 05:26:54,540 why don't we do a little quiz so 7596 05:26:51,780 --> 05:26:56,580 remember when we discussed use State I 7597 05:26:54,540 --> 05:26:57,958 mentioned that pretty much every time 7598 05:26:56,580 --> 05:26:59,760 we'll update the state value will 7599 05:26:57,958 --> 05:27:02,160 trigger rearrender 7600 05:26:59,760 --> 05:27:03,780 now in this example we have three 7601 05:27:02,160 --> 05:27:06,540 Handler functions 7602 05:27:03,780 --> 05:27:08,400 so ten thousand dollar question do you 7603 05:27:06,540 --> 05:27:09,958 think we're going to trigger three 7604 05:27:08,400 --> 05:27:13,200 re-renders 7605 05:27:09,958 --> 05:27:15,660 or it's going to be only one 7606 05:27:13,200 --> 05:27:17,458 and if you guessed only one you're 7607 05:27:15,660 --> 05:27:20,280 absolutely correct 7608 05:27:17,458 --> 05:27:23,040 you see behind the scenes 7609 05:27:20,280 --> 05:27:24,240 react is using technique called Auto 7610 05:27:23,040 --> 05:27:28,500 batching 7611 05:27:24,240 --> 05:27:31,378 to effectively group the update 7612 05:27:28,500 --> 05:27:34,560 so what that means in our example even 7613 05:27:31,378 --> 05:27:38,040 though we have three handle functions 7614 05:27:34,560 --> 05:27:39,420 it's only going to trigger one re-render 7615 05:27:38,040 --> 05:27:42,180 now 7616 05:27:39,420 --> 05:27:45,060 prior to react 18 7617 05:27:42,180 --> 05:27:46,080 there were actually some instances where 7618 05:27:45,060 --> 05:27:48,560 yes 7619 05:27:46,080 --> 05:27:53,100 such a code basically when we have 7620 05:27:48,560 --> 05:27:55,440 multiple Handler functions would trigger 7621 05:27:53,100 --> 05:28:00,320 three re-renders but that's not the case 7622 05:27:55,440 --> 05:28:03,958 anymore with reactating so in react 18 7623 05:28:00,320 --> 05:28:06,660 pretty much every time you have multiple 7624 05:28:03,958 --> 05:28:09,480 Handler functions yes 7625 05:28:06,660 --> 05:28:13,260 it will trigger only one re-render 7626 05:28:09,480 --> 05:28:16,378 beautiful so our functionality Works 7627 05:28:13,260 --> 05:28:19,320 technically everything is fine but we 7628 05:28:16,378 --> 05:28:22,560 can save some lines of code if we 7629 05:28:19,320 --> 05:28:24,120 refactor this to an object 7630 05:28:22,560 --> 05:28:26,100 and of course 7631 05:28:24,120 --> 05:28:28,320 it's going to make even more sense if 7632 05:28:26,100 --> 05:28:31,138 you have more State values what do you 7633 05:28:28,320 --> 05:28:34,320 think is easier to have one object that 7634 05:28:31,138 --> 05:28:38,820 has let's say 10 Properties or to have 7635 05:28:34,320 --> 05:28:40,378 10 State values again it depends on the 7636 05:28:38,820 --> 05:28:42,060 use case of course 7637 05:28:40,378 --> 05:28:45,120 if you have 7638 05:28:42,060 --> 05:28:47,100 a use case for 10 separate values then 7639 05:28:45,120 --> 05:28:51,180 of course don't just randomly group them 7640 05:28:47,100 --> 05:28:54,480 together but let's say alert example 7641 05:28:51,180 --> 05:28:55,680 what is usually in the alert well you 7642 05:28:54,480 --> 05:28:58,020 want to know whether it's going to be 7643 05:28:55,680 --> 05:29:00,780 displayed so basically there's going to 7644 05:28:58,020 --> 05:29:02,400 be a Boolean value true or false you 7645 05:29:00,780 --> 05:29:04,860 probably also want to know whether it's 7646 05:29:02,400 --> 05:29:06,780 going to be danger or success whether 7647 05:29:04,860 --> 05:29:09,180 it's going to be some red alert or it's 7648 05:29:06,780 --> 05:29:11,520 going to be green and you also probably 7649 05:29:09,180 --> 05:29:13,860 want to change the text so that's a 7650 05:29:11,520 --> 05:29:16,138 perfect example where actually you don't 7651 05:29:13,860 --> 05:29:18,360 have to set them up one by one 7652 05:29:16,138 --> 05:29:20,878 you can just group them together 7653 05:29:18,360 --> 05:29:23,878 in one object so here's what I want to 7654 05:29:20,878 --> 05:29:26,940 do I want to go with const 7655 05:29:23,878 --> 05:29:28,620 and I'm going to call this person and 7656 05:29:26,940 --> 05:29:31,680 set person 7657 05:29:28,620 --> 05:29:33,060 now this is going to be equal to my use 7658 05:29:31,680 --> 05:29:36,360 state 7659 05:29:33,060 --> 05:29:38,340 and here we'll pass in my object 7660 05:29:36,360 --> 05:29:40,860 and 7661 05:29:38,340 --> 05:29:41,878 as far as the properties well we use the 7662 05:29:40,860 --> 05:29:45,298 same ones 7663 05:29:41,878 --> 05:29:47,218 we'll say name is equal to 7664 05:29:45,298 --> 05:29:50,580 Peter 7665 05:29:47,218 --> 05:29:52,440 and let's copy and paste one two and 7666 05:29:50,580 --> 05:29:55,680 let's just change these values around as 7667 05:29:52,440 --> 05:29:58,378 well so we're going to go here with age 7668 05:29:55,680 --> 05:30:02,340 that will be equal to 7669 05:29:58,378 --> 05:30:04,860 24 yep you're exactly correct and then 7670 05:30:02,340 --> 05:30:07,260 lastly we have our Hobby 7671 05:30:04,860 --> 05:30:10,980 so we go here with Hobby 7672 05:30:07,260 --> 05:30:13,560 and we'll go with read books 7673 05:30:10,980 --> 05:30:15,780 now I can remove all 7674 05:30:13,560 --> 05:30:17,878 these new States we won't need them 7675 05:30:15,780 --> 05:30:19,500 anymore and we also don't need to call 7676 05:30:17,878 --> 05:30:24,060 them one by one 7677 05:30:19,500 --> 05:30:26,520 so now I can go with one function which 7678 05:30:24,060 --> 05:30:30,360 is set person and I just need to provide 7679 05:30:26,520 --> 05:30:31,340 the object here I'll say John first then 7680 05:30:30,360 --> 05:30:35,100 age 7681 05:30:31,340 --> 05:30:36,958 28 and then I'll say the hobby is equal 7682 05:30:35,100 --> 05:30:38,878 to scream 7683 05:30:36,958 --> 05:30:41,340 at the 7684 05:30:38,878 --> 05:30:44,520 computer and once I save I actually get 7685 05:30:41,340 --> 05:30:47,820 an error why well because remember now 7686 05:30:44,520 --> 05:30:50,638 our state is an object so we can either 7687 05:30:47,820 --> 05:30:52,620 destructure it over here one by one just 7688 05:30:50,638 --> 05:30:54,958 grab the properties or 7689 05:30:52,620 --> 05:30:56,940 we need to go with person 7690 05:30:54,958 --> 05:31:00,958 .name 7691 05:30:56,940 --> 05:31:03,120 then we're looking for person.age 7692 05:31:00,958 --> 05:31:06,180 and then lastly of course we have person 7693 05:31:03,120 --> 05:31:08,458 and hobby correct so let's do that let's 7694 05:31:06,180 --> 05:31:12,000 save and now everything should work so 7695 05:31:08,458 --> 05:31:15,180 once I click check it out now I have the 7696 05:31:12,000 --> 05:31:16,980 new values now be aware of the gotchas 7697 05:31:15,180 --> 05:31:21,240 remember 7698 05:31:16,980 --> 05:31:24,600 we can pass whatever value in the set 7699 05:31:21,240 --> 05:31:28,138 function so even though this is an 7700 05:31:24,600 --> 05:31:30,600 object I can pass the string and what do 7701 05:31:28,138 --> 05:31:32,540 you think is going to happen well our 7702 05:31:30,600 --> 05:31:35,100 application is going to somewhat break 7703 05:31:32,540 --> 05:31:37,440 because there is no more object so let's 7704 05:31:35,100 --> 05:31:39,780 try this one out first I'll copy and 7705 05:31:37,440 --> 05:31:43,500 paste or I'm sorry I'll comment this one 7706 05:31:39,780 --> 05:31:47,520 out my bad so go here with set person 7707 05:31:43,500 --> 05:31:49,620 and then let's say shake and bake and 7708 05:31:47,520 --> 05:31:52,440 what do you know the moment I save the 7709 05:31:49,620 --> 05:31:55,798 moment I click I'm nothing rendered why 7710 05:31:52,440 --> 05:31:58,680 well again there is no object anymore so 7711 05:31:55,798 --> 05:32:01,080 I simply have the enjoys these ones 7712 05:31:58,680 --> 05:32:02,760 they're undefined so that's the first 7713 05:32:01,080 --> 05:32:03,958 thing to be aware of 7714 05:32:02,760 --> 05:32:08,040 second 7715 05:32:03,958 --> 05:32:12,180 you can also overwrite by just providing 7716 05:32:08,040 --> 05:32:13,920 one property or two basically not all of 7717 05:32:12,180 --> 05:32:16,440 the properties so in this example we 7718 05:32:13,920 --> 05:32:19,080 provided all of the properties so it was 7719 05:32:16,440 --> 05:32:22,138 kind of hard to mess it up but what if I 7720 05:32:19,080 --> 05:32:23,700 go here and say set person and I just 7721 05:32:22,138 --> 05:32:25,200 say you know what I want to change the 7722 05:32:23,700 --> 05:32:27,900 name I don't want to change the other 7723 05:32:25,200 --> 05:32:31,980 ones I just want to change the name well 7724 05:32:27,900 --> 05:32:34,620 now I'll only have a object with a value 7725 05:32:31,980 --> 05:32:36,660 of Susan so let's go here let's click 7726 05:32:34,620 --> 05:32:39,600 and check it out again the other ones 7727 05:32:36,660 --> 05:32:43,260 they're not there anymore because this 7728 05:32:39,600 --> 05:32:46,020 is the new state value this is the 7729 05:32:43,260 --> 05:32:49,700 object now a way around it is 7730 05:32:46,020 --> 05:32:52,260 effectively to just copy the properties 7731 05:32:49,700 --> 05:32:56,340 from the state 7732 05:32:52,260 --> 05:32:59,700 so first let's just copy and paste let's 7733 05:32:56,340 --> 05:33:03,240 uncommon this one and 7734 05:32:59,700 --> 05:33:05,340 first we want to copy all the values and 7735 05:33:03,240 --> 05:33:06,840 then we just need to decide which ones 7736 05:33:05,340 --> 05:33:09,540 we want to overwrite 7737 05:33:06,840 --> 05:33:13,020 so I'm grabbing all of the properties 7738 05:33:09,540 --> 05:33:15,240 with all of the values and if I just 7739 05:33:13,020 --> 05:33:18,180 want to overwrite one of them I say name 7740 05:33:15,240 --> 05:33:19,980 is equal on whatever value so in this 7741 05:33:18,180 --> 05:33:23,280 case there isn't so now everything is 7742 05:33:19,980 --> 05:33:25,798 going to work we'll still preserve those 7743 05:33:23,280 --> 05:33:29,580 current values so if I click over here 7744 05:33:25,798 --> 05:33:31,620 notice it changes to Susan hopefully 7745 05:33:29,580 --> 05:33:34,500 this is clear again 7746 05:33:31,620 --> 05:33:36,600 it's not a solution for 7747 05:33:34,500 --> 05:33:38,760 every use case of course there's going 7748 05:33:36,600 --> 05:33:42,420 to be use cases where you will have 7749 05:33:38,760 --> 05:33:43,680 multiple State dollars but this is a 7750 05:33:42,420 --> 05:33:46,020 Nifty approach 7751 05:33:43,680 --> 05:33:49,020 if you can 7752 05:33:46,020 --> 05:33:51,120 effectively group together the state 7753 05:33:49,020 --> 05:33:55,020 values since it's just going to save you 7754 05:33:51,120 --> 05:33:57,600 time on setting up the logic again be 7755 05:33:55,020 --> 05:34:00,060 aware of the gotchas it's very easy to 7756 05:33:57,600 --> 05:34:03,240 override the value and then run around 7757 05:34:00,060 --> 05:34:04,200 like a headless chicken and look for the 7758 05:34:03,240 --> 05:34:08,638 bug 7759 05:34:04,200 --> 05:34:11,040 okay before we wrap up your state and 7760 05:34:08,638 --> 05:34:14,340 move on to the next topic let's also 7761 05:34:11,040 --> 05:34:18,660 cover set function gotcha which by the 7762 05:34:14,340 --> 05:34:21,480 way we already touched upon and let's 7763 05:34:18,660 --> 05:34:24,600 take a look at the fix and a good use 7764 05:34:21,480 --> 05:34:28,200 case for using the effects and first 7765 05:34:24,600 --> 05:34:31,458 let's just set up a trivial challenge 7766 05:34:28,200 --> 05:34:34,440 let's go to the file here's State gotcha 7767 05:34:31,458 --> 05:34:36,540 installer all that and set up the state 7768 05:34:34,440 --> 05:34:38,878 value so again practice on setting up 7769 05:34:36,540 --> 05:34:40,378 the use State create a button like I 7770 05:34:38,878 --> 05:34:42,600 said in the beginning we'll have these 7771 05:34:40,378 --> 05:34:46,680 buttons don't worry in next examples 7772 05:34:42,600 --> 05:34:49,740 we'll start making more complex setups 7773 05:34:46,680 --> 05:34:53,040 add functionality to increase by one and 7774 05:34:49,740 --> 05:34:54,060 log the state value right after the set 7775 05:34:53,040 --> 05:34:56,360 function 7776 05:34:54,060 --> 05:34:58,920 and see what is going to be the result 7777 05:34:56,360 --> 05:35:00,900 so again let me just double check yep 7778 05:34:58,920 --> 05:35:03,660 and here I'm in Houston 7779 05:35:00,900 --> 05:35:04,740 okay that's awesome and then in the 7780 05:35:03,660 --> 05:35:07,500 gotcha 7781 05:35:04,740 --> 05:35:11,218 first I'm gonna get the use State here 7782 05:35:07,500 --> 05:35:13,320 so let me try the Auto Import yep that 7783 05:35:11,218 --> 05:35:15,900 works as far as the initial value I'm 7784 05:35:13,320 --> 05:35:19,680 going to go with zero and then 7785 05:35:15,900 --> 05:35:21,180 value and the function well 7786 05:35:19,680 --> 05:35:23,458 I'm not going to be particularly 7787 05:35:21,180 --> 05:35:27,540 original and I'm just gonna go with set 7788 05:35:23,458 --> 05:35:28,740 value so that is equal to my view State 7789 05:35:27,540 --> 05:35:31,138 okay awesome 7790 05:35:28,740 --> 05:35:33,120 after that we want to right away create 7791 05:35:31,138 --> 05:35:36,180 that handle click since I know that it's 7792 05:35:33,120 --> 05:35:39,900 going to be there so handle click that's 7793 05:35:36,180 --> 05:35:42,958 my function and in here 7794 05:35:39,900 --> 05:35:45,180 I just want to set up increase by one 7795 05:35:42,958 --> 05:35:48,900 so it's going to be equal to set value 7796 05:35:45,180 --> 05:35:50,820 and let's go with Value Plus one so the 7797 05:35:48,900 --> 05:35:53,520 gotcha is not here 7798 05:35:50,820 --> 05:35:57,600 again this is going to work as expected 7799 05:35:53,520 --> 05:35:59,340 then let's navigate to our jsx 7800 05:35:57,600 --> 05:36:01,798 we want to change this around it's not 7801 05:35:59,340 --> 05:36:05,040 going to be hitting two it's going to be 7802 05:36:01,798 --> 05:36:08,160 heading 1. with our value 7803 05:36:05,040 --> 05:36:11,400 so that is going to be correct here and 7804 05:36:08,160 --> 05:36:15,360 then let's also set up that button type 7805 05:36:11,400 --> 05:36:18,240 is equal to a button so button here 7806 05:36:15,360 --> 05:36:21,180 and let's add a class name class name 7807 05:36:18,240 --> 05:36:24,240 will be equal to BTN and let's say 7808 05:36:21,180 --> 05:36:26,940 increase and let's also not forget about 7809 05:36:24,240 --> 05:36:30,540 the handle click so on click 7810 05:36:26,940 --> 05:36:33,120 is equal to handle click 7811 05:36:30,540 --> 05:36:36,600 so once we have this one in place like I 7812 05:36:33,120 --> 05:36:38,040 said the gotcha is not in this setup so 7813 05:36:36,600 --> 05:36:40,200 this is going to work 7814 05:36:38,040 --> 05:36:42,298 what I want you to keep in mind 7815 05:36:40,200 --> 05:36:43,920 that this is not going to happen 7816 05:36:42,298 --> 05:36:48,120 synchronously 7817 05:36:43,920 --> 05:36:50,600 so if you log here the value 7818 05:36:48,120 --> 05:36:53,160 this won't be already the new value 7819 05:36:50,600 --> 05:36:55,740 notice once I refresh once I start 7820 05:36:53,160 --> 05:36:57,840 clicking this is one behind 7821 05:36:55,740 --> 05:37:01,620 so here I have the seven 7822 05:36:57,840 --> 05:37:04,620 and actually in the console I have a six 7823 05:37:01,620 --> 05:37:06,780 and again the problem is going to be if 7824 05:37:04,620 --> 05:37:10,138 you have some kind of functionality that 7825 05:37:06,780 --> 05:37:12,600 relies on that latest value 7826 05:37:10,138 --> 05:37:15,480 and in the following video we'll see how 7827 05:37:12,600 --> 05:37:18,420 we can get that latest value which 7828 05:37:15,480 --> 05:37:21,958 basically is the most recent State 7829 05:37:18,420 --> 05:37:24,180 update all right so now that we know set 7830 05:37:21,958 --> 05:37:26,760 function is not going to be synchronous 7831 05:37:24,180 --> 05:37:28,860 now let's look at the solution you see 7832 05:37:26,760 --> 05:37:32,580 there's another way how we can update 7833 05:37:28,860 --> 05:37:34,560 the state and that is using the function 7834 05:37:32,580 --> 05:37:37,320 approach so 7835 05:37:34,560 --> 05:37:40,378 instead of passing the new state value 7836 05:37:37,320 --> 05:37:41,760 like we have been doing for the last 7837 05:37:40,378 --> 05:37:45,600 whatever videos 7838 05:37:41,760 --> 05:37:49,458 we can actually pass in the function and 7839 05:37:45,600 --> 05:37:52,740 as a first parameter by default we get 7840 05:37:49,458 --> 05:37:54,718 the current state now since this is a 7841 05:37:52,740 --> 05:37:59,040 parameter yes 7842 05:37:54,718 --> 05:38:02,280 Bobby Lee or Joe Coy is an awesome name 7843 05:37:59,040 --> 05:38:04,798 but a more common convention is to call 7844 05:38:02,280 --> 05:38:07,560 this current state previous state Old 7845 05:38:04,798 --> 05:38:09,840 State whatever I mean again you can name 7846 05:38:07,560 --> 05:38:12,420 this banana it's still going to work and 7847 05:38:09,840 --> 05:38:13,860 yes it's by default provided 7848 05:38:12,420 --> 05:38:16,200 by react 7849 05:38:13,860 --> 05:38:18,480 it effectively just provides whatever 7850 05:38:16,200 --> 05:38:21,000 you have currently in a state so in this 7851 05:38:18,480 --> 05:38:24,180 case it would provide that number right 7852 05:38:21,000 --> 05:38:26,100 before the update and then in here we 7853 05:38:24,180 --> 05:38:29,100 just need to set up the functionality 7854 05:38:26,100 --> 05:38:31,200 where essentially we must return 7855 05:38:29,100 --> 05:38:33,180 something and this is one of the gotchas 7856 05:38:31,200 --> 05:38:35,340 with this approach 7857 05:38:33,180 --> 05:38:37,680 if you don't return anything it will 7858 05:38:35,340 --> 05:38:39,240 right away fail why well because there 7859 05:38:37,680 --> 05:38:41,580 is no value anymore it's now going to be 7860 05:38:39,240 --> 05:38:44,040 undefined remember in JavaScript 7861 05:38:41,580 --> 05:38:46,440 functions return by default what they 7862 05:38:44,040 --> 05:38:47,520 return undefined so you must return that 7863 05:38:46,440 --> 05:38:50,700 value 7864 05:38:47,520 --> 05:38:52,980 and whatever you're going to return from 7865 05:38:50,700 --> 05:38:55,560 that function that is going to be the 7866 05:38:52,980 --> 05:38:56,580 new value so let's try this one out but 7867 05:38:55,560 --> 05:38:58,798 essentially 7868 05:38:56,580 --> 05:39:00,660 I'm going to remove this one we'll pass 7869 05:38:58,798 --> 05:39:02,820 in the function yes this is what we're 7870 05:39:00,660 --> 05:39:05,878 doing we're passing function into the 7871 05:39:02,820 --> 05:39:07,798 set value that's the approach and like I 7872 05:39:05,878 --> 05:39:09,540 said if you simply 7873 05:39:07,798 --> 05:39:12,120 just leave it the way it is this is 7874 05:39:09,540 --> 05:39:14,100 going to be undefined now I do want to 7875 05:39:12,120 --> 05:39:16,200 showcase that we'll access the state 7876 05:39:14,100 --> 05:39:19,378 value and again in my case I'm going to 7877 05:39:16,200 --> 05:39:21,240 call this current state so this is the 7878 05:39:19,378 --> 05:39:23,040 state right before the update hopefully 7879 05:39:21,240 --> 05:39:24,840 that is clear and yes 7880 05:39:23,040 --> 05:39:26,760 the reason why I'm being so annoying and 7881 05:39:24,840 --> 05:39:27,840 repeating that this is provided by react 7882 05:39:26,760 --> 05:39:29,760 because 7883 05:39:27,840 --> 05:39:32,340 this is also one of those things that I 7884 05:39:29,760 --> 05:39:33,540 see a lot in cost q a yes we're not 7885 05:39:32,340 --> 05:39:36,540 doing anything 7886 05:39:33,540 --> 05:39:39,240 right out of the gate reactus provides 7887 05:39:36,540 --> 05:39:42,000 that value so let's go here with current 7888 05:39:39,240 --> 05:39:43,740 state and I'm purposely returning 7889 05:39:42,000 --> 05:39:46,138 nothing just so you can see that 7890 05:39:43,740 --> 05:39:48,000 essentially the functionality is not 7891 05:39:46,138 --> 05:39:50,580 going to work notice we get back 7892 05:39:48,000 --> 05:39:54,000 undefined now we did get the correct 7893 05:39:50,580 --> 05:39:56,400 zero over here right so if we have some 7894 05:39:54,000 --> 05:39:59,580 kind of functionality that relies on 7895 05:39:56,400 --> 05:40:02,820 that latest value we need to use this 7896 05:39:59,580 --> 05:40:05,040 approach now since I'm recording this in 7897 05:40:02,820 --> 05:40:06,958 tutorial I'm going to be more explicit 7898 05:40:05,040 --> 05:40:09,540 keep in mind that of course you can 7899 05:40:06,958 --> 05:40:12,000 simply return that new value in my case 7900 05:40:09,540 --> 05:40:15,840 though I'm going to go with const then 7901 05:40:12,000 --> 05:40:17,340 I'll say new state is equal to and then 7902 05:40:15,840 --> 05:40:20,580 I'm just going to go with current state 7903 05:40:17,340 --> 05:40:22,020 and then plus one and now let's just go 7904 05:40:20,580 --> 05:40:25,200 with the return 7905 05:40:22,020 --> 05:40:27,180 and new state and I'm not gonna log the 7906 05:40:25,200 --> 05:40:30,000 new state hopefully it's clear that yes 7907 05:40:27,180 --> 05:40:33,120 it's going to be latest value so we go 7908 05:40:30,000 --> 05:40:36,780 here and notice everything still works 7909 05:40:33,120 --> 05:40:39,120 only in this case we access that very 7910 05:40:36,780 --> 05:40:42,780 latest value and in the following video 7911 05:40:39,120 --> 05:40:45,900 we'll take a look at nice example where 7912 05:40:42,780 --> 05:40:47,760 such approach makes a lot of sense now 7913 05:40:45,900 --> 05:40:50,160 before you ask 7914 05:40:47,760 --> 05:40:52,860 do your after setup 7915 05:40:50,160 --> 05:40:54,000 set functions every time with function 7916 05:40:52,860 --> 05:40:56,100 I mean 7917 05:40:54,000 --> 05:40:57,600 it's really up to you there's going to 7918 05:40:56,100 --> 05:40:58,860 be some instances where you definitely 7919 05:40:57,600 --> 05:41:00,360 need to do that 7920 05:40:58,860 --> 05:41:01,500 something we're going to cover in the 7921 05:41:00,360 --> 05:41:04,260 next video 7922 05:41:01,500 --> 05:41:07,320 but should you go back and now refactor 7923 05:41:04,260 --> 05:41:10,798 every set function instance of this one 7924 05:41:07,320 --> 05:41:12,360 no I mean if it works with just a 7925 05:41:10,798 --> 05:41:14,280 regular approach where you're passing 7926 05:41:12,360 --> 05:41:16,320 the new value you can definitely leave 7927 05:41:14,280 --> 05:41:17,340 it some people use function for 7928 05:41:16,320 --> 05:41:19,500 everything 7929 05:41:17,340 --> 05:41:21,240 that's pretty much their preference 7930 05:41:19,500 --> 05:41:23,520 there's nothing wrong with that but 7931 05:41:21,240 --> 05:41:25,200 there is no clear rule that you always 7932 05:41:23,520 --> 05:41:26,878 always have to use this one again 7933 05:41:25,200 --> 05:41:28,860 there's going to be some instances where 7934 05:41:26,878 --> 05:41:30,660 you must use it otherwise the 7935 05:41:28,860 --> 05:41:33,660 functionality is not going to work but 7936 05:41:30,660 --> 05:41:37,020 if you're just changing I don't know a 7937 05:41:33,660 --> 05:41:39,000 list to an empty list no you don't have 7938 05:41:37,020 --> 05:41:41,878 to pass in the function you can just 7939 05:41:39,000 --> 05:41:44,120 pass in the new value all right and now 7940 05:41:41,878 --> 05:41:47,580 let's take a look at the good use case 7941 05:41:44,120 --> 05:41:50,820 where we need to use 7942 05:41:47,580 --> 05:41:52,500 function approach and for that for now 7943 05:41:50,820 --> 05:41:54,840 I'm just going to remove this one and 7944 05:41:52,500 --> 05:41:58,260 set everything from scratch and let's 7945 05:41:54,840 --> 05:42:02,458 imagine that we want to invoke 7946 05:41:58,260 --> 05:42:05,480 the functionality in some time and for 7947 05:42:02,458 --> 05:42:07,680 that we are going to use the set timer 7948 05:42:05,480 --> 05:42:09,958 and essentially 7949 05:42:07,680 --> 05:42:12,660 if you just pass here the set timer 7950 05:42:09,958 --> 05:42:15,360 you'll see that the functionality won't 7951 05:42:12,660 --> 05:42:17,280 work as expected so let's try this one 7952 05:42:15,360 --> 05:42:19,920 out I'm going to navigate back let's say 7953 05:42:17,280 --> 05:42:22,138 set timeout so now effectively I want to 7954 05:42:19,920 --> 05:42:23,340 change that value in some time let's say 7955 05:42:22,138 --> 05:42:25,320 three seconds 7956 05:42:23,340 --> 05:42:27,660 as we know we need to pass here the 7957 05:42:25,320 --> 05:42:29,520 Callback function first and then the 7958 05:42:27,660 --> 05:42:31,280 second in how long like I said in my 7959 05:42:29,520 --> 05:42:34,500 example it's going to be three seconds 7960 05:42:31,280 --> 05:42:38,040 and then let's simply go with set value 7961 05:42:34,500 --> 05:42:40,860 and then let's type Value Plus One now 7962 05:42:38,040 --> 05:42:43,860 what is our expectation first of all 7963 05:42:40,860 --> 05:42:47,638 well our expectation is that every time 7964 05:42:43,860 --> 05:42:50,280 we will click the button let's say five 7965 05:42:47,638 --> 05:42:52,560 times this is going to be invoked so set 7966 05:42:50,280 --> 05:42:55,080 timeout is going to be invoked and then 7967 05:42:52,560 --> 05:42:56,218 after three seconds we're going to get 7968 05:42:55,080 --> 05:42:59,940 that 7969 05:42:56,218 --> 05:43:02,160 latest Value Plus one so if I click five 7970 05:42:59,940 --> 05:43:04,320 times my expectation is 7971 05:43:02,160 --> 05:43:06,840 that I'm gonna see over here 7972 05:43:04,320 --> 05:43:10,080 value five correct are we on the same 7973 05:43:06,840 --> 05:43:13,280 page hopefully we are so let's refresh 7974 05:43:10,080 --> 05:43:15,900 and one two three four five 7975 05:43:13,280 --> 05:43:17,400 and the value did change in three 7976 05:43:15,900 --> 05:43:20,040 seconds but 7977 05:43:17,400 --> 05:43:21,900 it's definitely not five and I can click 7978 05:43:20,040 --> 05:43:23,160 all day long and the same thing is going 7979 05:43:21,900 --> 05:43:25,680 to happen 7980 05:43:23,160 --> 05:43:27,360 and if you don't believe me we can set 7981 05:43:25,680 --> 05:43:28,680 up a log over here 7982 05:43:27,360 --> 05:43:30,718 otherwise you might be thinking okay 7983 05:43:28,680 --> 05:43:34,620 he's just not clicking the button so 7984 05:43:30,718 --> 05:43:36,540 clicked the button let's try one more 7985 05:43:34,620 --> 05:43:39,060 time I apologize if you find this 7986 05:43:36,540 --> 05:43:40,680 annoying one two three four five 7987 05:43:39,060 --> 05:43:43,860 and 7988 05:43:40,680 --> 05:43:46,080 should see over here five yep so this is 7989 05:43:43,860 --> 05:43:49,320 correct I did click it five times 7990 05:43:46,080 --> 05:43:52,440 but again the value does not change now 7991 05:43:49,320 --> 05:43:56,458 why is that happening oh you see in our 7992 05:43:52,440 --> 05:43:57,900 callback function when we invoke set 7993 05:43:56,458 --> 05:44:02,458 value 7994 05:43:57,900 --> 05:44:06,180 we're not grabbing the latest value 7995 05:44:02,458 --> 05:44:07,860 we are grabbing the value that is within 7996 05:44:06,180 --> 05:44:09,058 these three seconds so essentially what 7997 05:44:07,860 --> 05:44:11,878 I'm saying 7998 05:44:09,058 --> 05:44:15,660 let's imagine that I'm gonna click the 7999 05:44:11,878 --> 05:44:18,360 button 100 times in those three seconds 8000 05:44:15,660 --> 05:44:22,260 all of them will reference this value of 8001 05:44:18,360 --> 05:44:25,680 zero and only once we switch then the 8002 05:44:22,260 --> 05:44:28,860 value is going to become 2. so again 8003 05:44:25,680 --> 05:44:30,660 regardless of how many times I click I'm 8004 05:44:28,860 --> 05:44:33,240 not updating that value right away or 8005 05:44:30,660 --> 05:44:34,378 here I'm just saying yep please update 8006 05:44:33,240 --> 05:44:36,540 that value 8007 05:44:34,378 --> 05:44:39,180 but I'm all the time using that old 8008 05:44:36,540 --> 05:44:41,458 value hopefully that is clear again you 8009 05:44:39,180 --> 05:44:44,100 can click five more times or six or 8010 05:44:41,458 --> 05:44:46,680 whatever again if that happened within 8011 05:44:44,100 --> 05:44:50,160 those three seconds it's all that I'm 8012 05:44:46,680 --> 05:44:52,680 gonna be using the value right before we 8013 05:44:50,160 --> 05:44:55,320 invoke the Callback function first time 8014 05:44:52,680 --> 05:44:58,200 hopefully that is clear now what is the 8015 05:44:55,320 --> 05:44:59,878 solution now we know how we can use the 8016 05:44:58,200 --> 05:45:02,820 function approach right 8017 05:44:59,878 --> 05:45:05,520 so we can pass in the function again we 8018 05:45:02,820 --> 05:45:07,920 have access right now to the latest the 8019 05:45:05,520 --> 05:45:10,860 current state value which by the way 8020 05:45:07,920 --> 05:45:14,040 will increase as we're clicking on a 8021 05:45:10,860 --> 05:45:15,780 button and then we just return the new 8022 05:45:14,040 --> 05:45:17,760 state value so in this case I'm going to 8023 05:45:15,780 --> 05:45:19,500 say current 8024 05:45:17,760 --> 05:45:22,378 and State 8025 05:45:19,500 --> 05:45:26,280 and we want to go with return 8026 05:45:22,378 --> 05:45:28,320 and let's go with current state plus one 8027 05:45:26,280 --> 05:45:31,260 and now you'll see that 8028 05:45:28,320 --> 05:45:34,440 or functionality works where essentially 8029 05:45:31,260 --> 05:45:35,878 again if I refresh and if I click 8030 05:45:34,440 --> 05:45:39,360 whatever times 8031 05:45:35,878 --> 05:45:42,298 in those three seconds I'll nicely 8032 05:45:39,360 --> 05:45:45,360 increase the state value 8033 05:45:42,298 --> 05:45:48,180 by the amount of times I clicked on a 8034 05:45:45,360 --> 05:45:50,638 button hopefully it's clear 8035 05:45:48,180 --> 05:45:52,798 we're pretty much done with use state 8036 05:45:50,638 --> 05:45:55,558 and we are ready to move on to our next 8037 05:45:52,798 --> 05:46:00,298 topic and up next let's talk about 8038 05:45:55,558 --> 05:46:03,360 another important hook called use effect 8039 05:46:00,298 --> 05:46:07,440 and we're gonna start just like the 8040 05:46:03,360 --> 05:46:10,620 other examples basically in the app GSX 8041 05:46:07,440 --> 05:46:14,040 we want to navigate this time to zero to 8042 05:46:10,620 --> 05:46:17,280 use effect we're looking for the starter 8043 05:46:14,040 --> 05:46:19,200 and we're gonna go with code example and 8044 05:46:17,280 --> 05:46:21,958 just like with use state 8045 05:46:19,200 --> 05:46:24,900 the first thing we're going to do is to 8046 05:46:21,958 --> 05:46:27,540 understand why do we need a use effect 8047 05:46:24,900 --> 05:46:30,120 in the first place and in order to do 8048 05:46:27,540 --> 05:46:34,138 that basically we'll take a look how the 8049 05:46:30,120 --> 05:46:37,320 code inside of the component runs 8050 05:46:34,138 --> 05:46:38,820 so let's navigate to that file you 8051 05:46:37,320 --> 05:46:42,240 should see on a screen something like 8052 05:46:38,820 --> 05:46:46,200 this value equals to zero and a button 8053 05:46:42,240 --> 05:46:49,020 so let me navigate over here and this 8054 05:46:46,200 --> 05:46:51,360 shouldn't look too foreign so we have 8055 05:46:49,020 --> 05:46:54,480 over here huge State we import that we 8056 05:46:51,360 --> 05:46:56,700 set up the value set value use State and 8057 05:46:54,480 --> 05:46:58,740 then of course I have jsx and what do 8058 05:46:56,700 --> 05:47:01,860 you know I'm updating the value and 8059 05:46:58,740 --> 05:47:04,860 again the gotcha is not over here 8060 05:47:01,860 --> 05:47:07,080 now let's talk about how the code runs 8061 05:47:04,860 --> 05:47:10,980 inside of the component 8062 05:47:07,080 --> 05:47:13,020 so let's say if I set up a function and 8063 05:47:10,980 --> 05:47:15,000 I'm going to call this say hello so 8064 05:47:13,020 --> 05:47:18,840 const say 8065 05:47:15,000 --> 05:47:20,458 and hello so that is equal to my 8066 05:47:18,840 --> 05:47:22,558 function 8067 05:47:20,458 --> 05:47:24,840 and inside of the function what do you 8068 05:47:22,558 --> 05:47:26,520 know I just simply want to set up some 8069 05:47:24,840 --> 05:47:30,420 console log 8070 05:47:26,520 --> 05:47:32,700 I want to go with log and hello there a 8071 05:47:30,420 --> 05:47:36,958 million dollar question again we're 8072 05:47:32,700 --> 05:47:38,700 doing the quiz yes you got that right so 8073 05:47:36,958 --> 05:47:40,320 previous time we went for ten thousand 8074 05:47:38,700 --> 05:47:41,638 dollar question why don't we go for a 8075 05:47:40,320 --> 05:47:44,040 million this time 8076 05:47:41,638 --> 05:47:46,798 so million dollar question 8077 05:47:44,040 --> 05:47:50,700 if I have a function over here 8078 05:47:46,798 --> 05:47:53,100 and if I invoke it and as I say not yes 8079 05:47:50,700 --> 05:47:56,280 the function declaration can be 8080 05:47:53,100 --> 05:47:58,798 somewhere else for example it can be in 8081 05:47:56,280 --> 05:48:00,958 different file or it can be above the 8082 05:47:58,798 --> 05:48:04,980 component my question is 8083 05:48:00,958 --> 05:48:06,420 if I invoke this function inside of the 8084 05:48:04,980 --> 05:48:09,600 component 8085 05:48:06,420 --> 05:48:12,000 how often do you think it's going to run 8086 05:48:09,600 --> 05:48:14,580 and possible choices 8087 05:48:12,000 --> 05:48:17,400 we have initial render basically when 8088 05:48:14,580 --> 05:48:18,660 the component mounts like so so once I 8089 05:48:17,400 --> 05:48:20,878 refresh 8090 05:48:18,660 --> 05:48:24,240 only then the function is going to 8091 05:48:20,878 --> 05:48:26,520 execute then I have after every render 8092 05:48:24,240 --> 05:48:28,980 and as I know you can already kind of 8093 05:48:26,520 --> 05:48:31,378 see that it's definitely happening when 8094 05:48:28,980 --> 05:48:33,000 the component mounts then we have after 8095 05:48:31,378 --> 05:48:36,058 every render 8096 05:48:33,000 --> 05:48:39,298 and then we have I don't know randomly 8097 05:48:36,058 --> 05:48:41,638 after every third render and if you 8098 05:48:39,298 --> 05:48:43,680 guessed that it's actually happening 8099 05:48:41,638 --> 05:48:46,200 after every render you are absolutely 8100 05:48:43,680 --> 05:48:48,360 correct basically the initial render 8101 05:48:46,200 --> 05:48:51,120 which again happens when the component 8102 05:48:48,360 --> 05:48:53,580 mounts in this case when we load the 8103 05:48:51,120 --> 05:48:57,840 application let's say when we refresh 8104 05:48:53,580 --> 05:48:59,700 the browser and all that and also every 8105 05:48:57,840 --> 05:49:01,378 time we re-render 8106 05:48:59,700 --> 05:49:04,440 because what are we doing over here 8107 05:49:01,378 --> 05:49:07,080 remember we use state every time we'll 8108 05:49:04,440 --> 05:49:08,218 change the value we will re-render 8109 05:49:07,080 --> 05:49:10,260 correct 8110 05:49:08,218 --> 05:49:12,958 so let's try this one out 8111 05:49:10,260 --> 05:49:15,840 notice I start clicking and what do you 8112 05:49:12,958 --> 05:49:16,980 know in the console I have bunch of 8113 05:49:15,840 --> 05:49:20,638 hello there's 8114 05:49:16,980 --> 05:49:24,900 now it's not a good thing or a bad thing 8115 05:49:20,638 --> 05:49:27,660 it's just a fact that's how it works if 8116 05:49:24,900 --> 05:49:29,400 you have some kind of function here that 8117 05:49:27,660 --> 05:49:32,700 is being invoked 8118 05:49:29,400 --> 05:49:34,860 and for example this declaration yes 8119 05:49:32,700 --> 05:49:37,138 it's going to be set up pretty much from 8120 05:49:34,860 --> 05:49:38,218 scratch every time so every time we'll 8121 05:49:37,138 --> 05:49:40,860 re-render 8122 05:49:38,218 --> 05:49:43,980 react is going to create this function 8123 05:49:40,860 --> 05:49:45,180 and also if you invoke the function it 8124 05:49:43,980 --> 05:49:47,700 will run the code 8125 05:49:45,180 --> 05:49:50,760 now when do we need to be careful 8126 05:49:47,700 --> 05:49:54,180 well when we update the state why 8127 05:49:50,760 --> 05:49:56,100 because we can trigger infinite Loop so 8128 05:49:54,180 --> 05:49:59,540 let me showcase that I'm going to add 8129 05:49:56,100 --> 05:50:03,840 comment here so be careful 8130 05:49:59,540 --> 05:50:08,040 you will have infinite Loop 8131 05:50:03,840 --> 05:50:10,080 now how can we have infinite Loop if we 8132 05:50:08,040 --> 05:50:12,240 update the state value inside of this 8133 05:50:10,080 --> 05:50:14,218 function so let's go over here with set 8134 05:50:12,240 --> 05:50:17,520 value and then I'm going to go to Value 8135 05:50:14,218 --> 05:50:19,798 Plus One and before we run it let's 8136 05:50:17,520 --> 05:50:22,378 discuss why we're gonna have the 8137 05:50:19,798 --> 05:50:25,320 infinite Loop well let's think about it 8138 05:50:22,378 --> 05:50:28,320 so we have initial render this is where 8139 05:50:25,320 --> 05:50:32,160 we set up the state value and also we 8140 05:50:28,320 --> 05:50:35,580 set up the function and we invoke it 8141 05:50:32,160 --> 05:50:38,638 now inside of the function what do we do 8142 05:50:35,580 --> 05:50:41,100 we trigger re-render correct we go with 8143 05:50:38,638 --> 05:50:43,980 Value Plus One 8144 05:50:41,100 --> 05:50:47,218 so when we re-render again we do the 8145 05:50:43,980 --> 05:50:48,900 same thing we create a state value and 8146 05:50:47,218 --> 05:50:51,000 we set up the function inside of the 8147 05:50:48,900 --> 05:50:52,980 function we do what we update the state 8148 05:50:51,000 --> 05:50:54,120 value and hopefully you see where I'm 8149 05:50:52,980 --> 05:50:57,180 going with this 8150 05:50:54,120 --> 05:51:00,180 so hopefully this is clear and now let 8151 05:50:57,180 --> 05:51:03,000 me just save the file and you'll right 8152 05:51:00,180 --> 05:51:05,580 away see a bunch of errors in console so 8153 05:51:03,000 --> 05:51:07,500 again just be careful yes there are 8154 05:51:05,580 --> 05:51:09,660 going to be times when you will set up a 8155 05:51:07,500 --> 05:51:11,638 function here actually by the end of the 8156 05:51:09,660 --> 05:51:13,020 section I'll share a resource where 8157 05:51:11,638 --> 05:51:15,718 you'll see that 8158 05:51:13,020 --> 05:51:17,878 react is somewhat pushing for it 8159 05:51:15,718 --> 05:51:20,340 instead of using a bunch of use effects 8160 05:51:17,878 --> 05:51:23,040 to use functions like this but again 8161 05:51:20,340 --> 05:51:24,958 there are nuances we need to be mindful 8162 05:51:23,040 --> 05:51:27,120 when we set up such functions there's 8163 05:51:24,958 --> 05:51:29,218 nothing wrong with setting them up the 8164 05:51:27,120 --> 05:51:33,240 gotcha is you don't want to update the 8165 05:51:29,218 --> 05:51:36,058 state in such a fashion because yes you 8166 05:51:33,240 --> 05:51:38,820 will have an infinite Loop and that's 8167 05:51:36,058 --> 05:51:42,058 why in the next video we'll cover use 8168 05:51:38,820 --> 05:51:44,120 effect and how use effect helps us to 8169 05:51:42,058 --> 05:51:46,500 run code 8170 05:51:44,120 --> 05:51:50,340 conditionally in our application 8171 05:51:46,500 --> 05:51:53,100 basically we'll be able to choose when 8172 05:51:50,340 --> 05:51:55,558 do we want to run certain code inside of 8173 05:51:53,100 --> 05:51:59,160 our component all right so now let's get 8174 05:51:55,558 --> 05:52:03,120 familiar with use effect hook so in the 8175 05:51:59,160 --> 05:52:05,878 app.js I'm importing or I guess app.jsx 8176 05:52:03,120 --> 05:52:07,680 I'm importing the second one use effect 8177 05:52:05,878 --> 05:52:11,040 Basics and you'll notice that pretty 8178 05:52:07,680 --> 05:52:13,860 much it's the same starting point so if 8179 05:52:11,040 --> 05:52:15,718 I go here yes I have the same function I 8180 05:52:13,860 --> 05:52:18,540 have the log now in this case I'm not 8181 05:52:15,718 --> 05:52:21,600 updating the state value and I also have 8182 05:52:18,540 --> 05:52:23,878 my state value on a button and 8183 05:52:21,600 --> 05:52:27,378 essentially when it comes to use effect 8184 05:52:23,878 --> 05:52:30,780 hook in react it allows us to perform 8185 05:52:27,378 --> 05:52:33,900 side effect in the function component 8186 05:52:30,780 --> 05:52:36,840 now there's no need for Urban Dictionary 8187 05:52:33,900 --> 05:52:39,360 basically any work outside of the 8188 05:52:36,840 --> 05:52:40,980 component and if at the moment it 8189 05:52:39,360 --> 05:52:43,320 doesn't make any sense 8190 05:52:40,980 --> 05:52:45,958 trust me it's going to be more useful if 8191 05:52:43,320 --> 05:52:48,420 we cover some examples essentially it's 8192 05:52:45,958 --> 05:52:51,000 things like subscriptions fetching data 8193 05:52:48,420 --> 05:52:53,820 fetching data is by the way very popular 8194 05:52:51,000 --> 05:52:57,000 or let's say if we want to directly 8195 05:52:53,820 --> 05:52:58,860 update the Dom remember in vanilla.js we 8196 05:52:57,000 --> 05:53:01,200 can select the Dom nodes with things 8197 05:52:58,860 --> 05:53:03,958 like query selector and all that 8198 05:53:01,200 --> 05:53:05,820 this is where you would do that we also 8199 05:53:03,958 --> 05:53:07,860 can set up some event listeners and 8200 05:53:05,820 --> 05:53:09,718 timers and all that kind of stuff we'll 8201 05:53:07,860 --> 05:53:10,798 pretty much cover all of these examples 8202 05:53:09,718 --> 05:53:13,260 so 8203 05:53:10,798 --> 05:53:16,138 please just be patient and essentially 8204 05:53:13,260 --> 05:53:18,958 when it comes to use effect hook 8205 05:53:16,138 --> 05:53:20,940 we import use effect it's looking for 8206 05:53:18,958 --> 05:53:22,440 two arguments the second one is optional 8207 05:53:20,940 --> 05:53:23,400 and the first one is the Callback 8208 05:53:22,440 --> 05:53:25,798 function 8209 05:53:23,400 --> 05:53:28,020 and effectively whatever you have inside 8210 05:53:25,798 --> 05:53:31,860 of that Cobalt version is going to run 8211 05:53:28,020 --> 05:53:33,780 now by default by default it runs after 8212 05:53:31,860 --> 05:53:36,780 every render 8213 05:53:33,780 --> 05:53:39,540 which means pretty much initial render 8214 05:53:36,780 --> 05:53:42,120 and re-renders but there's a caveat 8215 05:53:39,540 --> 05:53:44,820 where we can provide the dependency 8216 05:53:42,120 --> 05:53:46,860 array and somewhat important we cannot 8217 05:53:44,820 --> 05:53:49,440 return a promise from the Callback 8218 05:53:46,860 --> 05:53:52,260 function so let's tackle all of those 8219 05:53:49,440 --> 05:53:54,540 things first we want to import use 8220 05:53:52,260 --> 05:53:57,000 effect from react whatever we do we 8221 05:53:54,540 --> 05:54:00,420 simply go with use and then the name is 8222 05:53:57,000 --> 05:54:03,180 use effect like I said hook starts with 8223 05:54:00,420 --> 05:54:06,000 use so all the hooks our hooks react 8224 05:54:03,180 --> 05:54:06,840 hooks they will start with use okay that 8225 05:54:06,000 --> 05:54:10,920 is clear 8226 05:54:06,840 --> 05:54:12,660 then let's go I guess pass say hello and 8227 05:54:10,920 --> 05:54:15,120 let's just go with huge effect like I 8228 05:54:12,660 --> 05:54:16,500 said it's looking for two arguments the 8229 05:54:15,120 --> 05:54:18,540 first one is going to be the Callback 8230 05:54:16,500 --> 05:54:20,458 function which is going to be invoked 8231 05:54:18,540 --> 05:54:23,100 well that depends what we have 8232 05:54:20,458 --> 05:54:26,100 independency array by default it's going 8233 05:54:23,100 --> 05:54:28,320 to be invoked after every render so 8234 05:54:26,100 --> 05:54:30,900 let's try this one out let's go here 8235 05:54:28,320 --> 05:54:33,540 with the function again you can pass the 8236 05:54:30,900 --> 05:54:35,700 reference you can pass here the arrow 8237 05:54:33,540 --> 05:54:38,820 function that is really up to you and 8238 05:54:35,700 --> 05:54:42,840 let's go here with log and I'm going to 8239 05:54:38,820 --> 05:54:45,540 say hello from and use effect 8240 05:54:42,840 --> 05:54:48,600 and what you'll notice that 8241 05:54:45,540 --> 05:54:50,400 we have few loaders so let me refresh 8242 05:54:48,600 --> 05:54:52,020 basically I want to start from scratch 8243 05:54:50,400 --> 05:54:54,958 and 8244 05:54:52,020 --> 05:54:57,780 you'll also see hello from use effect 8245 05:54:54,958 --> 05:55:00,660 and basically every time I'll click 8246 05:54:57,780 --> 05:55:02,760 we'll have both of those logs and as 8247 05:55:00,660 --> 05:55:04,378 you're looking at you're like well wait 8248 05:55:02,760 --> 05:55:07,920 a minute pal 8249 05:55:04,378 --> 05:55:10,138 we were supposed to fix this issue while 8250 05:55:07,920 --> 05:55:11,280 you're showing us the use effect well 8251 05:55:10,138 --> 05:55:14,520 remember 8252 05:55:11,280 --> 05:55:17,400 this is the default Behavior so by 8253 05:55:14,520 --> 05:55:20,160 default it runs on each render however 8254 05:55:17,400 --> 05:55:22,200 there's a second argument that we can 8255 05:55:20,160 --> 05:55:24,540 pass which technically isn't optional 8256 05:55:22,200 --> 05:55:26,760 but I mean quite often you'll pass that 8257 05:55:24,540 --> 05:55:30,360 argument and that is the dependency 8258 05:55:26,760 --> 05:55:31,860 array and if we set up the dependency 8259 05:55:30,360 --> 05:55:34,680 area empty 8260 05:55:31,860 --> 05:55:37,020 then it's only going to run on the 8261 05:55:34,680 --> 05:55:39,298 initial render so let's try this one out 8262 05:55:37,020 --> 05:55:41,878 like I said second argument so we go 8263 05:55:39,298 --> 05:55:44,400 here with comma and then we pass in the 8264 05:55:41,878 --> 05:55:46,138 dependency array so now once I save 8265 05:55:44,400 --> 05:55:48,240 check it out 8266 05:55:46,138 --> 05:55:49,980 you'll see that yes we have the initial 8267 05:55:48,240 --> 05:55:51,000 render and all that everything is 8268 05:55:49,980 --> 05:55:54,420 beautiful 8269 05:55:51,000 --> 05:55:56,580 but once we click the button actually 8270 05:55:54,420 --> 05:55:59,340 you won't see the second log in the 8271 05:55:56,580 --> 05:56:03,958 console so notice over here I only have 8272 05:55:59,340 --> 05:56:06,600 the hello there however the login 8273 05:56:03,958 --> 05:56:09,958 use effect only runs 8274 05:56:06,600 --> 05:56:12,058 in the initial render that's it so 8275 05:56:09,958 --> 05:56:15,000 that's the biggest difference if we have 8276 05:56:12,058 --> 05:56:16,980 just a plane function or we invoke the 8277 05:56:15,000 --> 05:56:20,100 function inside of the 8278 05:56:16,980 --> 05:56:23,280 component yes it's going to run 8279 05:56:20,100 --> 05:56:25,980 on initial render and on every re-render 8280 05:56:23,280 --> 05:56:29,040 our with use effect we can start 8281 05:56:25,980 --> 05:56:29,878 controlling when this functionality 8282 05:56:29,040 --> 05:56:32,700 around 8283 05:56:29,878 --> 05:56:35,340 now lastly I just want to mention that 8284 05:56:32,700 --> 05:56:38,700 from this function we don't want to 8285 05:56:35,340 --> 05:56:40,400 return a promise so 8286 05:56:38,700 --> 05:56:43,798 later on we'll be setting up 8287 05:56:40,400 --> 05:56:46,980 functionality to fetch data and a pretty 8288 05:56:43,798 --> 05:56:49,558 common approach is to go with a sync 8289 05:56:46,980 --> 05:56:51,298 right so we set up our function to be a 8290 05:56:49,558 --> 05:56:53,520 sync and then we can wait for something 8291 05:56:51,298 --> 05:56:55,798 fetch axios whatever 8292 05:56:53,520 --> 05:56:57,958 now we cannot do that with use effect 8293 05:56:55,798 --> 05:56:59,100 because there's a special thing that we 8294 05:56:57,958 --> 05:57:01,740 are returning from this use effect 8295 05:56:59,100 --> 05:57:05,040 something we're going to cover a cleanup 8296 05:57:01,740 --> 05:57:07,440 function and remember a sync functions 8297 05:57:05,040 --> 05:57:10,080 what do they return do I need to do 8298 05:57:07,440 --> 05:57:12,840 another quiz or no I think no so I'm 8299 05:57:10,080 --> 05:57:15,480 just gonna tell it to you async 8300 05:57:12,840 --> 05:57:17,878 functions return promise 8301 05:57:15,480 --> 05:57:20,520 so if we set up this function as a sync 8302 05:57:17,878 --> 05:57:23,280 it will return a promise and use effect 8303 05:57:20,520 --> 05:57:26,100 is not okay with that now keep in mind 8304 05:57:23,280 --> 05:57:28,500 within the Callback function I can still 8305 05:57:26,100 --> 05:57:33,240 set up a synchronous function and invoke 8306 05:57:28,500 --> 05:57:37,558 it so if I go here with some funk I set 8307 05:57:33,240 --> 05:57:38,580 it up as a sync and basically have the 8308 05:57:37,558 --> 05:57:41,520 await 8309 05:57:38,580 --> 05:57:43,200 keyword inside of it and again just for 8310 05:57:41,520 --> 05:57:45,420 sake of it I'm gonna go with Fetch and 8311 05:57:43,200 --> 05:57:49,260 I'm not going to provide anything and if 8312 05:57:45,420 --> 05:57:52,740 I invoke something this is okay you just 8313 05:57:49,260 --> 05:57:54,780 don't want to set up the first argument 8314 05:57:52,740 --> 05:57:58,138 the Callback function that we're 8315 05:57:54,780 --> 05:58:00,600 providing as a sync and don't worry once 8316 05:57:58,138 --> 05:58:03,480 we start fetching data most likely I'll 8317 05:58:00,600 --> 05:58:06,120 come back and discuss this particular 8318 05:58:03,480 --> 05:58:09,120 thing one more time so now let me just 8319 05:58:06,120 --> 05:58:12,298 remove that let's save and hopefully 8320 05:58:09,120 --> 05:58:15,020 it's clear that we have use effect talk 8321 05:58:12,298 --> 05:58:18,780 we import that from react we invoke it 8322 05:58:15,020 --> 05:58:21,600 and we provide few things 8323 05:58:18,780 --> 05:58:23,580 we provide a callback function which is 8324 05:58:21,600 --> 05:58:27,420 going to be invoked 8325 05:58:23,580 --> 05:58:31,500 pretty much after every render unless we 8326 05:58:27,420 --> 05:58:34,020 provide here a dependency array in that 8327 05:58:31,500 --> 05:58:35,218 case if we have dependency array and if 8328 05:58:34,020 --> 05:58:37,440 it's empty 8329 05:58:35,218 --> 05:58:40,798 the functionality inside of the use 8330 05:58:37,440 --> 05:58:43,860 effect is only going to run once 8331 05:58:40,798 --> 05:58:46,620 only when the component mounts on the 8332 05:58:43,860 --> 05:58:49,740 initial render unlike the regular 8333 05:58:46,620 --> 05:58:52,740 function which is going to be 8334 05:58:49,740 --> 05:58:55,798 declared and invoked 8335 05:58:52,740 --> 05:58:59,638 on initial render and also on every 8336 05:58:55,798 --> 05:59:03,240 re-render okay and up next let's talk 8337 05:58:59,638 --> 05:59:06,660 about dependency array and how we can 8338 05:59:03,240 --> 05:59:07,740 have multiple user effects in our 8339 05:59:06,660 --> 05:59:10,680 application 8340 05:59:07,740 --> 05:59:14,580 so in the app.js we want to navigate to 8341 05:59:10,680 --> 05:59:16,680 zero three and multiple effects and once 8342 05:59:14,580 --> 05:59:19,320 we navigate to this file you'll see use 8343 05:59:16,680 --> 05:59:21,420 State you'll see user effect and 8344 05:59:19,320 --> 05:59:23,520 essentially two State values 8345 05:59:21,420 --> 05:59:25,558 this is the case where I didn't see the 8346 05:59:23,520 --> 05:59:27,480 point of creating all of this from 8347 05:59:25,558 --> 05:59:30,540 scratch there's always so many times 8348 05:59:27,480 --> 05:59:33,540 when creating a button and a state value 8349 05:59:30,540 --> 05:59:36,120 is useful and notice over here we have 8350 05:59:33,540 --> 05:59:38,280 two heading ones in the jsx with two 8351 05:59:36,120 --> 05:59:41,400 buttons and guess what pretty much every 8352 05:59:38,280 --> 05:59:43,558 time we'll click we'll update that state 8353 05:59:41,400 --> 05:59:47,218 value and let's start with the simple 8354 05:59:43,558 --> 05:59:50,160 fact that yes you can have as many use 8355 05:59:47,218 --> 05:59:51,660 effects in your component as you want so 8356 05:59:50,160 --> 05:59:54,660 pretty much you can set up a use effect 8357 05:59:51,660 --> 05:59:56,700 for every smallest feature now just 8358 05:59:54,660 --> 05:59:58,138 because you can doesn't mean you should 8359 05:59:56,700 --> 06:00:00,058 and again I'm going to return to the 8360 05:59:58,138 --> 06:00:02,520 point that at the very end of this 8361 06:00:00,058 --> 06:00:05,940 section I'll show you a very useful 8362 06:00:02,520 --> 06:00:08,400 resource which is going to cover all of 8363 06:00:05,940 --> 06:00:10,740 these cases in more detail so please be 8364 06:00:08,400 --> 06:00:13,260 patient for now just remember just 8365 06:00:10,740 --> 06:00:15,240 because you can have like 10 use effects 8366 06:00:13,260 --> 06:00:17,458 in your component doesn't mean that you 8367 06:00:15,240 --> 06:00:19,740 should do that and essentially if I go 8368 06:00:17,458 --> 06:00:23,520 here with the first use effect if I 8369 06:00:19,740 --> 06:00:24,840 uncomment we'll see that basically once 8370 06:00:23,520 --> 06:00:27,718 I refresh 8371 06:00:24,840 --> 06:00:29,520 there is initial load right so this is 8372 06:00:27,718 --> 06:00:31,500 where we'll have that console log and 8373 06:00:29,520 --> 06:00:34,138 all that and then pretty much every time 8374 06:00:31,500 --> 06:00:36,360 we'll update the state values I mean 8375 06:00:34,138 --> 06:00:38,580 we'll see nothing in the console and the 8376 06:00:36,360 --> 06:00:42,000 same deal we can set up another use 8377 06:00:38,580 --> 06:00:44,878 effect that also is going to run only 8378 06:00:42,000 --> 06:00:47,280 when the application loads and again I 8379 06:00:44,878 --> 06:00:50,340 can keep clicking all day long and I'll 8380 06:00:47,280 --> 06:00:53,520 only see those two logs in the console 8381 06:00:50,340 --> 06:00:56,100 so that covers multiple use effects yes 8382 06:00:53,520 --> 06:00:58,860 you can have them in the component 8383 06:00:56,100 --> 06:00:59,940 doesn't mean that you can only have one 8384 06:00:58,860 --> 06:01:01,860 use effect 8385 06:00:59,940 --> 06:01:04,860 now also let's talk about this 8386 06:01:01,860 --> 06:01:08,040 dependency array notice how when it's 8387 06:01:04,860 --> 06:01:10,798 empty it only runs when the component 8388 06:01:08,040 --> 06:01:13,378 mounts correct but we can provide values 8389 06:01:10,798 --> 06:01:15,958 over here so if I'm going to go here 8390 06:01:13,378 --> 06:01:18,718 with value which essentially is my first 8391 06:01:15,958 --> 06:01:20,458 one you'll notice that not only this 8392 06:01:18,718 --> 06:01:23,700 user Factor runs 8393 06:01:20,458 --> 06:01:27,180 when the component mounts and again for 8394 06:01:23,700 --> 06:01:29,580 that I'll have to refresh but also 8395 06:01:27,180 --> 06:01:32,878 when we update this value 8396 06:01:29,580 --> 06:01:36,360 so this is already very powerful where 8397 06:01:32,878 --> 06:01:38,520 not only this functionality is invoked 8398 06:01:36,360 --> 06:01:40,378 on the mount but also when we're 8399 06:01:38,520 --> 06:01:42,718 clicking on the first button however 8400 06:01:40,378 --> 06:01:44,878 it's not going to happen if we click on 8401 06:01:42,718 --> 06:01:49,020 the second one it's only going to happen 8402 06:01:44,878 --> 06:01:52,378 if I add the second value over here 8403 06:01:49,020 --> 06:01:54,660 yes at that point 8404 06:01:52,378 --> 06:01:57,600 we'll have a log when we click on the 8405 06:01:54,660 --> 06:02:00,000 first one as well as the second one and 8406 06:01:57,600 --> 06:02:02,820 as you can see this is very powerful 8407 06:02:00,000 --> 06:02:05,458 stuff since I can have one use effect 8408 06:02:02,820 --> 06:02:08,040 for one value and then the second one is 8409 06:02:05,458 --> 06:02:09,660 going to be for the second one and you 8410 06:02:08,040 --> 06:02:11,520 can probably already guess that the 8411 06:02:09,660 --> 06:02:14,100 first one is only going to show up if we 8412 06:02:11,520 --> 06:02:16,080 click over here and then the second one 8413 06:02:14,100 --> 06:02:18,120 is going to be when we click on the 8414 06:02:16,080 --> 06:02:20,580 second button and of course you're not 8415 06:02:18,120 --> 06:02:22,740 limited to the amount of values you can 8416 06:02:20,580 --> 06:02:25,080 pass over here you can start with empty 8417 06:02:22,740 --> 06:02:28,138 dependency array and then you can have 8418 06:02:25,080 --> 06:02:30,298 three five or whatever so that again 8419 06:02:28,138 --> 06:02:32,580 really depends on situation just keep in 8420 06:02:30,298 --> 06:02:35,040 mind few things first you can have 8421 06:02:32,580 --> 06:02:37,440 multiple use effects again I'm not 8422 06:02:35,040 --> 06:02:41,100 saying you should but you can and then 8423 06:02:37,440 --> 06:02:42,680 the second one is the dependency array 8424 06:02:41,100 --> 06:02:45,958 where we can pass 8425 06:02:42,680 --> 06:02:49,080 multiple values and every time that 8426 06:02:45,958 --> 06:02:52,680 value is going to change we will invoke 8427 06:02:49,080 --> 06:02:55,740 the functionality inside of the Callback 8428 06:02:52,680 --> 06:02:58,680 function one more time and once we're 8429 06:02:55,740 --> 06:03:00,798 familiar with the user effect basics 8430 06:02:58,680 --> 06:03:04,520 why don't we work on a little challenge 8431 06:03:00,798 --> 06:03:09,840 we're essentially I want you to 8432 06:03:04,520 --> 06:03:12,360 fetch some GitHub users from the URL and 8433 06:03:09,840 --> 06:03:14,400 render them on the screen 8434 06:03:12,360 --> 06:03:17,160 now before we continue let me just 8435 06:03:14,400 --> 06:03:20,580 mention that during this video I will 8436 06:03:17,160 --> 06:03:23,878 use fetch API and yes I'm fully aware 8437 06:03:20,580 --> 06:03:25,920 that there's Library called axios and in 8438 06:03:23,878 --> 06:03:28,080 fact we will 8439 06:03:25,920 --> 06:03:30,240 use it later on in the course we'll have 8440 06:03:28,080 --> 06:03:33,120 tutorial and then for more complex 8441 06:03:30,240 --> 06:03:36,240 projects we'll definitely use axios and 8442 06:03:33,120 --> 06:03:38,458 if you're not a fan of fetch you can 8443 06:03:36,240 --> 06:03:42,120 definitely install axios and use it but 8444 06:03:38,458 --> 06:03:43,920 in my opinion for simple examples fetch 8445 06:03:42,120 --> 06:03:46,260 is good enough now if you're not 8446 06:03:43,920 --> 06:03:50,280 familiar with fetch at all then I 8447 06:03:46,260 --> 06:03:52,798 suggest utilizing this link effectively 8448 06:03:50,280 --> 06:03:56,458 this is the Jazz nuggets video where I 8449 06:03:52,798 --> 06:03:58,740 cover fetch API from scratch and as far 8450 06:03:56,458 --> 06:04:01,620 as the challenge we want to go to app 8451 06:03:58,740 --> 06:04:04,378 jsx we want to get the star from zero 8452 06:04:01,620 --> 06:04:07,320 four fetch data 8453 06:04:04,378 --> 06:04:09,900 and essentially in there we want to set 8454 06:04:07,320 --> 06:04:12,298 up the state and set up the use effect 8455 06:04:09,900 --> 06:04:14,840 and as far as the users the default 8456 06:04:12,298 --> 06:04:18,958 value is going to be empty all right 8457 06:04:14,840 --> 06:04:21,540 then set up a user effect but make sure 8458 06:04:18,958 --> 06:04:24,420 that it only runs on initial render keep 8459 06:04:21,540 --> 06:04:26,820 in mind that the URL I provided it has 8460 06:04:24,420 --> 06:04:29,700 some rate limits so if you'll just be 8461 06:04:26,820 --> 06:04:32,520 randomly testing out stuff by omitting 8462 06:04:29,700 --> 06:04:34,558 the dependency array well you'll exceed 8463 06:04:32,520 --> 06:04:36,600 that limit then you'll basically have to 8464 06:04:34,558 --> 06:04:39,298 wait to work on the challenge 8465 06:04:36,600 --> 06:04:42,420 then in the Callback function in the 8466 06:04:39,298 --> 06:04:45,120 user effect create a function which 8467 06:04:42,420 --> 06:04:48,540 performs fetch functionality 8468 06:04:45,120 --> 06:04:51,240 and use the URL I provided in Star file 8469 06:04:48,540 --> 06:04:53,280 now you can go with that then or async 8470 06:04:51,240 --> 06:04:56,160 that's really up to you so either you 8471 06:04:53,280 --> 06:04:58,558 can set up the async function or since 8472 06:04:56,160 --> 06:05:01,740 fetch returns a promise you can go with 8473 06:04:58,558 --> 06:05:02,820 that 10. and for now I suggest just 8474 06:05:01,740 --> 06:05:06,420 logging 8475 06:05:02,820 --> 06:05:09,180 so if you're somewhat confused just try 8476 06:05:06,420 --> 06:05:10,500 to set up fetch functionality and log 8477 06:05:09,180 --> 06:05:11,878 them 8478 06:05:10,500 --> 06:05:14,400 nothing more 8479 06:05:11,878 --> 06:05:16,878 just log the users if you can log the 8480 06:05:14,400 --> 06:05:19,980 users you already in good shape 8481 06:05:16,878 --> 06:05:24,660 now if you want to challenge yourself 8482 06:05:19,980 --> 06:05:28,320 more then set up users equal to a result 8483 06:05:24,660 --> 06:05:30,360 just remember that fetch returns a 8484 06:05:28,320 --> 06:05:32,820 promise we need to go with that then and 8485 06:05:30,360 --> 06:05:36,058 then we need to return that Json in 8486 06:05:32,820 --> 06:05:39,480 order to get the data and I want to set 8487 06:05:36,058 --> 06:05:41,100 that result from that Json equal to my 8488 06:05:39,480 --> 06:05:43,320 users 8489 06:05:41,100 --> 06:05:45,780 the state value and then I want to 8490 06:05:43,320 --> 06:05:46,980 iterate over the list and basically 8491 06:05:45,780 --> 06:05:49,860 display them 8492 06:05:46,980 --> 06:05:52,320 now I don't suggest worrying about CSS 8493 06:05:49,860 --> 06:05:55,400 it doesn't really matter 8494 06:05:52,320 --> 06:05:58,340 you can show them in any way you want 8495 06:05:55,400 --> 06:06:01,138 effectively I just want to show the 8496 06:05:58,340 --> 06:06:05,100 profile the link to the profile as well 8497 06:06:01,138 --> 06:06:06,900 as their image that's it and a tiny hint 8498 06:06:05,100 --> 06:06:08,878 if you don't want to deal with big 8499 06:06:06,900 --> 06:06:11,878 images and if you don't want to set them 8500 06:06:08,878 --> 06:06:13,980 up in CSS just add inline style for that 8501 06:06:11,878 --> 06:06:16,980 image and set up the width for I don't 8502 06:06:13,980 --> 06:06:19,980 know 50 pixels or something along those 8503 06:06:16,980 --> 06:06:22,940 lines again go to the file try to set up 8504 06:06:19,980 --> 06:06:26,218 the logic if you can log that's already 8505 06:06:22,940 --> 06:06:28,558 a awesome start and once you're ready to 8506 06:06:26,218 --> 06:06:30,660 compare the results continue with the 8507 06:06:28,558 --> 06:06:33,240 videos beautiful so now let's start 8508 06:06:30,660 --> 06:06:37,440 working on a challenge I'll comment out 8509 06:06:33,240 --> 06:06:39,000 the final one in the app jsx just so 8510 06:06:37,440 --> 06:06:42,420 it's not in the way 8511 06:06:39,000 --> 06:06:45,718 then we want to navigate to fetch data 8512 06:06:42,420 --> 06:06:46,980 and you can either use Auto Import or 8513 06:06:45,718 --> 06:06:49,378 you can go 8514 06:06:46,980 --> 06:06:51,480 basically with import and then those two 8515 06:06:49,378 --> 06:06:55,020 values I'll try to use the Auto Import 8516 06:06:51,480 --> 06:06:57,058 I'm going to go to use State yep that's 8517 06:06:55,020 --> 06:06:59,820 what we're getting from react and like I 8518 06:06:57,058 --> 06:07:02,520 said I'll set it equal to a users and 8519 06:06:59,820 --> 06:07:05,040 the default value here is going to be an 8520 06:07:02,520 --> 06:07:08,580 empty array so I'm going to go users and 8521 06:07:05,040 --> 06:07:11,340 then set users now that is equal to my 8522 06:07:08,580 --> 06:07:15,058 use State and as far as my default value 8523 06:07:11,340 --> 06:07:17,760 I'm going to provide empty array after 8524 06:07:15,058 --> 06:07:20,760 that let's set up that user effect so 8525 06:07:17,760 --> 06:07:24,900 let's go here and say use effect 8526 06:07:20,760 --> 06:07:26,638 notice we have nice Auto Import and I 8527 06:07:24,900 --> 06:07:29,218 pretty much always start just by logging 8528 06:07:26,638 --> 06:07:31,500 stuff because I don't want to set up the 8529 06:07:29,218 --> 06:07:33,780 functionality and then it turns out that 8530 06:07:31,500 --> 06:07:35,700 I don't know I'm invoking it the wrong 8531 06:07:33,780 --> 06:07:37,980 way or something along those lines 8532 06:07:35,700 --> 06:07:40,980 so I'm gonna go ahead with log and I'm 8533 06:07:37,980 --> 06:07:42,480 going to say hello let's save it and let 8534 06:07:40,980 --> 06:07:45,298 me check 8535 06:07:42,480 --> 06:07:46,860 yep I am nice hello in the console and 8536 06:07:45,298 --> 06:07:50,520 again please please please please please 8537 06:07:46,860 --> 06:07:53,638 please add over here the empty 8538 06:07:50,520 --> 06:07:54,958 dependency array otherwise you'll pretty 8539 06:07:53,638 --> 06:07:57,120 much have to wait to work on the 8540 06:07:54,958 --> 06:08:00,058 challenge you'll right away exceed the 8541 06:07:57,120 --> 06:08:01,500 rate limit and you'll have to wait I 8542 06:08:00,058 --> 06:08:04,020 believe like 15 minutes or something 8543 06:08:01,500 --> 06:08:05,520 along those lines so once we have the 8544 06:08:04,020 --> 06:08:08,340 use effect whatever we want to do well 8545 06:08:05,520 --> 06:08:11,340 we want to fetch data from this URL 8546 06:08:08,340 --> 06:08:13,680 correct and like I said we don't want to 8547 06:08:11,340 --> 06:08:16,200 set up this function as async because 8548 06:08:13,680 --> 06:08:18,240 for example in my case I'm going to use 8549 06:08:16,200 --> 06:08:20,940 a single weight 8550 06:08:18,240 --> 06:08:22,980 so I want to set up the fetching 8551 06:08:20,940 --> 06:08:25,500 function as a sync 8552 06:08:22,980 --> 06:08:28,260 but we cannot do that with the Callback 8553 06:08:25,500 --> 06:08:31,860 function that's not allowed what we can 8554 06:08:28,260 --> 06:08:35,600 do though is to set up a sync function 8555 06:08:31,860 --> 06:08:37,980 inside so I'm going to remove the hello 8556 06:08:35,600 --> 06:08:40,980 and I'm going to come up with a function 8557 06:08:37,980 --> 06:08:43,700 I'll call this fetch data 8558 06:08:40,980 --> 06:08:46,260 this is going to be my async function 8559 06:08:43,700 --> 06:08:48,240 and I'm not going to provide the URL 8560 06:08:46,260 --> 06:08:49,980 here I'm just going to say empty 8561 06:08:48,240 --> 06:08:53,040 parameters and all that 8562 06:08:49,980 --> 06:08:56,458 and I'm going to start by just waiting 8563 06:08:53,040 --> 06:08:58,740 for fetch so response that is equal to a 8564 06:08:56,458 --> 06:09:02,160 weight and then Fetch and let's provide 8565 06:08:58,740 --> 06:09:05,340 the URL like I said effectively this is 8566 06:09:02,160 --> 06:09:07,138 going to return a promise and I want to 8567 06:09:05,340 --> 06:09:09,600 turn this into a Json 8568 06:09:07,138 --> 06:09:12,180 so I'm going to go here with const users 8569 06:09:09,600 --> 06:09:14,280 and that is equal to a weight and then 8570 06:09:12,180 --> 06:09:17,040 we want to go with response and then Dot 8571 06:09:14,280 --> 06:09:18,480 and then Json like so we want to save 8572 06:09:17,040 --> 06:09:20,638 that and like I said 8573 06:09:18,480 --> 06:09:23,700 we just want to start by logging stuff 8574 06:09:20,638 --> 06:09:25,378 so in here I'm going to say users and if 8575 06:09:23,700 --> 06:09:28,378 in the console 8576 06:09:25,378 --> 06:09:30,360 I'll nicely see the users that are 8577 06:09:28,378 --> 06:09:32,458 coming from this URL we're good to go 8578 06:09:30,360 --> 06:09:34,260 now what's the problem well I'm not 8579 06:09:32,458 --> 06:09:36,780 invoking it correct so I'm going to go 8580 06:09:34,260 --> 06:09:39,958 here with Fetch and then data and check 8581 06:09:36,780 --> 06:09:41,458 it out once I refresh notice now I have 8582 06:09:39,958 --> 06:09:44,340 all my users 8583 06:09:41,458 --> 06:09:46,680 now when it comes to sync functionality 8584 06:09:44,340 --> 06:09:49,080 it's a good practice to set this up in 8585 06:09:46,680 --> 06:09:51,480 try catch just in case you have any 8586 06:09:49,080 --> 06:09:54,600 errors now I can tell you right away 8587 06:09:51,480 --> 06:09:58,138 that when it comes to fetch it doesn't 8588 06:09:54,600 --> 06:10:00,120 treat 404 as an error so unlike the 8589 06:09:58,138 --> 06:10:02,100 axios which essentially is going to run 8590 06:10:00,120 --> 06:10:05,400 the code inside of the catch if you have 8591 06:10:02,100 --> 06:10:07,920 404 that's not the case with the fetch 8592 06:10:05,400 --> 06:10:09,718 one that's kind of a gotcha so first let 8593 06:10:07,920 --> 06:10:13,320 me just cut this one out over here I'll 8594 06:10:09,718 --> 06:10:14,878 place this one inside of the try and I'm 8595 06:10:13,320 --> 06:10:17,400 just going to lock the error but again 8596 06:10:14,878 --> 06:10:19,680 if you'll go here and change the url for 8597 06:10:17,400 --> 06:10:21,900 example to users you'll see that 8598 06:10:19,680 --> 06:10:24,540 basically the error is not going to run 8599 06:10:21,900 --> 06:10:27,058 so go here with error and then again let 8600 06:10:24,540 --> 06:10:30,000 me go here with users and then notice 8601 06:10:27,058 --> 06:10:32,280 yes we do have an error but it's 8602 06:10:30,000 --> 06:10:35,218 actually called in line 13. 8603 06:10:32,280 --> 06:10:37,080 so we're not invoking the catch one over 8604 06:10:35,218 --> 06:10:39,900 here just again something to keep in 8605 06:10:37,080 --> 06:10:42,000 mind because for example axials if you 8606 06:10:39,900 --> 06:10:44,878 have 404 yes then the functionality is 8607 06:10:42,000 --> 06:10:47,400 going to be invoked in the catch that's 8608 06:10:44,878 --> 06:10:51,240 just a tiny side note so once I have the 8609 06:10:47,400 --> 06:10:53,700 users what I can do well we can set our 8610 06:10:51,240 --> 06:10:56,040 users equal to whatever users we're 8611 06:10:53,700 --> 06:10:57,718 getting back correct so why don't we do 8612 06:10:56,040 --> 06:11:00,540 this I'm going to go with set users 8613 06:10:57,718 --> 06:11:03,120 equals to the users and of course now I 8614 06:11:00,540 --> 06:11:04,920 do need to fix the URL 8615 06:11:03,120 --> 06:11:08,218 let's save it over here 8616 06:11:04,920 --> 06:11:11,520 and if you want you can actually go to a 8617 06:11:08,218 --> 06:11:14,100 big browser and let's test out the state 8618 06:11:11,520 --> 06:11:16,378 value so let me navigate to the big 8619 06:11:14,100 --> 06:11:17,638 browser I'm gonna go with the new tab 8620 06:11:16,378 --> 06:11:21,298 and all that 8621 06:11:17,638 --> 06:11:24,420 and I want to paste that 5173a 8622 06:11:21,298 --> 06:11:26,160 and if we inspect again we can take a 8623 06:11:24,420 --> 06:11:29,520 look at the console if we're logging or 8624 06:11:26,160 --> 06:11:32,160 we can simply go with components then we 8625 06:11:29,520 --> 06:11:34,138 have the fetch data and notice I have 8626 06:11:32,160 --> 06:11:37,740 the user effect so I have my function 8627 06:11:34,138 --> 06:11:39,360 and I also have my state value so if 8628 06:11:37,740 --> 06:11:42,780 we're successful this is what we're 8629 06:11:39,360 --> 06:11:45,740 gonna see we'll have this array of users 8630 06:11:42,780 --> 06:11:48,600 in the state and like I said that's why 8631 06:11:45,740 --> 06:11:50,760 the react Dev tools are so powerful 8632 06:11:48,600 --> 06:11:54,058 because you can right away pretty much 8633 06:11:50,760 --> 06:11:56,400 get the info okay that's good now let me 8634 06:11:54,058 --> 06:11:58,798 remove all these errors and all that I 8635 06:11:56,400 --> 06:12:01,320 don't think they're useful and now let's 8636 06:11:58,798 --> 06:12:03,660 just worry how we'll render them so 8637 06:12:01,320 --> 06:12:05,458 let's navigate to the jsx 8638 06:12:03,660 --> 06:12:06,780 and we'll start by setting up the 8639 06:12:05,458 --> 06:12:09,240 section so I guess I'm just going to 8640 06:12:06,780 --> 06:12:11,638 remove this one there's no need here so 8641 06:12:09,240 --> 06:12:13,680 section then inside of this section I'm 8642 06:12:11,638 --> 06:12:15,660 going to go with heading three and let's 8643 06:12:13,680 --> 06:12:19,920 just come up with some kind of value so 8644 06:12:15,660 --> 06:12:22,920 GitHub and users let's save that and 8645 06:12:19,920 --> 06:12:26,940 then we want to go with an ordered list 8646 06:12:22,920 --> 06:12:30,180 and actually already set up this CSS in 8647 06:12:26,940 --> 06:12:34,500 the CSS file so if you'll navigate to 8648 06:12:30,180 --> 06:12:36,780 index CSS and if you look for users 8649 06:12:34,500 --> 06:12:39,660 you'll see that basically there is 8650 06:12:36,780 --> 06:12:41,580 already some CSS attached to it 8651 06:12:39,660 --> 06:12:44,520 and essentially this is what you can use 8652 06:12:41,580 --> 06:12:46,080 if you want again the CSS part is really 8653 06:12:44,520 --> 06:12:50,218 irrelevant 8654 06:12:46,080 --> 06:12:52,200 the main point of this challenge was to 8655 06:12:50,218 --> 06:12:54,420 set up the logic so let's go here with 8656 06:12:52,200 --> 06:12:57,420 another list let's right away add a 8657 06:12:54,420 --> 06:13:00,718 class of users and then instead of that 8658 06:12:57,420 --> 06:13:03,058 on our list I want to iterate over so 8659 06:13:00,718 --> 06:13:05,520 I'm going to go here with users then I'm 8660 06:13:03,058 --> 06:13:08,458 going to go with map so we're mapping 8661 06:13:05,520 --> 06:13:12,540 over and I'll reference each and every 8662 06:13:08,458 --> 06:13:13,500 item as a user now for now let's just 8663 06:13:12,540 --> 06:13:16,680 return 8664 06:13:13,500 --> 06:13:19,138 the list item with some kind of value 8665 06:13:16,680 --> 06:13:21,360 and why I'm doing that because I want to 8666 06:13:19,138 --> 06:13:24,180 log and show you what is inside of the 8667 06:13:21,360 --> 06:13:26,218 user so let's go here with return list 8668 06:13:24,180 --> 06:13:28,680 item and I'm just going to say item 8669 06:13:26,218 --> 06:13:31,440 again there's going to be a warning 8670 06:13:28,680 --> 06:13:33,840 don't worry about it and yes I already 8671 06:13:31,440 --> 06:13:37,440 have a little bit of CSS over there but 8672 06:13:33,840 --> 06:13:39,900 if we log we should see what properties 8673 06:13:37,440 --> 06:13:43,080 we have inside of that user 8674 06:13:39,900 --> 06:13:44,280 so let me scroll up I mean all of them 8675 06:13:43,080 --> 06:13:46,378 are going to be the same but I'm going 8676 06:13:44,280 --> 06:13:49,980 to start with the first one and check it 8677 06:13:46,378 --> 06:13:52,378 out so we have the Avatar URL 8678 06:13:49,980 --> 06:13:53,760 effectively this is where we can get the 8679 06:13:52,378 --> 06:13:56,340 picture 8680 06:13:53,760 --> 06:14:00,480 we also have the login that is going to 8681 06:13:56,340 --> 06:14:04,740 be the user account and we have HTML URL 8682 06:14:00,480 --> 06:14:07,798 which leads back to the profile and also 8683 06:14:04,740 --> 06:14:10,680 we have a ID why do we need ID well 8684 06:14:07,798 --> 06:14:11,760 because we have the key correct so what 8685 06:14:10,680 --> 06:14:13,920 we can do 8686 06:14:11,760 --> 06:14:17,040 we can pull out those properties so I 8687 06:14:13,920 --> 06:14:21,980 can say here const and then ID then 8688 06:14:17,040 --> 06:14:25,200 login then Avatar and underscore URL 8689 06:14:21,980 --> 06:14:29,100 HTML underscore URL and all of that is 8690 06:14:25,200 --> 06:14:31,500 equal to my user and then as far as the 8691 06:14:29,100 --> 06:14:34,260 return well now let's provide the key 8692 06:14:31,500 --> 06:14:37,740 that's the first thing so here we'll go 8693 06:14:34,260 --> 06:14:39,900 with ID and then as far as the item I 8694 06:14:37,740 --> 06:14:41,700 want to place the image and I want to 8695 06:14:39,900 --> 06:14:44,340 set up the div again if you don't want 8696 06:14:41,700 --> 06:14:46,440 to use my CSS I just suggest adding the 8697 06:14:44,340 --> 06:14:49,200 inline for the image 8698 06:14:46,440 --> 06:14:51,660 otherwise they're going to be pretty big 8699 06:14:49,200 --> 06:14:53,638 as far as I remember so image now for 8700 06:14:51,660 --> 06:14:57,058 the source we want to go here with 8701 06:14:53,638 --> 06:14:58,500 Avatar URL alternative I'm just going to 8702 06:14:57,058 --> 06:15:01,980 provide the login 8703 06:14:58,500 --> 06:15:05,280 it's kind of a shortcut in here 8704 06:15:01,980 --> 06:15:06,360 and then right after that we want to go 8705 06:15:05,280 --> 06:15:08,878 with div 8706 06:15:06,360 --> 06:15:10,860 and then inside of the div we're gonna 8707 06:15:08,878 --> 06:15:13,680 go with heading five and we'll display 8708 06:15:10,860 --> 06:15:16,378 the name so let's go here with login and 8709 06:15:13,680 --> 06:15:18,240 then lastly let's set up that link that 8710 06:15:16,378 --> 06:15:20,760 navigates back and if you're interested 8711 06:15:18,240 --> 06:15:24,840 in the CSS again please just reference 8712 06:15:20,760 --> 06:15:27,540 the index CSS C essentially the styles 8713 06:15:24,840 --> 06:15:31,138 that I added they're not that many so 8714 06:15:27,540 --> 06:15:35,160 let's go here and let's go for HTML URL 8715 06:15:31,138 --> 06:15:37,980 and then instead of the link I just say 8716 06:15:35,160 --> 06:15:40,920 profile let's save that and this is what 8717 06:15:37,980 --> 06:15:43,980 we should see on the screen essentially 8718 06:15:40,920 --> 06:15:47,040 a list of GitHub users hopefully 8719 06:15:43,980 --> 06:15:49,378 everyone enjoyed the challenge and I'll 8720 06:15:47,040 --> 06:15:51,420 see you in the next video all right as 8721 06:15:49,378 --> 06:15:53,400 you can see in the readme technically 8722 06:15:51,420 --> 06:15:56,700 our next topic should be use effect 8723 06:15:53,400 --> 06:15:59,160 cleanup function however we'll actually 8724 06:15:56,700 --> 06:16:02,160 skip this and the following user effect 8725 06:15:59,160 --> 06:16:05,458 topic and come back to them once we're 8726 06:16:02,160 --> 06:16:07,920 done with conditional rendering 8727 06:16:05,458 --> 06:16:09,180 if you're wondering why are we hopping 8728 06:16:07,920 --> 06:16:12,000 like that 8729 06:16:09,180 --> 06:16:15,298 simply because use of a cleanup function 8730 06:16:12,000 --> 06:16:18,540 is somewhat tricky topic 8731 06:16:15,298 --> 06:16:22,200 so in this course iteration I decided to 8732 06:16:18,540 --> 06:16:24,780 use different example in the hopes that 8733 06:16:22,200 --> 06:16:26,820 it will help students to understand the 8734 06:16:24,780 --> 06:16:30,298 main Concepts better 8735 06:16:26,820 --> 06:16:32,100 and since in the example we're going to 8736 06:16:30,298 --> 06:16:34,860 toggle component 8737 06:16:32,100 --> 06:16:38,820 we first need to cover conditional 8738 06:16:34,860 --> 06:16:41,040 rendering yes I also prefer to cover 8739 06:16:38,820 --> 06:16:45,600 everything step by step 8740 06:16:41,040 --> 06:16:48,420 essentially in order but in this case 8741 06:16:45,600 --> 06:16:52,740 I did not see any other option 8742 06:16:48,420 --> 06:16:56,218 so again let me repeat we will skip use 8743 06:16:52,740 --> 06:17:00,058 effect cleanup function for now and come 8744 06:16:56,218 --> 06:17:01,520 back to it once we're done with our next 8745 06:17:00,058 --> 06:17:04,740 main topic 8746 06:17:01,520 --> 06:17:07,440 conditional rendering and next let's 8747 06:17:04,740 --> 06:17:08,280 talk about conditional rendering in 8748 06:17:07,440 --> 06:17:11,280 react 8749 06:17:08,280 --> 06:17:14,458 and in short in the following videos 8750 06:17:11,280 --> 06:17:18,000 we'll learn how to control what is 8751 06:17:14,458 --> 06:17:21,298 displayed in the browser based on some 8752 06:17:18,000 --> 06:17:23,458 commission and let's just start with 8753 06:17:21,298 --> 06:17:25,260 multiple returns 8754 06:17:23,458 --> 06:17:28,798 so as far as the setup 8755 06:17:25,260 --> 06:17:32,400 in the app jsx you should import starter 8756 06:17:28,798 --> 06:17:35,218 from zero three yep we're switching the 8757 06:17:32,400 --> 06:17:38,580 folders and then starter and then zero 8758 06:17:35,218 --> 06:17:41,458 one multiple returns 8759 06:17:38,580 --> 06:17:44,218 and effectively once you navigate to the 8760 06:17:41,458 --> 06:17:47,520 file you should see just the heading to 8761 06:17:44,218 --> 06:17:51,240 with two Imports for use effect and your 8762 06:17:47,520 --> 06:17:53,760 state and as far as the topic it's 8763 06:17:51,240 --> 06:17:55,320 actually not controversial or anything 8764 06:17:53,760 --> 06:17:58,378 new 8765 06:17:55,320 --> 06:18:00,480 let's think about it in vanilla.js 8766 06:17:58,378 --> 06:18:03,058 we can set up multiple returns in a 8767 06:18:00,480 --> 06:18:05,638 function so if I have a function say 8768 06:18:03,058 --> 06:18:09,540 hello that is looking for the name 8769 06:18:05,638 --> 06:18:12,360 parameter if name exists I can go to 8770 06:18:09,540 --> 06:18:13,980 early return I can say you know what the 8771 06:18:12,360 --> 06:18:17,940 function is going to return hello and 8772 06:18:13,980 --> 06:18:19,980 then whatever is the name now if 8773 06:18:17,940 --> 06:18:22,138 the name is not provided then of course 8774 06:18:19,980 --> 06:18:24,958 you will bypass the safe condition and 8775 06:18:22,138 --> 06:18:27,958 then we'll return whatever 8776 06:18:24,958 --> 06:18:30,840 we type here in the bottom now just keep 8777 06:18:27,958 --> 06:18:33,180 in mind that by default 8778 06:18:30,840 --> 06:18:34,558 JavaScript functions return undefined so 8779 06:18:33,180 --> 06:18:37,020 if you're not going to return anything 8780 06:18:34,558 --> 06:18:41,520 then basically it's just going to return 8781 06:18:37,020 --> 06:18:44,700 undefined so if let's say we invoke the 8782 06:18:41,520 --> 06:18:46,500 say hello with the parameter if we 8783 06:18:44,700 --> 06:18:48,480 provide the argument then it's going to 8784 06:18:46,500 --> 06:18:51,958 be hello and then whatever the argument 8785 06:18:48,480 --> 06:18:53,340 if not then it's going to be hello there 8786 06:18:51,958 --> 06:18:56,940 now 8787 06:18:53,340 --> 06:18:59,580 since our components are functions can 8788 06:18:56,940 --> 06:19:01,260 we do the same in react and you can 8789 06:18:59,580 --> 06:19:02,820 probably already guess that the answer 8790 06:19:01,260 --> 06:19:05,218 is yes 8791 06:19:02,820 --> 06:19:07,860 and effectively in this file 8792 06:19:05,218 --> 06:19:10,920 here is the setup we have used effect in 8793 06:19:07,860 --> 06:19:14,400 your state so why don't we set up a 8794 06:19:10,920 --> 06:19:17,700 State value and I'm going to call my 8795 06:19:14,400 --> 06:19:21,660 state value is loading so that's a tiny 8796 06:19:17,700 --> 06:19:25,080 side note where a convention is to call 8797 06:19:21,660 --> 06:19:26,638 Boolean values is and then whatever is 8798 06:19:25,080 --> 06:19:29,760 that Boolean 8799 06:19:26,638 --> 06:19:32,218 value so if it's loading then is loading 8800 06:19:29,760 --> 06:19:33,660 and then the function is set is loading 8801 06:19:32,218 --> 06:19:35,638 again 8802 06:19:33,660 --> 06:19:38,760 it's a convention 8803 06:19:35,638 --> 06:19:40,620 so taco and burrito is also a good 8804 06:19:38,760 --> 06:19:43,920 approach but 8805 06:19:40,620 --> 06:19:46,138 most likely a lot of times you'll see 8806 06:19:43,920 --> 06:19:49,020 this type of approach so I'm going to go 8807 06:19:46,138 --> 06:19:50,760 with is loading and then we're gonna go 8808 06:19:49,020 --> 06:19:52,798 with set is loading so that's the 8809 06:19:50,760 --> 06:19:56,218 function that controls it and we'll set 8810 06:19:52,798 --> 06:19:59,638 it equal to use State and the default 8811 06:19:56,218 --> 06:20:02,820 value is going to be true over here 8812 06:19:59,638 --> 06:20:04,860 and essentially it's a Boolean so we can 8813 06:20:02,820 --> 06:20:09,000 flip it like a switch 8814 06:20:04,860 --> 06:20:11,100 from True to false and false to true and 8815 06:20:09,000 --> 06:20:13,558 then let's keep on moving why don't we 8816 06:20:11,100 --> 06:20:14,400 set up a condition so I do have my 8817 06:20:13,558 --> 06:20:16,620 return 8818 06:20:14,400 --> 06:20:19,440 which essentially just says heading 2 8819 06:20:16,620 --> 06:20:22,200 with multiple returns Basics however I 8820 06:20:19,440 --> 06:20:25,920 can go here with if and I can say if is 8821 06:20:22,200 --> 06:20:27,900 loading basically my state value if this 8822 06:20:25,920 --> 06:20:29,760 is true then I want to return something 8823 06:20:27,900 --> 06:20:31,798 else so I'm going to go here with 8824 06:20:29,760 --> 06:20:34,920 heading 2 and we're going to go with 8825 06:20:31,798 --> 06:20:36,298 loading and then dot dot now before you 8826 06:20:34,920 --> 06:20:39,718 ask yes 8827 06:20:36,298 --> 06:20:42,480 essentially if you want you can return 8828 06:20:39,718 --> 06:20:46,020 entire application over here 8829 06:20:42,480 --> 06:20:48,780 I'm just showing heading 2 but you can 8830 06:20:46,020 --> 06:20:51,240 place here 10 000 divs and whatever 8831 06:20:48,780 --> 06:20:54,058 functionality you want and check it out 8832 06:20:51,240 --> 06:20:56,458 now since this is true we actually 8833 06:20:54,058 --> 06:20:58,740 return this heading too so we don't even 8834 06:20:56,458 --> 06:21:02,218 get to this return and that's how early 8835 06:20:58,740 --> 06:21:04,080 returns work in JavaScript functions if 8836 06:21:02,218 --> 06:21:07,620 this condition is true then we return 8837 06:21:04,080 --> 06:21:10,620 whatever we have inside of the cartilage 8838 06:21:07,620 --> 06:21:12,540 and now the next question is can we make 8839 06:21:10,620 --> 06:21:15,540 this dynamic because at this point it's 8840 06:21:12,540 --> 06:21:16,920 like okay I can go here and I type false 8841 06:21:15,540 --> 06:21:18,540 and of course everything is going to 8842 06:21:16,920 --> 06:21:20,940 work but 8843 06:21:18,540 --> 06:21:22,680 I mean in the real application you kind 8844 06:21:20,940 --> 06:21:26,280 of want to change this programmatically 8845 06:21:22,680 --> 06:21:28,860 correct and the answer again is yes now 8846 06:21:26,280 --> 06:21:31,440 in our case we'll just Tinker with set 8847 06:21:28,860 --> 06:21:33,420 timeout but in the following examples 8848 06:21:31,440 --> 06:21:35,820 I'll show you how we can actually do 8849 06:21:33,420 --> 06:21:38,280 that if let's say we fetch data for now 8850 06:21:35,820 --> 06:21:40,558 we'll just basically pretend that we are 8851 06:21:38,280 --> 06:21:43,020 fetching data so I'm going to go here 8852 06:21:40,558 --> 06:21:45,958 with use effect I'll provide here my 8853 06:21:43,020 --> 06:21:48,120 callback function and I'll say that I 8854 06:21:45,958 --> 06:21:51,600 only want to run it once and the 8855 06:21:48,120 --> 06:21:55,260 component mode and then let's set up set 8856 06:21:51,600 --> 06:21:57,420 timeout and inside of the set timeout we 8857 06:21:55,260 --> 06:21:59,580 again need to provide a function that's 8858 06:21:57,420 --> 06:22:02,100 going to be invoked and then in how long 8859 06:21:59,580 --> 06:22:03,958 and here I'm gonna go with three 8860 06:22:02,100 --> 06:22:06,480 thousand basically three seconds so 8861 06:22:03,958 --> 06:22:09,120 those are milliseconds and I'm just 8862 06:22:06,480 --> 06:22:10,740 gonna add comment here done fetching 8863 06:22:09,120 --> 06:22:12,540 there again this is going to be pretty 8864 06:22:10,740 --> 06:22:16,200 common example 8865 06:22:12,540 --> 06:22:18,840 for using multiple returns that's why I 8866 06:22:16,200 --> 06:22:21,840 keep referencing fetching the data 8867 06:22:18,840 --> 06:22:24,058 so let's go here with set is loading and 8868 06:22:21,840 --> 06:22:26,400 let's set it equal to false and what 8869 06:22:24,058 --> 06:22:28,320 you'll notice once you save basically 8870 06:22:26,400 --> 06:22:30,240 the way it's going to work while we're 8871 06:22:28,320 --> 06:22:31,740 loading while we're getting some kind of 8872 06:22:30,240 --> 06:22:34,320 data 8873 06:22:31,740 --> 06:22:37,440 will display something to the user so 8874 06:22:34,320 --> 06:22:40,200 user knows hey things are happening 8875 06:22:37,440 --> 06:22:43,138 users not looking at just blank screen 8876 06:22:40,200 --> 06:22:46,920 and then once we're good to go then 8877 06:22:43,138 --> 06:22:49,138 we'll display basically the jsx we want 8878 06:22:46,920 --> 06:22:52,020 to show to the user 8879 06:22:49,138 --> 06:22:55,080 um once we're familiar with the basics 8880 06:22:52,020 --> 06:22:58,440 why don't we work on another challenge 8881 06:22:55,080 --> 06:23:01,980 and effectively we want to implement 8882 06:22:58,440 --> 06:23:03,500 multiple returns when we fetch data 8883 06:23:01,980 --> 06:23:07,378 which again 8884 06:23:03,500 --> 06:23:08,820 is most common use case for using the 8885 06:23:07,378 --> 06:23:12,360 use effect and that's why of course 8886 06:23:08,820 --> 06:23:16,020 we're implementing multiple returns with 8887 06:23:12,360 --> 06:23:19,680 the fetching data example so back in the 8888 06:23:16,020 --> 06:23:23,940 app jsx you should import starter 8889 06:23:19,680 --> 06:23:26,340 from the zero to multiple returns fetch 8890 06:23:23,940 --> 06:23:28,138 data and then if you navigate to the 8891 06:23:26,340 --> 06:23:30,718 file this is what you'll see use effect 8892 06:23:28,138 --> 06:23:32,520 in your state imported and then the URL 8893 06:23:30,718 --> 06:23:35,760 that I want you to use 8894 06:23:32,520 --> 06:23:37,980 and then as far as the challenge first 8895 06:23:35,760 --> 06:23:41,100 practice on setting up these State 8896 06:23:37,980 --> 06:23:43,138 values so for now practice on a user my 8897 06:23:41,100 --> 06:23:46,440 default value is going to be null for 8898 06:23:43,138 --> 06:23:48,000 the user and then fetch data from the 8899 06:23:46,440 --> 06:23:49,680 URL again 8900 06:23:48,000 --> 06:23:51,240 you should do that of course and use 8901 06:23:49,680 --> 06:23:53,160 effect and all that and I'm not going to 8902 06:23:51,240 --> 06:23:55,798 provide any more details that's the 8903 06:23:53,160 --> 06:23:57,840 challenge and again for now just log the 8904 06:23:55,798 --> 06:23:59,878 result if you want of course you can set 8905 06:23:57,840 --> 06:24:01,080 up the state value and render something 8906 06:23:59,878 --> 06:24:03,360 on a screen 8907 06:24:01,080 --> 06:24:04,260 but if you see the user object in the 8908 06:24:03,360 --> 06:24:07,558 console 8909 06:24:04,260 --> 06:24:12,298 and you're already in good shape 8910 06:24:07,558 --> 06:24:14,100 so let me navigate back to the component 8911 06:24:12,298 --> 06:24:15,840 and effectively like I said the first 8912 06:24:14,100 --> 06:24:18,000 thing that I want to do is set up that 8913 06:24:15,840 --> 06:24:21,840 state value I'm going to go with const 8914 06:24:18,000 --> 06:24:24,180 and then user and then set user that's 8915 06:24:21,840 --> 06:24:27,540 my function that is going to be equal to 8916 06:24:24,180 --> 06:24:30,360 use State and like I said by default I 8917 06:24:27,540 --> 06:24:32,340 want to provide value no so nothing is 8918 06:24:30,360 --> 06:24:34,980 going to be there and now let's set up 8919 06:24:32,340 --> 06:24:38,340 that user effect so use effect in here 8920 06:24:34,980 --> 06:24:40,740 let's provide a callback function 8921 06:24:38,340 --> 06:24:43,080 and let's just make sure that we invoke 8922 06:24:40,740 --> 06:24:47,218 this again only 8923 06:24:43,080 --> 06:24:48,000 when the component loads so we don't 8924 06:24:47,218 --> 06:24:49,798 wanna 8925 06:24:48,000 --> 06:24:52,138 basically run this use effect after 8926 06:24:49,798 --> 06:24:53,400 every re-render and up next I want to 8927 06:24:52,138 --> 06:24:57,840 set up that function so I'm going to go 8928 06:24:53,400 --> 06:25:00,780 with fetch user is equal to a sync 8929 06:24:57,840 --> 06:25:03,180 that's the function and then we're gonna 8930 06:25:00,780 --> 06:25:05,458 set up the functionality where oh right 8931 06:25:03,180 --> 06:25:08,638 away Place everything in the try and 8932 06:25:05,458 --> 06:25:11,940 catch and as far as the 8933 06:25:08,638 --> 06:25:14,520 catch I'm just gonna go with error in 8934 06:25:11,940 --> 06:25:16,740 here and then when it comes to try now 8935 06:25:14,520 --> 06:25:19,740 let's again get the response back first 8936 06:25:16,740 --> 06:25:23,420 and that is equal to a weight so we're 8937 06:25:19,740 --> 06:25:26,340 waiting then fetch will provide the URL 8938 06:25:23,420 --> 06:25:29,700 and let's right away turn this basically 8939 06:25:26,340 --> 06:25:33,480 into a Json so const and then user is 8940 06:25:29,700 --> 06:25:36,660 equal to a weight again then response 8941 06:25:33,480 --> 06:25:38,520 and Json let's invoke this like I said 8942 06:25:36,660 --> 06:25:41,400 if you want you can set user equal to 8943 06:25:38,520 --> 06:25:43,860 user for now I just want to log this I 8944 06:25:41,400 --> 06:25:46,860 want to see whether everything 8945 06:25:43,860 --> 06:25:49,200 is working as I expected 8946 06:25:46,860 --> 06:25:51,780 so notice over here at the moment I 8947 06:25:49,200 --> 06:25:53,638 don't see anything 8948 06:25:51,780 --> 06:25:55,558 um and of course the reason why nothing 8949 06:25:53,638 --> 06:25:58,558 is happening because I keep forgetting 8950 06:25:55,558 --> 06:26:01,740 to invoke the functionalities with me go 8951 06:25:58,558 --> 06:26:03,000 with fetch user and now check it out in 8952 06:26:01,740 --> 06:26:05,638 the console 8953 06:26:03,000 --> 06:26:08,218 I should see the info about the user and 8954 06:26:05,638 --> 06:26:11,760 as you can see this is just more info 8955 06:26:08,218 --> 06:26:13,860 compared to when we fetched data and in 8956 06:26:11,760 --> 06:26:16,860 the following videos set up multiple 8957 06:26:13,860 --> 06:26:19,920 returns and also we'll set up a proper 8958 06:26:16,860 --> 06:26:21,958 jsx now before we continue you know what 8959 06:26:19,920 --> 06:26:25,020 let me just go over here and then 8960 06:26:21,958 --> 06:26:27,480 comment this one out so essentially 8961 06:26:25,020 --> 06:26:30,058 we can set this one up together 8962 06:26:27,480 --> 06:26:31,320 okay and now let's put two and two 8963 06:26:30,058 --> 06:26:33,780 together 8964 06:26:31,320 --> 06:26:37,558 effectively we know how we can work with 8965 06:26:33,780 --> 06:26:39,378 multiple returns and the reason why we 8966 06:26:37,558 --> 06:26:43,440 want to use them with fetch data because 8967 06:26:39,378 --> 06:26:45,298 essentially you'll have three states 8968 06:26:43,440 --> 06:26:47,940 you have the loading one 8969 06:26:45,298 --> 06:26:50,160 when we are waiting for data arrive 8970 06:26:47,940 --> 06:26:53,218 because keep in mind when it comes to 8971 06:26:50,160 --> 06:26:55,558 fetching data it's a synchronous so it 8972 06:26:53,218 --> 06:26:58,500 doesn't happen instantly then second 8973 06:26:55,558 --> 06:27:01,980 there might be an error so I don't know 8974 06:26:58,500 --> 06:27:04,200 maybe the values were not correctly 8975 06:27:01,980 --> 06:27:07,080 provided maybe the network doesn't work 8976 06:27:04,200 --> 06:27:09,000 I mean whatever there could be all kinds 8977 06:27:07,080 --> 06:27:10,138 of errors and then the last one is the 8978 06:27:09,000 --> 06:27:12,180 success 8979 06:27:10,138 --> 06:27:15,660 and essentially we go through those 8980 06:27:12,180 --> 06:27:17,940 States those are our options 8981 06:27:15,660 --> 06:27:20,940 and therefore in a state we actually 8982 06:27:17,940 --> 06:27:24,000 want to set up two more booleans for 8983 06:27:20,940 --> 06:27:27,298 loading and for error and then depending 8984 06:27:24,000 --> 06:27:30,180 on the values we want to display 8985 06:27:27,298 --> 06:27:32,700 or jsx so let's start working on that 8986 06:27:30,180 --> 06:27:34,980 where first I want to navigate back to 8987 06:27:32,700 --> 06:27:38,878 the component and let's set up those 8988 06:27:34,980 --> 06:27:41,760 State values again the convention is to 8989 06:27:38,878 --> 06:27:43,798 go with is and then the name 8990 06:27:41,760 --> 06:27:46,020 technically you don't have to so if I'm 8991 06:27:43,798 --> 06:27:49,500 gonna go here with is loading and then 8992 06:27:46,020 --> 06:27:51,058 set is loading and you know what by 8993 06:27:49,500 --> 06:27:53,520 default I'm going to set it equal to 8994 06:27:51,058 --> 06:27:56,160 true and then I'll do the same thing 8995 06:27:53,520 --> 06:27:57,540 with error now this one by default will 8996 06:27:56,160 --> 06:28:00,360 be false 8997 06:27:57,540 --> 06:28:03,780 so let's set it back to false 8998 06:28:00,360 --> 06:28:07,798 and then as far as these values let's go 8999 06:28:03,780 --> 06:28:08,820 error and we want to also change it over 9000 06:28:07,798 --> 06:28:10,980 here 9001 06:28:08,820 --> 06:28:14,040 let's save it and then let's keep on 9002 06:28:10,980 --> 06:28:17,040 moving before we set the user 9003 06:28:14,040 --> 06:28:19,080 why don't we set up two conditions so 9004 06:28:17,040 --> 06:28:22,798 one is going to be for loading and one 9005 06:28:19,080 --> 06:28:25,200 if we have an error now please 9006 06:28:22,798 --> 06:28:28,860 placement here is important 9007 06:28:25,200 --> 06:28:31,620 so if you place the loading after 9008 06:28:28,860 --> 06:28:34,320 the actual jsx you want to return 9009 06:28:31,620 --> 06:28:36,000 then it's not going to make sense 9010 06:28:34,320 --> 06:28:38,218 because 9011 06:28:36,000 --> 06:28:39,958 loading is going to be first and 9012 06:28:38,218 --> 06:28:41,700 JavaScript effectively reads everything 9013 06:28:39,958 --> 06:28:44,878 top to bottom 9014 06:28:41,700 --> 06:28:47,280 so if you have returned before the 9015 06:28:44,878 --> 06:28:49,740 loading commission then essentially you 9016 06:28:47,280 --> 06:28:52,020 all the time display the jsx and that's 9017 06:28:49,740 --> 06:28:53,638 not what we want so make sure that you 9018 06:28:52,020 --> 06:28:55,260 set the loading first 9019 06:28:53,638 --> 06:28:57,660 so the whole point 9020 06:28:55,260 --> 06:29:00,360 of this rant is that the placement is 9021 06:28:57,660 --> 06:29:01,620 extremely important and then let's go 9022 06:29:00,360 --> 06:29:04,680 here with the return 9023 06:29:01,620 --> 06:29:06,600 and we want to provide whatever value 9024 06:29:04,680 --> 06:29:09,780 so in my case it's going to be again the 9025 06:29:06,600 --> 06:29:11,340 heading 2 with loading and dot dot and 9026 06:29:09,780 --> 06:29:13,680 now let's do the same thing with an 9027 06:29:11,340 --> 06:29:16,680 error now keep in mind since this is 9028 06:29:13,680 --> 06:29:19,020 true we'll right away hit this condition 9029 06:29:16,680 --> 06:29:20,218 so this is what will return and error 9030 06:29:19,020 --> 06:29:23,218 basically 9031 06:29:20,218 --> 06:29:26,638 is going to be displayed only when we 9032 06:29:23,218 --> 06:29:29,520 set this one to false so we bypass that 9033 06:29:26,638 --> 06:29:31,500 condition and this one to True again 9034 06:29:29,520 --> 06:29:34,080 something very important to keep in mind 9035 06:29:31,500 --> 06:29:36,718 then I keep getting these questions all 9036 06:29:34,080 --> 06:29:38,940 the time and of course q9 that's why 9037 06:29:36,718 --> 06:29:41,340 spending more time on that so let's go 9038 06:29:38,940 --> 06:29:45,360 over here with loading instead of that 9039 06:29:41,340 --> 06:29:48,480 we'll just go with there was an error 9040 06:29:45,360 --> 06:29:50,218 let's save this and for now we'll right 9041 06:29:48,480 --> 06:29:53,100 away have the loading why well because 9042 06:29:50,218 --> 06:29:56,580 this is equal to true then let's 9043 06:29:53,100 --> 06:29:58,680 navigate back to our fetch user and 9044 06:29:56,580 --> 06:30:01,020 let's go through the logic 9045 06:29:58,680 --> 06:30:03,058 so we start over here fetching we 9046 06:30:01,020 --> 06:30:05,040 display loading okay everything is 9047 06:30:03,058 --> 06:30:08,280 awesome now what do we want to do when 9048 06:30:05,040 --> 06:30:11,100 we get back to user well we want to set 9049 06:30:08,280 --> 06:30:13,798 our state value right that kind of makes 9050 06:30:11,100 --> 06:30:16,980 sense so let's go over here let's go 9051 06:30:13,798 --> 06:30:20,458 with set user and now this one is equal 9052 06:30:16,980 --> 06:30:23,638 to a user okay beautiful 9053 06:30:20,458 --> 06:30:26,520 and then right after these conditions 9054 06:30:23,638 --> 06:30:28,200 the try and catch we wanna 9055 06:30:26,520 --> 06:30:31,320 essentially 9056 06:30:28,200 --> 06:30:33,660 set loading to false because we're done 9057 06:30:31,320 --> 06:30:34,860 loading so at that point we have only 9058 06:30:33,660 --> 06:30:37,558 two options 9059 06:30:34,860 --> 06:30:41,218 either there was an error we didn't get 9060 06:30:37,558 --> 06:30:43,138 the data or if everything was successful 9061 06:30:41,218 --> 06:30:44,638 then of course we want to display the 9062 06:30:43,138 --> 06:30:46,798 user again 9063 06:30:44,638 --> 06:30:49,980 it starts with loading 9064 06:30:46,798 --> 06:30:52,200 loading is true and then once 9065 06:30:49,980 --> 06:30:53,878 we're done with loading then we only 9066 06:30:52,200 --> 06:30:57,058 have two options and that's why right 9067 06:30:53,878 --> 06:31:00,600 after the catch I want to go with set is 9068 06:30:57,058 --> 06:31:03,120 loading and we'll set it equal to false 9069 06:31:00,600 --> 06:31:05,820 now let's move up here and let's take a 9070 06:31:03,120 --> 06:31:06,600 look at the error so if something goes 9071 06:31:05,820 --> 06:31:08,520 wrong 9072 06:31:06,600 --> 06:31:11,540 basically this should get triggered 9073 06:31:08,520 --> 06:31:15,000 right but again the gotcha with fetch 9074 06:31:11,540 --> 06:31:16,500 that it's not going to do it if let's 9075 06:31:15,000 --> 06:31:19,860 say the 9076 06:31:16,500 --> 06:31:21,298 resource doesn't exist let's say 404 and 9077 06:31:19,860 --> 06:31:22,680 this is something that we'll cover in 9078 06:31:21,298 --> 06:31:24,718 following videos so I'm not going to 9079 06:31:22,680 --> 06:31:27,058 spend too much time on that I'm just 9080 06:31:24,718 --> 06:31:29,280 going to say that yes we'll sit here set 9081 06:31:27,058 --> 06:31:32,160 is error and we'll set it equal to true 9082 06:31:29,280 --> 06:31:35,340 but you'll see that it doesn't work 9083 06:31:32,160 --> 06:31:36,958 with all the errors again with axios yes 9084 06:31:35,340 --> 06:31:40,138 this is going to be the case 9085 06:31:36,958 --> 06:31:42,058 this will be always in the catch but not 9086 06:31:40,138 --> 06:31:44,940 with the fetch something important to 9087 06:31:42,058 --> 06:31:49,200 keep in mind so now notice first we're 9088 06:31:44,940 --> 06:31:50,638 loading and then we have fetch beta now 9089 06:31:49,200 --> 06:31:52,860 the loading one is going to be very 9090 06:31:50,638 --> 06:31:54,600 quick by the way because again we're 9091 06:31:52,860 --> 06:31:56,638 working locally on all that so of course 9092 06:31:54,600 --> 06:31:58,620 it's going to happen instantly once we 9093 06:31:56,638 --> 06:32:00,480 set up the return yes I'll navigate to a 9094 06:31:58,620 --> 06:32:01,860 bigger browser and I'll slow down the 9095 06:32:00,480 --> 06:32:05,340 network just so you can see that it's 9096 06:32:01,860 --> 06:32:08,340 definitely there so once I have the user 9097 06:32:05,340 --> 06:32:10,740 in my state what we can do well we can 9098 06:32:08,340 --> 06:32:13,558 navigate down here and essentially 9099 06:32:10,740 --> 06:32:16,080 return something display the user we 9100 06:32:13,558 --> 06:32:18,480 were actually fetching and here I'm 9101 06:32:16,080 --> 06:32:21,480 gonna go with div I'm gonna set up first 9102 06:32:18,480 --> 06:32:24,180 the image this is the case where I said 9103 06:32:21,480 --> 06:32:25,320 that basically I want to go with inline 9104 06:32:24,180 --> 06:32:26,760 styles 9105 06:32:25,320 --> 06:32:28,320 just because I know that it's going to 9106 06:32:26,760 --> 06:32:30,958 be massive and I don't want to spend my 9107 06:32:28,320 --> 06:32:33,780 time in the CSS so this is a good use 9108 06:32:30,958 --> 06:32:36,360 case in my opinion for the inline ones 9109 06:32:33,780 --> 06:32:39,000 that's why I will go here with width 150 9110 06:32:36,360 --> 06:32:42,420 pixels and I'm going to add a little bit 9111 06:32:39,000 --> 06:32:44,400 of Border radius here so as you can see 9112 06:32:42,420 --> 06:32:46,378 I actually have the error make sure that 9113 06:32:44,400 --> 06:32:49,138 you have proper JavaScript syntax 9114 06:32:46,378 --> 06:32:51,718 basically we need to come over here and 9115 06:32:49,138 --> 06:32:54,120 we're going to set it equal to I don't 9116 06:32:51,718 --> 06:32:55,440 know 25 pixels now at the moment we 9117 06:32:54,120 --> 06:32:57,660 don't see anything because of course we 9118 06:32:55,440 --> 06:33:01,138 need to provide those values and 9119 06:32:57,660 --> 06:33:03,420 essentially for the 9120 06:33:01,138 --> 06:33:06,660 image I want to go again with that 9121 06:33:03,420 --> 06:33:09,660 Avatar I believe yep so we go here with 9122 06:33:06,660 --> 06:33:11,058 user and Avatar now keep in mind the 9123 06:33:09,660 --> 06:33:13,740 reason why we'll have to go with 9124 06:33:11,058 --> 06:33:15,000 user.user DOT user dot is because now we 9125 06:33:13,740 --> 06:33:17,878 have an object 9126 06:33:15,000 --> 06:33:19,740 so this essentially is the object that's 9127 06:33:17,878 --> 06:33:22,080 in our state 9128 06:33:19,740 --> 06:33:24,540 so I have a user object and now in order 9129 06:33:22,080 --> 06:33:26,520 to access those properties we either can 9130 06:33:24,540 --> 06:33:28,378 destructure it something we'll do later 9131 06:33:26,520 --> 06:33:31,440 because there's one gotcha I want to 9132 06:33:28,378 --> 06:33:33,900 show you or we can go with the object 9133 06:33:31,440 --> 06:33:37,138 which in my case is user and then the 9134 06:33:33,900 --> 06:33:38,940 property again the Avatar should be 9135 06:33:37,138 --> 06:33:40,920 somewhere over here 9136 06:33:38,940 --> 06:33:43,740 probably first one yep notice over here 9137 06:33:40,920 --> 06:33:45,480 that's the Avatar then as far as the 9138 06:33:43,740 --> 06:33:47,878 alternative I'll provide the name here 9139 06:33:45,480 --> 06:33:49,260 so let's go here with user and then 9140 06:33:47,878 --> 06:33:53,638 Avatar 9141 06:33:49,260 --> 06:33:57,600 underscore URL as far as the alternative 9142 06:33:53,638 --> 06:33:59,700 we're gonna go with user and name and 9143 06:33:57,600 --> 06:34:03,120 then we just want to provide 9144 06:33:59,700 --> 06:34:06,298 again name in heading 2 company and a 9145 06:34:03,120 --> 06:34:07,940 bio so we have an image then we want to 9146 06:34:06,298 --> 06:34:11,100 go here with 9147 06:34:07,940 --> 06:34:14,100 user and the name 9148 06:34:11,100 --> 06:34:17,280 and then after the name 9149 06:34:14,100 --> 06:34:20,100 we're gonna go with heading 4 works at 9150 06:34:17,280 --> 06:34:22,980 and again let's grab the curlies we'll 9151 06:34:20,100 --> 06:34:24,900 go with user and then company 9152 06:34:22,980 --> 06:34:27,780 and then at the very end we have the 9153 06:34:24,900 --> 06:34:31,558 paragraph with basically a bio 9154 06:34:27,780 --> 06:34:34,740 so user and then bio let's save this and 9155 06:34:31,558 --> 06:34:37,860 check it out now we have nicely fetched 9156 06:34:34,740 --> 06:34:40,558 a GitHub user so now let me just grab 9157 06:34:37,860 --> 06:34:42,540 this URL and let me set it up on a 9158 06:34:40,558 --> 06:34:44,760 bigger browser window just so you can 9159 06:34:42,540 --> 06:34:47,100 see how basically everything works so 9160 06:34:44,760 --> 06:34:49,378 let me open this one up in new tab copy 9161 06:34:47,100 --> 06:34:51,718 and paste everything is awesome and 9162 06:34:49,378 --> 06:34:54,360 essentially let me slow down the network 9163 06:34:51,718 --> 06:34:55,980 and we can do that if we navigate to the 9164 06:34:54,360 --> 06:34:58,440 devtools we're looking for the network 9165 06:34:55,980 --> 06:35:01,920 one and notice over here I'm actually 9166 06:34:58,440 --> 06:35:03,540 using fast 3G that's why I was wondering 9167 06:35:01,920 --> 06:35:06,660 why it 9168 06:35:03,540 --> 06:35:08,760 took so long in the use effect examples 9169 06:35:06,660 --> 06:35:12,120 oh well so actually I was all the time 9170 06:35:08,760 --> 06:35:14,160 using that never mind notice basically 9171 06:35:12,120 --> 06:35:16,260 if we go the regular it's going to load 9172 06:35:14,160 --> 06:35:19,440 right away however if I'm Gonna Change 9173 06:35:16,260 --> 06:35:21,780 it to let's say fast 3G 9174 06:35:19,440 --> 06:35:22,740 you'll notice that first we'll get the 9175 06:35:21,780 --> 06:35:24,298 loading 9176 06:35:22,740 --> 06:35:27,420 that's going to be displayed on the 9177 06:35:24,298 --> 06:35:32,160 screen so we'll have loading dot dot dot 9178 06:35:27,420 --> 06:35:34,680 and then we basically get the user so 9179 06:35:32,160 --> 06:35:37,440 that's our setup and up next let's talk 9180 06:35:34,680 --> 06:35:39,840 about the fetch errors not bad not bad 9181 06:35:37,440 --> 06:35:42,180 we can nicely fetch data in our 9182 06:35:39,840 --> 06:35:44,520 application we can display multiple 9183 06:35:42,180 --> 06:35:46,260 returns so user is aware what's 9184 06:35:44,520 --> 06:35:48,420 happening in our application 9185 06:35:46,260 --> 06:35:50,760 however before we 9186 06:35:48,420 --> 06:35:53,400 move on to the next topic 9187 06:35:50,760 --> 06:35:55,798 I also want to discuss fetch errors 9188 06:35:53,400 --> 06:35:57,180 gotcha now if you're not interested if 9189 06:35:55,798 --> 06:35:59,340 you're like I'm always going to use 9190 06:35:57,180 --> 06:36:01,200 axios and all that feel free to skip 9191 06:35:59,340 --> 06:36:03,780 this video technically this is optional 9192 06:36:01,200 --> 06:36:04,920 by the way let me add that over here I 9193 06:36:03,780 --> 06:36:07,200 mean if you don't want to follow along 9194 06:36:04,920 --> 06:36:10,980 you don't have to essentially when it 9195 06:36:07,200 --> 06:36:14,000 comes to fetch unlike the axios 9196 06:36:10,980 --> 06:36:18,260 it doesn't consider for example 9197 06:36:14,000 --> 06:36:21,540 400 or 500 to be an error instead 9198 06:36:18,260 --> 06:36:23,878 essentially it treats that as a 9199 06:36:21,540 --> 06:36:26,580 successful request now why does that 9200 06:36:23,878 --> 06:36:30,180 matter let's go back to our component 9201 06:36:26,580 --> 06:36:32,820 and let's try to mess with the URL 9202 06:36:30,180 --> 06:36:35,580 so first let me just mess it up the 9203 06:36:32,820 --> 06:36:37,980 domain and the moment I do that notice 9204 06:36:35,580 --> 06:36:39,900 right away I have error displayed so 9205 06:36:37,980 --> 06:36:43,500 this is going to be the error response 9206 06:36:39,900 --> 06:36:46,920 so this is going to trigger the sketch 9207 06:36:43,500 --> 06:36:49,080 block with fetch however 9208 06:36:46,920 --> 06:36:51,360 if I'm going to change this around and 9209 06:36:49,080 --> 06:36:54,360 if I'll add the S over here 9210 06:36:51,360 --> 06:36:56,280 and basically if I refresh now I'll have 9211 06:36:54,360 --> 06:36:58,260 this works at 9212 06:36:56,280 --> 06:37:01,558 so as you can see 9213 06:36:58,260 --> 06:37:03,900 there is an error but it's not actually 9214 06:37:01,558 --> 06:37:06,298 handled in here again why is that 9215 06:37:03,900 --> 06:37:09,120 happening because patch doesn't consider 9216 06:37:06,298 --> 06:37:11,100 this 404 as an error effectively we do 9217 06:37:09,120 --> 06:37:13,680 have the successful response over here 9218 06:37:11,100 --> 06:37:16,440 it's just not the user we're looking for 9219 06:37:13,680 --> 06:37:20,700 and the way around that is essentially 9220 06:37:16,440 --> 06:37:24,120 to look for the OK property if you go 9221 06:37:20,700 --> 06:37:26,820 over here and if you log the I'm going 9222 06:37:24,120 --> 06:37:30,240 to go with response notice we have the 9223 06:37:26,820 --> 06:37:33,180 response and then the value is true for 9224 06:37:30,240 --> 06:37:35,580 okay however again if we'll have here 9225 06:37:33,180 --> 06:37:37,320 404 and hopefully I'm not going to run 9226 06:37:35,580 --> 06:37:38,340 out of the requests while I'm showing 9227 06:37:37,320 --> 06:37:40,680 that 9228 06:37:38,340 --> 06:37:43,080 check it out over here this is now false 9229 06:37:40,680 --> 06:37:46,200 so what we can do in the success block 9230 06:37:43,080 --> 06:37:49,020 we can check for that response and if it 9231 06:37:46,200 --> 06:37:52,680 is an error we can set again the state 9232 06:37:49,020 --> 06:37:56,100 value so let me first I guess just 9233 06:37:52,680 --> 06:37:58,680 navigate to a try block and 9234 06:37:56,100 --> 06:38:02,760 notice over here where we have the user 9235 06:37:58,680 --> 06:38:04,020 so I don't want to get the Json if we're 9236 06:38:02,760 --> 06:38:06,240 not successful 9237 06:38:04,020 --> 06:38:08,160 so right after the response we can set 9238 06:38:06,240 --> 06:38:12,660 up a condition and I'm going to say over 9239 06:38:08,160 --> 06:38:15,840 here if response is not true that's what 9240 06:38:12,660 --> 06:38:17,760 that exclamation point means so if the 9241 06:38:15,840 --> 06:38:21,058 response that we're giving back is not 9242 06:38:17,760 --> 06:38:23,458 okay if this Boolean effectively is 9243 06:38:21,058 --> 06:38:25,860 false here's what I want to do I'm going 9244 06:38:23,458 --> 06:38:28,558 to go with set is error I want to set it 9245 06:38:25,860 --> 06:38:31,680 equal to true so now we'll display which 9246 06:38:28,558 --> 06:38:33,298 return this one over here correct 9247 06:38:31,680 --> 06:38:35,218 and then 9248 06:38:33,298 --> 06:38:37,620 after that we do want to set again 9249 06:38:35,218 --> 06:38:39,240 loading is false because I want to 9250 06:38:37,620 --> 06:38:41,400 return from this function 9251 06:38:39,240 --> 06:38:43,620 what I don't want to do is keep reading 9252 06:38:41,400 --> 06:38:46,200 I don't want to go with response Json 9253 06:38:43,620 --> 06:38:49,200 so I want to return however I also want 9254 06:38:46,200 --> 06:38:51,360 to set loading to false so this is where 9255 06:38:49,200 --> 06:38:53,878 we need to do technically double the 9256 06:38:51,360 --> 06:38:56,400 work where I'm gonna go up I'm gonna 9257 06:38:53,878 --> 06:38:59,820 copy and paste and now check it out I 9258 06:38:56,400 --> 06:39:01,740 have true for error loading is false and 9259 06:38:59,820 --> 06:39:03,540 then I'm returning from this function 9260 06:39:01,740 --> 06:39:05,580 what it means that JavaScript is not 9261 06:39:03,540 --> 06:39:07,500 going to read rest of the code because 9262 06:39:05,580 --> 06:39:10,200 again I don't want JavaScript to go with 9263 06:39:07,500 --> 06:39:11,820 response Json why well because I'm not 9264 06:39:10,200 --> 06:39:14,218 getting back the user 9265 06:39:11,820 --> 06:39:17,580 what's the point of turning the error 9266 06:39:14,218 --> 06:39:20,340 into a Json so let me go here and remove 9267 06:39:17,580 --> 06:39:22,740 the log let's save that and now notice 9268 06:39:20,340 --> 06:39:25,080 how we nicely have is error 9269 06:39:22,740 --> 06:39:28,138 even though technically fetch doesn't 9270 06:39:25,080 --> 06:39:30,240 trigger the catch block so that's the 9271 06:39:28,138 --> 06:39:32,160 fetch error catcher I want you to be 9272 06:39:30,240 --> 06:39:35,940 aware of and up next we're going to 9273 06:39:32,160 --> 06:39:38,100 return to the topic why or matters when 9274 06:39:35,940 --> 06:39:41,760 we are setting up multiple returns 9275 06:39:38,100 --> 06:39:43,740 beautiful and up next why don't we come 9276 06:39:41,760 --> 06:39:47,218 back to something we already discussed 9277 06:39:43,740 --> 06:39:49,920 before that order matters 9278 06:39:47,218 --> 06:39:52,440 and why don't we work on a little 9279 06:39:49,920 --> 06:39:55,620 Challenge and if I can ask you something 9280 06:39:52,440 --> 06:39:59,400 please don't dismiss this topic this is 9281 06:39:55,620 --> 06:40:00,958 literally the most discussed question in 9282 06:39:59,400 --> 06:40:03,298 course q a 9283 06:40:00,958 --> 06:40:05,280 because we do Implement that feature 9284 06:40:03,298 --> 06:40:07,920 later on in the project and all that and 9285 06:40:05,280 --> 06:40:10,500 this seems to be tripping up a lot of 9286 06:40:07,920 --> 06:40:13,260 people that's why I purposely in this 9287 06:40:10,500 --> 06:40:15,540 course iteration recording a video in 9288 06:40:13,260 --> 06:40:17,638 tutorial just so we can cover this in 9289 06:40:15,540 --> 06:40:20,580 great detail and essentially the 9290 06:40:17,638 --> 06:40:24,558 challenge is following I want you to the 9291 06:40:20,580 --> 06:40:27,020 structure properties so instead of 9292 06:40:24,558 --> 06:40:30,240 user.user.user.in jsx 9293 06:40:27,020 --> 06:40:32,900 destructure them out of the user 9294 06:40:30,240 --> 06:40:35,580 and access them directly 9295 06:40:32,900 --> 06:40:38,520 and I'm purposely not telling you where 9296 06:40:35,580 --> 06:40:40,680 you should do that because you might or 9297 06:40:38,520 --> 06:40:42,780 might not encounter the bug 9298 06:40:40,680 --> 06:40:44,820 and effectively that's the whole purpose 9299 06:40:42,780 --> 06:40:47,760 of the video 9300 06:40:44,820 --> 06:40:50,340 so let's start working on that I'm going 9301 06:40:47,760 --> 06:40:53,820 to go back to my file 9302 06:40:50,340 --> 06:40:55,440 let's keep on moving and again the idea 9303 06:40:53,820 --> 06:40:56,878 is that I don't want to go with user 9304 06:40:55,440 --> 06:40:58,740 user user 9305 06:40:56,878 --> 06:41:03,298 I just want to grab the properties and 9306 06:40:58,740 --> 06:41:05,100 nicely display them in the jsx so what 9307 06:41:03,298 --> 06:41:07,620 we can do well we can go above the 9308 06:41:05,100 --> 06:41:11,040 return and then one by one pull them out 9309 06:41:07,620 --> 06:41:11,940 right so I can go with Avatar URL then 9310 06:41:11,040 --> 06:41:15,180 name 9311 06:41:11,940 --> 06:41:16,980 then company and I believe I also have 9312 06:41:15,180 --> 06:41:19,080 the bio in there and that is going to be 9313 06:41:16,980 --> 06:41:23,340 equal to what that is going to be equal 9314 06:41:19,080 --> 06:41:24,480 to my user so my state value and now of 9315 06:41:23,340 --> 06:41:26,120 course 9316 06:41:24,480 --> 06:41:30,600 instead of 9317 06:41:26,120 --> 06:41:33,900 user.user.user dot you can simply select 9318 06:41:30,600 --> 06:41:36,120 and then remove those instances 9319 06:41:33,900 --> 06:41:37,620 so let's go over here and it looks like 9320 06:41:36,120 --> 06:41:40,558 a messed it up over here a little bit 9321 06:41:37,620 --> 06:41:43,200 that's okay let me save and if you're 9322 06:41:40,558 --> 06:41:44,638 refreshing everything still works we are 9323 06:41:43,200 --> 06:41:47,280 in good shape 9324 06:41:44,638 --> 06:41:48,600 now what's the purpose of this video 9325 06:41:47,280 --> 06:41:50,878 well 9326 06:41:48,600 --> 06:41:54,180 if you're gonna go above 9327 06:41:50,878 --> 06:41:55,500 the loading and I'm purposely going to 9328 06:41:54,180 --> 06:41:58,920 place it over here just so you can 9329 06:41:55,500 --> 06:42:01,260 clearly see the values but again 9330 06:41:58,920 --> 06:42:04,080 typical question is this one if you 9331 06:42:01,260 --> 06:42:06,298 place before these conditions why we 9332 06:42:04,080 --> 06:42:09,058 have there so let me move this sucker up 9333 06:42:06,298 --> 06:42:10,400 all the way over here by user and then 9334 06:42:09,058 --> 06:42:14,040 let's save 9335 06:42:10,400 --> 06:42:17,100 and what you'll notice that we right 9336 06:42:14,040 --> 06:42:19,860 away have big fat error in the console 9337 06:42:17,100 --> 06:42:22,860 and JavaScript is complaining 9338 06:42:19,860 --> 06:42:26,218 it says I cannot the structure properly 9339 06:42:22,860 --> 06:42:29,058 from the user since this is no 9340 06:42:26,218 --> 06:42:32,700 so very very important to keep in mind 9341 06:42:29,058 --> 06:42:34,860 if you have those multiple returns if 9342 06:42:32,700 --> 06:42:37,920 you're just structuring something you 9343 06:42:34,860 --> 06:42:41,580 need to do that after the conditions 9344 06:42:37,920 --> 06:42:44,100 because keep in mind this is still null 9345 06:42:41,580 --> 06:42:46,080 so as JavaScript is basically reading 9346 06:42:44,100 --> 06:42:48,718 the code okay users now let's keep on 9347 06:42:46,080 --> 06:42:51,480 moving and even before we have hit the 9348 06:42:48,718 --> 06:42:53,820 loading or error and all that you right 9349 06:42:51,480 --> 06:42:55,920 away start the structuring them from the 9350 06:42:53,820 --> 06:42:57,240 null and that's not going to work 9351 06:42:55,920 --> 06:43:00,958 there's a reason why we have those 9352 06:42:57,240 --> 06:43:02,820 conditions so only when we bypass both 9353 06:43:00,958 --> 06:43:03,958 of these conditions we will have that 9354 06:43:02,820 --> 06:43:06,120 user 9355 06:43:03,958 --> 06:43:07,620 I mean if there's an error then of 9356 06:43:06,120 --> 06:43:10,020 course we'll return this one we're not 9357 06:43:07,620 --> 06:43:11,638 even going to get to that line 9358 06:43:10,020 --> 06:43:13,920 so essentially we'll have the early 9359 06:43:11,638 --> 06:43:18,420 return because if there is an error then 9360 06:43:13,920 --> 06:43:21,360 there is no user this is still null and 9361 06:43:18,420 --> 06:43:24,000 again this is not controversial in the 9362 06:43:21,360 --> 06:43:26,400 readme I left some code examples where 9363 06:43:24,000 --> 06:43:28,798 this is null and we cannot pull 9364 06:43:26,400 --> 06:43:31,680 properties out of null so this doesn't 9365 06:43:28,798 --> 06:43:35,820 change we right away basically just try 9366 06:43:31,680 --> 06:43:38,160 to read properties out of the no not out 9367 06:43:35,820 --> 06:43:42,058 of the object now after returns after 9368 06:43:38,160 --> 06:43:44,940 the is loading and and the error yes at 9369 06:43:42,058 --> 06:43:48,660 that point we set the user 9370 06:43:44,940 --> 06:43:50,760 so at that point we should have the user 9371 06:43:48,660 --> 06:43:54,840 object in the state and at that point 9372 06:43:50,760 --> 06:43:57,000 it's great you can definitely do so 9373 06:43:54,840 --> 06:43:59,638 this is going to work for sure 9374 06:43:57,000 --> 06:44:02,540 but before that nope and again let me 9375 06:43:59,638 --> 06:44:05,340 just emphasize this by showing you a few 9376 06:44:02,540 --> 06:44:08,820 vanilla.js examples because I was hoping 9377 06:44:05,340 --> 06:44:10,980 that this is going to help you so if you 9378 06:44:08,820 --> 06:44:11,820 have some object you have a property 9379 06:44:10,980 --> 06:44:13,620 there 9380 06:44:11,820 --> 06:44:16,620 with some kind of value 9381 06:44:13,620 --> 06:44:19,200 I mean you can always go object.name 9382 06:44:16,620 --> 06:44:21,540 okay that's awesome we can get the 9383 06:44:19,200 --> 06:44:23,940 string now what we can also do is simply 9384 06:44:21,540 --> 06:44:26,100 go some object and then let's say that 9385 06:44:23,940 --> 06:44:27,718 we messed up the property but it's not 9386 06:44:26,100 --> 06:44:29,700 there this is still going to work 9387 06:44:27,718 --> 06:44:32,218 JavaScript is going to be like okay that 9388 06:44:29,700 --> 06:44:34,920 property doesn't exist however 9389 06:44:32,218 --> 06:44:36,120 I cannot do this we cannot say hey this 9390 06:44:34,920 --> 06:44:38,218 is null and then I'm going to pull 9391 06:44:36,120 --> 06:44:39,600 something out of the null JavaScript 9392 06:44:38,218 --> 06:44:43,620 will scream 9393 06:44:39,600 --> 06:44:45,360 yo uncomplained we cannot do that and 9394 06:44:43,620 --> 06:44:46,680 also the same thing is going to work 9395 06:44:45,360 --> 06:44:49,020 with arrays and the reason why I'm 9396 06:44:46,680 --> 06:44:51,718 showing you arrays example because in 9397 06:44:49,020 --> 06:44:54,180 one of the projects this is what we do 9398 06:44:51,718 --> 06:44:55,920 we fetch a list but I only want to 9399 06:44:54,180 --> 06:44:57,900 display the first item 9400 06:44:55,920 --> 06:45:00,180 and what do I need to do well I need to 9401 06:44:57,900 --> 06:45:02,218 go with random list or whatever array 9402 06:45:00,180 --> 06:45:05,100 you have and then grab the first one 9403 06:45:02,218 --> 06:45:08,940 however again in the beginning this is 9404 06:45:05,100 --> 06:45:10,620 empty so this will return undefined How 9405 06:45:08,940 --> 06:45:14,400 can there be a first 9406 06:45:10,620 --> 06:45:17,638 item in the array if the array is empty 9407 06:45:14,400 --> 06:45:19,378 so again if we're gonna go here with 9408 06:45:17,638 --> 06:45:21,780 random list 9409 06:45:19,378 --> 06:45:24,120 when this is just empty and then pull 9410 06:45:21,780 --> 06:45:25,680 out the property let's imagine I'm 9411 06:45:24,120 --> 06:45:28,378 trying to access some kind of property 9412 06:45:25,680 --> 06:45:31,860 from the first item not cool at all 9413 06:45:28,378 --> 06:45:34,500 JavaScript will scream yell and complain 9414 06:45:31,860 --> 06:45:36,540 now if you're familiar with optional 9415 06:45:34,500 --> 06:45:38,878 chaining of course you can make a good 9416 06:45:36,540 --> 06:45:41,100 argument well we can avoid all those 9417 06:45:38,878 --> 06:45:43,558 things and yes you're right a bunch of 9418 06:45:41,100 --> 06:45:45,958 times you will actually 9419 06:45:43,558 --> 06:45:47,218 avoid those errors if you use such 9420 06:45:45,958 --> 06:45:49,620 approach 9421 06:45:47,218 --> 06:45:52,200 and we'll definitely cover optional 9422 06:45:49,620 --> 06:45:55,020 training later on in this tutorial 9423 06:45:52,200 --> 06:45:56,760 because it is a very important concept 9424 06:45:55,020 --> 06:46:00,840 however 9425 06:45:56,760 --> 06:46:03,180 the biggest takeaway from this video is 9426 06:46:00,840 --> 06:46:06,120 that order matters and that's why we 9427 06:46:03,180 --> 06:46:07,740 have the error because the object is 9428 06:46:06,120 --> 06:46:09,480 still null 9429 06:46:07,740 --> 06:46:12,240 so essentially 9430 06:46:09,480 --> 06:46:14,760 it's whatever is the default value so 9431 06:46:12,240 --> 06:46:18,840 now let me go back over here and then 9432 06:46:14,760 --> 06:46:20,760 let me move this second all the way 9433 06:46:18,840 --> 06:46:22,740 where I have the return and now of 9434 06:46:20,760 --> 06:46:25,378 course everything is going to work and 9435 06:46:22,740 --> 06:46:28,680 we'll have no errors all right and 9436 06:46:25,378 --> 06:46:30,420 before we discuss one of the rules of 9437 06:46:28,680 --> 06:46:33,780 hooks 9438 06:46:30,420 --> 06:46:35,340 let's also quickly cover the location of 9439 06:46:33,780 --> 06:46:38,280 the fetch function 9440 06:46:35,340 --> 06:46:40,860 so as you're looking at this component 9441 06:46:38,280 --> 06:46:43,860 probably one of the questions you have 9442 06:46:40,860 --> 06:46:46,920 is following can we just move this fetch 9443 06:46:43,860 --> 06:46:49,378 user outside of the use effect or maybe 9444 06:46:46,920 --> 06:46:52,200 into a different file 9445 06:46:49,378 --> 06:46:54,240 and as far as different file yes you can 9446 06:46:52,200 --> 06:46:56,638 do that for sure but just keep in mind 9447 06:46:54,240 --> 06:46:58,320 that we're actually invoking functions 9448 06:46:56,638 --> 06:47:00,718 from this component 9449 06:46:58,320 --> 06:47:03,000 so in that case of course you need to 9450 06:47:00,718 --> 06:47:05,458 pass them as 9451 06:47:03,000 --> 06:47:08,760 parameters correct basically set up the 9452 06:47:05,458 --> 06:47:11,878 parameters and pass them as arguments 9453 06:47:08,760 --> 06:47:14,400 but as far as moving this in to a 9454 06:47:11,878 --> 06:47:17,638 function body yes basically component 9455 06:47:14,400 --> 06:47:19,558 body it's an option so what I could do 9456 06:47:17,638 --> 06:47:22,440 here cut it out 9457 06:47:19,558 --> 06:47:24,900 and then call it into a use effect now 9458 06:47:22,440 --> 06:47:27,958 this copy and paste and everything is 9459 06:47:24,900 --> 06:47:31,680 going to work now it doesn't do that in 9460 06:47:27,958 --> 06:47:32,820 feed however in create react app if you 9461 06:47:31,680 --> 06:47:35,218 do that 9462 06:47:32,820 --> 06:47:37,798 essentially there's going to be a 9463 06:47:35,218 --> 06:47:41,878 eslinteral that's going to complain that 9464 06:47:37,798 --> 06:47:45,180 you need to provide the fetch user as a 9465 06:47:41,878 --> 06:47:48,360 dependency and I want to tell you right 9466 06:47:45,180 --> 06:47:50,580 away not to do that why well because 9467 06:47:48,360 --> 06:47:53,340 we'll have ourselves a problem 9468 06:47:50,580 --> 06:47:55,620 so we'll have our initial render where 9469 06:47:53,340 --> 06:47:58,138 we'll set up the state values and all 9470 06:47:55,620 --> 06:48:01,378 that and during initial render what do 9471 06:47:58,138 --> 06:48:03,718 we do over here we invoke fetch data now 9472 06:48:01,378 --> 06:48:05,100 what is fetch data doing it's updating 9473 06:48:03,718 --> 06:48:07,200 the state value 9474 06:48:05,100 --> 06:48:09,120 we already know that so we will trigger 9475 06:48:07,200 --> 06:48:12,600 the rerend 9476 06:48:09,120 --> 06:48:15,180 so when the re-render happens this gets 9477 06:48:12,600 --> 06:48:17,160 created from the scratch and essentially 9478 06:48:15,180 --> 06:48:18,120 as far as this dependency array is 9479 06:48:17,160 --> 06:48:20,878 concerned 9480 06:48:18,120 --> 06:48:24,420 its value changed so what happens here 9481 06:48:20,878 --> 06:48:25,680 again we fetch data and again this 9482 06:48:24,420 --> 06:48:27,718 problem is going to be easier if I 9483 06:48:25,680 --> 06:48:29,580 showcase that over here now you don't 9484 06:48:27,718 --> 06:48:31,500 have to do that because I believe you'll 9485 06:48:29,580 --> 06:48:34,320 use up all your requests notice I have 9486 06:48:31,500 --> 06:48:36,600 403 right now basically I used up all my 9487 06:48:34,320 --> 06:48:40,200 searches so yeah for sure don't do that 9488 06:48:36,600 --> 06:48:42,000 now I have there was an error right and 9489 06:48:40,200 --> 06:48:44,638 effectively the reason why we have that 9490 06:48:42,000 --> 06:48:46,920 is because we have this infinite Loop so 9491 06:48:44,638 --> 06:48:49,378 my suggestion for now if you're placing 9492 06:48:46,920 --> 06:48:53,878 the function outside of the user effect 9493 06:48:49,378 --> 06:48:55,860 for now just ignore the eslint warning 9494 06:48:53,878 --> 06:48:58,320 you get in the console again you're not 9495 06:48:55,860 --> 06:49:00,900 going to have it with Veet but if you're 9496 06:48:58,320 --> 06:49:03,240 using let's say a create react app 9497 06:49:00,900 --> 06:49:06,120 that's going to be case and later when 9498 06:49:03,240 --> 06:49:09,180 we discuss use callback I'll show you 9499 06:49:06,120 --> 06:49:12,298 two approaches how essentially we can 9500 06:49:09,180 --> 06:49:14,820 make eslint happy and 9501 06:49:12,298 --> 06:49:18,958 have the function here 9502 06:49:14,820 --> 06:49:23,638 and also pass it here as a dependency 9503 06:49:18,958 --> 06:49:27,180 okay and next let's return to one of the 9504 06:49:23,638 --> 06:49:28,260 rules of the hooks and as a side note if 9505 06:49:27,180 --> 06:49:30,240 you don't want to follow along with this 9506 06:49:28,260 --> 06:49:33,000 video you don't have to so in my case 9507 06:49:30,240 --> 06:49:34,740 yes I will import the file and all that 9508 06:49:33,000 --> 06:49:36,718 but basically I just want to showcase a 9509 06:49:34,740 --> 06:49:38,340 few things and the reason why I set up 9510 06:49:36,718 --> 06:49:40,920 the file just so you can have it for 9511 06:49:38,340 --> 06:49:42,660 your reference so long story short if 9512 06:49:40,920 --> 06:49:46,558 you don't feel like it you can just sit 9513 06:49:42,660 --> 06:49:51,120 back and relax and watch me struggle so 9514 06:49:46,558 --> 06:49:53,940 if I navigate to the file d03 hooks rule 9515 06:49:51,120 --> 06:49:56,100 you'll notice that I have a your state 9516 06:49:53,940 --> 06:49:58,020 and use effect I have some kind of 9517 06:49:56,100 --> 06:49:59,280 condition again this is just a random 9518 06:49:58,020 --> 06:50:02,580 value and 9519 06:49:59,280 --> 06:50:05,700 the Boolean value is true 9520 06:50:02,580 --> 06:50:08,638 so remember when we discussed hooks one 9521 06:50:05,700 --> 06:50:10,080 of the rules was don't call hooks 9522 06:50:08,638 --> 06:50:12,660 conditionally 9523 06:50:10,080 --> 06:50:15,320 so this is not going to work you cannot 9524 06:50:12,660 --> 06:50:18,298 place a hook inside of the if condition 9525 06:50:15,320 --> 06:50:19,260 hooks need to be called in the same 9526 06:50:18,298 --> 06:50:22,040 order 9527 06:50:19,260 --> 06:50:26,400 and just to show you another example 9528 06:50:22,040 --> 06:50:29,638 which sometimes gets discussed in the 9529 06:50:26,400 --> 06:50:33,120 course q a let me first comment this one 9530 06:50:29,638 --> 06:50:35,820 out and then uncomment the second one so 9531 06:50:33,120 --> 06:50:37,500 you'll notice over here I have the early 9532 06:50:35,820 --> 06:50:40,740 return 9533 06:50:37,500 --> 06:50:43,200 now this again is not something you want 9534 06:50:40,740 --> 06:50:45,540 to do you don't want to place use effect 9535 06:50:43,200 --> 06:50:48,660 after this condition because keep in 9536 06:50:45,540 --> 06:50:51,298 mind if you're returning that's it we 9537 06:50:48,660 --> 06:50:54,780 stop reading the code so effectively we 9538 06:50:51,298 --> 06:50:57,240 call the use effect conditionally and 9539 06:50:54,780 --> 06:50:59,218 Veet is not going to throw you a bunch 9540 06:50:57,240 --> 06:51:00,780 of errors but if you use for example 9541 06:50:59,218 --> 06:51:03,058 create react app 9542 06:51:00,780 --> 06:51:05,520 you'll have a bunch of warnings and 9543 06:51:03,058 --> 06:51:07,320 errors in the console and all that so 9544 06:51:05,520 --> 06:51:09,120 again just few things to keep in mind 9545 06:51:07,320 --> 06:51:11,878 don't place 9546 06:51:09,120 --> 06:51:14,520 the hook inside of the if condition and 9547 06:51:11,878 --> 06:51:17,218 also be careful if you have multiple 9548 06:51:14,520 --> 06:51:20,160 returns basically if you have jsx 9549 06:51:17,218 --> 06:51:22,680 returned based on some condition don't 9550 06:51:20,160 --> 06:51:24,540 Place use effect after them make sure 9551 06:51:22,680 --> 06:51:26,520 that use effect is always before those 9552 06:51:24,540 --> 06:51:29,878 conditions and then yes of course you 9553 06:51:26,520 --> 06:51:32,218 can set up tons of multiple returns so 9554 06:51:29,878 --> 06:51:35,040 that is still okay but you don't want to 9555 06:51:32,218 --> 06:51:37,320 call this one conditionally it's not 9556 06:51:35,040 --> 06:51:40,320 going to work okay and up next let's 9557 06:51:37,320 --> 06:51:44,700 quickly discuss truthy and falsy values 9558 06:51:40,320 --> 06:51:46,020 as well as short circuit evaluation in 9559 06:51:44,700 --> 06:51:48,360 JavaScript 9560 06:51:46,020 --> 06:51:50,760 because we'll heavily rely on these 9561 06:51:48,360 --> 06:51:52,020 topics in the following videos now I can 9562 06:51:50,760 --> 06:51:53,878 tell you right away that if you're 9563 06:51:52,020 --> 06:51:56,580 familiar with these topics feel free to 9564 06:51:53,878 --> 06:51:59,700 skip this video I'll just quickly cover 9565 06:51:56,580 --> 06:52:01,860 the major things we need to be aware of 9566 06:51:59,700 --> 06:52:04,320 again the whole purpose is 9567 06:52:01,860 --> 06:52:06,478 we'll use that in react and I really 9568 06:52:04,320 --> 06:52:08,700 want to make sure that we're all on the 9569 06:52:06,478 --> 06:52:11,218 same page if you're familiar with these 9570 06:52:08,700 --> 06:52:13,500 Concepts then feel free to skip that and 9571 06:52:11,218 --> 06:52:15,900 effectively in JavaScript we have falsy 9572 06:52:13,500 --> 06:52:18,660 and truthy values and it's going to be 9573 06:52:15,900 --> 06:52:21,378 easier to remember the falsy ones and 9574 06:52:18,660 --> 06:52:24,840 those are false zero empty string null 9575 06:52:21,378 --> 06:52:26,700 undefined and not a number with that 9576 06:52:24,840 --> 06:52:29,160 said you have to sit here and memorize 9577 06:52:26,700 --> 06:52:32,100 them now just don't be surprised when we 9578 06:52:29,160 --> 06:52:33,958 use for example empty string in our 9579 06:52:32,100 --> 06:52:36,600 conditions just remember that it is 9580 06:52:33,958 --> 06:52:37,798 falsy now why do we care about that well 9581 06:52:36,600 --> 06:52:40,978 because 9582 06:52:37,798 --> 06:52:43,798 truthy and positive values can be 9583 06:52:40,978 --> 06:52:47,400 evaluated in the condition 9584 06:52:43,798 --> 06:52:51,120 so we can set up a condition not with 9585 06:52:47,400 --> 06:52:54,058 just true and false the Boolean value if 9586 06:52:51,120 --> 06:52:56,340 let's say I have hello and as a quick 9587 06:52:54,058 --> 06:52:58,440 side note so these are the falsy ones 9588 06:52:56,340 --> 06:52:59,700 and all the other ones are truthy that's 9589 06:52:58,440 --> 06:53:03,058 why it's going to be easier to remember 9590 06:52:59,700 --> 06:53:06,180 the falsely ones so fuzzy ones will 9591 06:53:03,058 --> 06:53:09,058 evaluate in the condition to false and 9592 06:53:06,180 --> 06:53:11,280 truthy ones will evaluate to true so 9593 06:53:09,058 --> 06:53:14,760 again back to your example if you have 9594 06:53:11,280 --> 06:53:16,860 these values so X Y and Z and the first 9595 06:53:14,760 --> 06:53:19,500 one is a string a valid string second 9596 06:53:16,860 --> 06:53:20,580 one is an empty one and third one is a 9597 06:53:19,500 --> 06:53:22,978 zero 9598 06:53:20,580 --> 06:53:25,558 for the first one if you'll set it up in 9599 06:53:22,978 --> 06:53:28,740 the condition actually you'll see in the 9600 06:53:25,558 --> 06:53:31,740 console axis truthy so this evaluates 9601 06:53:28,740 --> 06:53:34,500 the true again instead of a Boolean this 9602 06:53:31,740 --> 06:53:36,840 is a string with some kind of value now 9603 06:53:34,500 --> 06:53:38,638 when it comes to a second and third if 9604 06:53:36,840 --> 06:53:43,558 you'll run this code you'll actually see 9605 06:53:38,638 --> 06:53:47,638 in a console that it evaluates to false 9606 06:53:43,558 --> 06:53:50,400 so in the console you'll see Y is fuzzy 9607 06:53:47,638 --> 06:53:54,660 now why do we care about that because we 9608 06:53:50,400 --> 06:53:57,360 also have short circuit evaluation 9609 06:53:54,660 --> 06:54:00,240 and essentially it just allows us to 9610 06:53:57,360 --> 06:54:03,718 write code in more concise way and we 9611 06:54:00,240 --> 06:54:06,780 can do that by using and operator 9612 06:54:03,718 --> 06:54:09,718 and or operator 9613 06:54:06,780 --> 06:54:13,378 and as far as the end operator it 9614 06:54:09,718 --> 06:54:15,240 Returns the first operand basically this 9615 06:54:13,378 --> 06:54:19,200 value over here 9616 06:54:15,240 --> 06:54:22,620 if it is fuzzy or the second one if the 9617 06:54:19,200 --> 06:54:25,378 first one is truthy so let's just back 9618 06:54:22,620 --> 06:54:27,718 up a little bit and let's start 9619 06:54:25,378 --> 06:54:30,540 everything from scratch so this one is 9620 06:54:27,718 --> 06:54:32,878 zero what it is it is falsy correct and 9621 06:54:30,540 --> 06:54:35,580 this one is what it's truthy because 9622 06:54:32,878 --> 06:54:39,900 it's essentially a integer that is not 9623 06:54:35,580 --> 06:54:43,020 zero now if we have this and operator 9624 06:54:39,900 --> 06:54:45,298 then basically if this one is falsy 9625 06:54:43,020 --> 06:54:47,700 it will be returned 9626 06:54:45,298 --> 06:54:49,740 so if you have two of them and the first 9627 06:54:47,700 --> 06:54:51,958 one is falsy then that's the one that's 9628 06:54:49,740 --> 06:54:53,760 going to be returned from this 9629 06:54:51,958 --> 06:54:57,240 expression now 9630 06:54:53,760 --> 06:54:59,878 if it's the other way around if you have 9631 06:54:57,240 --> 06:55:02,520 truthy as the first one 9632 06:54:59,878 --> 06:55:03,900 then it will return regardless the 9633 06:55:02,520 --> 06:55:06,478 second value 9634 06:55:03,900 --> 06:55:08,760 something to keep in mind and when it 9635 06:55:06,478 --> 06:55:12,360 comes to or operator 9636 06:55:08,760 --> 06:55:13,558 it Returns the first operand if it is 9637 06:55:12,360 --> 06:55:17,040 truthy 9638 06:55:13,558 --> 06:55:19,320 or the second operand if the first one 9639 06:55:17,040 --> 06:55:20,940 is fuzzy so as you can see it's the 9640 06:55:19,320 --> 06:55:25,440 other way around 9641 06:55:20,940 --> 06:55:28,860 if we have example of again zero and one 9642 06:55:25,440 --> 06:55:30,840 since this is falsy 9643 06:55:28,860 --> 06:55:34,080 the second one is going to be returned 9644 06:55:30,840 --> 06:55:37,500 so opposite of end operator 9645 06:55:34,080 --> 06:55:40,580 and if this is truthy 9646 06:55:37,500 --> 06:55:44,160 then the first one is going to be return 9647 06:55:40,580 --> 06:55:46,320 hopefully that is clear and again the 9648 06:55:44,160 --> 06:55:50,638 reason why we quickly covered both of 9649 06:55:46,320 --> 06:55:54,298 them is because we will use them in our 9650 06:55:50,638 --> 06:55:56,520 react setup and as far as 9651 06:55:54,298 --> 06:55:58,740 function example since we're working 9652 06:55:56,520 --> 06:56:02,580 with functions if I have function 9653 06:55:58,740 --> 06:56:04,138 display name and if I'm looking for the 9654 06:56:02,580 --> 06:56:06,478 argument of name 9655 06:56:04,138 --> 06:56:10,020 if it is provided 9656 06:56:06,478 --> 06:56:13,860 then of course I'll have the argument 9657 06:56:10,020 --> 06:56:17,940 because in return I have a short circuit 9658 06:56:13,860 --> 06:56:20,700 operator I have the or one and remember 9659 06:56:17,940 --> 06:56:24,000 if this one is full zip 9660 06:56:20,700 --> 06:56:25,978 basically if we don't provide it then 9661 06:56:24,000 --> 06:56:27,058 it's right away gonna return the second 9662 06:56:25,978 --> 06:56:30,000 value 9663 06:56:27,058 --> 06:56:31,920 if this one is true then it's going to 9664 06:56:30,000 --> 06:56:33,360 return the first value again let's 9665 06:56:31,920 --> 06:56:35,940 double check 9666 06:56:33,360 --> 06:56:40,558 if the first one is palsy second one 9667 06:56:35,940 --> 06:56:42,600 will be returned if it's truthy then the 9668 06:56:40,558 --> 06:56:43,740 first one is going to be right away 9669 06:56:42,600 --> 06:56:46,620 return 9670 06:56:43,740 --> 06:56:48,600 hopefully we're on the same page and up 9671 06:56:46,620 --> 06:56:51,478 next let's see 9672 06:56:48,600 --> 06:56:53,820 how we can set up such functionality in 9673 06:56:51,478 --> 06:56:56,820 our react components beautiful and once 9674 06:56:53,820 --> 06:57:00,120 we have jog your memory on the short 9675 06:56:56,820 --> 06:57:03,180 circuit evaluation as well as truthy and 9676 06:57:00,120 --> 06:57:05,878 falsy values in JavaScript now let's try 9677 06:57:03,180 --> 06:57:07,920 to set them up in react and we'll write 9678 06:57:05,878 --> 06:57:09,718 away start with the challenge where 9679 06:57:07,920 --> 06:57:13,080 basically I want you to navigate to this 9680 06:57:09,718 --> 06:57:15,958 file and I want you to create two State 9681 06:57:13,080 --> 06:57:18,958 values one is going to be truthy and one 9682 06:57:15,958 --> 06:57:22,558 is going to be falsy and then in the jsx 9683 06:57:18,958 --> 06:57:26,040 I want you to set up both conditions for 9684 06:57:22,558 --> 06:57:30,240 each operator so 9685 06:57:26,040 --> 06:57:33,718 try two of them with palsy so or and and 9686 06:57:30,240 --> 06:57:34,680 and the same goes for truthy and a tiny 9687 06:57:33,718 --> 06:57:37,500 hint 9688 06:57:34,680 --> 06:57:40,080 you want to do that in the curlish in 9689 06:57:37,500 --> 06:57:42,058 the jsx so let's start working on that 9690 06:57:40,080 --> 06:57:45,540 basically 9691 06:57:42,058 --> 06:57:47,520 in the objects I have a star and at the 9692 06:57:45,540 --> 06:57:49,080 moment I'm rendering this file okay 9693 06:57:47,520 --> 06:57:50,700 that's good 9694 06:57:49,080 --> 06:57:53,160 that's an awesome start now let me 9695 06:57:50,700 --> 06:57:55,680 navigate over there as you can see I 9696 06:57:53,160 --> 06:57:56,940 just have huge State import and now 9697 06:57:55,680 --> 06:57:59,280 let's set up those 9698 06:57:56,940 --> 06:58:02,100 two State values now you can go with 9699 06:57:59,280 --> 06:58:03,900 Boolean that's definitely an option but 9700 06:58:02,100 --> 06:58:06,240 I'm just going to go with empty string 9701 06:58:03,900 --> 06:58:08,100 and string with some kind of value so 9702 06:58:06,240 --> 06:58:10,740 those are going to be my ones I'm going 9703 06:58:08,100 --> 06:58:15,180 to go over here and say fuzzy now let's 9704 06:58:10,740 --> 06:58:18,660 create that value so text and set text 9705 06:58:15,180 --> 06:58:19,978 and that is equal to use State and like 9706 06:58:18,660 --> 06:58:23,520 I said we'll start with the false even 9707 06:58:19,978 --> 06:58:25,978 then I'm gonna copy and paste so with me 9708 06:58:23,520 --> 06:58:28,798 select both of these lines over here and 9709 06:58:25,978 --> 06:58:31,378 this is going to be true thing so truthy 9710 06:58:28,798 --> 06:58:32,940 over here and as far as the name I'm 9711 06:58:31,378 --> 06:58:35,458 gonna go with name 9712 06:58:32,940 --> 06:58:38,580 and set name 9713 06:58:35,458 --> 06:58:40,920 set name and in here as far as the 9714 06:58:38,580 --> 06:58:42,420 default one I'll go with Susan 9715 06:58:40,920 --> 06:58:46,740 okay that's awesome 9716 06:58:42,420 --> 06:58:50,100 so now let's navigate to jsx and let's 9717 06:58:46,740 --> 06:58:52,500 go over here with div and before we type 9718 06:58:50,100 --> 06:58:54,478 anything let's just refresh our memory 9719 06:58:52,500 --> 06:58:58,020 that in a jsx we cannot do something 9720 06:58:54,478 --> 06:59:01,200 like this I cannot set up here a if 9721 06:58:58,020 --> 06:59:03,420 condition if and then something this is 9722 06:59:01,200 --> 06:59:05,160 not going to work so we'll have some 9723 06:59:03,420 --> 06:59:06,000 kind of condition regardless of the 9724 06:59:05,160 --> 06:59:08,100 value 9725 06:59:06,000 --> 06:59:10,138 will get right away an error something 9726 06:59:08,100 --> 06:59:10,920 to keep in mind so let me set this one 9727 06:59:10,138 --> 06:59:12,660 up 9728 06:59:10,920 --> 06:59:16,020 in a separate line 9729 06:59:12,660 --> 06:59:18,058 and let me just add that value 9730 06:59:16,020 --> 06:59:20,280 just so it stays for your reference so 9731 06:59:18,058 --> 06:59:21,500 let's go over here and let's say it 9732 06:59:20,280 --> 06:59:24,958 won't work 9733 06:59:21,500 --> 06:59:27,540 I'll work over here and let me comment 9734 06:59:24,958 --> 06:59:31,378 this one up and since we cannot use if 9735 06:59:27,540 --> 06:59:34,260 conditions in jsx but we obviously still 9736 06:59:31,378 --> 06:59:37,320 want to display stuff based on some 9737 06:59:34,260 --> 06:59:40,620 condition that's exactly why we need to 9738 06:59:37,320 --> 06:59:43,798 understand how short circuit operators 9739 06:59:40,620 --> 06:59:45,840 work in react and now let's keep on 9740 06:59:43,798 --> 06:59:49,378 moving and effectively you want to set 9741 06:59:45,840 --> 06:59:52,680 up four heading fours and in there again 9742 06:59:49,378 --> 06:59:54,420 I'll have both conditions so let me 9743 06:59:52,680 --> 06:59:57,780 start with the first one and I'll say 9744 06:59:54,420 --> 07:00:01,080 fuzzy and this is going to be or example 9745 06:59:57,780 --> 07:00:03,298 let me set up a colon then I'm going to 9746 07:00:01,080 --> 07:00:06,298 go with the curlies and I'm looking for 9747 07:00:03,298 --> 07:00:08,100 a text so that's my falsy value and I'm 9748 07:00:06,298 --> 07:00:10,798 going to use my or operator 9749 07:00:08,100 --> 07:00:13,200 so now you have to guess which one is 9750 07:00:10,798 --> 07:00:15,840 going to be displayed is it going to be 9751 07:00:13,200 --> 07:00:18,840 the text one basically empty string or 9752 07:00:15,840 --> 07:00:21,298 is it going to be a hello world and once 9753 07:00:18,840 --> 07:00:23,878 I save I have Hello World why well 9754 07:00:21,298 --> 07:00:26,458 because with or operator if this is 9755 07:00:23,878 --> 07:00:29,878 going to be falsy then it's going to 9756 07:00:26,458 --> 07:00:32,458 display the second value so now let me 9757 07:00:29,878 --> 07:00:34,378 copy and paste then like I said I want 9758 07:00:32,458 --> 07:00:37,860 to set up four of them so this is going 9759 07:00:34,378 --> 07:00:40,378 to be and and then this will be truthy 9760 07:00:37,860 --> 07:00:42,860 so now let me select both of them 9761 07:00:40,378 --> 07:00:45,900 and I'll say truthy 9762 07:00:42,860 --> 07:00:47,580 and this one will be and 9763 07:00:45,900 --> 07:00:49,558 and here 9764 07:00:47,580 --> 07:00:52,620 effectively I just want to change the 9765 07:00:49,558 --> 07:00:54,120 operator and once I save notice now I 9766 07:00:52,620 --> 07:00:57,000 have empty string 9767 07:00:54,120 --> 07:00:59,400 so this is how it's going to work in 9768 07:00:57,000 --> 07:01:01,978 react again it's the same as in 9769 07:00:59,400 --> 07:01:04,798 JavaScript if we have falzi and we have 9770 07:01:01,978 --> 07:01:06,780 and then effectively we display the 9771 07:01:04,798 --> 07:01:08,900 first one so now let's just change this 9772 07:01:06,780 --> 07:01:12,180 around where it's not going to be text 9773 07:01:08,900 --> 07:01:13,798 essentially I want to go with name and 9774 07:01:12,180 --> 07:01:17,458 you'll see that we have 9775 07:01:13,798 --> 07:01:20,580 opposite Behavior where if this is or 9776 07:01:17,458 --> 07:01:22,920 then we'll display the first one however 9777 07:01:20,580 --> 07:01:24,900 if we have the and one 9778 07:01:22,920 --> 07:01:27,718 then of course we'll display the second 9779 07:01:24,900 --> 07:01:30,298 one so if this is truthy 9780 07:01:27,718 --> 07:01:32,458 then we right away display the second 9781 07:01:30,298 --> 07:01:34,798 value and lastly I just want to mention 9782 07:01:32,458 --> 07:01:36,660 that of course we're not 9783 07:01:34,798 --> 07:01:39,780 limited to just set up this 9784 07:01:36,660 --> 07:01:42,058 functionality image.jsx directly I can 9785 07:01:39,780 --> 07:01:43,760 also do something like this where I'm 9786 07:01:42,058 --> 07:01:46,860 going to go with constant and code 9787 07:01:43,760 --> 07:01:49,080 example and that one is equal to 9788 07:01:46,860 --> 07:01:50,520 whichever value I want so again I'm 9789 07:01:49,080 --> 07:01:53,160 going to use the same one where I'm 9790 07:01:50,520 --> 07:01:56,878 going to go with text and then 9791 07:01:53,160 --> 07:01:59,340 hello world and then we can render it 9792 07:01:56,878 --> 07:02:00,360 right away in the jsx just showing you 9793 07:01:59,340 --> 07:02:02,580 multiple 9794 07:02:00,360 --> 07:02:04,320 options that you have let me save that 9795 07:02:02,580 --> 07:02:07,620 and of course since this is falsy again 9796 07:02:04,320 --> 07:02:08,780 we have or operator so we display the 9797 07:02:07,620 --> 07:02:12,058 second value 9798 07:02:08,780 --> 07:02:14,340 ratcheted for General concept and up 9799 07:02:12,058 --> 07:02:17,760 next let's look at the most common 9800 07:02:14,340 --> 07:02:20,040 examples okay and once we're familiar 9801 07:02:17,760 --> 07:02:22,820 with the general concept now let's take 9802 07:02:20,040 --> 07:02:27,600 a look at some examples so in the app 9803 07:02:22,820 --> 07:02:30,120 jsx I want you to import zero five and 9804 07:02:27,600 --> 07:02:32,160 then the examples and as far as the 9805 07:02:30,120 --> 07:02:36,240 component notice over here 9806 07:02:32,160 --> 07:02:38,100 we have a few more values basically we 9807 07:02:36,240 --> 07:02:41,520 still have the empty string still have 9808 07:02:38,100 --> 07:02:44,100 some kind of text also a user and we 9809 07:02:41,520 --> 07:02:46,500 also have a Boolean value 9810 07:02:44,100 --> 07:02:49,978 and effectively when it comes to or 9811 07:02:46,500 --> 07:02:52,138 quite often it is used to display some 9812 07:02:49,978 --> 07:02:55,138 kind of default value so let's say if I 9813 07:02:52,138 --> 07:02:56,940 navigate here and if I set up a div and 9814 07:02:55,138 --> 07:02:58,558 then inside of that div 9815 07:02:56,940 --> 07:03:01,080 I want to go with some kind of heading 9816 07:02:58,558 --> 07:03:03,240 2. Imagine This 9817 07:03:01,080 --> 07:03:05,520 I have some kind of value 9818 07:03:03,240 --> 07:03:08,218 in this case I'm going to go text and if 9819 07:03:05,520 --> 07:03:10,798 that value doesn't exist then I want to 9820 07:03:08,218 --> 07:03:13,138 go with some kind of default one now if 9821 07:03:10,798 --> 07:03:15,780 you're wondering like why value wouldn't 9822 07:03:13,138 --> 07:03:17,700 exist well keep in mind that quite often 9823 07:03:15,780 --> 07:03:21,180 we'll be getting stuff 9824 07:03:17,700 --> 07:03:24,780 from the API and it's not guaranteed 9825 07:03:21,180 --> 07:03:26,458 that for all the items that property is 9826 07:03:24,780 --> 07:03:28,260 actually going to be there so this is 9827 07:03:26,458 --> 07:03:32,100 very useful where you can be like okay 9828 07:03:28,260 --> 07:03:35,100 try to set this property if not then use 9829 07:03:32,100 --> 07:03:37,138 the default value and of course let's go 9830 07:03:35,100 --> 07:03:39,240 with name here and you'll see that if 9831 07:03:37,138 --> 07:03:41,520 the value is there then of course it 9832 07:03:39,240 --> 07:03:45,058 will display the value so that is a 9833 07:03:41,520 --> 07:03:48,240 pretty common approach for or operator 9834 07:03:45,058 --> 07:03:50,040 in react now when it comes to and it's a 9835 07:03:48,240 --> 07:03:53,280 little bit different where instead of 9836 07:03:50,040 --> 07:03:55,440 text you essentially control what 9837 07:03:53,280 --> 07:03:57,478 elements are going to be displayed now 9838 07:03:55,440 --> 07:04:00,718 I'm going to show you both examples 9839 07:03:57,478 --> 07:04:03,058 where we use the elements as well as the 9840 07:04:00,718 --> 07:04:04,978 components just keep in mind that both 9841 07:04:03,058 --> 07:04:08,638 of them effectively work exactly the 9842 07:04:04,978 --> 07:04:10,798 same way so if I go here and right after 9843 07:04:08,638 --> 07:04:14,160 text I'm just going to go with the 9844 07:04:10,798 --> 07:04:16,500 curlies and I'll say if text 9845 07:04:14,160 --> 07:04:19,798 evaluates to true what do I want to do 9846 07:04:16,500 --> 07:04:22,680 well then I want to return a div with 9847 07:04:19,798 --> 07:04:26,218 two heading twos so let's go over here 9848 07:04:22,680 --> 07:04:29,580 and let's say div 9849 07:04:26,218 --> 07:04:31,740 close div here and then inside of the 9850 07:04:29,580 --> 07:04:33,780 div we want to go with those two heading 9851 07:04:31,740 --> 07:04:36,298 Tools in one I'm gonna display the name 9852 07:04:33,780 --> 07:04:38,760 so I'm going to go over here and I'll 9853 07:04:36,298 --> 07:04:41,100 say adding two whatever 9854 07:04:38,760 --> 07:04:43,798 and return 9855 07:04:41,100 --> 07:04:46,680 and then right after that we want to go 9856 07:04:43,798 --> 07:04:49,440 with heading two and I'm going to access 9857 07:04:46,680 --> 07:04:52,260 the name over here and notice once I 9858 07:04:49,440 --> 07:04:55,558 save I don't see anything why well 9859 07:04:52,260 --> 07:04:57,120 because this is false correct so this 9860 07:04:55,558 --> 07:05:00,360 one is an empty one 9861 07:04:57,120 --> 07:05:03,360 and as a result we don't return anything 9862 07:05:00,360 --> 07:05:04,860 basically we have nothing in the browser 9863 07:05:03,360 --> 07:05:07,500 and that's something important to keep 9864 07:05:04,860 --> 07:05:09,058 in mind where in this case 9865 07:05:07,500 --> 07:05:12,540 there's always going to be that having 9866 07:05:09,058 --> 07:05:15,058 two tag however if we use and operator 9867 07:05:12,540 --> 07:05:17,940 and basically we set up whatever we want 9868 07:05:15,058 --> 07:05:20,458 to return you'll actually notice that 9869 07:05:17,940 --> 07:05:23,280 there is no div with those heading tools 9870 07:05:20,458 --> 07:05:25,558 it just doesn't exist so we only have 9871 07:05:23,280 --> 07:05:28,080 this heading 2 with default value we 9872 07:05:25,558 --> 07:05:31,020 don't have that second dip now of course 9873 07:05:28,080 --> 07:05:33,000 if this is going to be truthy if we'll 9874 07:05:31,020 --> 07:05:35,218 add just let's say even one character 9875 07:05:33,000 --> 07:05:36,120 you'll right away notice that first of 9876 07:05:35,218 --> 07:05:38,940 all 9877 07:05:36,120 --> 07:05:41,040 since we have the or operator here it's 9878 07:05:38,940 --> 07:05:43,740 going to display this one and when it 9879 07:05:41,040 --> 07:05:45,900 comes to text well now since this 9880 07:05:43,740 --> 07:05:48,660 evaluates the true now we actually 9881 07:05:45,900 --> 07:05:51,840 render div with those two running twos 9882 07:05:48,660 --> 07:05:53,940 and of course we access the name here as 9883 07:05:51,840 --> 07:05:56,400 well so those are pretty common 9884 07:05:53,940 --> 07:05:58,500 approaches where again we use or inside 9885 07:05:56,400 --> 07:06:01,138 of the element if we want to display 9886 07:05:58,500 --> 07:06:03,718 some kind of default one and we use and 9887 07:06:01,138 --> 07:06:05,638 to control effectively what we return 9888 07:06:03,718 --> 07:06:07,558 now of course this is going to be 9889 07:06:05,638 --> 07:06:09,478 dynamic at the moment we're just hard 9890 07:06:07,558 --> 07:06:12,478 coding but normally this is going to be 9891 07:06:09,478 --> 07:06:14,340 dynamic so let me set it back here to an 9892 07:06:12,478 --> 07:06:16,860 empty string and again notice how we're 9893 07:06:14,340 --> 07:06:19,320 not returning anything and before I show 9894 07:06:16,860 --> 07:06:21,600 you the component example let's also 9895 07:06:19,320 --> 07:06:24,718 cover that we can use the not operator 9896 07:06:21,600 --> 07:06:26,160 so at the moment this is fuzzy right 9897 07:06:24,718 --> 07:06:27,600 that's why we don't display anything 9898 07:06:26,160 --> 07:06:31,138 however 9899 07:06:27,600 --> 07:06:33,660 if I stick a not operator in front of it 9900 07:06:31,138 --> 07:06:36,120 you'll see that actually everything is 9901 07:06:33,660 --> 07:06:39,840 displayed now if you're not familiar 9902 07:06:36,120 --> 07:06:42,958 with not operator in readme essentially 9903 07:06:39,840 --> 07:06:45,298 you'll find code examples for vanilla.js 9904 07:06:42,958 --> 07:06:47,340 again I don't want to spend too much 9905 07:06:45,298 --> 07:06:49,798 time on that because I want to move on 9906 07:06:47,340 --> 07:06:51,840 with react topics but again if you need 9907 07:06:49,798 --> 07:06:54,900 to jog your memory on the not operator 9908 07:06:51,840 --> 07:06:56,820 please utilize the readme where you'll 9909 07:06:54,900 --> 07:06:58,500 find the vanilla Jazz examples and of 9910 07:06:56,820 --> 07:07:00,360 course it's going to be easier to 9911 07:06:58,500 --> 07:07:03,600 understand so essentially if I just 9912 07:07:00,360 --> 07:07:05,580 stick here the exclamation point in 9913 07:07:03,600 --> 07:07:08,218 front of it I'm good to go I'm actually 9914 07:07:05,580 --> 07:07:10,740 displaying this why well because by 9915 07:07:08,218 --> 07:07:13,020 default essentially this is all Z 9916 07:07:10,740 --> 07:07:15,298 however with not operator I'm looking 9917 07:07:13,020 --> 07:07:16,500 for the opposite value so now this is 9918 07:07:15,298 --> 07:07:18,718 true thing 9919 07:07:16,500 --> 07:07:21,180 and I display it over here again 9920 07:07:18,718 --> 07:07:24,000 something important to keep in mind so 9921 07:07:21,180 --> 07:07:26,400 I'll copy and paste I'll remove this one 9922 07:07:24,000 --> 07:07:29,100 and I'll just comment this one out over 9923 07:07:26,400 --> 07:07:31,260 here just so we don't have too many and 9924 07:07:29,100 --> 07:07:33,958 as always you can reference do you read 9925 07:07:31,260 --> 07:07:36,600 me as well and now since we're familiar 9926 07:07:33,958 --> 07:07:39,180 how we can use the and operator to 9927 07:07:36,600 --> 07:07:41,878 return some elements why don't we also 9928 07:07:39,180 --> 07:07:46,020 take a look how we can do the same thing 9929 07:07:41,878 --> 07:07:47,940 with components so below my first 9930 07:07:46,020 --> 07:07:50,100 component I want to go with second one 9931 07:07:47,940 --> 07:07:53,940 I'm going to go with const and then I'll 9932 07:07:50,100 --> 07:07:56,218 just say sum component now I know that 9933 07:07:53,940 --> 07:07:57,958 I'll pass the prop so I'll write away 9934 07:07:56,218 --> 07:08:00,240 the structure it and essentially I'm 9935 07:07:57,958 --> 07:08:01,740 going to be looking for the name prop 9936 07:08:00,240 --> 07:08:04,620 so let's go over here 9937 07:08:01,740 --> 07:08:05,820 and then as far as the return we'll do 9938 07:08:04,620 --> 07:08:07,978 the same thing 9939 07:08:05,820 --> 07:08:10,920 it's just now we'll return from the 9940 07:08:07,978 --> 07:08:13,500 component so if I go with return Then 9941 07:08:10,920 --> 07:08:15,600 space and I'll cheat a little bit then 9942 07:08:13,500 --> 07:08:17,700 I'm just going to copy and paste so 9943 07:08:15,600 --> 07:08:20,040 that's going to be my component 9944 07:08:17,700 --> 07:08:23,340 let me scroll up over here and I guess 9945 07:08:20,040 --> 07:08:25,500 I'll do after not so I'll set up over 9946 07:08:23,340 --> 07:08:27,718 here a logic where 9947 07:08:25,500 --> 07:08:31,378 essentially if 9948 07:08:27,718 --> 07:08:33,298 the value is true then I want to return 9949 07:08:31,378 --> 07:08:36,298 that component and in this case I'm 9950 07:08:33,298 --> 07:08:39,240 going to be looking for the user just to 9951 07:08:36,298 --> 07:08:42,000 use a more complex example so notice 9952 07:08:39,240 --> 07:08:44,340 user set user use State and this is an 9953 07:08:42,000 --> 07:08:47,218 object and of course this will evaluate 9954 07:08:44,340 --> 07:08:49,558 to true so now let's scroll down and 9955 07:08:47,218 --> 07:08:52,860 let's say over here that if the user 9956 07:08:49,558 --> 07:08:55,080 exists only then I want to display the 9957 07:08:52,860 --> 07:08:57,478 component so say over here some 9958 07:08:55,080 --> 07:08:59,638 component and then I want to pass in 9959 07:08:57,478 --> 07:09:01,080 that name prop now what is it going to 9960 07:08:59,638 --> 07:09:04,620 be equal to well I'm going to go with 9961 07:09:01,080 --> 07:09:07,260 name equals to user.name and then I want 9962 07:09:04,620 --> 07:09:09,298 to close over here the component and as 9963 07:09:07,260 --> 07:09:12,298 you can see now in the browser we have 9964 07:09:09,298 --> 07:09:16,080 John because that's the value over here 9965 07:09:12,298 --> 07:09:17,160 again both of them do exactly the same 9966 07:09:16,080 --> 07:09:19,378 thing 9967 07:09:17,160 --> 07:09:21,718 so either you can return a bunch of 9968 07:09:19,378 --> 07:09:23,458 elements or you can set it up here in 9969 07:09:21,718 --> 07:09:25,558 the component and also please keep in 9970 07:09:23,458 --> 07:09:27,360 mind that like I keep saying this is 9971 07:09:25,558 --> 07:09:29,580 going to be dynamic so essentially 9972 07:09:27,360 --> 07:09:34,200 you'll try to fetch the user from the 9973 07:09:29,580 --> 07:09:35,280 database if the user exists then I want 9974 07:09:34,200 --> 07:09:38,218 to display 9975 07:09:35,280 --> 07:09:40,860 some kind of component with the values 9976 07:09:38,218 --> 07:09:43,378 that represent that user and if it 9977 07:09:40,860 --> 07:09:46,200 doesn't exist let's say if 9978 07:09:43,378 --> 07:09:48,058 the API or database returns that this is 9979 07:09:46,200 --> 07:09:50,600 null what do you know we're not 9980 07:09:48,058 --> 07:09:53,400 displaying that component altogether 9981 07:09:50,600 --> 07:09:54,660 hopefully that is clear and up next I 9982 07:09:53,400 --> 07:09:58,320 also want to show you how we can use 9983 07:09:54,660 --> 07:10:00,180 ternary operator in jsx okay and while 9984 07:09:58,320 --> 07:10:03,478 we're still on the topic of conditional 9985 07:10:00,180 --> 07:10:07,138 rendering examples when we also quickly 9986 07:10:03,478 --> 07:10:08,940 cover turning operator because this is 9987 07:10:07,138 --> 07:10:12,000 also something that you'll see quite 9988 07:10:08,940 --> 07:10:13,860 often in jsx and just jog your memory 9989 07:10:12,000 --> 07:10:17,400 effectively in JavaScript we have this 9990 07:10:13,860 --> 07:10:19,860 option to check for condition and if 9991 07:10:17,400 --> 07:10:23,400 it's true when we return the first 9992 07:10:19,860 --> 07:10:25,500 expression if not then we return the 9993 07:10:23,400 --> 07:10:27,360 second one and the way it's going to 9994 07:10:25,500 --> 07:10:30,120 work in jsx first I want to navigate 9995 07:10:27,360 --> 07:10:32,100 back to the same file so again we're not 9996 07:10:30,120 --> 07:10:34,260 changing the files in here we're working 9997 07:10:32,100 --> 07:10:36,478 the same one first I'll start with some 9998 07:10:34,260 --> 07:10:38,940 kind of divider and I'm going to go with 9999 07:10:36,478 --> 07:10:41,160 heading 2 and I'll just say bernary 10000 07:10:38,940 --> 07:10:44,878 operator 10001 07:10:41,160 --> 07:10:45,958 and then in here when we add a little 10002 07:10:44,878 --> 07:10:47,940 bit of 10003 07:10:45,958 --> 07:10:49,798 CSS and I'm just going to add some 10004 07:10:47,940 --> 07:10:52,200 margin top bottom 10005 07:10:49,798 --> 07:10:54,958 and left and right I'll keep it zero so 10006 07:10:52,200 --> 07:10:57,000 one Rems and then a zero and after that 10007 07:10:54,958 --> 07:10:58,978 I want to render the button so let's go 10008 07:10:57,000 --> 07:11:02,340 with button let's add a little bit of 10009 07:10:58,978 --> 07:11:04,680 styles here so I'll say here last name 10010 07:11:02,340 --> 07:11:08,100 and then that one is equal to 10011 07:11:04,680 --> 07:11:11,280 BTR and then inside of the button 10012 07:11:08,100 --> 07:11:13,620 I'm gonna check for is editing and if is 10013 07:11:11,280 --> 07:11:16,320 editing is true then as far as the 10014 07:11:13,620 --> 07:11:19,260 content is going to be added and if not 10015 07:11:16,320 --> 07:11:23,638 it's going to be add so let's go over 10016 07:11:19,260 --> 07:11:26,218 here and let's say is editing and then 10017 07:11:23,638 --> 07:11:29,160 we want to set up the question mark here 10018 07:11:26,218 --> 07:11:32,280 and then the first value is going to be 10019 07:11:29,160 --> 07:11:34,798 if it's true so if is editing is true 10020 07:11:32,280 --> 07:11:37,378 then we'll display edit if not then 10021 07:11:34,798 --> 07:11:40,020 we'll go with ADD save that and now of 10022 07:11:37,378 --> 07:11:43,320 course since this is false that's why we 10023 07:11:40,020 --> 07:11:45,958 go with ADD now if we'll change this to 10024 07:11:43,320 --> 07:11:48,900 True you'll see right away that text 10025 07:11:45,958 --> 07:11:51,180 actually is added and not to sound like 10026 07:11:48,900 --> 07:11:53,820 a broken record but of course normally 10027 07:11:51,180 --> 07:11:56,100 this is going to be dynamic so if I go 10028 07:11:53,820 --> 07:11:58,978 back over here to false then I'll have 10029 07:11:56,100 --> 07:12:00,120 ADD and I also want to showcase how we 10030 07:11:58,978 --> 07:12:03,000 can 10031 07:12:00,120 --> 07:12:05,700 use the theory operator to return 10032 07:12:03,000 --> 07:12:09,058 elements just like we covered with and 10033 07:12:05,700 --> 07:12:11,638 operator so this is one option where we 10034 07:12:09,058 --> 07:12:13,978 just check if the value is true and then 10035 07:12:11,638 --> 07:12:16,620 we return the component but we can also 10036 07:12:13,978 --> 07:12:20,458 use turnary operator and essentially if 10037 07:12:16,620 --> 07:12:24,540 it's true then we return one set of 10038 07:12:20,458 --> 07:12:26,700 elements or the components and if it's 10039 07:12:24,540 --> 07:12:29,940 false then of course we return 10040 07:12:26,700 --> 07:12:31,740 other set of elements and in here again 10041 07:12:29,940 --> 07:12:34,500 I'm going to check for user so I set up 10042 07:12:31,740 --> 07:12:37,798 the curlies I'll go here with my ternary 10043 07:12:34,500 --> 07:12:40,860 operator and let's start with a div and 10044 07:12:37,798 --> 07:12:43,978 again just to speed this up I'm going to 10045 07:12:40,860 --> 07:12:47,458 grab this value over here so let me go 10046 07:12:43,978 --> 07:12:50,340 here we wanna copy and paste Yep this is 10047 07:12:47,458 --> 07:12:52,440 what we want to do now I'm not going to 10048 07:12:50,340 --> 07:12:53,760 probably return two of them you know 10049 07:12:52,440 --> 07:12:57,180 what I'm just going to go here with 10050 07:12:53,760 --> 07:13:00,478 heading 4. and then inside of it I'll 10051 07:12:57,180 --> 07:13:02,940 say hello there and user so what am I 10052 07:13:00,478 --> 07:13:04,020 doing over here well since I have a user 10053 07:13:02,940 --> 07:13:07,020 object 10054 07:13:04,020 --> 07:13:09,840 if it's true if it's not null then of 10055 07:13:07,020 --> 07:13:12,958 course I'll display div with the 10056 07:13:09,840 --> 07:13:14,900 username now if it is false 10057 07:13:12,958 --> 07:13:18,000 then I'm gonna go with colon over here 10058 07:13:14,900 --> 07:13:21,240 and basically I'll just say hey 10059 07:13:18,000 --> 07:13:23,218 you need to log in so there is no user 10060 07:13:21,240 --> 07:13:25,138 so in order to do that I'm just going to 10061 07:13:23,218 --> 07:13:26,760 change here heading 2. I'm not going to 10062 07:13:25,138 --> 07:13:29,340 be looking for a user because now I know 10063 07:13:26,760 --> 07:13:32,700 that this is false of course and then 10064 07:13:29,340 --> 07:13:34,558 I'll just say please login and you'll 10065 07:13:32,700 --> 07:13:37,138 notice that right away we have hello 10066 07:13:34,558 --> 07:13:40,978 there user John 10067 07:13:37,138 --> 07:13:43,200 and if we change this to null again then 10068 07:13:40,978 --> 07:13:45,540 of course I'll have please login so let 10069 07:13:43,200 --> 07:13:48,360 me go back to John and we successfully 10070 07:13:45,540 --> 07:13:50,340 covered the most common examples I know 10071 07:13:48,360 --> 07:13:52,260 that last few videos were probably a 10072 07:13:50,340 --> 07:13:54,780 little bit tedious because we were just 10073 07:13:52,260 --> 07:13:57,058 flipping the values from True to false 10074 07:13:54,780 --> 07:14:00,058 and that's why up next I'm going to work 10075 07:13:57,058 --> 07:14:02,760 on two challenges where you'll be able 10076 07:14:00,058 --> 07:14:04,798 to utilize this knowledge all right and 10077 07:14:02,760 --> 07:14:08,100 first I want to work on the toggle 10078 07:14:04,798 --> 07:14:10,740 challenge so in the app GSX import this 10079 07:14:08,100 --> 07:14:12,900 file zero six toggle challenge this is 10080 07:14:10,740 --> 07:14:14,878 what you'll see on the screen and as far 10081 07:14:12,900 --> 07:14:16,680 as the challenge I want you to create a 10082 07:14:14,878 --> 07:14:18,478 state value and in this case I'm going 10083 07:14:16,680 --> 07:14:20,878 to go with Boolean please keep in mind 10084 07:14:18,478 --> 07:14:22,440 that of course you can set up a 10085 07:14:20,878 --> 07:14:25,020 different value again we're talking 10086 07:14:22,440 --> 07:14:28,740 about truthy and falsy just to make it 10087 07:14:25,020 --> 07:14:32,760 easier I'm gonna go with Boolean then in 10088 07:14:28,740 --> 07:14:36,120 the jsx we want to return a button and a 10089 07:14:32,760 --> 07:14:37,138 component or element so for sure turn a 10090 07:14:36,120 --> 07:14:40,558 button 10091 07:14:37,138 --> 07:14:44,040 and then after the button set up some 10092 07:14:40,558 --> 07:14:45,780 kind of element or a component that's 10093 07:14:44,040 --> 07:14:48,600 really up to you in my example I'm going 10094 07:14:45,780 --> 07:14:51,058 to use the component but of course you 10095 07:14:48,600 --> 07:14:54,840 can use the element as well and then 10096 07:14:51,058 --> 07:14:57,420 when the user clicks the button toggle 10097 07:14:54,840 --> 07:15:00,138 the state value this sucker over here 10098 07:14:57,420 --> 07:15:03,240 and then based on that value 10099 07:15:00,138 --> 07:15:05,700 conditionally render component element 10100 07:15:03,240 --> 07:15:07,860 and I want to right away tell you that 10101 07:15:05,700 --> 07:15:11,218 there's multiple ways how we can set 10102 07:15:07,860 --> 07:15:13,920 this up so again if your logic works 10103 07:15:11,218 --> 07:15:16,920 do whatever makes the most sense to you 10104 07:15:13,920 --> 07:15:18,840 now as far as my setup first of all I 10105 07:15:16,920 --> 07:15:21,298 want to navigate to the file so I'm 10106 07:15:18,840 --> 07:15:23,400 going to go to toggle challenge you want 10107 07:15:21,298 --> 07:15:25,260 to grab the use State again I'm going to 10108 07:15:23,400 --> 07:15:28,020 go with Auto Imports so I'm gonna go 10109 07:15:25,260 --> 07:15:30,540 with use state 10110 07:15:28,020 --> 07:15:33,540 let's grab that one and it's going to be 10111 07:15:30,540 --> 07:15:36,780 false by default now as far as the name 10112 07:15:33,540 --> 07:15:39,360 I'm gonna go with show alert and then 10113 07:15:36,780 --> 07:15:42,718 comma and then set show 10114 07:15:39,360 --> 07:15:45,958 alert now that is equal to of course my 10115 07:15:42,718 --> 07:15:48,240 value okay good let's save that and like 10116 07:15:45,958 --> 07:15:51,180 I said now we want to set up that button 10117 07:15:48,240 --> 07:15:54,478 so in here I'm going to go with div 10118 07:15:51,180 --> 07:15:57,540 and first is going to be my button 10119 07:15:54,478 --> 07:16:00,900 so begin blah blah blah blah class name 10120 07:15:57,540 --> 07:16:02,760 BTN and then on click for now let's not 10121 07:16:00,900 --> 07:16:04,680 worry about it I just want to showcase 10122 07:16:02,760 --> 07:16:07,020 how it's going to look like so I have my 10123 07:16:04,680 --> 07:16:09,180 toggle button and like I said after that 10124 07:16:07,020 --> 07:16:11,878 if you want to set up elements or 10125 07:16:09,180 --> 07:16:13,978 element you can definitely do so in my 10126 07:16:11,878 --> 07:16:16,378 case I'm going to set up the alert just 10127 07:16:13,978 --> 07:16:18,600 so I can showcase some classes that we 10128 07:16:16,378 --> 07:16:20,400 have in CSS so in here if I go with 10129 07:16:18,600 --> 07:16:22,740 alert component 10130 07:16:20,400 --> 07:16:25,080 and then as far as the return if I go 10131 07:16:22,740 --> 07:16:28,200 with let's say div 10132 07:16:25,080 --> 07:16:31,200 return and then div let's add class name 10133 07:16:28,200 --> 07:16:33,540 so we have two of them alert which 10134 07:16:31,200 --> 07:16:36,120 basically adds the general Styles and 10135 07:16:33,540 --> 07:16:38,638 then I also have alert danger which 10136 07:16:36,120 --> 07:16:40,500 essentially just adds some colors and 10137 07:16:38,638 --> 07:16:43,558 all that kind of stuff so let me close 10138 07:16:40,500 --> 07:16:45,600 it here and let me say hello world let 10139 07:16:43,558 --> 07:16:48,180 me save this and now I want to go up 10140 07:16:45,600 --> 07:16:49,978 where I have the toggle Challenge and I 10141 07:16:48,180 --> 07:16:52,080 want to just display the alert for now 10142 07:16:49,978 --> 07:16:54,298 this is what we're gonna see on the 10143 07:16:52,080 --> 07:16:56,760 screen now awesome 10144 07:16:54,298 --> 07:16:59,400 up next I want to actually toggle this 10145 07:16:56,760 --> 07:17:01,978 value so first of all let's come up with 10146 07:16:59,400 --> 07:17:04,500 the function and in my case that's going 10147 07:17:01,978 --> 07:17:08,520 to be toggle alert so I'm going to call 10148 07:17:04,500 --> 07:17:11,040 this toggle alert that's my function and 10149 07:17:08,520 --> 07:17:13,200 then before I set up the functionality I 10150 07:17:11,040 --> 07:17:15,600 want to actually invoke it every time I 10151 07:17:13,200 --> 07:17:18,420 click so remember it was on click and 10152 07:17:15,600 --> 07:17:21,600 then we want to go with toggle and alert 10153 07:17:18,420 --> 07:17:24,120 and as far as the functionality well we 10154 07:17:21,600 --> 07:17:26,218 want to toggle the state value right and 10155 07:17:24,120 --> 07:17:27,718 like I said there's multiple ways how we 10156 07:17:26,218 --> 07:17:31,200 can set this up at the end of the video 10157 07:17:27,718 --> 07:17:33,120 I'll show you a shortcut but for now we 10158 07:17:31,200 --> 07:17:36,360 can simply do it this way where I'm 10159 07:17:33,120 --> 07:17:39,600 going to check what is the value of show 10160 07:17:36,360 --> 07:17:42,000 alert basically if this is true then I 10161 07:17:39,600 --> 07:17:44,940 want to go set show alert then I'll set 10162 07:17:42,000 --> 07:17:47,340 it equal to false and very important we 10163 07:17:44,940 --> 07:17:49,260 want to go here with the return Why well 10164 07:17:47,340 --> 07:17:51,958 because I'm going to be setting up logic 10165 07:17:49,260 --> 07:17:55,080 after my condition and if I don't add 10166 07:17:51,958 --> 07:17:56,218 the return Then basically it's going to 10167 07:17:55,080 --> 07:17:58,378 hit this 10168 07:17:56,218 --> 07:18:00,900 functionality as well because that's how 10169 07:17:58,378 --> 07:18:03,360 JavaScript is reading the code so let me 10170 07:18:00,900 --> 07:18:05,820 go here with true and notice so if this 10171 07:18:03,360 --> 07:18:08,340 is true I want to set equal to false if 10172 07:18:05,820 --> 07:18:10,740 not then basically we'll just skip this 10173 07:18:08,340 --> 07:18:14,458 condition and we'll set it equal to true 10174 07:18:10,740 --> 07:18:16,860 so this is awesome but of course alert 10175 07:18:14,458 --> 07:18:19,558 is always going to be displayed we also 10176 07:18:16,860 --> 07:18:21,660 need to implement right now our and 10177 07:18:19,558 --> 07:18:24,420 operator correct so I'm going to go with 10178 07:18:21,660 --> 07:18:26,638 show alert if this is true and what do I 10179 07:18:24,420 --> 07:18:28,798 want to do well that's when I want to 10180 07:18:26,638 --> 07:18:31,020 display that alert so let me save it 10181 07:18:28,798 --> 07:18:34,200 over here and notice 10182 07:18:31,020 --> 07:18:37,320 once I click this is of course now true 10183 07:18:34,200 --> 07:18:39,840 and I display the alert once I click one 10184 07:18:37,320 --> 07:18:42,840 more time then of course it's false and 10185 07:18:39,840 --> 07:18:46,920 I don't display the alert and as far as 10186 07:18:42,840 --> 07:18:48,840 the shortcut let me keep scrolling and 10187 07:18:46,920 --> 07:18:51,120 notice over here this is not operator so 10188 07:18:48,840 --> 07:18:54,240 essentially we can just make it a 10189 07:18:51,120 --> 07:18:56,878 one-liner where instead of setting up 10190 07:18:54,240 --> 07:18:59,878 the entire function and all that I can 10191 07:18:56,878 --> 07:19:04,138 just pass here the arrow function and I 10192 07:18:59,878 --> 07:19:07,378 can say set show alert and then not so 10193 07:19:04,138 --> 07:19:10,740 the opposite value of the current value 10194 07:19:07,378 --> 07:19:13,200 of show alert so if it's true then set 10195 07:19:10,740 --> 07:19:15,840 it equal to false and if it's false then 10196 07:19:13,200 --> 07:19:16,500 set it equal to true hopefully that is 10197 07:19:15,840 --> 07:19:19,080 clear 10198 07:19:16,500 --> 07:19:21,420 so I'm gonna navigate back again I'm 10199 07:19:19,080 --> 07:19:23,100 gonna comment this one out just so it 10200 07:19:21,420 --> 07:19:25,680 stays for your reference and now let's 10201 07:19:23,100 --> 07:19:28,558 do that one liner where basically we 10202 07:19:25,680 --> 07:19:31,138 will go here with my arrow function and 10203 07:19:28,558 --> 07:19:33,718 then we'll just say set show alert and 10204 07:19:31,138 --> 07:19:36,298 set it to the opposite value 10205 07:19:33,718 --> 07:19:38,400 let's save that and then notice our 10206 07:19:36,298 --> 07:19:40,920 functionality still works Everything is 10207 07:19:38,400 --> 07:19:44,100 Awesome we still have that alert 10208 07:19:40,920 --> 07:19:47,040 component if this is true however now we 10209 07:19:44,100 --> 07:19:49,680 have less lines of code 10210 07:19:47,040 --> 07:19:52,378 all right and up next I want to work on 10211 07:19:49,680 --> 07:19:54,780 the user challenge so essentially in the 10212 07:19:52,378 --> 07:19:56,100 app GSX we want to import this one the 10213 07:19:54,780 --> 07:19:58,440 zero seven 10214 07:19:56,100 --> 07:19:59,820 user Challenge and as far as the 10215 07:19:58,440 --> 07:20:02,160 challenge we want to set up the state 10216 07:19:59,820 --> 07:20:05,160 value in my case I'm going to go user 10217 07:20:02,160 --> 07:20:07,920 and default value is going to be no then 10218 07:20:05,160 --> 07:20:10,920 I want you to create two functions One 10219 07:20:07,920 --> 07:20:14,160 login function that sets the user equal 10220 07:20:10,920 --> 07:20:16,200 to a object with a name property so you 10221 07:20:14,160 --> 07:20:18,240 simply want to set it equal to object 10222 07:20:16,200 --> 07:20:20,340 that name property and some kind of 10223 07:20:18,240 --> 07:20:22,680 value again that value is totally up to 10224 07:20:20,340 --> 07:20:23,820 you and then we also want to set up a 10225 07:20:22,680 --> 07:20:25,020 log out 10226 07:20:23,820 --> 07:20:27,540 button 10227 07:20:25,020 --> 07:20:28,920 and for that we'll need a logger 10228 07:20:27,540 --> 07:20:31,978 functionality 10229 07:20:28,920 --> 07:20:34,200 and in there I want you to set user 10230 07:20:31,978 --> 07:20:36,240 equal to a null 10231 07:20:34,200 --> 07:20:39,180 so set it back to the default value 10232 07:20:36,240 --> 07:20:41,878 which is null and then in the jsx use 10233 07:20:39,180 --> 07:20:45,240 the ternary operator to display two 10234 07:20:41,878 --> 07:20:47,940 different setups so if the user exists 10235 07:20:45,240 --> 07:20:49,680 we want to go hello there and then try 10236 07:20:47,940 --> 07:20:52,620 to get the username because remember 10237 07:20:49,680 --> 07:20:55,138 we're setting the user equal to an 10238 07:20:52,620 --> 07:20:57,298 object with the name property and also I 10239 07:20:55,138 --> 07:21:00,718 want to display the logout button 10240 07:20:57,298 --> 07:21:02,100 so if the user has already logged in I 10241 07:21:00,718 --> 07:21:03,540 don't want to display the login button 10242 07:21:02,100 --> 07:21:06,058 that doesn't make sense I want to 10243 07:21:03,540 --> 07:21:08,520 display the logout button and then if 10244 07:21:06,058 --> 07:21:10,798 it's false if the user is false I want 10245 07:21:08,520 --> 07:21:14,280 to go with heading 4 and please log in 10246 07:21:10,798 --> 07:21:16,500 and a login button so let's start 10247 07:21:14,280 --> 07:21:18,360 working on this challenge first I'm 10248 07:21:16,500 --> 07:21:19,978 going to go with const and like I said 10249 07:21:18,360 --> 07:21:23,580 in my case I'm going to go to user and 10250 07:21:19,978 --> 07:21:27,058 then set user that is equal to my use 10251 07:21:23,580 --> 07:21:29,580 and then state so that's imported and 10252 07:21:27,058 --> 07:21:33,058 we're going to go with no that's going 10253 07:21:29,580 --> 07:21:34,978 to be the default value and right out of 10254 07:21:33,058 --> 07:21:36,900 the gate let's set up those functions so 10255 07:21:34,978 --> 07:21:39,660 const login 10256 07:21:36,900 --> 07:21:43,500 and that one is equal to a function here 10257 07:21:39,660 --> 07:21:45,478 and then I want to go with set user and 10258 07:21:43,500 --> 07:21:48,000 I want to set it equal to an object so 10259 07:21:45,478 --> 07:21:49,740 name and in my case I'm going to go with 10260 07:21:48,000 --> 07:21:52,860 vegan 10261 07:21:49,740 --> 07:21:54,958 food truck because why not and then I 10262 07:21:52,860 --> 07:21:57,180 also want to add here a comment where 10263 07:21:54,958 --> 07:21:58,798 normally 10264 07:21:57,180 --> 07:22:03,840 connect 10265 07:21:58,798 --> 07:22:05,458 to DB or API so essentially you're not 10266 07:22:03,840 --> 07:22:08,280 going to be hardcoding this 10267 07:22:05,458 --> 07:22:11,940 normally you'll connect to the database 10268 07:22:08,280 --> 07:22:14,160 if everything is correct if it's the 10269 07:22:11,940 --> 07:22:16,558 right password and an email and all that 10270 07:22:14,160 --> 07:22:19,080 you'll get back the credentials for the 10271 07:22:16,558 --> 07:22:22,740 user and that's where you'll set it in a 10272 07:22:19,080 --> 07:22:24,240 state value now if it's not then of 10273 07:22:22,740 --> 07:22:25,798 course nothing's gonna happen you'll 10274 07:22:24,240 --> 07:22:26,638 display probably some kind of error 10275 07:22:25,798 --> 07:22:29,280 message 10276 07:22:26,638 --> 07:22:31,320 but hopefully this is clear so of course 10277 07:22:29,280 --> 07:22:33,420 in this example we're basically hard 10278 07:22:31,320 --> 07:22:35,160 coding we're saying yeah once we log in 10279 07:22:33,420 --> 07:22:36,478 everything is going to be fine but it's 10280 07:22:35,160 --> 07:22:38,160 not always the case 10281 07:22:36,478 --> 07:22:40,620 and of course later in the course we 10282 07:22:38,160 --> 07:22:42,840 will connect to a database and we'll 10283 07:22:40,620 --> 07:22:46,260 check what's happening and if everything 10284 07:22:42,840 --> 07:22:48,120 is correct that's when we set the user 10285 07:22:46,260 --> 07:22:51,900 but again the main idea is going to be 10286 07:22:48,120 --> 07:22:54,360 exactly the same you have that flag the 10287 07:22:51,900 --> 07:22:56,700 user basically and if it exists it's 10288 07:22:54,360 --> 07:22:59,040 awesome I want to display it if not then 10289 07:22:56,700 --> 07:23:02,218 I want to go with a logarithm hopefully 10290 07:22:59,040 --> 07:23:04,200 that's clear copy and paste I don't need 10291 07:23:02,218 --> 07:23:07,558 a comment here 10292 07:23:04,200 --> 07:23:10,200 I can set it up as null as well 10293 07:23:07,558 --> 07:23:12,660 no and 10294 07:23:10,200 --> 07:23:15,600 I want to call it logout here 10295 07:23:12,660 --> 07:23:17,940 let's save this and now let's work on 10296 07:23:15,600 --> 07:23:19,978 the jsx so first I want to display the 10297 07:23:17,940 --> 07:23:23,580 div then instead of the div I want to 10298 07:23:19,978 --> 07:23:25,080 set up right away js6 and I want to go 10299 07:23:23,580 --> 07:23:26,940 with user 10300 07:23:25,080 --> 07:23:29,040 so essentially I'm checking that user 10301 07:23:26,940 --> 07:23:31,558 value and remember I have two options if 10302 07:23:29,040 --> 07:23:33,660 it's true it's one thing if not then 10303 07:23:31,558 --> 07:23:36,298 it's something else so let's start if 10304 07:23:33,660 --> 07:23:39,360 it's true and again yes you can set up a 10305 07:23:36,298 --> 07:23:41,878 component over here so if you want to 10306 07:23:39,360 --> 07:23:45,718 work on the challenge more 10307 07:23:41,878 --> 07:23:47,878 you can set up these returns as separate 10308 07:23:45,718 --> 07:23:50,040 components so in here let's go with div 10309 07:23:47,878 --> 07:23:53,400 I'm going to set it up as heading four 10310 07:23:50,040 --> 07:23:55,580 remember in this case user exists so we 10311 07:23:53,400 --> 07:23:58,680 can simply go with hello there 10312 07:23:55,580 --> 07:24:00,780 and we can try to access the username 10313 07:23:58,680 --> 07:24:03,660 now if it's null then it's not going to 10314 07:24:00,780 --> 07:24:05,878 make sense correct so user.name okay 10315 07:24:03,660 --> 07:24:08,580 that's awesome and as far as the button 10316 07:24:05,878 --> 07:24:10,378 well like I keep saying we're not going 10317 07:24:08,580 --> 07:24:11,940 to display the login 10318 07:24:10,378 --> 07:24:15,120 we want to display the opposite one 10319 07:24:11,940 --> 07:24:17,218 which one is that well that's the logger 10320 07:24:15,120 --> 07:24:21,298 one so let's go with button class name 10321 07:24:17,218 --> 07:24:24,780 BTN okay that's beautiful on click and 10322 07:24:21,298 --> 07:24:27,000 now let's go with our log out button and 10323 07:24:24,780 --> 07:24:30,058 the same goes for the text so log out 10324 07:24:27,000 --> 07:24:32,400 here let's save it and we'll have an 10325 07:24:30,058 --> 07:24:33,478 error don't worry we'll fix that in a 10326 07:24:32,400 --> 07:24:35,100 second 10327 07:24:33,478 --> 07:24:36,540 essentially we want to grab here this 10328 07:24:35,100 --> 07:24:39,600 div 10329 07:24:36,540 --> 07:24:42,120 and we want to copy and paste 10330 07:24:39,600 --> 07:24:44,280 now here we're working with opposite 10331 07:24:42,120 --> 07:24:47,760 values correct so we're going to go here 10332 07:24:44,280 --> 07:24:50,580 with please log in and we're going to 10333 07:24:47,760 --> 07:24:52,020 change the button this is going to be a 10334 07:24:50,580 --> 07:24:53,160 login button 10335 07:24:52,020 --> 07:24:56,040 over here 10336 07:24:53,160 --> 07:24:57,298 as well as here let's save that and 10337 07:24:56,040 --> 07:24:59,040 check it out 10338 07:24:57,298 --> 07:25:01,920 now we have 10339 07:24:59,040 --> 07:25:04,978 div with these values and the moment 10340 07:25:01,920 --> 07:25:07,138 we'll click will actually flip it 10341 07:25:04,978 --> 07:25:10,138 so notice this is what we have right now 10342 07:25:07,138 --> 07:25:12,120 e in our browser right so these are the 10343 07:25:10,138 --> 07:25:14,820 elements we're returning however once we 10344 07:25:12,120 --> 07:25:17,040 click check it out now we're turning 10345 07:25:14,820 --> 07:25:19,320 different values so now we have hello 10346 07:25:17,040 --> 07:25:21,360 there vegan food truck so essentially 10347 07:25:19,320 --> 07:25:24,420 we're displaying that user has logged in 10348 07:25:21,360 --> 07:25:27,180 successfully and now he or she has an 10349 07:25:24,420 --> 07:25:29,958 option to log out and once we log out of 10350 07:25:27,180 --> 07:25:33,298 course we have the previous screen The 10351 07:25:29,958 --> 07:25:35,700 Heading 4 with please login hopefully 10352 07:25:33,298 --> 07:25:37,798 everyone enjoyed the challenge and I'll 10353 07:25:35,700 --> 07:25:39,920 see you in the next video all right and 10354 07:25:37,798 --> 07:25:43,440 now let's go back and discuss use effect 10355 07:25:39,920 --> 07:25:44,218 cleanup function and why it's useful so 10356 07:25:43,440 --> 07:25:46,620 yes 10357 07:25:44,218 --> 07:25:50,638 once we're done with conditional 10358 07:25:46,620 --> 07:25:53,100 rendering we'll actually go back to zero 10359 07:25:50,638 --> 07:25:55,500 to use effect and 10360 07:25:53,100 --> 07:25:57,120 take a look at the cleanup function and 10361 07:25:55,500 --> 07:25:59,040 you'll see in a second why and let's 10362 07:25:57,120 --> 07:26:01,860 start with the challenge so I want you 10363 07:25:59,040 --> 07:26:05,040 to create a state value and 10364 07:26:01,860 --> 07:26:07,080 in jsx set up a button which toggles the 10365 07:26:05,040 --> 07:26:10,260 state and based on that condition return 10366 07:26:07,080 --> 07:26:12,058 a second component now that's a totally 10367 07:26:10,260 --> 07:26:14,400 simple return you don't need to worry 10368 07:26:12,058 --> 07:26:16,860 about returning 10 000 elements 10369 07:26:14,400 --> 07:26:19,280 literally it can be a heading 2. now 10370 07:26:16,860 --> 07:26:24,058 what's important in the second component 10371 07:26:19,280 --> 07:26:27,780 create a use effect and run it only 10372 07:26:24,058 --> 07:26:30,420 on initial render and remember in order 10373 07:26:27,780 --> 07:26:31,680 to do that we needed to use a second 10374 07:26:30,420 --> 07:26:34,620 argument 10375 07:26:31,680 --> 07:26:37,860 in the use effect and once you have the 10376 07:26:34,620 --> 07:26:40,978 example in place then we'll discuss 10377 07:26:37,860 --> 07:26:43,080 why we have such interesting Behavior so 10378 07:26:40,978 --> 07:26:45,420 again we're looking for use Effect Star 10379 07:26:43,080 --> 07:26:49,138 and then zero five cleanup function 10380 07:26:45,420 --> 07:26:51,000 that's what I have in the app jsx and 10381 07:26:49,138 --> 07:26:54,298 then as far as the file 10382 07:26:51,000 --> 07:26:56,340 first let's just go with some kind of 10383 07:26:54,298 --> 07:27:00,058 state value so in my case I'm going to 10384 07:26:56,340 --> 07:27:01,500 call this toggle const and then toggle 10385 07:27:00,058 --> 07:27:05,420 set 10386 07:27:01,500 --> 07:27:08,760 toggle and that is equal to use 10387 07:27:05,420 --> 07:27:11,638 huge State and by default let's set it 10388 07:27:08,760 --> 07:27:12,840 equal to false but after that let's set 10389 07:27:11,638 --> 07:27:15,120 up that return 10390 07:27:12,840 --> 07:27:17,100 so right away go with return and I'm 10391 07:27:15,120 --> 07:27:19,558 going to remove the heading too and I'll 10392 07:27:17,100 --> 07:27:22,680 go with div instead and then in here 10393 07:27:19,558 --> 07:27:25,080 let's create that button then we're 10394 07:27:22,680 --> 07:27:27,660 going to go to class name and that BTN 10395 07:27:25,080 --> 07:27:31,558 we also want to right away add on click 10396 07:27:27,660 --> 07:27:33,840 and let's pass in the arrow function as 10397 07:27:31,558 --> 07:27:37,558 far as the logic will go with set toggle 10398 07:27:33,840 --> 07:27:40,500 and we'll use that not operator so set 10399 07:27:37,558 --> 07:27:41,940 it equal to the opposite value and then 10400 07:27:40,500 --> 07:27:45,120 in here 10401 07:27:41,940 --> 07:27:48,420 I want to set up some kind of text and 10402 07:27:45,120 --> 07:27:50,160 I'm going to go toggle component let's 10403 07:27:48,420 --> 07:27:52,320 save that yep I have the button 10404 07:27:50,160 --> 07:27:54,540 everything's awesome so now let's set up 10405 07:27:52,320 --> 07:27:56,700 another component and based on that 10406 07:27:54,540 --> 07:27:58,500 state value let's toggle it so in here 10407 07:27:56,700 --> 07:27:59,878 I'm going to go with const and then for 10408 07:27:58,500 --> 07:28:01,320 random 10409 07:27:59,878 --> 07:28:04,320 component 10410 07:28:01,320 --> 07:28:06,958 again the entire gotcha is the result 10411 07:28:04,320 --> 07:28:09,240 you'll see in the console so that's the 10412 07:28:06,958 --> 07:28:11,700 most important one first for now let's 10413 07:28:09,240 --> 07:28:13,978 just go in random component then 10414 07:28:11,700 --> 07:28:15,420 let's go with heading one man we'll say 10415 07:28:13,978 --> 07:28:17,760 hello there 10416 07:28:15,420 --> 07:28:20,160 hello there now we already know that of 10417 07:28:17,760 --> 07:28:22,978 course we can toggle it correct so if I 10418 07:28:20,160 --> 07:28:25,620 go here and if I'll say toggle and paste 10419 07:28:22,978 --> 07:28:27,718 on that value if it's true then I want 10420 07:28:25,620 --> 07:28:30,240 to return the random component if not 10421 07:28:27,718 --> 07:28:32,820 then nothing is going to be displayed 10422 07:28:30,240 --> 07:28:35,760 so we're expecting this Behavior correct 10423 07:28:32,820 --> 07:28:38,280 all of that is good so now we're putting 10424 07:28:35,760 --> 07:28:40,200 two and two together where we can use 10425 07:28:38,280 --> 07:28:43,020 conditional rendering we can display the 10426 07:28:40,200 --> 07:28:44,638 component but also keep in mind that in 10427 07:28:43,020 --> 07:28:47,280 any component we can set up the use 10428 07:28:44,638 --> 07:28:50,100 effect correct so let's try it out we're 10429 07:28:47,280 --> 07:28:52,320 going to go here with used effect so 10430 07:28:50,100 --> 07:28:55,680 we'll pass it here and then remember 10431 07:28:52,320 --> 07:28:58,978 like I said we have two arguments we 10432 07:28:55,680 --> 07:29:01,138 have the function that's first 10433 07:28:58,978 --> 07:29:04,138 and then we have the dependency array 10434 07:29:01,138 --> 07:29:07,020 now as far as the function here we're 10435 07:29:04,138 --> 07:29:09,478 going to go with some kind of log we'll 10436 07:29:07,020 --> 07:29:11,100 say hmm that's interesting and you'll 10437 07:29:09,478 --> 07:29:11,700 see in a second why so 10438 07:29:11,100 --> 07:29:15,420 um 10439 07:29:11,700 --> 07:29:18,660 this is interesting okay good let's save 10440 07:29:15,420 --> 07:29:21,000 it and now million dollar question yes 10441 07:29:18,660 --> 07:29:23,040 we're going back to the quiz 10442 07:29:21,000 --> 07:29:24,718 how often do you think this is going to 10443 07:29:23,040 --> 07:29:27,240 show up 10444 07:29:24,718 --> 07:29:30,478 so remember use effect 10445 07:29:27,240 --> 07:29:32,478 it has the dependency array and 10446 07:29:30,478 --> 07:29:35,100 technically this is empty 10447 07:29:32,478 --> 07:29:38,280 so two options 10448 07:29:35,100 --> 07:29:41,218 either it's going to show up only once 10449 07:29:38,280 --> 07:29:43,260 when the component renders or 10450 07:29:41,218 --> 07:29:46,020 there's actually a gotcha and we'll see 10451 07:29:43,260 --> 07:29:47,638 it more often so let's try it out I'm 10452 07:29:46,020 --> 07:29:50,340 gonna go over here and check it out 10453 07:29:47,638 --> 07:29:52,798 every time I click 10454 07:29:50,340 --> 07:29:55,080 I actually have this Behavior 10455 07:29:52,798 --> 07:29:57,958 now before we continue why is this 10456 07:29:55,080 --> 07:30:01,500 happening well remember long long long 10457 07:29:57,958 --> 07:30:05,580 long time ago we discussed 10458 07:30:01,500 --> 07:30:07,558 the render and re-render correct so 10459 07:30:05,580 --> 07:30:10,500 let's go with I don't know initial 10460 07:30:07,558 --> 07:30:11,820 render maybe this way I'll be able to 10461 07:30:10,500 --> 07:30:14,100 find it 10462 07:30:11,820 --> 07:30:16,080 yep over here remember 10463 07:30:14,100 --> 07:30:18,718 we discussed the initial render and 10464 07:30:16,080 --> 07:30:20,760 re-renders and I said that 10465 07:30:18,718 --> 07:30:21,958 for time being let's think of initial 10466 07:30:20,760 --> 07:30:25,680 render 10467 07:30:21,958 --> 07:30:28,200 where basically the application loads so 10468 07:30:25,680 --> 07:30:30,120 that's when we render for example this 10469 07:30:28,200 --> 07:30:31,620 sucker over here the cleanup function 10470 07:30:30,120 --> 07:30:34,740 however 10471 07:30:31,620 --> 07:30:37,920 when we start toggling the component 10472 07:30:34,740 --> 07:30:41,660 essentially we Mount and unmount the 10473 07:30:37,920 --> 07:30:43,860 component and during the mount we repeat 10474 07:30:41,660 --> 07:30:46,020 initial render 10475 07:30:43,860 --> 07:30:49,978 so of course the functionality inside of 10476 07:30:46,020 --> 07:30:53,340 the use effect will run even though the 10477 07:30:49,978 --> 07:30:55,620 dependency array is empty but notice now 10478 07:30:53,340 --> 07:30:57,718 I'm displaying the hello there that's 10479 07:30:55,620 --> 07:30:59,760 the initial render and yes if there's 10480 07:30:57,718 --> 07:31:01,680 going to be some kind of logic inside of 10481 07:30:59,760 --> 07:31:03,660 the random component of course it will 10482 07:31:01,680 --> 07:31:05,878 re-render if you'll have to use state 10483 07:31:03,660 --> 07:31:08,458 but my main point in this video is to 10484 07:31:05,878 --> 07:31:11,580 Showcase that just because we're used to 10485 07:31:08,458 --> 07:31:13,620 use effect just running once 10486 07:31:11,580 --> 07:31:15,378 when the application loads it's not 10487 07:31:13,620 --> 07:31:17,878 going to be the case if 10488 07:31:15,378 --> 07:31:21,120 you're displaying the component 10489 07:31:17,878 --> 07:31:22,920 conditionally because in that case that 10490 07:31:21,120 --> 07:31:25,558 initial render is going to happen every 10491 07:31:22,920 --> 07:31:27,660 time you basically Mount and unmount 10492 07:31:25,558 --> 07:31:29,700 Mount and unmount 10493 07:31:27,660 --> 07:31:31,320 and this is why 10494 07:31:29,700 --> 07:31:32,940 the cleanup function which we're going 10495 07:31:31,320 --> 07:31:35,160 to discuss in the next video is going to 10496 07:31:32,940 --> 07:31:37,680 be useful just don't be surprised if you 10497 07:31:35,160 --> 07:31:40,320 have some kind of component that is 10498 07:31:37,680 --> 07:31:43,320 being mounted and unmounted and even 10499 07:31:40,320 --> 07:31:46,080 though you do have this dependency array 10500 07:31:43,320 --> 07:31:47,700 as empty the functionality inside of the 10501 07:31:46,080 --> 07:31:49,320 use effect which in this case is just a 10502 07:31:47,700 --> 07:31:52,458 simple console log 10503 07:31:49,320 --> 07:31:56,040 keeps running every time we basically 10504 07:31:52,458 --> 07:31:58,020 show the component again it's not a good 10505 07:31:56,040 --> 07:32:00,600 or a bad thing in the following videos 10506 07:31:58,020 --> 07:32:02,520 we'll take a look at few examples and 10507 07:32:00,600 --> 07:32:05,700 when it becomes a problem just something 10508 07:32:02,520 --> 07:32:07,860 very important to keep in mind but yes 10509 07:32:05,700 --> 07:32:10,558 you have this initial render when the 10510 07:32:07,860 --> 07:32:13,200 application loads but for components 10511 07:32:10,558 --> 07:32:15,420 that are being displayed conditionally 10512 07:32:13,200 --> 07:32:18,360 actually it's going to happen when you 10513 07:32:15,420 --> 07:32:20,280 also Mount the component okay so 10514 07:32:18,360 --> 07:32:22,680 hopefully it is clear when we're 10515 07:32:20,280 --> 07:32:25,920 toggling the component when we're 10516 07:32:22,680 --> 07:32:28,620 mounting an unmounting component the use 10517 07:32:25,920 --> 07:32:31,978 effect even though it has empty 10518 07:32:28,620 --> 07:32:34,080 dependency array is going to run every 10519 07:32:31,978 --> 07:32:36,718 time we show the component basically 10520 07:32:34,080 --> 07:32:38,760 every time we Mount the component unlike 10521 07:32:36,718 --> 07:32:42,600 in this example the cleanup function 10522 07:32:38,760 --> 07:32:45,180 which is only going to be mounted once 10523 07:32:42,600 --> 07:32:47,340 now this is just a fact 10524 07:32:45,180 --> 07:32:48,780 when it becomes an issue well if we have 10525 07:32:47,340 --> 07:32:50,760 some kind of functionality that 10526 07:32:48,780 --> 07:32:52,860 essentially just keeps on running 10527 07:32:50,760 --> 07:32:55,978 now I picked two examples the set 10528 07:32:52,860 --> 07:32:58,138 interval and the event listeners keep in 10529 07:32:55,978 --> 07:33:01,620 mind that of course there are other use 10530 07:32:58,138 --> 07:33:05,160 cases and essentially the main point of 10531 07:33:01,620 --> 07:33:07,860 these videos is just to showcase 10532 07:33:05,160 --> 07:33:10,020 how we need to be careful when we are 10533 07:33:07,860 --> 07:33:12,058 toggling the component if we have some 10534 07:33:10,020 --> 07:33:15,478 kind of functionality that might 10535 07:33:12,058 --> 07:33:17,340 possibly lead to an issue that's when we 10536 07:33:15,478 --> 07:33:18,958 need to go back and set up the cleanup 10537 07:33:17,340 --> 07:33:21,000 function please keep in mind you won't 10538 07:33:18,958 --> 07:33:23,218 need to set up cleanup function for 10539 07:33:21,000 --> 07:33:26,820 every use effect but yes in some 10540 07:33:23,218 --> 07:33:29,100 instances it's a good practice now when 10541 07:33:26,820 --> 07:33:31,200 it comes to set interval basically it 10542 07:33:29,100 --> 07:33:33,900 comes from vanilla just just like the 10543 07:33:31,200 --> 07:33:36,478 event listeners and essentially we go 10544 07:33:33,900 --> 07:33:39,058 here with set interval we want to pass 10545 07:33:36,478 --> 07:33:39,840 in the function we want to invoke and 10546 07:33:39,058 --> 07:33:41,760 then 10547 07:33:39,840 --> 07:33:43,798 essentially the interval so in my case 10548 07:33:41,760 --> 07:33:46,320 I'm going to go with one second and I'm 10549 07:33:43,798 --> 07:33:49,080 not going to be too 10550 07:33:46,320 --> 07:33:52,200 dramatic and I'm just going to pass here 10551 07:33:49,080 --> 07:33:55,138 the log I'm going to say hello from 10552 07:33:52,200 --> 07:33:56,458 interval and what do you think right now 10553 07:33:55,138 --> 07:33:59,820 is going to happen 10554 07:33:56,458 --> 07:34:01,080 so if I toggle the component 10555 07:33:59,820 --> 07:34:03,298 I have 10556 07:34:01,080 --> 07:34:05,580 is interesting okay that's what I 10557 07:34:03,298 --> 07:34:08,120 expected but I also have this hello from 10558 07:34:05,580 --> 07:34:10,978 interval so that's the functionality 10559 07:34:08,120 --> 07:34:12,958 now where it becomes really interesting 10560 07:34:10,978 --> 07:34:16,558 is when we actually 10561 07:34:12,958 --> 07:34:19,620 on toggle the component notice how this 10562 07:34:16,558 --> 07:34:21,540 just keeps running even though the 10563 07:34:19,620 --> 07:34:23,878 component is not displayed and again 10564 07:34:21,540 --> 07:34:26,160 this can lead to some serious issues 10565 07:34:23,878 --> 07:34:27,360 let's imagine you're subscribing to some 10566 07:34:26,160 --> 07:34:29,340 kind of service 10567 07:34:27,360 --> 07:34:31,320 and essentially you still have this 10568 07:34:29,340 --> 07:34:32,878 running in a background even though the 10569 07:34:31,320 --> 07:34:35,040 component is not displayed anymore 10570 07:34:32,878 --> 07:34:37,260 what's even more interesting the more 10571 07:34:35,040 --> 07:34:39,058 times I'm going to click the faster it's 10572 07:34:37,260 --> 07:34:42,360 going to go because now I'll have more 10573 07:34:39,058 --> 07:34:46,558 set intervals now what is the solution 10574 07:34:42,360 --> 07:34:48,780 well it's a cleanup function and the 10575 07:34:46,558 --> 07:34:50,458 syntax is a little bit funky so just 10576 07:34:48,780 --> 07:34:51,420 bear with me we want to go here with 10577 07:34:50,458 --> 07:34:53,520 return 10578 07:34:51,420 --> 07:34:55,920 and we want to return a function and 10579 07:34:53,520 --> 07:34:58,440 then whatever is within this function is 10580 07:34:55,920 --> 07:35:01,200 going to be invoked and I'll showcase 10581 07:34:58,440 --> 07:35:02,700 when it's actually invoked and that's 10582 07:35:01,200 --> 07:35:05,580 the reason why we don't want to set this 10583 07:35:02,700 --> 07:35:07,978 one up as a sync like so because react 10584 07:35:05,580 --> 07:35:10,798 is expecting that if you're returning 10585 07:35:07,978 --> 07:35:12,958 something you're actually returning a 10586 07:35:10,798 --> 07:35:16,680 function now when it comes to set 10587 07:35:12,958 --> 07:35:19,260 interval it actually returns an ID so in 10588 07:35:16,680 --> 07:35:22,378 here I can go to const and then whatever 10589 07:35:19,260 --> 07:35:25,260 the variable and then in order to clear 10590 07:35:22,378 --> 07:35:28,080 this interval we want to go with special 10591 07:35:25,260 --> 07:35:30,600 method it's called clear interval and we 10592 07:35:28,080 --> 07:35:34,260 want to pass in that ID again this is 10593 07:35:30,600 --> 07:35:36,718 just for Interval but effectively yes 10594 07:35:34,260 --> 07:35:39,600 when you have a cleanup function what 10595 07:35:36,718 --> 07:35:41,878 you want to do is essentially just to 10596 07:35:39,600 --> 07:35:45,120 clean up whatever functionality you have 10597 07:35:41,878 --> 07:35:47,040 over here so if you're subscribing to 10598 07:35:45,120 --> 07:35:49,200 some kind of service you want to 10599 07:35:47,040 --> 07:35:51,600 unsubscribe otherwise it's going to be 10600 07:35:49,200 --> 07:35:54,120 running in the background and now notice 10601 07:35:51,600 --> 07:35:56,638 something interesting we're basically 10602 07:35:54,120 --> 07:35:59,280 I'll click I'll have the hello from 10603 07:35:56,638 --> 07:36:01,620 interval but the moment I hide that's it 10604 07:35:59,280 --> 07:36:03,660 the interval is not running anymore and 10605 07:36:01,620 --> 07:36:04,860 I can click all the time 10606 07:36:03,660 --> 07:36:07,200 but 10607 07:36:04,860 --> 07:36:10,620 you'll see that essentially it's not 10608 07:36:07,200 --> 07:36:13,440 going to be running if the component is 10609 07:36:10,620 --> 07:36:16,200 not displayed now when this one runs and 10610 07:36:13,440 --> 07:36:18,718 this one is a little bit tricky where 10611 07:36:16,200 --> 07:36:20,400 basically if I go here with log and if I 10612 07:36:18,718 --> 07:36:22,500 say render 10613 07:36:20,400 --> 07:36:23,700 and you know what let's clean out these 10614 07:36:22,500 --> 07:36:25,978 logs for now 10615 07:36:23,700 --> 07:36:28,260 let me comment them out you'll see that 10616 07:36:25,978 --> 07:36:30,058 of course when it runs well on a show 10617 07:36:28,260 --> 07:36:31,620 render right so this one we're not 10618 07:36:30,058 --> 07:36:33,718 toggling only when we Mount the 10619 07:36:31,620 --> 07:36:37,558 component basically when the application 10620 07:36:33,718 --> 07:36:40,500 loads that's when we essentially render 10621 07:36:37,558 --> 07:36:42,540 this component the cleanup function now 10622 07:36:40,500 --> 07:36:45,240 if we go here inside of the cleanup 10623 07:36:42,540 --> 07:36:46,440 function and if we type log and we'll 10624 07:36:45,240 --> 07:36:48,900 say 10625 07:36:46,440 --> 07:36:53,218 clean up 10626 07:36:48,900 --> 07:36:54,900 you'll see that essentially it runs 10627 07:36:53,218 --> 07:36:57,420 after this render 10628 07:36:54,900 --> 07:36:59,820 and before the use effect so that's how 10629 07:36:57,420 --> 07:37:01,620 we can clean up after ourselves so let's 10630 07:36:59,820 --> 07:37:05,218 refresh one more time initial render 10631 07:37:01,620 --> 07:37:07,260 then once we click what happens well we 10632 07:37:05,218 --> 07:37:11,040 re-render this component right 10633 07:37:07,260 --> 07:37:13,138 because we change the state value 10634 07:37:11,040 --> 07:37:15,840 and now check it out once I'll click 10635 07:37:13,138 --> 07:37:17,458 again I'll re-render and I'll clean it 10636 07:37:15,840 --> 07:37:19,978 up so notice 10637 07:37:17,458 --> 07:37:22,378 I have another re-render because again 10638 07:37:19,978 --> 07:37:25,740 we flip the value and then I have the 10639 07:37:22,378 --> 07:37:28,798 cleanup so essentially it runs after the 10640 07:37:25,740 --> 07:37:30,900 render and before the use effect so in 10641 07:37:28,798 --> 07:37:33,298 our case basically when the component 10642 07:37:30,900 --> 07:37:35,878 and mount that's when we run it 10643 07:37:33,298 --> 07:37:37,440 hopefully that is clear again this is a 10644 07:37:35,878 --> 07:37:39,000 function we want to return we want to 10645 07:37:37,440 --> 07:37:40,680 set up here some kind of functionality 10646 07:37:39,000 --> 07:37:43,260 and 10647 07:37:40,680 --> 07:37:46,020 we want to do that in order to clean up 10648 07:37:43,260 --> 07:37:48,478 whatever we have in the use effect just 10649 07:37:46,020 --> 07:37:50,400 so it doesn't run in the background okay 10650 07:37:48,478 --> 07:37:52,860 and now let's take a look at another 10651 07:37:50,400 --> 07:37:55,440 example and in this case we're going to 10652 07:37:52,860 --> 07:37:58,740 use event listeners again something that 10653 07:37:55,440 --> 07:38:01,680 is coming from vanilla JS and 10654 07:37:58,740 --> 07:38:04,740 essentially the way the event listeners 10655 07:38:01,680 --> 07:38:06,540 work in vanilla JavaScript we just go 10656 07:38:04,740 --> 07:38:08,940 with the element in this case I'm going 10657 07:38:06,540 --> 07:38:12,240 to go with window and then we add The 10658 07:38:08,940 --> 07:38:13,860 Listener and then whatever event we want 10659 07:38:12,240 --> 07:38:16,260 to listen for so in this case I'm 10660 07:38:13,860 --> 07:38:18,420 looking for the scroll one and then we 10661 07:38:16,260 --> 07:38:20,400 want to pass in the Callback function so 10662 07:38:18,420 --> 07:38:23,878 essentially this callback function is 10663 07:38:20,400 --> 07:38:28,138 going to be invoked every time this 10664 07:38:23,878 --> 07:38:30,120 event fires and in our example 10665 07:38:28,138 --> 07:38:31,740 we want to go over here we want to set 10666 07:38:30,120 --> 07:38:34,020 up the function inside of the use effect 10667 07:38:31,740 --> 07:38:37,020 and all that and then we want to attach 10668 07:38:34,020 --> 07:38:38,760 that listener on the window and then 10669 07:38:37,020 --> 07:38:40,920 we'll see some interesting behavior and 10670 07:38:38,760 --> 07:38:43,740 of course after that we will clean this 10671 07:38:40,920 --> 07:38:46,080 up because that's the main point of 10672 07:38:43,740 --> 07:38:48,600 these videos so let's start over here by 10673 07:38:46,080 --> 07:38:50,760 just removing everything and let's come 10674 07:38:48,600 --> 07:38:53,458 up with that some funk again doesn't 10675 07:38:50,760 --> 07:38:55,200 really matter what happens in here and 10676 07:38:53,458 --> 07:38:57,660 therefore I'll basically leave it blank 10677 07:38:55,200 --> 07:39:00,600 that's irrelevant over here so I'm just 10678 07:38:57,660 --> 07:39:04,320 going to say here some logic and then 10679 07:39:00,600 --> 07:39:06,660 now let's go with window again in react 10680 07:39:04,320 --> 07:39:09,180 we do have access to the window object 10681 07:39:06,660 --> 07:39:12,180 we go with add event listener we're 10682 07:39:09,180 --> 07:39:14,400 listening for scroll and basically we go 10683 07:39:12,180 --> 07:39:16,740 with comma and then some function so 10684 07:39:14,400 --> 07:39:18,840 this is going to be invoked so let's 10685 07:39:16,740 --> 07:39:21,298 again click and 10686 07:39:18,840 --> 07:39:24,718 refresh and all that let me click over 10687 07:39:21,298 --> 07:39:27,058 here notice we have the render okay 10688 07:39:24,718 --> 07:39:30,540 that's what we expect what we don't 10689 07:39:27,058 --> 07:39:33,180 expect probably is if we go 10690 07:39:30,540 --> 07:39:36,240 to a bigger browser window and if we 10691 07:39:33,180 --> 07:39:39,600 check the event listeners in tabs and if 10692 07:39:36,240 --> 07:39:42,900 we refresh basically see over here that 10693 07:39:39,600 --> 07:39:44,820 we have quite a few event listeners so 10694 07:39:42,900 --> 07:39:47,820 let's go here notice how I toggle the 10695 07:39:44,820 --> 07:39:50,280 component and once I refresh I have here 10696 07:39:47,820 --> 07:39:53,040 the scroll and now notice how many I 10697 07:39:50,280 --> 07:39:56,218 have over here so essentially 10698 07:39:53,040 --> 07:39:58,798 every time I toggle the component I'm 10699 07:39:56,218 --> 07:40:00,840 actually attaching that event listener 10700 07:39:58,798 --> 07:40:03,840 on a window and again 10701 07:40:00,840 --> 07:40:06,180 eventually this can lead to an issue now 10702 07:40:03,840 --> 07:40:08,820 some students have complained that they 10703 07:40:06,180 --> 07:40:10,798 don't see that and every time I go and 10704 07:40:08,820 --> 07:40:12,600 double check in my browser I can still 10705 07:40:10,798 --> 07:40:15,900 see it again you're looking for event 10706 07:40:12,600 --> 07:40:18,420 listeners and then you want to refresh 10707 07:40:15,900 --> 07:40:21,320 and you want to check out every time 10708 07:40:18,420 --> 07:40:24,540 you'll toggle the component essentially 10709 07:40:21,320 --> 07:40:27,000 the value is going to increase over here 10710 07:40:24,540 --> 07:40:29,160 so you'll have more event listeners now 10711 07:40:27,000 --> 07:40:31,978 if you don't see that again just trust 10712 07:40:29,160 --> 07:40:33,600 me that that's the behavior so now let 10713 07:40:31,978 --> 07:40:34,320 me navigate back and let's see how we 10714 07:40:33,600 --> 07:40:36,840 can 10715 07:40:34,320 --> 07:40:38,340 fix that issue and again we want to go 10716 07:40:36,840 --> 07:40:42,138 here with the return 10717 07:40:38,340 --> 07:40:45,240 a function and we want to go with window 10718 07:40:42,138 --> 07:40:47,638 then the method name is remove event 10719 07:40:45,240 --> 07:40:50,580 listener and in here we want to pass in 10720 07:40:47,638 --> 07:40:52,378 the scroll then comma and then we want 10721 07:40:50,580 --> 07:40:55,558 to pass the same function so in this 10722 07:40:52,378 --> 07:40:57,780 case it's going to be some function and 10723 07:40:55,558 --> 07:40:59,400 now you'll notice that 10724 07:40:57,780 --> 07:41:01,978 once I refresh 10725 07:40:59,400 --> 07:41:05,580 once I toggle the component and refresh 10726 07:41:01,978 --> 07:41:07,740 yep I have one so that's fine but then 10727 07:41:05,580 --> 07:41:10,558 once I hide the component and refresh 10728 07:41:07,740 --> 07:41:13,798 notice I don't have any more of that 10729 07:41:10,558 --> 07:41:17,160 scroll event and effectively that's how 10730 07:41:13,798 --> 07:41:19,440 we can use the cleanup function in a 10731 07:41:17,160 --> 07:41:22,080 Year's effect not bad not bad we're 10732 07:41:19,440 --> 07:41:25,200 pretty much done with use effect as well 10733 07:41:22,080 --> 07:41:29,540 as conditional rendering for that matter 10734 07:41:25,200 --> 07:41:33,120 but before I let you go let me share one 10735 07:41:29,540 --> 07:41:35,760 particularly useful resource which is 10736 07:41:33,120 --> 07:41:39,660 from react docs and it essentially 10737 07:41:35,760 --> 07:41:41,840 covers use effect Alternatives now 10738 07:41:39,660 --> 07:41:45,180 before you start yelling at the screen 10739 07:41:41,840 --> 07:41:48,718 yes there are still use cases for use 10740 07:41:45,180 --> 07:41:52,740 effect so no we did not waste our time 10741 07:41:48,718 --> 07:41:55,620 on learning use effect especially 10742 07:41:52,740 --> 07:41:58,378 if you consider how much code out there 10743 07:41:55,620 --> 07:42:02,218 is still using use effect 10744 07:41:58,378 --> 07:42:04,740 now since the article is quite extensive 10745 07:42:02,218 --> 07:42:06,180 I'm just going to give you a general 10746 07:42:04,740 --> 07:42:09,718 gist 10747 07:42:06,180 --> 07:42:12,600 you see when the react hooks came out I 10748 07:42:09,718 --> 07:42:14,878 believe it was version 16. 10749 07:42:12,600 --> 07:42:16,378 developers started using use effect for 10750 07:42:14,878 --> 07:42:18,718 pretty much everything 10751 07:42:16,378 --> 07:42:22,260 and as a side note yes 10752 07:42:18,718 --> 07:42:25,680 I was one of those developers as well so 10753 07:42:22,260 --> 07:42:28,200 I'm not casting any shade however 10754 07:42:25,680 --> 07:42:30,058 such approach basically to have use 10755 07:42:28,200 --> 07:42:32,878 effect on top of use effect can 10756 07:42:30,058 --> 07:42:36,000 potentially lead to clunky code 10757 07:42:32,878 --> 07:42:38,458 basically hard to read and manage as 10758 07:42:36,000 --> 07:42:41,700 well as some performance issues 10759 07:42:38,458 --> 07:42:44,040 so in this article the reactive is 10760 07:42:41,700 --> 07:42:48,360 simply encouraging the community to 10761 07:42:44,040 --> 07:42:49,440 consider alternatives before jamming yet 10762 07:42:48,360 --> 07:42:51,840 another 10763 07:42:49,440 --> 07:42:55,378 use effect in the component 10764 07:42:51,840 --> 07:42:58,500 and if I may make a suggestion try to 10765 07:42:55,378 --> 07:43:01,378 find some free time to skim through this 10766 07:42:58,500 --> 07:43:04,340 resource again you don't have to go line 10767 07:43:01,378 --> 07:43:07,500 by line take a look at the general ideas 10768 07:43:04,340 --> 07:43:10,798 and the next time when you want to set 10769 07:43:07,500 --> 07:43:13,138 up a use effect just come back to it and 10770 07:43:10,798 --> 07:43:14,878 see whether there is a better 10771 07:43:13,138 --> 07:43:17,580 alternative 10772 07:43:14,878 --> 07:43:20,160 quite often you'll be able to achieve 10773 07:43:17,580 --> 07:43:24,180 the same functionality by adding logic 10774 07:43:20,160 --> 07:43:26,400 straight in the jsx or by setting up an 10775 07:43:24,180 --> 07:43:29,218 extra function in the component 10776 07:43:26,400 --> 07:43:32,638 lastly one major use case for use effect 10777 07:43:29,218 --> 07:43:35,458 used to be data fetching something we 10778 07:43:32,638 --> 07:43:38,100 already covered in the previous videos 10779 07:43:35,458 --> 07:43:41,040 however as I'm recording this course 10780 07:43:38,100 --> 07:43:43,740 iteration at this point in time there 10781 07:43:41,040 --> 07:43:46,500 are some great libraries for example 10782 07:43:43,740 --> 07:43:49,978 react query which actually allows to 10783 07:43:46,500 --> 07:43:52,920 fetch data with just one line of code 10784 07:43:49,978 --> 07:43:56,040 so basically without doing too much work 10785 07:43:52,920 --> 07:43:59,100 we already can use less use effect 10786 07:43:56,040 --> 07:44:02,160 instances in our applications 10787 07:43:59,100 --> 07:44:04,620 as you can see in the code example we 10788 07:44:02,160 --> 07:44:08,340 simply install the library use a custom 10789 07:44:04,620 --> 07:44:10,680 hook and right away get back data error 10790 07:44:08,340 --> 07:44:14,160 loading and a bunch of other useful 10791 07:44:10,680 --> 07:44:16,378 stuff about our request what's more such 10792 07:44:14,160 --> 07:44:19,200 libraries also take care of things like 10793 07:44:16,378 --> 07:44:20,520 caching and synchronization so it's no 10794 07:44:19,200 --> 07:44:22,760 surprise that they are gaining 10795 07:44:20,520 --> 07:44:26,218 popularity with the speed of light 10796 07:44:22,760 --> 07:44:29,040 especially for bigger projects with 10797 07:44:26,218 --> 07:44:32,458 bunch of requests and yes as a side note 10798 07:44:29,040 --> 07:44:35,878 we will build a project later on of 10799 07:44:32,458 --> 07:44:37,860 course with react query now does that 10800 07:44:35,878 --> 07:44:40,680 mean that you have to use such library 10801 07:44:37,860 --> 07:44:42,240 in a small project where you have only a 10802 07:44:40,680 --> 07:44:45,478 few get requests 10803 07:44:42,240 --> 07:44:47,340 no I probably wouldn't do that just 10804 07:44:45,478 --> 07:44:50,638 something to think about when you start 10805 07:44:47,340 --> 07:44:54,240 working on your own projects simply be 10806 07:44:50,638 --> 07:44:56,520 mindful and consider alternatives all 10807 07:44:54,240 --> 07:44:59,280 right and up next let's talk about the 10808 07:44:56,520 --> 07:45:02,520 project structure essentially I just 10809 07:44:59,280 --> 07:45:05,700 want to showcase few ways how we can 10810 07:45:02,520 --> 07:45:08,458 structure our project because while 10811 07:45:05,700 --> 07:45:11,340 we're working on somewhat small project 10812 07:45:08,458 --> 07:45:13,860 this type of approach where you set up 10813 07:45:11,340 --> 07:45:16,378 the components folder or whatever you 10814 07:45:13,860 --> 07:45:18,660 want to name this folder and then set up 10815 07:45:16,378 --> 07:45:22,080 all your components there is awesome but 10816 07:45:18,660 --> 07:45:25,500 as your project grows and you have a 10817 07:45:22,080 --> 07:45:28,200 need for more files you might want to 10818 07:45:25,500 --> 07:45:31,138 implement different approach and 10819 07:45:28,200 --> 07:45:34,740 effectively we're going to work in the 10820 07:45:31,138 --> 07:45:36,718 zero four project structure will work in 10821 07:45:34,740 --> 07:45:38,940 a star one and as you can see at the 10822 07:45:36,718 --> 07:45:42,360 moment it's empty and also in the app.js 10823 07:45:38,940 --> 07:45:44,878 I'm not importing anything so jsx is 10824 07:45:42,360 --> 07:45:48,478 empty I just have the container 10825 07:45:44,878 --> 07:45:51,900 and effectively one way how we can 10826 07:45:48,478 --> 07:45:53,040 set up the structure for our project is 10827 07:45:51,900 --> 07:45:57,600 by 10828 07:45:53,040 --> 07:46:00,298 setting up every component as a folder 10829 07:45:57,600 --> 07:46:03,298 so let's imagine that we have some kind 10830 07:46:00,298 --> 07:46:06,360 of nav bar component and instead of just 10831 07:46:03,298 --> 07:46:08,638 setting it up as component I want to set 10832 07:46:06,360 --> 07:46:11,760 it up as a folder and in there I'll 10833 07:46:08,638 --> 07:46:16,378 actually have the CSS file now 10834 07:46:11,760 --> 07:46:19,978 the goal here is following essentially 10835 07:46:16,378 --> 07:46:22,500 as our project grows we might want to 10836 07:46:19,978 --> 07:46:25,020 keep all the files that are associated 10837 07:46:22,500 --> 07:46:27,420 with that component in one folder 10838 07:46:25,020 --> 07:46:29,280 and in turn is just easier you don't 10839 07:46:27,420 --> 07:46:31,440 have to run around your project and look 10840 07:46:29,280 --> 07:46:33,240 for CSS files the test files and all 10841 07:46:31,440 --> 07:46:35,218 that you know that hey this is a 10842 07:46:33,240 --> 07:46:38,638 component and these are all the files 10843 07:46:35,218 --> 07:46:41,340 that are associated with that component 10844 07:46:38,638 --> 07:46:43,558 so let's try this one out and in the 10845 07:46:41,340 --> 07:46:46,138 process we'll take a look at multiple 10846 07:46:43,558 --> 07:46:49,860 approaches so in here I'm going to go to 10847 07:46:46,138 --> 07:46:53,580 starter I'm going to go to a new folder 10848 07:46:49,860 --> 07:46:55,740 and let's just call this navbar and in 10849 07:46:53,580 --> 07:46:57,420 here we'll just set up some example 10850 07:46:55,740 --> 07:46:59,520 component again 10851 07:46:57,420 --> 07:47:00,600 it's really irrelevant what we have over 10852 07:46:59,520 --> 07:47:05,580 there so I'm just going to go with 10853 07:47:00,600 --> 07:47:07,020 navbar jsx let's create the component 10854 07:47:05,580 --> 07:47:09,058 I think I'm going to change it to 10855 07:47:07,020 --> 07:47:11,700 heading like so and then I'll create 10856 07:47:09,058 --> 07:47:13,500 empty CSS file again it's irrelevant 10857 07:47:11,700 --> 07:47:15,240 what we have over there 10858 07:47:13,500 --> 07:47:17,638 just want to showcase the example so 10859 07:47:15,240 --> 07:47:19,080 I'll go here with navbar and then CSS 10860 07:47:17,638 --> 07:47:20,820 and of course 10861 07:47:19,080 --> 07:47:23,040 if you want to test it out you can set 10862 07:47:20,820 --> 07:47:26,280 up the CSS over here and you can Import 10863 07:47:23,040 --> 07:47:30,058 in Napa I'm not going to do that then I 10864 07:47:26,280 --> 07:47:32,058 want to go back to app jsx and I want to 10865 07:47:30,058 --> 07:47:36,240 import that component again 10866 07:47:32,058 --> 07:47:39,000 the goal of this setup is essentially to 10867 07:47:36,240 --> 07:47:41,580 keep all the files that are associated 10868 07:47:39,000 --> 07:47:43,978 with that component in one place in this 10869 07:47:41,580 --> 07:47:45,958 case it's just navbar CSS but of course 10870 07:47:43,978 --> 07:47:49,620 there's other things that you can keep 10871 07:47:45,958 --> 07:47:52,080 over there and in the app jsx if we want 10872 07:47:49,620 --> 07:47:54,718 to grab this component we simply go with 10873 07:47:52,080 --> 07:47:57,540 the name of the component and we use the 10874 07:47:54,718 --> 07:47:59,218 Auto Import and check it out now of 10875 07:47:57,540 --> 07:48:01,260 course I have the nav bar displayed on 10876 07:47:59,218 --> 07:48:03,360 the screen and again all the files that 10877 07:48:01,260 --> 07:48:06,900 are associated with this particular 10878 07:48:03,360 --> 07:48:08,878 component are located in one place but 10879 07:48:06,900 --> 07:48:10,500 here's the gotcha 10880 07:48:08,878 --> 07:48:13,260 not as this one 10881 07:48:10,500 --> 07:48:16,080 we have navbar and then navbar 10882 07:48:13,260 --> 07:48:18,840 and this is somewhat annoying 10883 07:48:16,080 --> 07:48:22,020 where essentially yes I have a nice 10884 07:48:18,840 --> 07:48:24,660 folder but I don't want to keep using 10885 07:48:22,020 --> 07:48:26,940 two names here one after another which 10886 07:48:24,660 --> 07:48:28,680 essentially are the same and one 10887 07:48:26,940 --> 07:48:29,760 solution for that is to create here 10888 07:48:28,680 --> 07:48:31,760 index 10889 07:48:29,760 --> 07:48:35,638 so we're going to go over here with 10890 07:48:31,760 --> 07:48:38,878 index.js and basically we can set up the 10891 07:48:35,638 --> 07:48:40,798 logic here in the index.js so for time 10892 07:48:38,878 --> 07:48:44,218 being I'll just copy and paste because 10893 07:48:40,798 --> 07:48:46,138 we will change our logic in a second for 10894 07:48:44,218 --> 07:48:50,820 now I just want to showcase that if 10895 07:48:46,138 --> 07:48:54,420 let's say I go to index.js and then if I 10896 07:48:50,820 --> 07:48:56,820 go back to my app one I can omit that 10897 07:48:54,420 --> 07:48:58,920 nav bar now I do need to fix the 10898 07:48:56,820 --> 07:49:01,260 extension my apologies I keep forgetting 10899 07:48:58,920 --> 07:49:03,840 with Veet that we need to go with jsx so 10900 07:49:01,260 --> 07:49:06,660 let me save Here and Now check it out I 10901 07:49:03,840 --> 07:49:10,500 have the navbar so essentially we save 10902 07:49:06,660 --> 07:49:12,898 that one word here so instead of navbar 10903 07:49:10,500 --> 07:49:14,878 navbar since this is a node project it's 10904 07:49:12,898 --> 07:49:17,878 essentially a node thing where if you 10905 07:49:14,878 --> 07:49:20,280 have index in the folder that's going to 10906 07:49:17,878 --> 07:49:22,740 be used as an entry point so whatever 10907 07:49:20,280 --> 07:49:25,740 you have over here here is going to be 10908 07:49:22,740 --> 07:49:27,780 right away exported so in our example we 10909 07:49:25,740 --> 07:49:30,600 don't need to look for specific file we 10910 07:49:27,780 --> 07:49:34,320 can just say Okay so get me the navbar 10911 07:49:30,600 --> 07:49:36,240 folder aka the navbar component 10912 07:49:34,320 --> 07:49:39,240 now what is the issue with this setup 10913 07:49:36,240 --> 07:49:41,520 well if you have tons of files 10914 07:49:39,240 --> 07:49:43,798 effectively it just becomes bunch of 10915 07:49:41,520 --> 07:49:47,580 indexes so it's very hard to navigate 10916 07:49:43,798 --> 07:49:48,898 around your tabs so let me show you a 10917 07:49:47,580 --> 07:49:52,260 way how we can 10918 07:49:48,898 --> 07:49:54,180 have our cake and eat it too basically 10919 07:49:52,260 --> 07:49:57,420 we won't have to use that nav bar nav 10920 07:49:54,180 --> 07:50:00,240 bar and we also will have a bunch of 10921 07:49:57,420 --> 07:50:02,040 these indexes over here so I have the 10922 07:50:00,240 --> 07:50:05,420 navbar component and I have the 10923 07:50:02,040 --> 07:50:08,340 index.jsx what I can do in 10924 07:50:05,420 --> 07:50:11,760 index.jsx I can import 10925 07:50:08,340 --> 07:50:13,798 the nav bar and then export it as 10926 07:50:11,760 --> 07:50:16,620 default so it's going to look like we're 10927 07:50:13,798 --> 07:50:17,760 going to go over here export and then 10928 07:50:16,620 --> 07:50:20,458 default 10929 07:50:17,760 --> 07:50:22,440 and we're looking from and since it's in 10930 07:50:20,458 --> 07:50:26,218 the same folder we just look for the 10931 07:50:22,440 --> 07:50:29,040 navbar and if I navigate back to the app 10932 07:50:26,218 --> 07:50:31,798 jsx first of all if I want to do 10933 07:50:29,040 --> 07:50:33,478 something in the navbar well I'll be 10934 07:50:31,798 --> 07:50:36,478 using this tab right so this is my 10935 07:50:33,478 --> 07:50:38,580 component and I'm just using index to 10936 07:50:36,478 --> 07:50:40,920 export that component and here nothing 10937 07:50:38,580 --> 07:50:42,540 changes so if I want to grab the navbar 10938 07:50:40,920 --> 07:50:44,280 component then of course I'm just 10939 07:50:42,540 --> 07:50:48,000 looking for the folder and as you can 10940 07:50:44,280 --> 07:50:49,440 see everything still works so we have no 10941 07:50:48,000 --> 07:50:51,600 issues 10942 07:50:49,440 --> 07:50:55,138 so that's how we can set up essentially 10943 07:50:51,600 --> 07:50:58,378 a folder for every component and few 10944 07:50:55,138 --> 07:51:01,978 ways that we can use index.js to 10945 07:50:58,378 --> 07:51:04,080 simplify our workflow now let me stress 10946 07:51:01,978 --> 07:51:06,478 something it doesn't mean that now you 10947 07:51:04,080 --> 07:51:09,798 have to use this kind of setup for every 10948 07:51:06,478 --> 07:51:13,138 project again this only makes sense 10949 07:51:09,798 --> 07:51:15,898 if your project is big and you need some 10950 07:51:13,138 --> 07:51:18,420 kind of structure to navigate around the 10951 07:51:15,898 --> 07:51:20,700 project all right and up next let's take 10952 07:51:18,420 --> 07:51:23,340 a look at another approach where we can 10953 07:51:20,700 --> 07:51:24,898 set up named exports and this is 10954 07:51:23,340 --> 07:51:26,760 definitely something we're going to use 10955 07:51:24,898 --> 07:51:29,160 during the course but again I want to 10956 07:51:26,760 --> 07:51:33,298 stress something this only makes sense 10957 07:51:29,160 --> 07:51:34,920 if you have a bunch of components in the 10958 07:51:33,298 --> 07:51:37,558 folder and you'll see what I'm talking 10959 07:51:34,920 --> 07:51:39,898 about in a second and effectively we'll 10960 07:51:37,558 --> 07:51:41,940 definitely use this one when we're going 10961 07:51:39,898 --> 07:51:43,978 to work with react router because in 10962 07:51:41,940 --> 07:51:45,840 that case you have quite a few pages and 10963 07:51:43,978 --> 07:51:49,320 it just makes easier 10964 07:51:45,840 --> 07:51:51,120 if you set up such named exports so 10965 07:51:49,320 --> 07:51:54,000 first we want to navigate to the starter 10966 07:51:51,120 --> 07:51:56,340 and we want to create another folder and 10967 07:51:54,000 --> 07:51:59,160 in this case let's call this pages 10968 07:51:56,340 --> 07:51:59,820 now inside of the pages what we want to 10969 07:51:59,160 --> 07:52:02,398 do 10970 07:51:59,820 --> 07:52:05,760 we want to create two pages the home 10971 07:52:02,398 --> 07:52:07,680 page and the about page and little 10972 07:52:05,760 --> 07:52:09,600 spoiler alert when we're talking about 10973 07:52:07,680 --> 07:52:11,820 pages in react essentially we're just 10974 07:52:09,600 --> 07:52:15,000 talking about the components so I'm just 10975 07:52:11,820 --> 07:52:17,820 going to create here home jsx I just 10976 07:52:15,000 --> 07:52:20,760 need to remind myself all the time that 10977 07:52:17,820 --> 07:52:23,398 I need to go with jsx so we want to set 10978 07:52:20,760 --> 07:52:26,280 this one up this is going to be our home 10979 07:52:23,398 --> 07:52:31,080 page and then we want to do the same 10980 07:52:26,280 --> 07:52:33,478 thing with about so about and then js6 10981 07:52:31,080 --> 07:52:36,000 same there we want to create that this 10982 07:52:33,478 --> 07:52:39,600 is going to be our about page and if we 10983 07:52:36,000 --> 07:52:41,700 navigate back to app jsx 10984 07:52:39,600 --> 07:52:44,700 I mean everything is awesome we'll be 10985 07:52:41,700 --> 07:52:47,340 able to access the pages same deal we go 10986 07:52:44,700 --> 07:52:50,700 here and then we also want to go with 10987 07:52:47,340 --> 07:52:51,718 about so this doesn't change but notice 10988 07:52:50,700 --> 07:52:53,878 how 10989 07:52:51,718 --> 07:52:55,860 we're getting quite a few lines of code 10990 07:52:53,878 --> 07:52:57,058 over here and imagine if you have 10 10991 07:52:55,860 --> 07:52:59,638 pages 10992 07:52:57,058 --> 07:53:01,798 so essentially every time you want to 10993 07:52:59,638 --> 07:53:03,840 use all of them you'll have like I don't 10994 07:53:01,798 --> 07:53:06,058 know 20 lines of code or something like 10995 07:53:03,840 --> 07:53:07,558 that and there's a way how we can 10996 07:53:06,058 --> 07:53:10,200 simplify this 10997 07:53:07,558 --> 07:53:12,540 so first let me remove this number we're 10998 07:53:10,200 --> 07:53:16,860 not going to need that and back in our 10999 07:53:12,540 --> 07:53:18,780 Pages we can create another index.jsx so 11000 07:53:16,860 --> 07:53:22,860 remember that's going to be our 11001 07:53:18,780 --> 07:53:26,638 entry point over here and we can import 11002 07:53:22,860 --> 07:53:30,120 or the components from this directory 11003 07:53:26,638 --> 07:53:32,398 so essentially we can do like this I can 11004 07:53:30,120 --> 07:53:35,160 go with home so I'm importing home and I 11005 07:53:32,398 --> 07:53:37,378 also want to do the same thing for about 11006 07:53:35,160 --> 07:53:40,440 like so so I'm going to go with about 11007 07:53:37,378 --> 07:53:43,558 and then from the index I want to export 11008 07:53:40,440 --> 07:53:46,020 that so I'm going to go with export and 11009 07:53:43,558 --> 07:53:47,580 I'm going to go with home and about and 11010 07:53:46,020 --> 07:53:49,920 essentially if you have 20 components 11011 07:53:47,580 --> 07:53:51,898 this is what you'll do you'll import all 11012 07:53:49,920 --> 07:53:54,840 of them in the index.jsx and you'll 11013 07:53:51,898 --> 07:53:57,540 export and the beauty here is that if I 11014 07:53:54,840 --> 07:54:00,898 go back to app jsx 11015 07:53:57,540 --> 07:54:04,440 I don't have to provide the path for 11016 07:54:00,898 --> 07:54:06,898 every component I can do like this let 11017 07:54:04,440 --> 07:54:09,718 me remove now these ones are named 11018 07:54:06,898 --> 07:54:12,478 exports so the names need to match 11019 07:54:09,718 --> 07:54:14,820 that's something important so here I do 11020 07:54:12,478 --> 07:54:16,978 need to go at home and I do need to go 11021 07:54:14,820 --> 07:54:18,780 with about if you'll go here with 11022 07:54:16,978 --> 07:54:20,520 contact or whatever it's not going to 11023 07:54:18,780 --> 07:54:22,080 work so first let me just showcase how 11024 07:54:20,520 --> 07:54:24,058 this works and then 11025 07:54:22,080 --> 07:54:26,520 we'll take a look at the error example 11026 07:54:24,058 --> 07:54:28,740 as well so now let me remove home 11027 07:54:26,520 --> 07:54:31,138 because notice now we're looking for 11028 07:54:28,740 --> 07:54:32,760 that index and check it out everything 11029 07:54:31,138 --> 07:54:35,458 still works 11030 07:54:32,760 --> 07:54:38,398 so essentially if you have 20 components 11031 07:54:35,458 --> 07:54:40,978 you can nicely import them effectively 11032 07:54:38,398 --> 07:54:42,180 in one line of code no I mean the path 11033 07:54:40,978 --> 07:54:44,160 is a little bit longer so it's 11034 07:54:42,180 --> 07:54:46,138 technically two but hopefully you see 11035 07:54:44,160 --> 07:54:47,958 where I'm going with this so instead of 11036 07:54:46,138 --> 07:54:52,020 using 11037 07:54:47,958 --> 07:54:53,820 the import for every component we're 11038 07:54:52,020 --> 07:54:57,780 going to import all of them together 11039 07:54:53,820 --> 07:54:59,580 again we set up index jsx in that folder 11040 07:54:57,780 --> 07:55:02,040 that's going to be the entry point and 11041 07:54:59,580 --> 07:55:05,040 one by one we grab those components and 11042 07:55:02,040 --> 07:55:07,138 then we export them but again keep in 11043 07:55:05,040 --> 07:55:11,040 mind if you're gonna do here like this 11044 07:55:07,138 --> 07:55:12,478 if you'll say hey get me the contact 11045 07:55:11,040 --> 07:55:15,360 what do you think is going to happen 11046 07:55:12,478 --> 07:55:18,120 we'll have big fat error right so if 11047 07:55:15,360 --> 07:55:21,120 we'll save notice it says well there is 11048 07:55:18,120 --> 07:55:22,978 no named export contact that's just 11049 07:55:21,120 --> 07:55:24,898 something to keep in mind now you can 11050 07:55:22,978 --> 07:55:27,180 set up the aliases and all that I'm not 11051 07:55:24,898 --> 07:55:29,700 gonna go as deep 11052 07:55:27,180 --> 07:55:32,280 in these examples we'll just stick with 11053 07:55:29,700 --> 07:55:34,978 the ones that we just covered so this is 11054 07:55:32,280 --> 07:55:37,500 a nice way how we can group all of our 11055 07:55:34,978 --> 07:55:40,860 components together and Export them as 11056 07:55:37,500 --> 07:55:43,978 named exports okay and lastly let me 11057 07:55:40,860 --> 07:55:45,478 showcase how we can export group of 11058 07:55:43,978 --> 07:55:48,000 components so this is going to be a 11059 07:55:45,478 --> 07:55:53,100 little bit different and also a very 11060 07:55:48,000 --> 07:55:54,898 useful extension glean and maybe less 11061 07:55:53,100 --> 07:55:58,558 useful to some people but to me also 11062 07:55:54,898 --> 07:56:00,840 very useful a code spell checker so 11063 07:55:58,558 --> 07:56:02,458 first let's start with example directory 11064 07:56:00,840 --> 07:56:04,080 where 11065 07:56:02,458 --> 07:56:05,580 we'll take a look at how we can export 11066 07:56:04,080 --> 07:56:07,920 as a group 11067 07:56:05,580 --> 07:56:10,020 first I want to navigate to app.jsx I 11068 07:56:07,920 --> 07:56:12,478 want to clean this up so I'm going to 11069 07:56:10,020 --> 07:56:14,878 remove the existing ones then we want to 11070 07:56:12,478 --> 07:56:16,020 navigate back to the star we'll create 11071 07:56:14,878 --> 07:56:19,138 another 11072 07:56:16,020 --> 07:56:21,600 folder and in this case the folder name 11073 07:56:19,138 --> 07:56:24,000 is going to be example 11074 07:56:21,600 --> 07:56:25,080 example in here let's create two more 11075 07:56:24,000 --> 07:56:27,478 components 11076 07:56:25,080 --> 07:56:28,558 so I'll call this first component and 11077 07:56:27,478 --> 07:56:31,920 second 11078 07:56:28,558 --> 07:56:33,540 as always sex jsx so let's create that 11079 07:56:31,920 --> 07:56:35,700 component I think I'm going to go with 11080 07:56:33,540 --> 07:56:38,398 adding two just so we can see it better 11081 07:56:35,700 --> 07:56:40,320 so first component okay awesome and then 11082 07:56:38,398 --> 07:56:43,260 we want to do the same thing here with 11083 07:56:40,320 --> 07:56:46,020 the second one so second and 11084 07:56:43,260 --> 07:56:48,600 component 11085 07:56:46,020 --> 07:56:51,180 jsx okay beautiful let's create that 11086 07:56:48,600 --> 07:56:53,878 awesome and then let's change it to a 11087 07:56:51,180 --> 07:56:57,000 heading two so another way how we can 11088 07:56:53,878 --> 07:56:59,280 export from this example essentially I 11089 07:56:57,000 --> 07:57:03,660 can set up a index 11090 07:56:59,280 --> 07:57:06,718 and I can import all of these ones 11091 07:57:03,660 --> 07:57:08,878 what I have in the example in the jsx 11092 07:57:06,718 --> 07:57:10,260 now why that would be beneficial well 11093 07:57:08,878 --> 07:57:12,718 let's imagine you have some kind of 11094 07:57:10,260 --> 07:57:15,478 component that is using 11095 07:57:12,718 --> 07:57:18,660 for example five other components again 11096 07:57:15,478 --> 07:57:20,580 instead of those components being just 11097 07:57:18,660 --> 07:57:22,138 scattered around your project if you 11098 07:57:20,580 --> 07:57:24,420 know that you'll definitely use those 11099 07:57:22,138 --> 07:57:26,340 five components inside of that one 11100 07:57:24,420 --> 07:57:30,000 component well it kind of makes sense 11101 07:57:26,340 --> 07:57:33,000 you set up index.jsx you just import all 11102 07:57:30,000 --> 07:57:35,280 of them and you're good to go so just to 11103 07:57:33,000 --> 07:57:38,398 Showcase that we can go new file then 11104 07:57:35,280 --> 07:57:42,000 remember yes index 11105 07:57:38,398 --> 07:57:43,500 jsx or JS is going to be the main entry 11106 07:57:42,000 --> 07:57:45,718 point so at the moment we create that 11107 07:57:43,500 --> 07:57:47,458 file yep as far as the paths and all 11108 07:57:45,718 --> 07:57:49,740 that it's going to point to this example 11109 07:57:47,458 --> 07:57:51,000 and in here we simply want to create a 11110 07:57:49,740 --> 07:57:54,360 component 11111 07:57:51,000 --> 07:57:57,240 and let's call this I don't know example 11112 07:57:54,360 --> 07:57:59,638 why not so let me select 11113 07:57:57,240 --> 07:58:03,058 all of them and we're going to call this 11114 07:57:59,638 --> 07:58:05,820 example we'll also rename this and then 11115 07:58:03,058 --> 07:58:07,200 we want to import those two components 11116 07:58:05,820 --> 07:58:09,360 so we want to go here with the first 11117 07:58:07,200 --> 07:58:11,580 component and second component and you 11118 07:58:09,360 --> 07:58:13,740 know what I think it's going to make 11119 07:58:11,580 --> 07:58:16,558 more sense if I'll place a div over here 11120 07:58:13,740 --> 07:58:19,558 and then one by one I'll add both of 11121 07:58:16,558 --> 07:58:22,680 those components so first component 11122 07:58:19,558 --> 07:58:25,260 close it here and then second component 11123 07:58:22,680 --> 07:58:28,020 as well so Auto Import second component 11124 07:58:25,260 --> 07:58:30,298 awesome let's save that and it looks 11125 07:58:28,020 --> 07:58:32,100 like I forgot to change it over here so 11126 07:58:30,298 --> 07:58:34,200 we need to go with example 11127 07:58:32,100 --> 07:58:36,600 and now we simply want to go back to app 11128 07:58:34,200 --> 07:58:39,600 jsx and when we want to grab that 11129 07:58:36,600 --> 07:58:42,478 example folder so let's go here example 11130 07:58:39,600 --> 07:58:45,780 and check it out I'll have example and 11131 07:58:42,478 --> 07:58:48,420 first and second component again 11132 07:58:45,780 --> 07:58:51,360 something that has a very specific use 11133 07:58:48,420 --> 07:58:53,458 case you don't have to do that for every 11134 07:58:51,360 --> 07:58:56,520 project but here and there it's somewhat 11135 07:58:53,458 --> 07:58:58,558 Nifty and that's why I decided to add a 11136 07:58:56,520 --> 07:59:02,280 two tutorial and now let's take a look 11137 07:58:58,558 --> 07:59:04,138 at those two extensions like promised so 11138 07:59:02,280 --> 07:59:06,420 first of all this one somewhat 11139 07:59:04,138 --> 07:59:08,820 straightforward but extremely extremely 11140 07:59:06,420 --> 07:59:12,000 useful for me the code spell checker and 11141 07:59:08,820 --> 07:59:14,820 it's somewhat self-explanatory it just 11142 07:59:12,000 --> 07:59:17,520 checks for spelling errors this is very 11143 07:59:14,820 --> 07:59:18,420 useful especially if you're following 11144 07:59:17,520 --> 07:59:21,478 along 11145 07:59:18,420 --> 07:59:24,360 because it will right away notify you if 11146 07:59:21,478 --> 07:59:26,218 your name is off now it's not looking 11147 07:59:24,360 --> 07:59:27,180 for the error you're just going to say 11148 07:59:26,218 --> 07:59:30,600 Hey listen 11149 07:59:27,180 --> 07:59:32,760 the spelling here is incorrect and why 11150 07:59:30,600 --> 07:59:34,620 I'm telling you that because quite often 11151 07:59:32,760 --> 07:59:37,080 I see students 11152 07:59:34,620 --> 07:59:40,260 sharing their code and I can see that 11153 07:59:37,080 --> 07:59:41,700 the problem is that they used the wrong 11154 07:59:40,260 --> 07:59:44,520 name in the first place let's say 11155 07:59:41,700 --> 07:59:46,920 there's some kind of spelling error and 11156 07:59:44,520 --> 07:59:49,860 then later on they use the correct one 11157 07:59:46,920 --> 07:59:51,298 and then things don't work so I think 11158 07:59:49,860 --> 07:59:53,040 it's going to be easier if I showcase 11159 07:59:51,298 --> 07:59:55,080 that let's imagine that in here I'll 11160 07:59:53,040 --> 07:59:58,320 have some kind of function so I'm going 11161 07:59:55,080 --> 07:59:59,580 to go with const and then handle and 11162 07:59:58,320 --> 08:00:02,580 change 11163 07:59:59,580 --> 08:00:04,620 now everything is awesome but let's 11164 08:00:02,580 --> 08:00:07,740 imagine that as I was adding this 11165 08:00:04,620 --> 08:00:09,660 function instead of handle I went like 11166 08:00:07,740 --> 08:00:11,458 this so right away I have this blue 11167 08:00:09,660 --> 08:00:13,978 squiggly line which is going to tell me 11168 08:00:11,458 --> 08:00:15,478 Hey listen I mean technically there's 11169 08:00:13,978 --> 08:00:17,280 nothing wrong with your code but you 11170 08:00:15,478 --> 08:00:19,320 should probably check this does this 11171 08:00:17,280 --> 08:00:21,000 really make sense as I know this is 11172 08:00:19,320 --> 08:00:24,058 super useful for me because I'm the 11173 08:00:21,000 --> 08:00:27,120 worst speller ever and then I also want 11174 08:00:24,058 --> 08:00:29,280 to showcase the glean extension and 11175 08:00:27,120 --> 08:00:31,798 what's really cool about this extension 11176 08:00:29,280 --> 08:00:34,620 we can pretty much take elements we want 11177 08:00:31,798 --> 08:00:38,218 to set up as component and right away 11178 08:00:34,620 --> 08:00:41,218 create a file so let's imagine the 11179 08:00:38,218 --> 08:00:44,520 scenario in the app I have some kind of 11180 08:00:41,218 --> 08:00:46,558 element let's say div and inside of it 11181 08:00:44,520 --> 08:00:48,958 I'm going to have a heading three and 11182 08:00:46,558 --> 08:00:51,600 I'm gonna say hello world now if I want 11183 08:00:48,958 --> 08:00:53,820 to set up a separate component what is 11184 08:00:51,600 --> 08:00:56,218 normally the 11185 08:00:53,820 --> 08:00:59,340 path well I need to create a component 11186 08:00:56,218 --> 08:01:02,820 somewhere here right then I need to grab 11187 08:00:59,340 --> 08:01:05,280 the elements and then copy and paste and 11188 08:01:02,820 --> 08:01:08,878 it's much faster with gleam so 11189 08:01:05,280 --> 08:01:10,740 effectively I can select the elements I 11190 08:01:08,878 --> 08:01:12,898 want to extract I want to click on the 11191 08:01:10,740 --> 08:01:16,138 light bulb and check it out we have this 11192 08:01:12,898 --> 08:01:18,180 option extract component to file we 11193 08:01:16,138 --> 08:01:19,680 click we need to pick the directory my 11194 08:01:18,180 --> 08:01:21,600 case I'm going to go to Source keep in 11195 08:01:19,680 --> 08:01:23,878 mind but of course you can go tutorial 11196 08:01:21,600 --> 08:01:26,398 for example I'm just picking the easy 11197 08:01:23,878 --> 08:01:28,798 option and then we're going to go here 11198 08:01:26,398 --> 08:01:32,340 with test so the file name and I'm going 11199 08:01:28,798 --> 08:01:35,280 to go with jsx and notice how right away 11200 08:01:32,340 --> 08:01:39,600 not only it sets up the component not 11201 08:01:35,280 --> 08:01:42,298 only it Imports it but it also right 11202 08:01:39,600 --> 08:01:45,058 away sets up the component file as well 11203 08:01:42,298 --> 08:01:48,180 and what's really cool it even provides 11204 08:01:45,058 --> 08:01:49,860 the props which is super Nifty if for 11205 08:01:48,180 --> 08:01:51,780 example you're iterating over the list 11206 08:01:49,860 --> 08:01:55,260 and then you're passing in some props 11207 08:01:51,780 --> 08:01:59,100 into the elements you can essentially 11208 08:01:55,260 --> 08:02:02,760 right away set it up as a component and 11209 08:01:59,100 --> 08:02:05,100 extract it again super super useful 11210 08:02:02,760 --> 08:02:06,000 extension we save it over here and 11211 08:02:05,100 --> 08:02:09,000 notice 11212 08:02:06,000 --> 08:02:12,298 the result did not change however this 11213 08:02:09,000 --> 08:02:15,898 is much faster and effectively this 11214 08:02:12,298 --> 08:02:18,660 concludes the project structure 11215 08:02:15,898 --> 08:02:22,260 section and up next I want to talk about 11216 08:02:18,660 --> 08:02:25,620 how we can leverage JavaScript in react 11217 08:02:22,260 --> 08:02:28,138 all right and up next let's talk about 11218 08:02:25,620 --> 08:02:31,680 how we can leverage JavaScript basically 11219 08:02:28,138 --> 08:02:35,700 how we can use JavaScript to fix issues 11220 08:02:31,680 --> 08:02:38,040 if the data is missing and if you're a 11221 08:02:35,700 --> 08:02:40,740 little bit confused on this entire topic 11222 08:02:38,040 --> 08:02:42,958 don't worry as we're going to be working 11223 08:02:40,740 --> 08:02:45,478 through the examples you'll see what I'm 11224 08:02:42,958 --> 08:02:47,760 talking about first notice over here 11225 08:02:45,478 --> 08:02:49,378 that in the app I'm not importing that 11226 08:02:47,760 --> 08:02:51,540 and I'm doing that on a purpose because 11227 08:02:49,378 --> 08:02:54,840 we'll set up everything from the scratch 11228 08:02:51,540 --> 08:02:57,120 so let's navigate to tutorial and we'll 11229 08:02:54,840 --> 08:02:59,700 right away start with the challenge and 11230 08:02:57,120 --> 08:03:02,218 as you can see the further we get in 11231 08:02:59,700 --> 08:03:04,378 tutorial the more challenges we have 11232 08:03:02,218 --> 08:03:06,958 because now I want you to utilize 11233 08:03:04,378 --> 08:03:10,260 everything that we have learned so far 11234 08:03:06,958 --> 08:03:13,320 and in this video or 11235 08:03:10,260 --> 08:03:15,898 more precisely in this section so most 11236 08:03:13,320 --> 08:03:19,440 likely in the following video will rely 11237 08:03:15,898 --> 08:03:21,780 heavily on optional chaining and if 11238 08:03:19,440 --> 08:03:24,180 you're not familiar with this topic 11239 08:03:21,780 --> 08:03:27,420 please utilize this 11240 08:03:24,180 --> 08:03:30,718 JavaScript nuggets video now I do have 11241 08:03:27,420 --> 08:03:32,218 the info also in the readme so if you 11242 08:03:30,718 --> 08:03:34,378 don't feel like watching the video you 11243 08:03:32,218 --> 08:03:37,680 can just scroll down in the readme as 11244 08:03:34,378 --> 08:03:41,520 well so first I want you to navigate to 11245 08:03:37,680 --> 08:03:43,200 zero five leverage JavaScript and you're 11246 08:03:41,520 --> 08:03:45,540 looking for the starter 11247 08:03:43,200 --> 08:03:47,940 and in there we basically want to set up 11248 08:03:45,540 --> 08:03:50,660 the component now before we set up the 11249 08:03:47,940 --> 08:03:54,660 component I just want you to navigate to 11250 08:03:50,660 --> 08:03:57,240 data.js and take a look we have this 11251 08:03:54,660 --> 08:04:00,660 people array so this is what we're going 11252 08:03:57,240 --> 08:04:03,660 to be importing into a list one so 11253 08:04:00,660 --> 08:04:05,878 whenever I say people just think of this 11254 08:04:03,660 --> 08:04:10,020 array and as you can see in here we 11255 08:04:05,878 --> 08:04:13,320 basically have list of objects and each 11256 08:04:10,020 --> 08:04:17,040 object represents the person and the 11257 08:04:13,320 --> 08:04:19,280 question here is that some object 11258 08:04:17,040 --> 08:04:24,660 have the properties for example nickname 11259 08:04:19,280 --> 08:04:27,780 and the images URL and some don't and 11260 08:04:24,660 --> 08:04:30,840 before you wonder why is that the setup 11261 08:04:27,780 --> 08:04:33,120 because this is I wouldn't say quite 11262 08:04:30,840 --> 08:04:35,340 typical but don't be surprised if you 11263 08:04:33,120 --> 08:04:37,620 work with an API and that's the case 11264 08:04:35,340 --> 08:04:39,840 again I'm not saying that every API has 11265 08:04:37,620 --> 08:04:42,298 this issue where basically the data is 11266 08:04:39,840 --> 08:04:44,820 missing but you will run it through some 11267 08:04:42,298 --> 08:04:48,058 instances where essentially yes 11268 08:04:44,820 --> 08:04:50,580 some items have certain properties and 11269 08:04:48,058 --> 08:04:52,860 some don't and also it happens quite 11270 08:04:50,580 --> 08:04:56,040 often when you work with headless cmss 11271 08:04:52,860 --> 08:04:58,798 essentially a nice graphical interface 11272 08:04:56,040 --> 08:05:00,718 where you can add your own data and for 11273 08:04:58,798 --> 08:05:03,120 some weird reason let's say you forgot 11274 08:05:00,718 --> 08:05:05,638 to add that data that quite often 11275 08:05:03,120 --> 08:05:07,798 happens with an images trust me on this 11276 08:05:05,638 --> 08:05:09,660 one for sure and if you're wondering why 11277 08:05:07,798 --> 08:05:13,200 we have this weird nested structure 11278 08:05:09,660 --> 08:05:15,240 again when it comes to images don't be 11279 08:05:13,200 --> 08:05:18,180 surprised if you actually see this in 11280 08:05:15,240 --> 08:05:21,420 the real world it's not as 11281 08:05:18,180 --> 08:05:23,218 flat as you might expect so you're not 11282 08:05:21,420 --> 08:05:27,420 going to have a bunch of items with just 11283 08:05:23,218 --> 08:05:30,898 okay name nickname and image no when it 11284 08:05:27,420 --> 08:05:34,138 comes to real apis yes you have this 11285 08:05:30,898 --> 08:05:36,478 nested sometimes weird structure so the 11286 08:05:34,138 --> 08:05:38,340 sooner we get comfortable with that the 11287 08:05:36,478 --> 08:05:40,860 better it's going to be in the long run 11288 08:05:38,340 --> 08:05:42,120 so hopefully this is clear hopefully you 11289 08:05:40,860 --> 08:05:44,820 haven't turned off 11290 08:05:42,120 --> 08:05:47,760 since I was just ranting here about the 11291 08:05:44,820 --> 08:05:50,100 array and now let's navigate back so 11292 08:05:47,760 --> 08:05:52,200 effectively this is the challenge we 11293 08:05:50,100 --> 08:05:55,260 want to create a new component I'm going 11294 08:05:52,200 --> 08:05:58,620 to call this list call this I don't know 11295 08:05:55,260 --> 08:06:01,440 orange still going to work and in the 11296 08:05:58,620 --> 08:06:03,540 list I want to import the people all 11297 08:06:01,440 --> 08:06:06,680 right again from the data I just showed 11298 08:06:03,540 --> 08:06:09,898 you and I just want you to iterate over 11299 08:06:06,680 --> 08:06:11,878 and render name that's it let's just 11300 08:06:09,898 --> 08:06:14,820 start very simply 11301 08:06:11,878 --> 08:06:16,260 we'll display the name now once 11302 08:06:14,820 --> 08:06:18,478 everything is correct once you can see 11303 08:06:16,260 --> 08:06:21,718 the name in the browser 11304 08:06:18,478 --> 08:06:23,340 after that I want to set this one up in 11305 08:06:21,718 --> 08:06:25,558 a separate component basically what 11306 08:06:23,340 --> 08:06:28,740 we're turning and this is a good use 11307 08:06:25,558 --> 08:06:30,718 case to try glean extension now you 11308 08:06:28,740 --> 08:06:33,240 don't have to but I will I'll just 11309 08:06:30,718 --> 08:06:36,660 showcase how the extension works and 11310 08:06:33,240 --> 08:06:38,940 then in the person try to render all 11311 08:06:36,660 --> 08:06:40,020 three properties and I'm sorry this is a 11312 08:06:38,940 --> 08:06:41,638 mistake we're not going to be looking 11313 08:06:40,020 --> 08:06:45,000 for a sister we're going to be looking 11314 08:06:41,638 --> 08:06:47,580 for the image so initially when I was 11315 08:06:45,000 --> 08:06:49,200 building this example I used sister but 11316 08:06:47,580 --> 08:06:52,440 in fact we're looking for the image and 11317 08:06:49,200 --> 08:06:55,020 as far as your question do I effectively 11318 08:06:52,440 --> 08:06:57,718 need to get this URL yes you're 11319 08:06:55,020 --> 08:07:00,958 absolutely correct so in order to show 11320 08:06:57,718 --> 08:07:04,500 the image you'll have to figure out how 11321 08:07:00,958 --> 08:07:05,700 to access the URL which is quite nested 11322 08:07:04,500 --> 08:07:08,638 as you can see 11323 08:07:05,700 --> 08:07:10,860 and if everything is correct yes there 11324 08:07:08,638 --> 08:07:13,020 will be a bug effectively that's the 11325 08:07:10,860 --> 08:07:16,558 point of these videos to show you how we 11326 08:07:13,020 --> 08:07:18,958 can avoid such bugs so try to work on a 11327 08:07:16,558 --> 08:07:21,298 challenge and whenever you're ready to 11328 08:07:18,958 --> 08:07:24,180 compare the results resume with the 11329 08:07:21,298 --> 08:07:27,780 videos Okay so let's start cracking 11330 08:07:24,180 --> 08:07:29,638 first I want to navigate to the star and 11331 08:07:27,780 --> 08:07:31,978 I'm going to create that list so as you 11332 08:07:29,638 --> 08:07:36,660 can see the star is empty and I simply 11333 08:07:31,978 --> 08:07:38,160 want to go list j s x in here let's grab 11334 08:07:36,660 --> 08:07:41,760 the data 11335 08:07:38,160 --> 08:07:44,040 so we're looking for people array in the 11336 08:07:41,760 --> 08:07:46,920 data I'm pretty sure it's going to give 11337 08:07:44,040 --> 08:07:49,920 me an auto import so I just want to set 11338 08:07:46,920 --> 08:07:52,860 up the list first and then inside of it 11339 08:07:49,920 --> 08:07:55,860 I want to iterate over data so where I 11340 08:07:52,860 --> 08:07:58,558 have the div I'll simply go with an 11341 08:07:55,860 --> 08:08:01,080 expression and I'll try to type people 11342 08:07:58,558 --> 08:08:04,080 and if everything is correct I should 11343 08:08:01,080 --> 08:08:07,558 see them yep as you can see everything 11344 08:08:04,080 --> 08:08:09,420 Works nicely now I have the people okay 11345 08:08:07,558 --> 08:08:11,100 that's good it's going to be an array so 11346 08:08:09,420 --> 08:08:14,280 I do want to iterate over 11347 08:08:11,100 --> 08:08:16,558 so let's go here with map and I'll 11348 08:08:14,280 --> 08:08:18,420 reference each and every item as a 11349 08:08:16,558 --> 08:08:21,058 person and then as far as the return 11350 08:08:18,420 --> 08:08:23,820 like I said simply for now we want to go 11351 08:08:21,058 --> 08:08:27,240 with person.name because that's the 11352 08:08:23,820 --> 08:08:28,680 property that all of the items have as a 11353 08:08:27,240 --> 08:08:31,940 quick send out 11354 08:08:28,680 --> 08:08:34,680 when it comes to key you can actually 11355 08:08:31,940 --> 08:08:36,600 use the name in this case as well so 11356 08:08:34,680 --> 08:08:39,120 remember all this time I've been showing 11357 08:08:36,600 --> 08:08:41,700 you following approach where if you have 11358 08:08:39,120 --> 08:08:42,840 the div or whatever element you want to 11359 08:08:41,700 --> 08:08:45,240 return 11360 08:08:42,840 --> 08:08:46,920 um if this is where we're iterating we 11361 08:08:45,240 --> 08:08:49,440 want to go with key of course and then 11362 08:08:46,920 --> 08:08:50,940 we want to go with that unique one so up 11363 08:08:49,440 --> 08:08:53,700 to this point all the time we have been 11364 08:08:50,940 --> 08:08:56,280 using the ID keep in mind that in this 11365 08:08:53,700 --> 08:08:58,320 case let's imagine that we don't have 11366 08:08:56,280 --> 08:09:01,440 the ID over here 11367 08:08:58,320 --> 08:09:04,440 we're still loving the name and at least 11368 08:09:01,440 --> 08:09:06,540 in this case since I have four items all 11369 08:09:04,440 --> 08:09:09,478 of them are unique so yes 11370 08:09:06,540 --> 08:09:11,580 you don't always have to go with ID but 11371 08:09:09,478 --> 08:09:14,218 it's going to be somewhat common because 11372 08:09:11,580 --> 08:09:16,500 with the case of ID it's always going to 11373 08:09:14,218 --> 08:09:19,440 be unique so here I set up the key it's 11374 08:09:16,500 --> 08:09:21,058 going to be equal to person ID and then 11375 08:09:19,440 --> 08:09:24,240 I want to render that I want to go with 11376 08:09:21,058 --> 08:09:27,780 person dot name let's save it now I want 11377 08:09:24,240 --> 08:09:30,000 to navigate back to app jsx I think I'm 11378 08:09:27,780 --> 08:09:32,520 just gonna do it right after the hearing 11379 08:09:30,000 --> 08:09:35,340 2 because why not and in here I'm 11380 08:09:32,520 --> 08:09:37,440 looking for less than again I'll rely on 11381 08:09:35,340 --> 08:09:39,840 the Auto Import like I said if 11382 08:09:37,440 --> 08:09:41,580 everything is correct this is what we 11383 08:09:39,840 --> 08:09:44,820 should see on the screen 11384 08:09:41,580 --> 08:09:47,340 great start and now let's navigate back 11385 08:09:44,820 --> 08:09:50,100 to the list and now try to set up this 11386 08:09:47,340 --> 08:09:52,620 one as a separate component now if you 11387 08:09:50,100 --> 08:09:56,040 want you can create the file yourself 11388 08:09:52,620 --> 08:09:59,638 and all that or you can utilize the 11389 08:09:56,040 --> 08:10:01,138 extension I showcased I believe in the 11390 08:09:59,638 --> 08:10:04,320 previous section 11391 08:10:01,138 --> 08:10:06,660 basically which one was that this one 11392 08:10:04,320 --> 08:10:08,638 over here the project structure so I 11393 08:10:06,660 --> 08:10:10,920 want to hover over it I'm looking for 11394 08:10:08,638 --> 08:10:14,760 that light bulb and I want to go here 11395 08:10:10,920 --> 08:10:16,798 with extract component to the file and 11396 08:10:14,760 --> 08:10:19,200 in here we just need to look for the 11397 08:10:16,798 --> 08:10:21,298 correct path we need to make sure that 11398 08:10:19,200 --> 08:10:24,660 this is the one that we click okay 11399 08:10:21,298 --> 08:10:27,058 awesome we want to create a new file and 11400 08:10:24,660 --> 08:10:29,340 we want to come up with a name so in my 11401 08:10:27,058 --> 08:10:33,120 case I'm going to call this person and 11402 08:10:29,340 --> 08:10:35,520 then j s x and check it out again we 11403 08:10:33,120 --> 08:10:37,620 right away have the import we right away 11404 08:10:35,520 --> 08:10:41,458 have the component and then if you check 11405 08:10:37,620 --> 08:10:44,458 the person notice we right away have the 11406 08:10:41,458 --> 08:10:47,520 prop set up as well now as you can see 11407 08:10:44,458 --> 08:10:49,740 there are some issues here so basically 11408 08:10:47,520 --> 08:10:53,040 either all I have to pass the person 11409 08:10:49,740 --> 08:10:54,240 from the list or in my case since I'm 11410 08:10:53,040 --> 08:10:56,700 going to be spreading out I'm actually 11411 08:10:54,240 --> 08:11:00,120 going to remove these values over here 11412 08:10:56,700 --> 08:11:02,398 so it's not perfect but at least it gets 11413 08:11:00,120 --> 08:11:05,340 us halfway correct so let's just 11414 08:11:02,398 --> 08:11:08,940 navigate back and in here essentially 11415 08:11:05,340 --> 08:11:11,100 again I want to set up that key so I 11416 08:11:08,940 --> 08:11:13,558 guess this is the case where I can just 11417 08:11:11,100 --> 08:11:17,820 simply grab this one 11418 08:11:13,558 --> 08:11:21,298 over here person and like I said we can 11419 08:11:17,820 --> 08:11:23,458 use the name over here like so and then 11420 08:11:21,298 --> 08:11:25,260 after that I want to pass the entire 11421 08:11:23,458 --> 08:11:26,878 person 11422 08:11:25,260 --> 08:11:30,660 so I'm going to be spreading out all of 11423 08:11:26,878 --> 08:11:33,540 the properties we can most likely save I 11424 08:11:30,660 --> 08:11:35,820 believe and then back in the person 11425 08:11:33,540 --> 08:11:36,780 I'm not going to be accessing person in 11426 08:11:35,820 --> 08:11:39,120 such a way 11427 08:11:36,780 --> 08:11:41,700 I'm gonna go one by one so I'm going to 11428 08:11:39,120 --> 08:11:44,100 go with name then I'm also looking for 11429 08:11:41,700 --> 08:11:47,040 nickname that's the second property that 11430 08:11:44,100 --> 08:11:49,558 I want to showcase and I also want to 11431 08:11:47,040 --> 08:11:53,100 get that URL 11432 08:11:49,558 --> 08:11:55,378 now in order to access that URL what 11433 08:11:53,100 --> 08:11:58,978 I'll have to do well I'm gonna go with 11434 08:11:55,378 --> 08:12:00,540 images correct since that's the array 11435 08:11:58,978 --> 08:12:02,580 over here 11436 08:12:00,540 --> 08:12:05,638 and for now let's just leave it the way 11437 08:12:02,580 --> 08:12:08,638 it is and let's decide what are we gonna 11438 08:12:05,638 --> 08:12:11,398 be returning so in here there's going to 11439 08:12:08,638 --> 08:12:13,500 be a div and let's just try it out with 11440 08:12:11,398 --> 08:12:16,558 a name because like I said there's going 11441 08:12:13,500 --> 08:12:18,120 to be a bug So eventually we'll have an 11442 08:12:16,558 --> 08:12:20,520 error so for now let me just see whether 11443 08:12:18,120 --> 08:12:22,440 everything works yep so I can nicely 11444 08:12:20,520 --> 08:12:24,600 display the name now let's go with 11445 08:12:22,440 --> 08:12:27,420 nickname so right after that we want to 11446 08:12:24,600 --> 08:12:30,298 go with paragraph and I'm going to come 11447 08:12:27,420 --> 08:12:32,218 up with whatever value so nickname and 11448 08:12:30,298 --> 08:12:33,360 as I say note this is now what I wanted 11449 08:12:32,218 --> 08:12:36,360 to do 11450 08:12:33,360 --> 08:12:39,180 so let me just take this one out 11451 08:12:36,360 --> 08:12:41,398 and then I'll place it here before the 11452 08:12:39,180 --> 08:12:42,478 curlies and now let me access the 11453 08:12:41,398 --> 08:12:44,700 property 11454 08:12:42,478 --> 08:12:47,520 and we already have a little bit of 11455 08:12:44,700 --> 08:12:51,120 issue where notice only the stud muffin 11456 08:12:47,520 --> 08:12:53,280 is displayed since only Bob as this 11457 08:12:51,120 --> 08:12:55,440 particular property and again this is 11458 08:12:53,280 --> 08:12:58,680 something that we're gonna fix in the 11459 08:12:55,440 --> 08:13:01,558 following video and when we'll 11460 08:12:58,680 --> 08:13:03,958 definitely get a bug is when we'll try 11461 08:13:01,558 --> 08:13:07,138 to get the image now I'll purposely 11462 08:13:03,958 --> 08:13:10,320 access it above since it's just going to 11463 08:13:07,138 --> 08:13:14,280 save me essentially setting up the whole 11464 08:13:10,320 --> 08:13:16,860 thing in the jsx so if I have a bunch of 11465 08:13:14,280 --> 08:13:19,500 nested properties essentially that is my 11466 08:13:16,860 --> 08:13:20,940 preference where I do it above the jsx 11467 08:13:19,500 --> 08:13:23,280 keep in mind that of course you can do 11468 08:13:20,940 --> 08:13:24,840 it in the GSX as well as far as the 11469 08:13:23,280 --> 08:13:27,780 alternative I'm going to go here with 11470 08:13:24,840 --> 08:13:30,058 the name and I'm sorry I forgot to 11471 08:13:27,780 --> 08:13:32,040 actually mention a 11472 08:13:30,058 --> 08:13:34,920 tiny suggestion you probably want to go 11473 08:13:32,040 --> 08:13:37,558 with a small width my bad so let's go 11474 08:13:34,920 --> 08:13:39,660 here so if you have massive images just 11475 08:13:37,558 --> 08:13:42,180 add in-line Styles like I'm doing right 11476 08:13:39,660 --> 08:13:45,420 now so we're looking for 50 pixels and 11477 08:13:42,180 --> 08:13:47,218 now let me grab that image so let's go 11478 08:13:45,420 --> 08:13:50,100 over here const I'm going to call this 11479 08:13:47,218 --> 08:13:51,840 IMG and whatever we want to do well we 11480 08:13:50,100 --> 08:13:54,298 want to go with images correct that's 11481 08:13:51,840 --> 08:13:56,580 the area over here and 11482 08:13:54,298 --> 08:13:59,280 I'm looking for the first item 11483 08:13:56,580 --> 08:14:01,440 and there I'll have the small so that's 11484 08:13:59,280 --> 08:14:03,478 the object itself and then in there I 11485 08:14:01,440 --> 08:14:05,160 have the URL so one by one let's 11486 08:14:03,478 --> 08:14:06,360 navigate there again we're looking for 11487 08:14:05,160 --> 08:14:10,378 the first item 11488 08:14:06,360 --> 08:14:11,160 then I want to grab the small object in 11489 08:14:10,378 --> 08:14:13,260 there 11490 08:14:11,160 --> 08:14:16,260 I'll have the URL property and that is 11491 08:14:13,260 --> 08:14:18,360 going to be equal to my image and let's 11492 08:14:16,260 --> 08:14:21,780 try to access it over here 11493 08:14:18,360 --> 08:14:24,420 and like I said we'll have big fat error 11494 08:14:21,780 --> 08:14:26,398 so in the following videos 11495 08:14:24,420 --> 08:14:29,280 we'll work on these errors and we'll see 11496 08:14:26,398 --> 08:14:30,420 how we can leverage straight up 11497 08:14:29,280 --> 08:14:33,898 JavaScript 11498 08:14:30,420 --> 08:14:36,360 to fix it and have something displayed 11499 08:14:33,898 --> 08:14:38,100 on screen and before we cover the 11500 08:14:36,360 --> 08:14:41,820 solution let me just quickly mention 11501 08:14:38,100 --> 08:14:43,798 that in the readme you'll find info on 11502 08:14:41,820 --> 08:14:47,760 default values 11503 08:14:43,798 --> 08:14:50,520 and also the optional training so 11504 08:14:47,760 --> 08:14:52,920 both features will implement 11505 08:14:50,520 --> 08:14:54,840 in this video and if you want to find 11506 08:14:52,920 --> 08:14:57,360 out more info if you're not familiar 11507 08:14:54,840 --> 08:15:00,420 with them you can always utilize the 11508 08:14:57,360 --> 08:15:01,978 readme so back in the person 11509 08:15:00,420 --> 08:15:04,320 let's start with the nickname that's 11510 08:15:01,978 --> 08:15:07,260 going to be a little bit easier so let 11511 08:15:04,320 --> 08:15:10,500 me just comment out the image just so we 11512 08:15:07,260 --> 08:15:12,540 don't have that massive error and notice 11513 08:15:10,500 --> 08:15:15,240 over here we still have it because of 11514 08:15:12,540 --> 08:15:16,740 course I am also accessing the image 11515 08:15:15,240 --> 08:15:18,840 over here so I'll have to do it in two 11516 08:15:16,740 --> 08:15:20,520 places let's comment this one out and 11517 08:15:18,840 --> 08:15:21,718 for now let me just remove those 11518 08:15:20,520 --> 08:15:24,240 messages 11519 08:15:21,718 --> 08:15:25,680 and as far as the nickname what is the 11520 08:15:24,240 --> 08:15:27,540 issue well again 11521 08:15:25,680 --> 08:15:30,000 not all of them have that property 11522 08:15:27,540 --> 08:15:32,520 what's the solution well one of the 11523 08:15:30,000 --> 08:15:33,840 solutions is obviously to use the poor 11524 08:15:32,520 --> 08:15:36,000 Operator just like we covered in 11525 08:15:33,840 --> 08:15:40,080 conditional rendering correct 11526 08:15:36,000 --> 08:15:42,478 so I can go here and say shake and bake 11527 08:15:40,080 --> 08:15:44,760 and what do you know now all of them 11528 08:15:42,478 --> 08:15:47,340 have at least some kind of value 11529 08:15:44,760 --> 08:15:49,798 okay that's good but we can also utilize 11530 08:15:47,340 --> 08:15:52,500 the fact that when it comes to functions 11531 08:15:49,798 --> 08:15:54,240 in JavaScript we can provide right away 11532 08:15:52,500 --> 08:15:59,280 a default value 11533 08:15:54,240 --> 08:16:02,580 so if you're setting up a function and 11534 08:15:59,280 --> 08:16:04,860 you're passing in the parameter you can 11535 08:16:02,580 --> 08:16:08,218 also provide a default value just in 11536 08:16:04,860 --> 08:16:10,200 case it's not provided shown here I know 11537 08:16:08,218 --> 08:16:12,298 that I'm going to be looking for a 11538 08:16:10,200 --> 08:16:15,420 nickname however if it doesn't exist 11539 08:16:12,298 --> 08:16:19,200 what can we do well you can set it equal 11540 08:16:15,420 --> 08:16:21,420 to shake and bake 11541 08:16:19,200 --> 08:16:23,340 and what do you know now the result is 11542 08:16:21,420 --> 08:16:25,500 going to be exactly the same again this 11543 08:16:23,340 --> 08:16:28,680 is just straight up JavaScript something 11544 08:16:25,500 --> 08:16:31,020 that you can utilize as you're setting 11545 08:16:28,680 --> 08:16:32,580 up your react components now the second 11546 08:16:31,020 --> 08:16:35,760 one is 11547 08:16:32,580 --> 08:16:39,360 sort of the same the problem again we're 11548 08:16:35,760 --> 08:16:41,160 having in here if we comment out we have 11549 08:16:39,360 --> 08:16:44,638 that annoying 11550 08:16:41,160 --> 08:16:47,100 error which essentially says hey you 11551 08:16:44,638 --> 08:16:50,340 cannot access 11552 08:16:47,100 --> 08:16:53,120 the properties from nothing 11553 08:16:50,340 --> 08:16:58,320 why well because if we go back again 11554 08:16:53,120 --> 08:17:00,840 only the third item has the images 11555 08:16:58,320 --> 08:17:03,360 so if I'm trying to do this for Bob 11556 08:17:00,840 --> 08:17:04,558 Pierre and all that it's not going to 11557 08:17:03,360 --> 08:17:07,860 work 11558 08:17:04,558 --> 08:17:10,920 so what word can do well we can do it 11559 08:17:07,860 --> 08:17:12,958 the long way so bear with me I'll show 11560 08:17:10,920 --> 08:17:15,000 you basically what we were doing before 11561 08:17:12,958 --> 08:17:17,580 the 11562 08:17:15,000 --> 08:17:20,760 optional training and then I'll show you 11563 08:17:17,580 --> 08:17:21,780 effectively why optional chaining is so 11564 08:17:20,760 --> 08:17:24,420 cool 11565 08:17:21,780 --> 08:17:27,000 and the way it worked before 11566 08:17:24,420 --> 08:17:30,420 we simply needed to 11567 08:17:27,000 --> 08:17:32,878 repeat bunch of and operators 11568 08:17:30,420 --> 08:17:34,978 so in here I'm going to go with image 11569 08:17:32,878 --> 08:17:36,600 and I don't think I'm going to leave 11570 08:17:34,978 --> 08:17:39,360 this one for your reference I don't 11571 08:17:36,600 --> 08:17:42,298 think there's any need so first I go 11572 08:17:39,360 --> 08:17:45,478 with images and I'll say listen 11573 08:17:42,298 --> 08:17:46,440 if the images exist then look for the 11574 08:17:45,478 --> 08:17:50,340 first one 11575 08:17:46,440 --> 08:17:53,040 so essentially I'm using the N1 now if 11576 08:17:50,340 --> 08:17:55,378 there is a first one then I'll look for 11577 08:17:53,040 --> 08:17:57,540 small so essentially one by one you just 11578 08:17:55,378 --> 08:18:00,298 keep repeating them so what I'm saying 11579 08:17:57,540 --> 08:18:04,378 here is if images exist get me the first 11580 08:18:00,298 --> 08:18:07,558 one now if the first one exists then get 11581 08:18:04,378 --> 08:18:10,260 me the small property so in here I just 11582 08:18:07,558 --> 08:18:11,700 want to take this copy and paste and yep 11583 08:18:10,260 --> 08:18:14,160 I want to say hey 11584 08:18:11,700 --> 08:18:16,020 if it's there then grab me the small one 11585 08:18:14,160 --> 08:18:19,500 and you can probably already guess that 11586 08:18:16,020 --> 08:18:22,020 yes in order to get the URL what do we 11587 08:18:19,500 --> 08:18:24,478 need to do we need to copy and paste and 11588 08:18:22,020 --> 08:18:29,218 then we need to just chain essentially 11589 08:18:24,478 --> 08:18:32,160 here this URL like so so we grab it here 11590 08:18:29,218 --> 08:18:35,520 and notice how we nicely don't have the 11591 08:18:32,160 --> 08:18:40,200 error so essentially if I refresh I'll 11592 08:18:35,520 --> 08:18:43,138 see no errors in console and also 11593 08:18:40,200 --> 08:18:45,898 the one that has the image 11594 08:18:43,138 --> 08:18:49,200 is going to have that in the jsx as well 11595 08:18:45,898 --> 08:18:51,898 so once we save notice I have 11596 08:18:49,200 --> 08:18:53,040 person displayed now rest of them don't 11597 08:18:51,898 --> 08:18:54,718 have it 11598 08:18:53,040 --> 08:18:56,398 so that's something that we need to work 11599 08:18:54,718 --> 08:19:00,120 on but at least the one that has the 11600 08:18:56,398 --> 08:19:01,820 image well I'm nicely rendering that in 11601 08:19:00,120 --> 08:19:05,218 the jsx 11602 08:19:01,820 --> 08:19:08,100 and essentially let's first worry about 11603 08:19:05,218 --> 08:19:10,440 how we can shorten this code and then 11604 08:19:08,100 --> 08:19:14,160 we'll worry about how we can display at 11605 08:19:10,440 --> 08:19:16,138 least something in the browser so first 11606 08:19:14,160 --> 08:19:19,860 yes this is awesome 11607 08:19:16,138 --> 08:19:23,700 but I mean it would be nicer if we could 11608 08:19:19,860 --> 08:19:26,340 just get it done in less lines of code 11609 08:19:23,700 --> 08:19:29,398 essentially less characters and the way 11610 08:19:26,340 --> 08:19:32,398 we can do that is by using optional 11611 08:19:29,398 --> 08:19:36,240 chaining in JavaScript so here we go 11612 08:19:32,398 --> 08:19:38,040 with const and then again IMG and if 11613 08:19:36,240 --> 08:19:41,040 you're wondering this code is going to 11614 08:19:38,040 --> 08:19:43,200 be again located in the readme so you 11615 08:19:41,040 --> 08:19:44,878 can always reference it so we want to go 11616 08:19:43,200 --> 08:19:46,320 here with images and then we just go 11617 08:19:44,878 --> 08:19:48,298 with question mark 11618 08:19:46,320 --> 08:19:50,398 and if 11619 08:19:48,298 --> 08:19:53,280 the property 11620 08:19:50,398 --> 08:19:55,798 exists basically if it's not null then 11621 08:19:53,280 --> 08:19:58,320 everything is good if not then we'll 11622 08:19:55,798 --> 08:20:00,540 right away just get undefined this is 11623 08:19:58,320 --> 08:20:02,580 also awesome where we don't have that 11624 08:20:00,540 --> 08:20:04,500 annoying hey you cannot get properties 11625 08:20:02,580 --> 08:20:07,440 out of null so essentially what I'm 11626 08:20:04,500 --> 08:20:10,440 saying here if images exist awesome get 11627 08:20:07,440 --> 08:20:13,740 me the first one if that exists then get 11628 08:20:10,440 --> 08:20:16,798 me small if that exists then actually 11629 08:20:13,740 --> 08:20:19,260 get me the URL now 11630 08:20:16,798 --> 08:20:20,160 this is great but it doesn't solve the 11631 08:20:19,260 --> 08:20:22,920 issue 11632 08:20:20,160 --> 08:20:26,760 where at the moment notice only one of 11633 08:20:22,920 --> 08:20:29,040 them has the image and it's probably a 11634 08:20:26,760 --> 08:20:31,680 nicer setup if you have at least some 11635 08:20:29,040 --> 08:20:32,638 kind of default image correct and not to 11636 08:20:31,680 --> 08:20:34,798 do that 11637 08:20:32,638 --> 08:20:38,280 you actually need to look at the assets 11638 08:20:34,798 --> 08:20:40,260 and you'll see that I provided some SVG 11639 08:20:38,280 --> 08:20:42,260 and yes basically this is something that 11640 08:20:40,260 --> 08:20:45,120 you'll need to set up manually yourself 11641 08:20:42,260 --> 08:20:46,860 whether that is in the cloud Mary 11642 08:20:45,120 --> 08:20:49,558 whether that is locally 11643 08:20:46,860 --> 08:20:52,378 you'll need somewhere a default image so 11644 08:20:49,558 --> 08:20:54,840 in my case that's the default Avatar in 11645 08:20:52,378 --> 08:20:56,340 the asset so now let's navigate back to 11646 08:20:54,840 --> 08:20:58,978 the person and we want to import that 11647 08:20:56,340 --> 08:21:01,680 now please keep in mind that we're 11648 08:20:58,978 --> 08:21:05,100 sitting in a source so we need to import 11649 08:21:01,680 --> 08:21:07,440 we need to name the variable and then we 11650 08:21:05,100 --> 08:21:09,600 need to provide the path so in this case 11651 08:21:07,440 --> 08:21:13,260 I'm going to go with this one I'll call 11652 08:21:09,600 --> 08:21:16,558 this Avatar and then from and now let's 11653 08:21:13,260 --> 08:21:19,020 go one level up then two levels up three 11654 08:21:16,558 --> 08:21:21,420 levels up we're looking in the assets 11655 08:21:19,020 --> 08:21:25,020 and we want to go default and then 11656 08:21:21,420 --> 08:21:27,420 Avatar and then SVG 11657 08:21:25,020 --> 08:21:31,138 and now we can utilize the or operator 11658 08:21:27,420 --> 08:21:33,058 where I can say Hey listen check four 11659 08:21:31,138 --> 08:21:38,458 Images check for the first one check for 11660 08:21:33,058 --> 08:21:42,180 small and URL and if it's there awesome 11661 08:21:38,458 --> 08:21:45,000 return this one if not well just set it 11662 08:21:42,180 --> 08:21:49,138 equal to my default Avatar and what do 11663 08:21:45,000 --> 08:21:51,298 you know now we have a list and if the 11664 08:21:49,138 --> 08:21:54,120 item doesn't have the image at least we 11665 08:21:51,298 --> 08:21:56,878 display something now quite often as 11666 08:21:54,120 --> 08:21:59,878 you're looking at the optional chaining 11667 08:21:56,878 --> 08:22:02,398 code you'll see this approach as well 11668 08:21:59,878 --> 08:22:03,298 where essentially let me comment this 11669 08:22:02,398 --> 08:22:06,958 one out 11670 08:22:03,298 --> 08:22:10,020 and you'll see this one and effectively 11671 08:22:06,958 --> 08:22:13,138 this operator let me go back to read me 11672 08:22:10,020 --> 08:22:16,020 I left it here for your reference so 11673 08:22:13,138 --> 08:22:18,478 it's this one that's the operator you're 11674 08:22:16,020 --> 08:22:20,398 looking for now I'm not gonna go into 11675 08:22:18,478 --> 08:22:22,680 great detail what's the difference there 11676 08:22:20,398 --> 08:22:24,840 extremely similar uh if you're 11677 08:22:22,680 --> 08:22:26,878 interested then please utilize the 11678 08:22:24,840 --> 08:22:29,340 search engine again 11679 08:22:26,878 --> 08:22:32,040 instead of the or you'll see these two 11680 08:22:29,340 --> 08:22:34,020 question marks and that's this operator 11681 08:22:32,040 --> 08:22:37,200 and if you want to find out more info 11682 08:22:34,020 --> 08:22:39,660 just please go to your favorite search 11683 08:22:37,200 --> 08:22:41,580 engine and pretty much we're done with 11684 08:22:39,660 --> 08:22:44,878 the challenge as you can see we can 11685 08:22:41,580 --> 08:22:48,000 utilize straight up JavaScript 11686 08:22:44,878 --> 08:22:50,458 to essentially fix the potential bugs 11687 08:22:48,000 --> 08:22:53,280 that we might encounter once we start 11688 08:22:50,458 --> 08:22:55,440 working with apis and I know that some 11689 08:22:53,280 --> 08:22:57,958 of you think this is totally weird 11690 08:22:55,440 --> 08:22:59,940 example there's no way there would be 11691 08:22:57,958 --> 08:23:02,340 such structure and then there's no way 11692 08:22:59,940 --> 08:23:04,620 that properties would be missing I mean 11693 08:23:02,340 --> 08:23:07,020 he's totally making this up and again I 11694 08:23:04,620 --> 08:23:10,500 don't want to be a bearer of bad news 11695 08:23:07,020 --> 08:23:12,718 and I'm not saying that every API is 11696 08:23:10,500 --> 08:23:15,240 going to have the issues but once you 11697 08:23:12,718 --> 08:23:18,378 start working with external apis just be 11698 08:23:15,240 --> 08:23:21,058 prepared that structure 11699 08:23:18,378 --> 08:23:24,240 might not be what you 11700 08:23:21,058 --> 08:23:25,740 expect it's going to be pretty nested I 11701 08:23:24,240 --> 08:23:29,160 can tell you that right from the get-go 11702 08:23:25,740 --> 08:23:31,440 and also here and there properties are 11703 08:23:29,160 --> 08:23:34,080 going to be missing so if you ever seen 11704 08:23:31,440 --> 08:23:36,420 a console JavaScript complaining that it 11705 08:23:34,080 --> 08:23:38,760 cannot access certain property right 11706 08:23:36,420 --> 08:23:41,820 away think ding ding ding ding ding 11707 08:23:38,760 --> 08:23:43,978 there's a property missing so even 11708 08:23:41,820 --> 08:23:47,040 though I'm expecting something 11709 08:23:43,978 --> 08:23:48,660 back from the API and I'm trying to 11710 08:23:47,040 --> 08:23:52,200 render it here 11711 08:23:48,660 --> 08:23:55,020 well it's not coming through and as far 11712 08:23:52,200 --> 08:23:58,320 as suggestions my always suggestion is 11713 08:23:55,020 --> 08:24:01,978 to limit the amount of items you're 11714 08:23:58,320 --> 08:24:04,320 rendering so if I hit this bug yes one 11715 08:24:01,978 --> 08:24:06,660 by one effectively I comment out the 11716 08:24:04,320 --> 08:24:09,718 stuff and then I go back and check which 11717 08:24:06,660 --> 08:24:12,718 value is missing so normally I would log 11718 08:24:09,718 --> 08:24:15,120 the person back here in the list and 11719 08:24:12,718 --> 08:24:18,240 then I would go over where potentially 11720 08:24:15,120 --> 08:24:20,520 that property might be missing just to 11721 08:24:18,240 --> 08:24:22,978 Showcase that if we go here with log in 11722 08:24:20,520 --> 08:24:25,620 person we'll right away see that only 11723 08:24:22,978 --> 08:24:27,478 some of them have those values now of 11724 08:24:25,620 --> 08:24:29,940 course this is somewhat simple example 11725 08:24:27,478 --> 08:24:32,398 where I right away can see that but 11726 08:24:29,940 --> 08:24:35,218 normally this is the place where at 11727 08:24:32,398 --> 08:24:37,978 least I can start working on the problem 11728 08:24:35,218 --> 08:24:40,978 because this is going to give me at 11729 08:24:37,978 --> 08:24:44,280 least the idea of what data I'm getting 11730 08:24:40,978 --> 08:24:48,600 back awesome and next let's talk about 11731 08:24:44,280 --> 08:24:51,600 forms in react and we're going to start 11732 08:24:48,600 --> 08:24:54,540 working in this folder so we're going to 11733 08:24:51,600 --> 08:24:57,120 go to zero six forms and we'll start 11734 08:24:54,540 --> 08:24:59,878 with the first file however 11735 08:24:57,120 --> 08:25:01,260 since I want the examples to look 11736 08:24:59,878 --> 08:25:03,780 somewhat nice 11737 08:25:01,260 --> 08:25:07,080 we'll spend the first video just by 11738 08:25:03,780 --> 08:25:10,558 setting up the form with some 11739 08:25:07,080 --> 08:25:13,020 classes and labels and all that now if 11740 08:25:10,558 --> 08:25:16,378 you're not interested in doing that just 11741 08:25:13,020 --> 08:25:19,440 navigate to the readme look for 11742 08:25:16,378 --> 08:25:22,920 controlled inputs and basically copy and 11743 08:25:19,440 --> 08:25:24,780 paste this content so this component 11744 08:25:22,920 --> 08:25:26,580 copy and paste and set it up in the 11745 08:25:24,780 --> 08:25:28,620 controlled input essentially that's 11746 08:25:26,580 --> 08:25:30,600 going to be the first step now don't 11747 08:25:28,620 --> 08:25:32,398 worry we're not going to do that pretty 11748 08:25:30,600 --> 08:25:35,160 much for every file 11749 08:25:32,398 --> 08:25:37,558 I believe we have all here five of them 11750 08:25:35,160 --> 08:25:41,458 so essentially in the next videos we'll 11751 08:25:37,558 --> 08:25:44,458 just reuse this nice looking form 11752 08:25:41,458 --> 08:25:46,200 so first I want to navigate to app jsx 11753 08:25:44,458 --> 08:25:48,780 like I said we're looking for zero six 11754 08:25:46,200 --> 08:25:50,820 forms then starter and then controlled 11755 08:25:48,780 --> 08:25:53,360 inputs so that's what I have on the 11756 08:25:50,820 --> 08:25:57,000 screen now let's navigate over here 11757 08:25:53,360 --> 08:25:59,458 and effectively we just want to set up a 11758 08:25:57,000 --> 08:26:01,138 form and if you're wondering where the 11759 08:25:59,458 --> 08:26:04,200 classes are coming from that I'm about 11760 08:26:01,138 --> 08:26:07,500 to use well they're located over here 11761 08:26:04,200 --> 08:26:10,280 so if you look here in this file let's 11762 08:26:07,500 --> 08:26:13,080 say if we search for class of form 11763 08:26:10,280 --> 08:26:15,360 you'll see these Styles and then same 11764 08:26:13,080 --> 08:26:16,798 goes for label input and all that so if 11765 08:26:15,360 --> 08:26:19,920 you're interested of course 11766 08:26:16,798 --> 08:26:22,080 you can navigate over there and see what 11767 08:26:19,920 --> 08:26:24,718 styles I'm setting up 11768 08:26:22,080 --> 08:26:28,320 so let's go over here in the controlled 11769 08:26:24,718 --> 08:26:31,080 inputs we'll remove the heading 2. and 11770 08:26:28,320 --> 08:26:34,500 effectively we're going to return a 11771 08:26:31,080 --> 08:26:35,340 formal ad right away a class form as 11772 08:26:34,500 --> 08:26:38,100 well 11773 08:26:35,340 --> 08:26:41,398 inside of it let's place a hitting four 11774 08:26:38,100 --> 08:26:45,180 and let's say controlled input so that's 11775 08:26:41,398 --> 08:26:47,280 going to be our first topic after that I 11776 08:26:45,180 --> 08:26:49,978 want to set up a div with a classiform 11777 08:26:47,280 --> 08:26:52,620 row and you can think of it as a divider 11778 08:26:49,978 --> 08:26:55,200 so it's just going to add some nice 11779 08:26:52,620 --> 08:26:56,958 margin so let's go here with div let's 11780 08:26:55,200 --> 08:27:00,240 add a class of form 11781 08:26:56,958 --> 08:27:02,940 row and then inside of it first let's 11782 08:27:00,240 --> 08:27:05,280 set up a label again it's just a HTML 11783 08:27:02,940 --> 08:27:08,780 thing and notice over here it's going to 11784 08:27:05,280 --> 08:27:11,638 give this html4 so that's the HTML 11785 08:27:08,780 --> 08:27:13,378 attribute by the name of four and you 11786 08:27:11,638 --> 08:27:16,378 add that to the label and then once you 11787 08:27:13,378 --> 08:27:18,540 click on the label then it nicely sets 11788 08:27:16,378 --> 08:27:20,878 the input in the focus and you'll see 11789 08:27:18,540 --> 08:27:22,740 once we set it up how it works so in 11790 08:27:20,878 --> 08:27:25,020 here we want to provide the ID so 11791 08:27:22,740 --> 08:27:26,638 whatever ID we're about to set up on the 11792 08:27:25,020 --> 08:27:28,798 input so I know that my one is going to 11793 08:27:26,638 --> 08:27:31,978 be named so of course I'm going to set 11794 08:27:28,798 --> 08:27:35,160 this one up as name then we want to type 11795 08:27:31,978 --> 08:27:37,138 the text inside of the label and you 11796 08:27:35,160 --> 08:27:38,638 know what there's one thing missing and 11797 08:27:37,138 --> 08:27:41,638 that is the class 11798 08:27:38,638 --> 08:27:44,458 and for that we want to go with form and 11799 08:27:41,638 --> 08:27:47,458 label then we want to go with that input 11800 08:27:44,458 --> 08:27:50,100 and we'll start with the most basic one 11801 08:27:47,458 --> 08:27:52,978 the text input so let's go here let's 11802 08:27:50,100 --> 08:27:55,978 look for text name for now let's leave 11803 08:27:52,978 --> 08:27:58,260 it blank because I want to show you 11804 08:27:55,978 --> 08:28:00,000 what is going to be the use case so for 11805 08:27:58,260 --> 08:28:02,638 now let's not worry about it but yes 11806 08:28:00,000 --> 08:28:05,638 eventually we will use it and then here 11807 08:28:02,638 --> 08:28:08,298 we want to go with name so again these 11808 08:28:05,638 --> 08:28:11,700 two need to match and in react it's 11809 08:28:08,298 --> 08:28:16,320 html4 in normal HTML it's going to be 11810 08:28:11,700 --> 08:28:19,440 just A4 attribute and we do want to add 11811 08:28:16,320 --> 08:28:22,020 here a class so we'll go with class name 11812 08:28:19,440 --> 08:28:24,240 and that is going to be perform input 11813 08:28:22,020 --> 08:28:26,820 let's save it and this is what we should 11814 08:28:24,240 --> 08:28:28,978 see on the screen and again if these two 11815 08:28:26,820 --> 08:28:31,500 match then basically what happens notice 11816 08:28:28,978 --> 08:28:34,320 once you click on a label 11817 08:28:31,500 --> 08:28:37,200 it nicely sets the 11818 08:28:34,320 --> 08:28:39,000 Focus for the input and 11819 08:28:37,200 --> 08:28:41,398 since I also want to set up one for 11820 08:28:39,000 --> 08:28:43,320 password or email sorry password is 11821 08:28:41,398 --> 08:28:45,600 coming up for now let's just go with 11822 08:28:43,320 --> 08:28:48,718 email and we want to change those values 11823 08:28:45,600 --> 08:28:51,120 so everywhere where I have the name I'll 11824 08:28:48,718 --> 08:28:53,100 set it up as email and as far as type 11825 08:28:51,120 --> 08:28:55,020 yes it's also going to be an email so 11826 08:28:53,100 --> 08:28:57,600 let me select over here 11827 08:28:55,020 --> 08:28:59,820 let me go to email let's say that okay 11828 08:28:57,600 --> 08:29:02,040 we have the second input and lastly 11829 08:28:59,820 --> 08:29:04,200 let's just add that submit Button as 11830 08:29:02,040 --> 08:29:08,040 well so for that we want to go button 11831 08:29:04,200 --> 08:29:11,820 then we want to go with type and submit 11832 08:29:08,040 --> 08:29:15,600 and in here let's go with class name ptn 11833 08:29:11,820 --> 08:29:17,458 again Global button class and then we 11834 08:29:15,600 --> 08:29:18,718 want to go with BTN block which just 11835 08:29:17,458 --> 08:29:20,398 stretches 11836 08:29:18,718 --> 08:29:22,798 that button 11837 08:29:20,398 --> 08:29:24,898 to the size of the container in this 11838 08:29:22,798 --> 08:29:27,660 case form and as far as the text I'm 11839 08:29:24,898 --> 08:29:30,780 going to go with sub emit let's save 11840 08:29:27,660 --> 08:29:33,840 that and we're pretty much good to go so 11841 08:29:30,780 --> 08:29:36,240 this is going to be our setup for the 11842 08:29:33,840 --> 08:29:38,398 remaining videos in this section 11843 08:29:36,240 --> 08:29:40,558 and of course in the next video also set 11844 08:29:38,398 --> 08:29:43,620 up the functionality for now we just 11845 08:29:40,558 --> 08:29:46,080 have a good looking form okay and now 11846 08:29:43,620 --> 08:29:48,660 let's start working on controlled inputs 11847 08:29:46,080 --> 08:29:51,240 and essentially when you hear controlled 11848 08:29:48,660 --> 08:29:54,780 inputs just think that there's going to 11849 08:29:51,240 --> 08:29:58,138 be a state value now it can be one value 11850 08:29:54,780 --> 08:29:59,878 which represents all of the inputs 11851 08:29:58,138 --> 08:30:03,660 something we're going to cover a little 11852 08:29:59,878 --> 08:30:06,478 bit later or it can be a case where each 11853 08:30:03,660 --> 08:30:09,898 input is going to have 11854 08:30:06,478 --> 08:30:11,760 a state value that is associated with 11855 08:30:09,898 --> 08:30:13,920 that input and as you're changing the 11856 08:30:11,760 --> 08:30:16,020 value in the input 11857 08:30:13,920 --> 08:30:17,340 you're automatically also changing the 11858 08:30:16,020 --> 08:30:20,940 value in the state 11859 08:30:17,340 --> 08:30:23,398 and then in turn the input shows the 11860 08:30:20,940 --> 08:30:24,780 state value and if this is somewhat 11861 08:30:23,398 --> 08:30:26,100 confusing just think of it this way 11862 08:30:24,780 --> 08:30:28,138 basically whatever we're going to be 11863 08:30:26,100 --> 08:30:30,360 typing this is going to be added to the 11864 08:30:28,138 --> 08:30:32,280 state value and then whenever you're 11865 08:30:30,360 --> 08:30:34,558 ready to submit the form you can just 11866 08:30:32,280 --> 08:30:37,860 grab that state value and do whatever 11867 08:30:34,558 --> 08:30:40,080 you need to do whether that is to post 11868 08:30:37,860 --> 08:30:42,180 some data on a server or to set up some 11869 08:30:40,080 --> 08:30:44,280 kind of functionality and hopefully you 11870 08:30:42,180 --> 08:30:46,320 see where I'm going with this so if this 11871 08:30:44,280 --> 08:30:48,540 sounds a little bit confusing again just 11872 08:30:46,320 --> 08:30:51,420 bear with me basically like I said we 11873 08:30:48,540 --> 08:30:54,120 want to start by setting up State values 11874 08:30:51,420 --> 08:30:57,478 and in this case we'll do a state value 11875 08:30:54,120 --> 08:31:00,180 for each input later yes we'll combine 11876 08:30:57,478 --> 08:31:03,120 all of them in one so for that we just 11877 08:31:00,180 --> 08:31:04,978 need to go with your state and 11878 08:31:03,120 --> 08:31:07,680 since I have name and email it kind of 11879 08:31:04,978 --> 08:31:09,240 makes sense if I name my state values 11880 08:31:07,680 --> 08:31:11,760 the same correct so I'm going to go here 11881 08:31:09,240 --> 08:31:13,680 with name and then set 11882 08:31:11,760 --> 08:31:16,680 name and that is going to be equal to 11883 08:31:13,680 --> 08:31:18,780 use and state that's what we want to set 11884 08:31:16,680 --> 08:31:20,878 it up over here and we'll start with an 11885 08:31:18,780 --> 08:31:23,878 empty string because this is what we 11886 08:31:20,878 --> 08:31:26,040 have in the input and then we want to 11887 08:31:23,878 --> 08:31:27,180 change it around where this is not going 11888 08:31:26,040 --> 08:31:31,020 to be name 11889 08:31:27,180 --> 08:31:34,138 it will be email and then we're looking 11890 08:31:31,020 --> 08:31:37,378 for set an email 11891 08:31:34,138 --> 08:31:40,200 so this is our initial setup and then we 11892 08:31:37,378 --> 08:31:42,898 want to set up a function that is 11893 08:31:40,200 --> 08:31:44,638 actually going to be invoked every time 11894 08:31:42,898 --> 08:31:47,340 the user is going to type something in 11895 08:31:44,638 --> 08:31:49,320 the input and remember in the react 11896 08:31:47,340 --> 08:31:52,378 fundamentals we covered that the event 11897 08:31:49,320 --> 08:31:55,200 that we want to add to the input is on 11898 08:31:52,378 --> 08:31:58,080 change so as the user is going to be 11899 08:31:55,200 --> 08:32:00,600 typing something in the input we will 11900 08:31:58,080 --> 08:32:02,878 invoke the Callback function which of 11901 08:32:00,600 --> 08:32:05,100 course we need to set up first so let's 11902 08:32:02,878 --> 08:32:06,120 go here and let's say const and then 11903 08:32:05,100 --> 08:32:09,420 handle 11904 08:32:06,120 --> 08:32:11,458 and I'll change that's going to be my 11905 08:32:09,420 --> 08:32:14,000 function and remember that we right away 11906 08:32:11,458 --> 08:32:16,920 have access to the event object there 11907 08:32:14,000 --> 08:32:20,100 and for now let's just leave it blank 11908 08:32:16,920 --> 08:32:22,978 and then when it comes to input if we 11909 08:32:20,100 --> 08:32:25,260 want to set up controlled input on each 11910 08:32:22,978 --> 08:32:27,898 input we need to add two things 11911 08:32:25,260 --> 08:32:31,260 we need to add a value 11912 08:32:27,898 --> 08:32:33,718 and this needs to be basically equal 11913 08:32:31,260 --> 08:32:36,780 to that state value so I'm going to go 11914 08:32:33,718 --> 08:32:38,820 here with name and second one is that on 11915 08:32:36,780 --> 08:32:39,660 change and yes both of them need to be 11916 08:32:38,820 --> 08:32:42,478 there 11917 08:32:39,660 --> 08:32:45,600 otherwise it's not going to work so in 11918 08:32:42,478 --> 08:32:47,340 here I'm going to go on change and then 11919 08:32:45,600 --> 08:32:50,940 I want to pass in the Callback function 11920 08:32:47,340 --> 08:32:54,120 correct so I'll go with handle change 11921 08:32:50,940 --> 08:32:56,398 and save it now the last thing 11922 08:32:54,120 --> 08:32:59,218 that we want to do is to set up the 11923 08:32:56,398 --> 08:33:01,500 functionality and if you remember in 11924 08:32:59,218 --> 08:33:04,378 react fundamentals I said that we have 11925 08:33:01,500 --> 08:33:07,620 access to the event object and from 11926 08:33:04,378 --> 08:33:10,080 there we can get tons of cool things the 11927 08:33:07,620 --> 08:33:11,520 two most important ones I guess the ones 11928 08:33:10,080 --> 08:33:12,740 that we're going to use the most in the 11929 08:33:11,520 --> 08:33:17,340 course are 11930 08:33:12,740 --> 08:33:19,320 event.target.name and event.target.value 11931 08:33:17,340 --> 08:33:21,240 now for now we don't have the name and 11932 08:33:19,320 --> 08:33:23,218 I'll showcase that basically we'll get 11933 08:33:21,240 --> 08:33:24,120 an empty string but we'll definitely get 11934 08:33:23,218 --> 08:33:25,798 the value 11935 08:33:24,120 --> 08:33:27,420 so let's go over here 11936 08:33:25,798 --> 08:33:31,160 let's say log 11937 08:33:27,420 --> 08:33:34,740 and we want to go with event dot Target 11938 08:33:31,160 --> 08:33:37,138 dot name copy and paste and we also want 11939 08:33:34,740 --> 08:33:39,478 to set up the value and for this I'll 11940 08:33:37,138 --> 08:33:41,160 actually move to the bigger browser 11941 08:33:39,478 --> 08:33:43,798 window because I do definitely want to 11942 08:33:41,160 --> 08:33:45,780 showcase the state so this is going to 11943 08:33:43,798 --> 08:33:48,058 be the form on a bigger browser window 11944 08:33:45,780 --> 08:33:50,638 and if we inspect 11945 08:33:48,058 --> 08:33:52,558 the components right away see the 11946 08:33:50,638 --> 08:33:55,378 controlled inputs okay awesome notice 11947 08:33:52,558 --> 08:33:58,260 our two State values so they do exist 11948 08:33:55,378 --> 08:33:59,820 and then as I'm going to be typing I'm 11949 08:33:58,260 --> 08:34:01,558 going to get two things first one is 11950 08:33:59,820 --> 08:34:04,860 going to be empty because there is no 11951 08:34:01,558 --> 08:34:07,680 name set on the input our second one is 11952 08:34:04,860 --> 08:34:09,660 going to represent whatever the user has 11953 08:34:07,680 --> 08:34:11,820 typed now notice something interesting 11954 08:34:09,660 --> 08:34:14,638 though we're not persisting this value 11955 08:34:11,820 --> 08:34:17,040 input so this will always stay empty 11956 08:34:14,638 --> 08:34:19,558 string why well take a look 11957 08:34:17,040 --> 08:34:20,458 because our state value is an empty 11958 08:34:19,558 --> 08:34:22,138 string 11959 08:34:20,458 --> 08:34:24,540 so when we're setting up controlled 11960 08:34:22,138 --> 08:34:26,280 inputs that's why we need 11961 08:34:24,540 --> 08:34:29,100 all of those things we need a state 11962 08:34:26,280 --> 08:34:31,378 value we need both of those 11963 08:34:29,100 --> 08:34:34,860 attributes on the input we need the 11964 08:34:31,378 --> 08:34:37,138 value as well as the on change and in 11965 08:34:34,860 --> 08:34:39,240 the Callback function this is where 11966 08:34:37,138 --> 08:34:41,760 we'll update the state value 11967 08:34:39,240 --> 08:34:45,058 how we can do that well we can use set 11968 08:34:41,760 --> 08:34:48,120 name correct and again keep in mind this 11969 08:34:45,058 --> 08:34:50,580 is going to be empty if there is no name 11970 08:34:48,120 --> 08:34:52,740 attribute and later we'll set up the 11971 08:34:50,580 --> 08:34:54,420 name attribute and we'll use it but I 11972 08:34:52,740 --> 08:34:56,760 definitely want to showcase that if 11973 08:34:54,420 --> 08:34:58,978 there is no name attribute then I mean 11974 08:34:56,760 --> 08:35:01,080 it's just going to be empty so I'll 11975 08:34:58,978 --> 08:35:03,718 leave these ones for your reference 11976 08:35:01,080 --> 08:35:06,420 and here's what I want to do I'm going 11977 08:35:03,718 --> 08:35:09,478 to go with set name and then I want to 11978 08:35:06,420 --> 08:35:11,638 grab whatever I have in the value so 11979 08:35:09,478 --> 08:35:14,940 let's involve that and let's say event 11980 08:35:11,638 --> 08:35:17,040 dot Target dot value and now the 11981 08:35:14,940 --> 08:35:19,558 interesting thing is going to happen 11982 08:35:17,040 --> 08:35:21,478 so we have the state value we have the 11983 08:35:19,558 --> 08:35:25,200 on change and as the user is typing 11984 08:35:21,478 --> 08:35:29,520 we'll be setting the state value equal 11985 08:35:25,200 --> 08:35:32,100 to whatever is in the input and then in 11986 08:35:29,520 --> 08:35:35,820 turn we use that value here to display 11987 08:35:32,100 --> 08:35:37,860 it in the form so check it out here and 11988 08:35:35,820 --> 08:35:39,660 I think again I'm going to use the 11989 08:35:37,860 --> 08:35:41,280 bigger browser window just so you can 11990 08:35:39,660 --> 08:35:43,740 see that we're definitely updating that 11991 08:35:41,280 --> 08:35:46,138 state value now check it out essentially 11992 08:35:43,740 --> 08:35:50,340 whatever I have in the form gets here 11993 08:35:46,138 --> 08:35:52,558 has a state value and that's why 11994 08:35:50,340 --> 08:35:55,558 I can see that in the form as well so 11995 08:35:52,558 --> 08:35:57,000 hopefully this makes sense okay so that 11996 08:35:55,558 --> 08:36:00,058 should do it for the first controlled 11997 08:35:57,000 --> 08:36:01,138 input but what about the second one what 11998 08:36:00,058 --> 08:36:03,058 about the email 11999 08:36:01,138 --> 08:36:06,840 because as you can see at this point 12000 08:36:03,058 --> 08:36:08,940 we're just updating the name so if I'll 12001 08:36:06,840 --> 08:36:10,500 add this handle change to the email I 12002 08:36:08,940 --> 08:36:14,218 mean it's not going to work 12003 08:36:10,500 --> 08:36:14,940 even though I can add the email as a 12004 08:36:14,218 --> 08:36:18,058 value 12005 08:36:14,940 --> 08:36:20,398 to the input I mean in here I have set 12006 08:36:18,058 --> 08:36:23,040 name not set email 12007 08:36:20,398 --> 08:36:25,500 so what are our options well we can set 12008 08:36:23,040 --> 08:36:27,780 up another function so let's say we can 12009 08:36:25,500 --> 08:36:30,360 rename this so it's not generic I can 12010 08:36:27,780 --> 08:36:32,398 say handle name change and then for the 12011 08:36:30,360 --> 08:36:35,340 email of course I'm going to go handle 12012 08:36:32,398 --> 08:36:37,920 email change or we can use the error 12013 08:36:35,340 --> 08:36:40,558 function and effectively 12014 08:36:37,920 --> 08:36:43,920 this is totally up to you if you have 12015 08:36:40,558 --> 08:36:46,798 more logic then of course it makes sense 12016 08:36:43,920 --> 08:36:50,780 that you set it up here as a separate 12017 08:36:46,798 --> 08:36:53,878 function however if you're just passing 12018 08:36:50,780 --> 08:36:55,860 event.target.value then we also have 12019 08:36:53,878 --> 08:36:57,958 this option where instead of the handle 12020 08:36:55,860 --> 08:37:00,840 change which by the way you can always 12021 08:36:57,958 --> 08:37:03,360 find it in the readme and therefore just 12022 08:37:00,840 --> 08:37:06,478 so it's cleaner I'm going to remove 12023 08:37:03,360 --> 08:37:09,298 what we can do here is set up our Arrow 12024 08:37:06,478 --> 08:37:12,298 function remember we do need to grab the 12025 08:37:09,298 --> 08:37:16,620 event so that doesn't change and then 12026 08:37:12,298 --> 08:37:21,260 invoke set name directly here so I can 12027 08:37:16,620 --> 08:37:24,360 go here with email dot Target dot value 12028 08:37:21,260 --> 08:37:27,058 and you guessed it correct 12029 08:37:24,360 --> 08:37:29,878 for all the inputs we basically need to 12030 08:37:27,058 --> 08:37:32,878 repeat these two steps we need to set up 12031 08:37:29,878 --> 08:37:34,500 the value I mean considering that you 12032 08:37:32,878 --> 08:37:36,540 already have the state value 12033 08:37:34,500 --> 08:37:38,520 we need to set up the value and we need 12034 08:37:36,540 --> 08:37:42,180 to set up the on change and then in the 12035 08:37:38,520 --> 08:37:45,058 on change we just go with set and then 12036 08:37:42,180 --> 08:37:46,138 whatever is the name of the input so 12037 08:37:45,058 --> 08:37:48,600 let's go here 12038 08:37:46,138 --> 08:37:51,058 let's keep on moving this is going to be 12039 08:37:48,600 --> 08:37:53,458 my input I want to copy and paste again 12040 08:37:51,058 --> 08:37:55,680 these are different values so it's not 12041 08:37:53,458 --> 08:37:58,920 going to be named it's going to be email 12042 08:37:55,680 --> 08:38:02,520 and the function is also going to be 12043 08:37:58,920 --> 08:38:05,218 email let's save that and again let's 12044 08:38:02,520 --> 08:38:06,958 navigate to the big browser window and 12045 08:38:05,218 --> 08:38:10,200 if I'll type over here 12046 08:38:06,958 --> 08:38:12,240 let's say some kind of dummy email 12047 08:38:10,200 --> 08:38:15,000 doesn't look like it works here 12048 08:38:12,240 --> 08:38:16,920 maybe just needs a little nudge let me 12049 08:38:15,000 --> 08:38:19,440 try right now John 12050 08:38:16,920 --> 08:38:21,240 and as you can see now everything works 12051 08:38:19,440 --> 08:38:23,760 so now both of them are controlled 12052 08:38:21,240 --> 08:38:26,638 inputs and essentially our workflow is 12053 08:38:23,760 --> 08:38:29,280 going to be we'll set up value and on 12054 08:38:26,638 --> 08:38:32,580 change and then we'll set up the on 12055 08:38:29,280 --> 08:38:35,520 submit as well so let's navigate 12056 08:38:32,580 --> 08:38:38,820 where we have the state values 12057 08:38:35,520 --> 08:38:40,020 and let's just add a on submit on the 12058 08:38:38,820 --> 08:38:41,760 form 12059 08:38:40,020 --> 08:38:44,100 and let's 12060 08:38:41,760 --> 08:38:46,740 create a handle submit 12061 08:38:44,100 --> 08:38:48,120 so I just need to create that function 12062 08:38:46,740 --> 08:38:49,798 handle 12063 08:38:48,120 --> 08:38:51,420 submit and 12064 08:38:49,798 --> 08:38:53,100 hopefully you remember from the 12065 08:38:51,420 --> 08:38:55,798 fundamentals that again we have access 12066 08:38:53,100 --> 08:38:57,958 to the event object and in here the 12067 08:38:55,798 --> 08:39:00,360 first thing we want to do is prevent the 12068 08:38:57,958 --> 08:39:02,820 default Behavior so we invoke prevent 12069 08:39:00,360 --> 08:39:05,280 default and then we can do something 12070 08:39:02,820 --> 08:39:06,540 again at the moment we're not going to 12071 08:39:05,280 --> 08:39:09,058 do anything in this video but 12072 08:39:06,540 --> 08:39:11,100 technically this is where you post to 12073 08:39:09,058 --> 08:39:14,638 the server where you do something with 12074 08:39:11,100 --> 08:39:17,040 the value so say do something and 12075 08:39:14,638 --> 08:39:18,420 essentially we just want to access both 12076 08:39:17,040 --> 08:39:20,760 of the values so in my case I'm just 12077 08:39:18,420 --> 08:39:22,138 going to log it so I'm going to say 12078 08:39:20,760 --> 08:39:26,160 name 12079 08:39:22,138 --> 08:39:29,280 and email and once I save 12080 08:39:26,160 --> 08:39:31,620 notice how I can nicely type so John and 12081 08:39:29,280 --> 08:39:34,558 then John at gmail 12082 08:39:31,620 --> 08:39:37,440 and once I click on submit and check the 12083 08:39:34,558 --> 08:39:40,680 console notice I have both of the values 12084 08:39:37,440 --> 08:39:42,180 I have the John as well as the email so 12085 08:39:40,680 --> 08:39:46,320 essentially that's how we can set up 12086 08:39:42,180 --> 08:39:48,360 controlled inputs in react beautiful and 12087 08:39:46,320 --> 08:39:51,180 once we're familiar with controlled 12088 08:39:48,360 --> 08:39:53,940 inputs why don't we work on a small 12089 08:39:51,180 --> 08:39:56,878 challenge and first what I want you to 12090 08:39:53,940 --> 08:40:01,860 do if you haven't done that already go 12091 08:39:56,878 --> 08:40:03,958 to app jsx and look for zero two user 12092 08:40:01,860 --> 08:40:06,058 Challenge and this is what you should 12093 08:40:03,958 --> 08:40:09,000 see on a screen 12094 08:40:06,058 --> 08:40:11,760 basically I prepare the form now I 12095 08:40:09,000 --> 08:40:14,100 didn't set up any functionality again 12096 08:40:11,760 --> 08:40:16,680 it's just a straight up form you'll have 12097 08:40:14,100 --> 08:40:19,798 to do all of the work and as you can see 12098 08:40:16,680 --> 08:40:22,500 in this case we're just using one input 12099 08:40:19,798 --> 08:40:25,520 that's it that's pretty much the only 12100 08:40:22,500 --> 08:40:29,458 difference and as far as the challenge 12101 08:40:25,520 --> 08:40:32,040 well it's following so first 12102 08:40:29,458 --> 08:40:33,840 create a control input 12103 08:40:32,040 --> 08:40:35,340 and in my case I'm going to call this 12104 08:40:33,840 --> 08:40:39,298 name and you'll see why it actually 12105 08:40:35,340 --> 08:40:41,820 makes sense then set up on submit 12106 08:40:39,298 --> 08:40:44,458 basically for now just a placeholder 12107 08:40:41,820 --> 08:40:46,680 don't worry about the logic yet 12108 08:40:44,458 --> 08:40:48,780 if you can log 12109 08:40:46,680 --> 08:40:51,718 that user has submitted the form 12110 08:40:48,780 --> 08:40:53,700 basically if you have prevented the 12111 08:40:51,718 --> 08:40:56,398 default and all that you're in good 12112 08:40:53,700 --> 08:41:00,120 shape so first set up those two things 12113 08:40:56,398 --> 08:41:01,978 then I want you to import data array 12114 08:41:00,120 --> 08:41:04,920 from data 12115 08:41:01,978 --> 08:41:08,040 and just to drag our memory so we're 12116 08:41:04,920 --> 08:41:10,378 here so this is what we want to get 12117 08:41:08,040 --> 08:41:11,940 we want to get this data array and 12118 08:41:10,378 --> 08:41:14,398 that's why we'll go with name input 12119 08:41:11,940 --> 08:41:16,200 because this is the property we have in 12120 08:41:14,398 --> 08:41:19,200 the object and once you have 12121 08:41:16,200 --> 08:41:21,780 successfully imported the data set up 12122 08:41:19,200 --> 08:41:26,040 another state value in my case I'm going 12123 08:41:21,780 --> 08:41:28,920 to call this users and set the array as 12124 08:41:26,040 --> 08:41:31,500 a default value and I'm purposely being 12125 08:41:28,920 --> 08:41:34,440 somewhat vague because we have covered 12126 08:41:31,500 --> 08:41:36,540 all of these things already then I want 12127 08:41:34,440 --> 08:41:39,958 you to iterate over 12128 08:41:36,540 --> 08:41:42,540 the list and display right after the 12129 08:41:39,958 --> 08:41:44,160 form so if we navigate to the user 12130 08:41:42,540 --> 08:41:46,458 challenge notice this comment over here 12131 08:41:44,160 --> 08:41:50,100 so this is where I want you to display 12132 08:41:46,458 --> 08:41:53,218 the users that are in the array again 12133 08:41:50,100 --> 08:41:54,840 don't worry about the CSS if you can see 12134 08:41:53,218 --> 08:41:57,240 the name 12135 08:41:54,840 --> 08:41:58,500 correctly displayed in a browser you're 12136 08:41:57,240 --> 08:42:00,540 in good shape 12137 08:41:58,500 --> 08:42:02,820 and at the very end we want to set up 12138 08:42:00,540 --> 08:42:04,320 following functionality when the user 12139 08:42:02,820 --> 08:42:09,360 submits the form 12140 08:42:04,320 --> 08:42:12,600 add a new person to the list now 12141 08:42:09,360 --> 08:42:15,000 if you find this one easy you can work 12142 08:42:12,600 --> 08:42:16,620 on extra challenge where I want you to 12143 08:42:15,000 --> 08:42:19,320 add a button 12144 08:42:16,620 --> 08:42:23,458 to whatever we're returning basically 12145 08:42:19,320 --> 08:42:26,398 the item and set up the functionality to 12146 08:42:23,458 --> 08:42:28,740 remove the user from the list 12147 08:42:26,398 --> 08:42:31,020 so if you're interested work on this 12148 08:42:28,740 --> 08:42:33,180 Challenge and once you're ready to 12149 08:42:31,020 --> 08:42:36,058 compare the results resume with the 12150 08:42:33,180 --> 08:42:37,798 videos Okay so let's get cracking first 12151 08:42:36,058 --> 08:42:38,760 I'm going to navigate to the user 12152 08:42:37,798 --> 08:42:41,398 challenge 12153 08:42:38,760 --> 08:42:44,218 and I'll create those State values 12154 08:42:41,398 --> 08:42:45,240 basically I know that I'll have two of 12155 08:42:44,218 --> 08:42:48,120 them so 12156 08:42:45,240 --> 08:42:50,940 while I'm setting up one I might as well 12157 08:42:48,120 --> 08:42:54,740 set up the second one so name and then 12158 08:42:50,940 --> 08:42:57,600 set name that is equal to use 12159 08:42:54,740 --> 08:42:59,520 State let's pass in the empty Value 12160 08:42:57,600 --> 08:43:02,340 First copy and paste then like I said 12161 08:42:59,520 --> 08:43:05,700 Marcus I'm gonna go with users 12162 08:43:02,340 --> 08:43:07,200 set and also when I do it over here now 12163 08:43:05,700 --> 08:43:09,540 for now since I haven't imported 12164 08:43:07,200 --> 08:43:12,718 anything let me just set it up as an 12165 08:43:09,540 --> 08:43:14,940 empty all right let's save this and now 12166 08:43:12,718 --> 08:43:16,440 I want to set up that controlled input 12167 08:43:14,940 --> 08:43:20,520 so 12168 08:43:16,440 --> 08:43:22,620 I need to look for my input and then 12169 08:43:20,520 --> 08:43:24,958 here remember value 12170 08:43:22,620 --> 08:43:27,600 that is equal to the state value and 12171 08:43:24,958 --> 08:43:29,580 then the second one is the on change and 12172 08:43:27,600 --> 08:43:33,000 this is going to be equal to our Arrow 12173 08:43:29,580 --> 08:43:35,700 function we'll grab the event object and 12174 08:43:33,000 --> 08:43:38,878 we'll go to set name and we'll pass in 12175 08:43:35,700 --> 08:43:41,820 the event dot Target and then the value 12176 08:43:38,878 --> 08:43:45,058 so now we have our controlled input next 12177 08:43:41,820 --> 08:43:49,520 I wanna set up just the log 12178 08:43:45,058 --> 08:43:52,440 when we submit the form so here notice 12179 08:43:49,520 --> 08:43:54,240 Veet is pretty quick basically the 12180 08:43:52,440 --> 08:43:56,160 moment you type the value it spins back 12181 08:43:54,240 --> 08:43:59,160 the center don't worry you don't have it 12182 08:43:56,160 --> 08:44:00,780 everything is correct and like I was 12183 08:43:59,160 --> 08:44:03,898 saying we want to set up that handle 12184 08:44:00,780 --> 08:44:07,860 Summit so let's go here and I'm going to 12185 08:44:03,898 --> 08:44:07,860 call this handle submit 12186 08:44:07,920 --> 08:44:13,138 we're gonna get the object yes the event 12187 08:44:10,500 --> 08:44:14,780 object more precisely and here let's 12188 08:44:13,138 --> 08:44:18,000 start with prevent 12189 08:44:14,780 --> 08:44:19,080 default and I'm going to go with just 12190 08:44:18,000 --> 08:44:21,058 log 12191 08:44:19,080 --> 08:44:23,660 I'll say 12192 08:44:21,058 --> 08:44:23,660 form 12193 08:44:23,840 --> 08:44:29,160 submitted okay good and now let's 12194 08:44:27,478 --> 08:44:31,680 navigate to the form 12195 08:44:29,160 --> 08:44:34,700 and then let's set up on submit 12196 08:44:31,680 --> 08:44:37,440 beautiful and let's pass in the handle 12197 08:44:34,700 --> 08:44:40,200 submit let's save that and if everything 12198 08:44:37,440 --> 08:44:41,458 is correct we should see form submitted 12199 08:44:40,200 --> 08:44:43,798 awesome 12200 08:44:41,458 --> 08:44:46,500 and once we have this setup in place now 12201 08:44:43,798 --> 08:44:50,280 let's grab that data so let me try here 12202 08:44:46,500 --> 08:44:52,680 whether I can actually do it data and 12203 08:44:50,280 --> 08:44:54,920 nope that doesn't give me anything okay 12204 08:44:52,680 --> 08:44:59,100 let's grab it from the file so import 12205 08:44:54,920 --> 08:45:01,920 data and then from and now we need to go 12206 08:44:59,100 --> 08:45:04,558 quite a few levels up so one two and 12207 08:45:01,920 --> 08:45:07,138 then this should be coming from the data 12208 08:45:04,558 --> 08:45:09,898 let me just double check quickly so data 12209 08:45:07,138 --> 08:45:12,718 data yep that's the correct one probably 12210 08:45:09,898 --> 08:45:13,680 not the best naming but as I was setting 12211 08:45:12,718 --> 08:45:15,898 up 12212 08:45:13,680 --> 08:45:18,298 that's the one that I used so this is 12213 08:45:15,898 --> 08:45:21,718 going to be our default value for the 12214 08:45:18,298 --> 08:45:23,820 users and now let's navigate over here 12215 08:45:21,718 --> 08:45:26,398 where we render and then like I said 12216 08:45:23,820 --> 08:45:28,860 right after form we're gonna go users 12217 08:45:26,398 --> 08:45:32,218 and now let's iterate over those users 12218 08:45:28,860 --> 08:45:34,500 and simply return a div since we 12219 08:45:32,218 --> 08:45:36,360 potentially might set up a button again 12220 08:45:34,500 --> 08:45:38,458 that is totally up to you but in my case 12221 08:45:36,360 --> 08:45:40,620 I'll try to do that at least and then 12222 08:45:38,458 --> 08:45:42,478 users map so we're iterating over and 12223 08:45:40,620 --> 08:45:44,580 then this should be already very 12224 08:45:42,478 --> 08:45:47,398 familiar we have done that quite a few 12225 08:45:44,580 --> 08:45:49,978 times during the course and that's why I 12226 08:45:47,398 --> 08:45:50,820 keep pushing for you to set it up on 12227 08:45:49,978 --> 08:45:52,680 your own 12228 08:45:50,820 --> 08:45:54,600 because we have covered that you just 12229 08:45:52,680 --> 08:45:56,820 need to practice that's it there's no 12230 08:45:54,600 --> 08:45:58,378 real secret and as far as the return I'm 12231 08:45:56,820 --> 08:46:01,260 gonna go here with div 12232 08:45:58,378 --> 08:46:03,478 and then let's set up the key this is 12233 08:46:01,260 --> 08:46:05,878 where we're iterating over so I'm gonna 12234 08:46:03,478 --> 08:46:09,240 go to user ID because I know 12235 08:46:05,878 --> 08:46:11,580 that in my array in the object more 12236 08:46:09,240 --> 08:46:14,100 precisely and then as far as the value 12237 08:46:11,580 --> 08:46:16,080 well like I said let's not over 12238 08:46:14,100 --> 08:46:18,958 complicate things we're just going to go 12239 08:46:16,080 --> 08:46:21,718 to user and then name let's save and 12240 08:46:18,958 --> 08:46:25,080 this is what we should see on the screen 12241 08:46:21,718 --> 08:46:26,878 so here's our task and you know what let 12242 08:46:25,080 --> 08:46:30,360 me make this one as heading to so it 12243 08:46:26,878 --> 08:46:33,058 stands out now every time 12244 08:46:30,360 --> 08:46:36,718 the user submits the form 12245 08:46:33,058 --> 08:46:37,798 essentially we want to add a new user to 12246 08:46:36,718 --> 08:46:41,580 our array 12247 08:46:37,798 --> 08:46:45,180 so this basically is the main challenge 12248 08:46:41,580 --> 08:46:47,820 and effectively we can do it this way so 12249 08:46:45,180 --> 08:46:51,420 first I have the handle submit correct 12250 08:46:47,820 --> 08:46:54,360 now I need to grab the value of the name 12251 08:46:51,420 --> 08:46:56,340 but here's the thing user might try to 12252 08:46:54,360 --> 08:46:57,840 submit the form basically without 12253 08:46:56,340 --> 08:46:59,760 entering anything 12254 08:46:57,840 --> 08:47:02,760 and this is normally where we would 12255 08:46:59,760 --> 08:47:05,280 display the alert or some kind of toast 12256 08:47:02,760 --> 08:47:07,020 or whatever that all the values need to 12257 08:47:05,280 --> 08:47:09,120 be submitted but in this case we'll 12258 08:47:07,020 --> 08:47:11,940 simply return from this function 12259 08:47:09,120 --> 08:47:13,200 so the most basic check is following I 12260 08:47:11,940 --> 08:47:15,958 can say if 12261 08:47:13,200 --> 08:47:18,898 and if there is no name if you want of 12262 08:47:15,958 --> 08:47:20,638 course you can log stuff that's totally 12263 08:47:18,898 --> 08:47:22,440 up to you but in my case I'm just going 12264 08:47:20,638 --> 08:47:25,440 to go with the return and you'll notice 12265 08:47:22,440 --> 08:47:27,958 that if the user tries to submit the 12266 08:47:25,440 --> 08:47:30,240 form with empty values I'll have nothing 12267 08:47:27,958 --> 08:47:32,040 in a console so again this stresses the 12268 08:47:30,240 --> 08:47:33,600 point that we're not going to get to 12269 08:47:32,040 --> 08:47:35,420 this line JavaScript is going to keep on 12270 08:47:33,600 --> 08:47:37,558 reading and this is the earlier turn 12271 08:47:35,420 --> 08:47:39,240 jumpsuit is going to be okay 12272 08:47:37,558 --> 08:47:42,000 this actually matches there's no value 12273 08:47:39,240 --> 08:47:44,340 in here so I'm just going to return from 12274 08:47:42,000 --> 08:47:46,558 this function that's the most basic 12275 08:47:44,340 --> 08:47:48,780 check again for the empty value and yes 12276 08:47:46,558 --> 08:47:51,660 before you ask if you have multiple 12277 08:47:48,780 --> 08:47:54,780 values let's say name email password and 12278 08:47:51,660 --> 08:47:57,360 all that then basically you go with or 12279 08:47:54,780 --> 08:47:59,280 and then again you check so we go this 12280 08:47:57,360 --> 08:48:03,600 not operator and we say 12281 08:47:59,280 --> 08:48:05,700 if it meets the condition then we're not 12282 08:48:03,600 --> 08:48:07,500 essentially doing anything we're just 12283 08:48:05,700 --> 08:48:10,378 returning from the function so that's 12284 08:48:07,500 --> 08:48:12,600 the first step now the second one if 12285 08:48:10,378 --> 08:48:14,458 there's actually some value so let's say 12286 08:48:12,600 --> 08:48:16,860 if the user types something and I click 12287 08:48:14,458 --> 08:48:18,898 notice form submitted now I want to 12288 08:48:16,860 --> 08:48:21,058 construct that user 12289 08:48:18,898 --> 08:48:23,760 and this is the case where yes basically 12290 08:48:21,058 --> 08:48:26,940 I'll have to create a new object on the 12291 08:48:23,760 --> 08:48:28,798 Fly and I'll have to add it to my 12292 08:48:26,940 --> 08:48:31,440 existing array 12293 08:48:28,798 --> 08:48:33,240 so let's start over here let's say that 12294 08:48:31,440 --> 08:48:35,160 there is some kind of value 12295 08:48:33,240 --> 08:48:37,920 first thing we need to do is set up that 12296 08:48:35,160 --> 08:48:41,160 ID now of course you can hard code 12297 08:48:37,920 --> 08:48:43,378 somehow but one neat trick while you're 12298 08:48:41,160 --> 08:48:46,138 basically working on some simple 12299 08:48:43,378 --> 08:48:48,958 application like this one you can just 12300 08:48:46,138 --> 08:48:51,058 use some helpers and in this case I'm 12301 08:48:48,958 --> 08:48:53,160 going to use date dot now again this is 12302 08:48:51,058 --> 08:48:54,780 not for serious projects in that case of 12303 08:48:53,160 --> 08:48:56,600 course it's going to be more complex but 12304 08:48:54,780 --> 08:48:58,798 also keep in mind one thing that 12305 08:48:56,600 --> 08:49:01,020 normally you're communicating with 12306 08:48:58,798 --> 08:49:03,478 database anyway and database is 12307 08:49:01,020 --> 08:49:06,058 responsible for setting up those IDs so 12308 08:49:03,478 --> 08:49:07,820 this is just for practice where since I 12309 08:49:06,058 --> 08:49:11,820 know that 12310 08:49:07,820 --> 08:49:13,978 my user has both ID and name I just 12311 08:49:11,820 --> 08:49:15,540 manually need to create that ID 12312 08:49:13,978 --> 08:49:18,120 because name of course is going to be 12313 08:49:15,540 --> 08:49:20,398 provided by this input hopefully that 12314 08:49:18,120 --> 08:49:23,100 makes sense so let's go here with cons 12315 08:49:20,398 --> 08:49:25,138 and then fake ID and like I said I'm 12316 08:49:23,100 --> 08:49:27,478 just going to use date dot now but 12317 08:49:25,138 --> 08:49:30,440 please don't use that in any serious 12318 08:49:27,478 --> 08:49:32,340 project and if you want you can log 12319 08:49:30,440 --> 08:49:33,718 essentially those are going to be 12320 08:49:32,340 --> 08:49:36,958 milliseconds 12321 08:49:33,718 --> 08:49:39,180 from I believe 1970 or something along 12322 08:49:36,958 --> 08:49:42,478 those lines so if we go over here 12323 08:49:39,180 --> 08:49:44,218 and if we type check it out notice this 12324 08:49:42,478 --> 08:49:46,558 is going to be my value again 12325 08:49:44,218 --> 08:49:49,620 just something Nifty that we can use 12326 08:49:46,558 --> 08:49:51,120 while we're working on this project so 12327 08:49:49,620 --> 08:49:53,638 let me just comment this one out for 12328 08:49:51,120 --> 08:49:55,558 your reference if you want and then now 12329 08:49:53,638 --> 08:49:58,500 let's construct that user so we're going 12330 08:49:55,558 --> 08:50:01,500 to go here with const new user and that 12331 08:49:58,500 --> 08:50:03,660 is equal to an object so I have two 12332 08:50:01,500 --> 08:50:05,160 properties first one is going to be at e 12333 08:50:03,660 --> 08:50:07,920 which is going to be equal to fake ID 12334 08:50:05,160 --> 08:50:10,320 and then the second one is the name Now 12335 08:50:07,920 --> 08:50:12,540 where's the name coming from well it's 12336 08:50:10,320 --> 08:50:15,360 coming from the state correct over here 12337 08:50:12,540 --> 08:50:17,580 and we only get to this functionality if 12338 08:50:15,360 --> 08:50:20,700 there's some value in the input if not 12339 08:50:17,580 --> 08:50:22,740 then we won't even get to it and now 12340 08:50:20,700 --> 08:50:26,218 let's construct that new array the one 12341 08:50:22,740 --> 08:50:29,280 that we will set up using set users so 12342 08:50:26,218 --> 08:50:32,580 in here let's go with cons and then I'll 12343 08:50:29,280 --> 08:50:35,100 call this updated users and then that 12344 08:50:32,580 --> 08:50:37,138 one is equal to first I want to spread 12345 08:50:35,100 --> 08:50:39,718 out all of these values because I'll be 12346 08:50:37,138 --> 08:50:42,478 adding to this array again I don't want 12347 08:50:39,718 --> 08:50:44,580 to override that keep in mind we can 12348 08:50:42,478 --> 08:50:46,320 pass here whatever we want so if you'll 12349 08:50:44,580 --> 08:50:48,718 just simply Place new user in the set 12350 08:50:46,320 --> 08:50:50,340 users you'll set your array now equal to 12351 08:50:48,718 --> 08:50:52,798 an object that's not what we want to do 12352 08:50:50,340 --> 08:50:55,500 we want to spread out so we want to copy 12353 08:50:52,798 --> 08:50:58,080 all of the current values from the users 12354 08:50:55,500 --> 08:51:00,840 and then we want to add the new one so 12355 08:50:58,080 --> 08:51:03,780 say here new user and we'll use this 12356 08:51:00,840 --> 08:51:07,260 updated users and set it as our new 12357 08:51:03,780 --> 08:51:10,978 value so go to set users and we'll pass 12358 08:51:07,260 --> 08:51:13,200 in an updated users and lastly 12359 08:51:10,978 --> 08:51:14,760 it kind of makes sense if we clean out 12360 08:51:13,200 --> 08:51:16,398 the input right 12361 08:51:14,760 --> 08:51:19,318 otherwise it's just going to stay there 12362 08:51:16,398 --> 08:51:22,138 and the way we do that is simply by 12363 08:51:19,318 --> 08:51:24,360 typing set name again same function 12364 08:51:22,138 --> 08:51:27,360 whatever we have over there and we just 12365 08:51:24,360 --> 08:51:30,000 provide the empty value now I don't 12366 08:51:27,360 --> 08:51:32,160 think we need any more of this one we 12367 08:51:30,000 --> 08:51:33,540 can remove and now check it out 12368 08:51:32,160 --> 08:51:35,458 essentially 12369 08:51:33,540 --> 08:51:38,520 as I'm going to be submitting something 12370 08:51:35,458 --> 08:51:41,280 this is going to be my new user and then 12371 08:51:38,520 --> 08:51:43,860 if user tries to submit with empty 12372 08:51:41,280 --> 08:51:46,740 values nothing happens however if we do 12373 08:51:43,860 --> 08:51:49,318 provide the value and if I say Bob 12374 08:51:46,740 --> 08:51:52,020 then check it out if I take a look at my 12375 08:51:49,318 --> 08:51:54,420 browser I have Bob over here and input 12376 08:51:52,020 --> 08:51:57,240 is also nicely set to an empty string 12377 08:51:54,420 --> 08:51:59,760 and Just for kicks as far as the extra 12378 08:51:57,240 --> 08:52:05,160 challenge now let's also set up those 12379 08:51:59,760 --> 08:52:07,860 buttons and let's remove the user if the 12380 08:52:05,160 --> 08:52:09,540 button is clicked so first we want to 12381 08:52:07,860 --> 08:52:11,818 navigate where we have the return that's 12382 08:52:09,540 --> 08:52:14,638 why I set up the div and we're going to 12383 08:52:11,818 --> 08:52:17,398 go with button and I'll just type remove 12384 08:52:14,638 --> 08:52:19,080 in here we're gonna go with on click and 12385 08:52:17,398 --> 08:52:22,558 eventually we'll set up some Logic for 12386 08:52:19,080 --> 08:52:27,120 now simply go and empty Arrow function 12387 08:52:22,558 --> 08:52:28,920 just so we don't have the error and I'm 12388 08:52:27,120 --> 08:52:31,760 also going to add the class name and I'm 12389 08:52:28,920 --> 08:52:34,860 going to say BTN so that's going to be 12390 08:52:31,760 --> 08:52:37,260 my generic button class 12391 08:52:34,860 --> 08:52:39,958 and now let's move up and set up that 12392 08:52:37,260 --> 08:52:42,058 functionality where essentially I'm 12393 08:52:39,958 --> 08:52:43,798 going to call this remove user it's 12394 08:52:42,058 --> 08:52:45,360 going to be looking for one thing and 12395 08:52:43,798 --> 08:52:49,020 one thing only it's going to be looking 12396 08:52:45,360 --> 08:52:50,700 for the Eddie and as far as the logic I 12397 08:52:49,020 --> 08:52:53,520 want to use filter again 12398 08:52:50,700 --> 08:52:56,638 and essentially return only the items 12399 08:52:53,520 --> 08:52:59,100 whose ID does not match whatever I'm 12400 08:52:56,638 --> 08:53:01,740 passing in so again I'm going to call 12401 08:52:59,100 --> 08:53:03,718 this updated users in this case I'm 12402 08:53:01,740 --> 08:53:05,580 going to speed this up a little bit by 12403 08:53:03,718 --> 08:53:08,160 just copying pasting so this is going to 12404 08:53:05,580 --> 08:53:10,920 be the same however I'm not gonna spread 12405 08:53:08,160 --> 08:53:13,260 out values this way I know that filter 12406 08:53:10,920 --> 08:53:14,878 returns a new one so I can simply say 12407 08:53:13,260 --> 08:53:18,058 users 12408 08:53:14,878 --> 08:53:20,458 and then filter 12409 08:53:18,058 --> 08:53:22,860 and as far as the Callback function I'll 12410 08:53:20,458 --> 08:53:24,298 call this person and I'll say if the 12411 08:53:22,860 --> 08:53:26,398 person ID 12412 08:53:24,298 --> 08:53:29,340 person ID 12413 08:53:26,398 --> 08:53:31,620 doesn't match then return 12414 08:53:29,340 --> 08:53:33,840 if it doesn't match the ID that we're 12415 08:53:31,620 --> 08:53:35,398 passing in however if the ID matches 12416 08:53:33,840 --> 08:53:38,218 then that person is not going to be 12417 08:53:35,398 --> 08:53:41,700 returned and of course again we set 12418 08:53:38,218 --> 08:53:44,218 users equal to this new value and now we 12419 08:53:41,700 --> 08:53:46,798 just navigate down here where we have 12420 08:53:44,218 --> 08:53:49,440 the arrow function and essentially we 12421 08:53:46,798 --> 08:53:53,700 want to go with the remove user and 12422 08:53:49,440 --> 08:53:56,940 we'll grab the user ID so user dot ID 12423 08:53:53,700 --> 08:53:59,340 let's save this and now one by one I can 12424 08:53:56,940 --> 08:54:01,620 remove them that concludes the entire 12425 08:53:59,340 --> 08:54:03,660 challenge hopefully everyone enjoyed it 12426 08:54:01,620 --> 08:54:06,240 and I'll see you in the next video okay 12427 08:54:03,660 --> 08:54:10,200 and up next let's talk about how we can 12428 08:54:06,240 --> 08:54:13,200 set up one state value for multiple 12429 08:54:10,200 --> 08:54:16,200 inputs and before we begin let me just 12430 08:54:13,200 --> 08:54:18,360 stress something it's not something that 12431 08:54:16,200 --> 08:54:21,420 you have to do if you have multiple 12432 08:54:18,360 --> 08:54:23,520 inputs so it's not a requirement however 12433 08:54:21,420 --> 08:54:25,200 you'll see this quite often and 12434 08:54:23,520 --> 08:54:27,660 therefore I think it's important that 12435 08:54:25,200 --> 08:54:30,718 you know how to set it up and basically 12436 08:54:27,660 --> 08:54:34,340 the logic behind it also 12437 08:54:30,718 --> 08:54:38,458 during this video will heavily rely on 12438 08:54:34,340 --> 08:54:41,100 Dynamic object keys and if you need to 12439 08:54:38,458 --> 08:54:44,280 jog your memory on that please utilize 12440 08:54:41,100 --> 08:54:47,340 this JS nuggets video so first we want 12441 08:54:44,280 --> 08:54:49,200 to navigate to app jsx you want to make 12442 08:54:47,340 --> 08:54:52,798 sure that we're sitting in the zero 12443 08:54:49,200 --> 08:54:54,600 three multiple inputs and then let's 12444 08:54:52,798 --> 08:54:56,218 navigate to the file 12445 08:54:54,600 --> 08:54:59,160 so in here 12446 08:54:56,218 --> 08:55:01,378 we want to set up one state value 12447 08:54:59,160 --> 08:55:03,780 which is going to represent all of the 12448 08:55:01,378 --> 08:55:06,420 inputs so at the moment notice instead 12449 08:55:03,780 --> 08:55:08,818 of just name and email I also have the 12450 08:55:06,420 --> 08:55:11,580 password so instead of setting up three 12451 08:55:08,818 --> 08:55:14,520 state values I'll set up one how we can 12452 08:55:11,580 --> 08:55:17,160 do that well we can create that State 12453 08:55:14,520 --> 08:55:18,360 value as an object correct so let's go 12454 08:55:17,160 --> 08:55:21,360 to use 12455 08:55:18,360 --> 08:55:22,378 State this is going to be my object and 12456 08:55:21,360 --> 08:55:24,660 I just need to come up with the 12457 08:55:22,378 --> 08:55:26,580 properties now before we do that why 12458 08:55:24,660 --> 08:55:29,760 don't we also 12459 08:55:26,580 --> 08:55:33,718 come up with a name so in my case that 12460 08:55:29,760 --> 08:55:35,760 is going to be user and set user so that 12461 08:55:33,718 --> 08:55:38,040 is going to be my set function 12462 08:55:35,760 --> 08:55:40,020 so as you can see nothing changes 12463 08:55:38,040 --> 08:55:42,120 that part still stays the same 12464 08:55:40,020 --> 08:55:44,878 difference right now is that we'll have 12465 08:55:42,120 --> 08:55:47,760 an object and basically each input 12466 08:55:44,878 --> 08:55:50,160 we can set it up as a property in here 12467 08:55:47,760 --> 08:55:51,120 so we'll have name I'll also have the 12468 08:55:50,160 --> 08:55:53,700 email 12469 08:55:51,120 --> 08:55:55,440 and also have the password and as far as 12470 08:55:53,700 --> 08:55:56,638 my default values they will be empty 12471 08:55:55,440 --> 08:55:57,898 string if you want to go with some 12472 08:55:56,638 --> 08:56:00,718 different 12473 08:55:57,898 --> 08:56:03,958 value of course that is totally up to 12474 08:56:00,718 --> 08:56:05,160 you so now I have the user and now let's 12475 08:56:03,958 --> 08:56:07,740 think about it 12476 08:56:05,160 --> 08:56:10,798 in order to set up the input 12477 08:56:07,740 --> 08:56:13,260 we will have to have a function correct 12478 08:56:10,798 --> 08:56:15,180 so why don't we create that and the 12479 08:56:13,260 --> 08:56:17,580 beauty here is that we can use only one 12480 08:56:15,180 --> 08:56:19,798 function since we have only one 12481 08:56:17,580 --> 08:56:23,100 State value as an object we can also set 12482 08:56:19,798 --> 08:56:25,798 up only one function so I can say handle 12483 08:56:23,100 --> 08:56:28,260 change and hopefully you see that it 12484 08:56:25,798 --> 08:56:31,138 kind of saves us a little bit of time if 12485 08:56:28,260 --> 08:56:33,180 we have multiple inputs so again we only 12486 08:56:31,138 --> 08:56:36,000 have one state value and we'll only have 12487 08:56:33,180 --> 08:56:39,000 one function so that's basically the 12488 08:56:36,000 --> 08:56:40,558 benefit of such approach and now once I 12489 08:56:39,000 --> 08:56:43,558 have the function 12490 08:56:40,558 --> 08:56:46,558 let's not worry about any logic in here 12491 08:56:43,558 --> 08:56:48,180 I will navigate to all of the inputs and 12492 08:56:46,558 --> 08:56:50,398 here's what we want to do 12493 08:56:48,180 --> 08:56:51,478 we want to set up the value however in 12494 08:56:50,398 --> 08:56:53,760 this case 12495 08:56:51,478 --> 08:56:57,058 it's not equal to the state value it is 12496 08:56:53,760 --> 08:57:00,000 equal to a state value which is a user 12497 08:56:57,058 --> 08:57:02,878 and then that and then the property and 12498 08:57:00,000 --> 08:57:04,798 in this case of course it is name and as 12499 08:57:02,878 --> 08:57:08,760 far as the function well we go here with 12500 08:57:04,798 --> 08:57:10,200 on change and we set it equal to our 12501 08:57:08,760 --> 08:57:12,840 handle change 12502 08:57:10,200 --> 08:57:15,898 and effectively we want to repeat that 12503 08:57:12,840 --> 08:57:19,558 for all of the inputs again we have name 12504 08:57:15,898 --> 08:57:22,378 email as well as the password so let's 12505 08:57:19,558 --> 08:57:24,180 change it here to email 12506 08:57:22,378 --> 08:57:26,760 and 12507 08:57:24,180 --> 08:57:29,040 let me grab this one more time 12508 08:57:26,760 --> 08:57:31,138 and copy and paste 12509 08:57:29,040 --> 08:57:35,398 so of course now the state value is 12510 08:57:31,138 --> 08:57:38,760 going to be a password all right awesome 12511 08:57:35,398 --> 08:57:40,318 now let's decide how we can access the 12512 08:57:38,760 --> 08:57:42,600 actual value 12513 08:57:40,318 --> 08:57:45,718 because if I'm going to go over here 12514 08:57:42,600 --> 08:57:49,200 if I'm going to say e and 12515 08:57:45,718 --> 08:57:51,180 first let's just log e dot Target and 12516 08:57:49,200 --> 08:57:54,780 value you'll see that everything is 12517 08:57:51,180 --> 08:57:57,780 correct but we don't know which 12518 08:57:54,780 --> 08:57:59,760 input we're referencing here as far as 12519 08:57:57,780 --> 08:58:02,100 the target again let me remove this one 12520 08:57:59,760 --> 08:58:03,000 and then notice yeah everything is 12521 08:58:02,100 --> 08:58:05,700 awesome 12522 08:58:03,000 --> 08:58:07,740 I mean I can see whatever letters I'm 12523 08:58:05,700 --> 08:58:09,180 typing and of course the reason why we 12524 08:58:07,740 --> 08:58:11,878 don't see anything in the form is 12525 08:58:09,180 --> 08:58:15,898 because we're not updating anything so 12526 08:58:11,878 --> 08:58:18,120 the state values stay empty however 12527 08:58:15,898 --> 08:58:20,638 the big problem here is that I don't 12528 08:58:18,120 --> 08:58:24,000 know which input am I typing here 12529 08:58:20,638 --> 08:58:27,478 so yeah I have S but what is it name 12530 08:58:24,000 --> 08:58:30,540 email password and therefore we need to 12531 08:58:27,478 --> 08:58:31,558 use the name attribute remember the very 12532 08:58:30,540 --> 08:58:34,318 very 12533 08:58:31,558 --> 08:58:37,558 beginning of this section I covered it 12534 08:58:34,318 --> 08:58:41,040 we can also access the event Dot 12535 08:58:37,558 --> 08:58:43,378 target.9 and of course 12536 08:58:41,040 --> 08:58:45,120 that needs to be here in the input as 12537 08:58:43,378 --> 08:58:48,660 well and therefore I'm going to go here 12538 08:58:45,120 --> 08:58:51,180 with name and I'll set it equal to name 12539 08:58:48,660 --> 08:58:53,280 why I work because that's my state value 12540 08:58:51,180 --> 08:58:55,080 basically so this is always the funny 12541 08:58:53,280 --> 08:58:57,478 one when you show in the example because 12542 08:58:55,080 --> 08:59:00,840 it's like name equals to name yeah 12543 08:58:57,478 --> 08:59:02,818 that's effectively my property now it's 12544 08:59:00,840 --> 08:59:05,218 probably going to make more sense here 12545 08:59:02,818 --> 08:59:08,218 with an email because now name will be 12546 08:59:05,218 --> 08:59:10,680 equal to email and you guessed it 12547 08:59:08,218 --> 08:59:13,860 correctly we need to do the same thing 12548 08:59:10,680 --> 08:59:16,798 for the password so this is a must if 12549 08:59:13,860 --> 08:59:20,580 you have such setup where you have one 12550 08:59:16,798 --> 08:59:22,378 state value and it's an object yes you 12551 08:59:20,580 --> 08:59:24,540 will need to use this type of approach 12552 08:59:22,378 --> 08:59:27,180 you'll definitely need the name as you 12553 08:59:24,540 --> 08:59:28,920 saw before when we had two inputs and we 12554 08:59:27,180 --> 08:59:31,138 used Straight Up functions these State 12555 08:59:28,920 --> 08:59:33,420 functions you don't have to do that 12556 08:59:31,138 --> 08:59:35,638 I mean you can always add the name but 12557 08:59:33,420 --> 08:59:38,040 it's not a requirement in this case 12558 08:59:35,638 --> 08:59:40,318 since we have one function yeah this is 12559 08:59:38,040 --> 08:59:44,040 what we need to do so let's go here with 12560 08:59:40,318 --> 08:59:46,620 log and let's say event.target 12561 08:59:44,040 --> 08:59:49,378 and yes this will reference 12562 08:59:46,620 --> 08:59:52,620 basically whatever 12563 08:59:49,378 --> 08:59:55,020 input the user is typing in so this is 12564 08:59:52,620 --> 08:59:57,478 name and then this is the value the same 12565 08:59:55,020 --> 09:00:00,058 is going to be for email and you guessed 12566 08:59:57,478 --> 09:00:02,100 it for the password again we see nothing 12567 09:00:00,058 --> 09:00:03,058 yet in a browser because we're not 12568 09:00:02,100 --> 09:00:06,000 updating 12569 09:00:03,058 --> 09:00:08,160 but this is already an awesome start 12570 09:00:06,000 --> 09:00:10,500 so we have the name and we have the 12571 09:00:08,160 --> 09:00:12,958 value so here's what we want to do 12572 09:00:10,500 --> 09:00:15,000 we want to take this name and we're 12573 09:00:12,958 --> 09:00:15,958 going to update that specific property 12574 09:00:15,000 --> 09:00:19,378 here 12575 09:00:15,958 --> 09:00:21,780 in the object with what with this value 12576 09:00:19,378 --> 09:00:23,940 how we can do that well 12577 09:00:21,780 --> 09:00:25,740 this is the interesting part we need to 12578 09:00:23,940 --> 09:00:27,420 use the following logic first of all we 12579 09:00:25,740 --> 09:00:30,120 go with set user so that's our set 12580 09:00:27,420 --> 09:00:33,420 function then remember we have multiple 12581 09:00:30,120 --> 09:00:34,620 values so I cannot simply go with name 12582 09:00:33,420 --> 09:00:37,080 and then whatever 12583 09:00:34,620 --> 09:00:38,160 this is going to effectively remove 12584 09:00:37,080 --> 09:00:40,620 these two 12585 09:00:38,160 --> 09:00:42,180 if I use such approach so that's not 12586 09:00:40,620 --> 09:00:45,240 going to work what we can do 12587 09:00:42,180 --> 09:00:46,978 solution is dot dot and then user so 12588 09:00:45,240 --> 09:00:50,520 essentially I want to spread out again 12589 09:00:46,978 --> 09:00:53,040 all of the existing values first 12590 09:00:50,520 --> 09:00:56,160 because I'll be overriding 12591 09:00:53,040 --> 09:00:59,700 only one at a time right I'll be typing 12592 09:00:56,160 --> 09:01:03,240 in one input at a time and after that we 12593 09:00:59,700 --> 09:01:06,540 want to dynamically update the proper in 12594 09:01:03,240 --> 09:01:09,000 the state and set it equal to Value so 12595 09:01:06,540 --> 09:01:10,920 first let me set up the code and then 12596 09:01:09,000 --> 09:01:13,558 I'll try to 12597 09:01:10,920 --> 09:01:15,600 explain as best as I can because again 12598 09:01:13,558 --> 09:01:18,600 this is something that pops up quite 12599 09:01:15,600 --> 09:01:21,478 often in the course q a where I'm gonna 12600 09:01:18,600 --> 09:01:23,420 go with these square brackets I'll pass 12601 09:01:21,478 --> 09:01:25,680 in 12602 09:01:23,420 --> 09:01:27,898 event.target.name and please keep in 12603 09:01:25,680 --> 09:01:31,740 mind that this value 12604 09:01:27,898 --> 09:01:32,700 will be either name email or password in 12605 09:01:31,740 --> 09:01:35,100 this case 12606 09:01:32,700 --> 09:01:37,378 so of course if you have 15 of them 12607 09:01:35,100 --> 09:01:39,660 then it's going to be one of those 15. 12608 09:01:37,378 --> 09:01:42,180 so this will reference whatever we have 12609 09:01:39,660 --> 09:01:45,120 here in the name and what happens it 12610 09:01:42,180 --> 09:01:47,120 will access dynamically this property in 12611 09:01:45,120 --> 09:01:49,978 the object that's why it's so important 12612 09:01:47,120 --> 09:01:53,160 that they are the same 12613 09:01:49,978 --> 09:01:55,680 that essentially you have the properties 12614 09:01:53,160 --> 09:01:57,978 in the object and they match exactly 12615 09:01:55,680 --> 09:02:01,080 what you have over here because 12616 09:01:57,978 --> 09:02:02,160 otherwise if this value for example is 12617 09:02:01,080 --> 09:02:04,558 names 12618 09:02:02,160 --> 09:02:06,660 and you'll try to set it in the object 12619 09:02:04,558 --> 09:02:09,240 it's not going to work so make sure that 12620 09:02:06,660 --> 09:02:11,340 they match exactly whatever you have in 12621 09:02:09,240 --> 09:02:13,500 the object so we are accessing 12622 09:02:11,340 --> 09:02:16,020 dynamically this property and we'll set 12623 09:02:13,500 --> 09:02:18,080 it equal to what we have here the value 12624 09:02:16,020 --> 09:02:22,020 right so we'll go with 12625 09:02:18,080 --> 09:02:23,580 event.target and then dot value let me 12626 09:02:22,020 --> 09:02:25,798 save and like I promised I will come 12627 09:02:23,580 --> 09:02:28,080 back to this one don't worry and now 12628 09:02:25,798 --> 09:02:31,200 let's go over here and notice we nicely 12629 09:02:28,080 --> 09:02:33,660 type and I still have those logs I don't 12630 09:02:31,200 --> 09:02:35,160 think I need them let me navigate to the 12631 09:02:33,660 --> 09:02:38,280 big browser 12632 09:02:35,160 --> 09:02:40,440 and let's see we should see the state 12633 09:02:38,280 --> 09:02:42,718 value as well 12634 09:02:40,440 --> 09:02:45,000 looks like I need to refresh here as far 12635 09:02:42,718 --> 09:02:49,500 as Veet let me try multiple inputs 12636 09:02:45,000 --> 09:02:51,780 notice so now my state is an object and 12637 09:02:49,500 --> 09:02:53,280 as I'm typing I'm actually providing 12638 09:02:51,780 --> 09:02:56,398 values over here 12639 09:02:53,280 --> 09:02:58,798 for the name for the email hand password 12640 09:02:56,398 --> 09:03:01,740 and like I said yeah this triggers a 12641 09:02:58,798 --> 09:03:04,200 bunch of questions in course q a 12642 09:03:01,740 --> 09:03:06,660 so as far as my explanation well let's 12643 09:03:04,200 --> 09:03:09,540 think about it if I'm gonna go over here 12644 09:03:06,660 --> 09:03:11,340 and if I'm gonna just hard code one of 12645 09:03:09,540 --> 09:03:14,818 the properties if I'll say 12646 09:03:11,340 --> 09:03:15,718 name like so in any input I'm going to 12647 09:03:14,818 --> 09:03:17,700 type 12648 09:03:15,718 --> 09:03:20,040 I'll be just updating the name so let's 12649 09:03:17,700 --> 09:03:23,160 try this one out notice 12650 09:03:20,040 --> 09:03:26,520 I can save all here and basically notice 12651 09:03:23,160 --> 09:03:28,740 how it essentially just typing a value 12652 09:03:26,520 --> 09:03:30,958 over here even though we're typing in 12653 09:03:28,740 --> 09:03:32,878 this input and the reason why we see 12654 09:03:30,958 --> 09:03:35,100 nothing in these ones well because these 12655 09:03:32,878 --> 09:03:37,378 ones are still empty correct so even 12656 09:03:35,100 --> 09:03:39,420 though I'm typing in the correct input 12657 09:03:37,378 --> 09:03:43,378 as far as the value it's showing up here 12658 09:03:39,420 --> 09:03:44,760 in the name because I'm hard coding here 12659 09:03:43,378 --> 09:03:47,520 and the same is going to work for 12660 09:03:44,760 --> 09:03:49,798 password the email and all that that's 12661 09:03:47,520 --> 09:03:52,020 why we need to set this one dynamic 12662 09:03:49,798 --> 09:03:54,840 so this will be dynamic depending on 12663 09:03:52,020 --> 09:03:57,540 what depending what we have here 12664 09:03:54,840 --> 09:04:01,080 so if it's time then this will 12665 09:03:57,540 --> 09:04:03,000 dynamically access the property in the 12666 09:04:01,080 --> 09:04:05,818 object that is name 12667 09:04:03,000 --> 09:04:07,200 if it's email then it's email hopefully 12668 09:04:05,818 --> 09:04:09,860 you see where I'm going with this and 12669 09:04:07,200 --> 09:04:12,540 that's why we go here with event Target 12670 09:04:09,860 --> 09:04:15,840 and name of course if you have already 12671 09:04:12,540 --> 09:04:18,540 some experience with Dynamic object Keys 12672 09:04:15,840 --> 09:04:20,940 then this should look very familiar 12673 09:04:18,540 --> 09:04:23,218 again the reason for that is because we 12674 09:04:20,940 --> 09:04:26,218 have multiple properties we have 12675 09:04:23,218 --> 09:04:29,340 multiple inputs but we have one function 12676 09:04:26,218 --> 09:04:31,920 and therefore every time the user types 12677 09:04:29,340 --> 09:04:34,260 something in one of the inputs I want to 12678 09:04:31,920 --> 09:04:36,180 check two things not only I want to get 12679 09:04:34,260 --> 09:04:38,398 the value but I also want to get the 12680 09:04:36,180 --> 09:04:40,080 property and I want to access that 12681 09:04:38,398 --> 09:04:44,218 property and I'm going to set it equal 12682 09:04:40,080 --> 09:04:46,798 to whatever value I have in the input 12683 09:04:44,218 --> 09:04:50,160 and as far as the handle submit let's 12684 09:04:46,798 --> 09:04:51,180 set it up here let's say on submit in 12685 09:04:50,160 --> 09:04:54,540 this case 12686 09:04:51,180 --> 09:04:59,040 let's come up with the function name and 12687 09:04:54,540 --> 09:05:01,440 I'll submit and you know what I think 12688 09:04:59,040 --> 09:05:04,260 I'm just going to copy and paste I think 12689 09:05:01,440 --> 09:05:08,520 it's going to be faster so handle not 12690 09:05:04,260 --> 09:05:10,378 change we're looking for submit we do 12691 09:05:08,520 --> 09:05:13,020 want to get the object we do want to 12692 09:05:10,378 --> 09:05:15,898 prevent the default 12693 09:05:13,020 --> 09:05:17,218 and instead of setting the user I simply 12694 09:05:15,898 --> 09:05:20,700 want to log it 12695 09:05:17,218 --> 09:05:23,760 and again the only gotcha right now is 12696 09:05:20,700 --> 09:05:26,458 that it's a state value so once I type 12697 09:05:23,760 --> 09:05:27,420 here and of course since this is an 12698 09:05:26,458 --> 09:05:30,540 email 12699 09:05:27,420 --> 09:05:33,120 field we will have to add the add one 12700 09:05:30,540 --> 09:05:35,280 and let's go with mail.com 12701 09:05:33,120 --> 09:05:36,898 and check it out the moment I press 12702 09:05:35,280 --> 09:05:39,600 submit 12703 09:05:36,898 --> 09:05:41,458 now I have the object in the console so 12704 09:05:39,600 --> 09:05:44,218 essentially rest of the logic is the 12705 09:05:41,458 --> 09:05:46,440 same whatever you want to do after that 12706 09:05:44,218 --> 09:05:48,660 so yes of course you'll check for empty 12707 09:05:46,440 --> 09:05:51,360 values then you'll set up some kind of 12708 09:05:48,660 --> 09:05:53,878 functionality and a common approach is 12709 09:05:51,360 --> 09:05:55,740 then to clear the values so this doesn't 12710 09:05:53,878 --> 09:05:58,318 change the only difference that right 12711 09:05:55,740 --> 09:06:01,260 now we have an object and while we're 12712 09:05:58,318 --> 09:06:03,840 still in a row let's also cover 12713 09:06:01,260 --> 09:06:06,420 a few other input options more 12714 09:06:03,840 --> 09:06:09,898 specifically I'll show you the gotcha 12715 09:06:06,420 --> 09:06:12,480 when it comes to check boxes and we'll 12716 09:06:09,898 --> 09:06:15,480 also take a look how we can display the 12717 09:06:12,480 --> 09:06:20,098 select input basically when we have a 12718 09:06:15,480 --> 09:06:23,282 list of options so in the app jsx grab 12719 09:06:20,098 --> 09:06:24,840 the starter from zero for other inputs 12720 09:06:23,282 --> 09:06:27,418 and this is what you should see on the 12721 09:06:24,840 --> 09:06:30,300 screen essentially a forum with few 12722 09:06:27,418 --> 09:06:32,938 labels and a submit button 12723 09:06:30,300 --> 09:06:36,598 so first let's go to the file we're 12724 09:06:32,938 --> 09:06:40,258 looking for other inputs in Star and 12725 09:06:36,598 --> 09:06:41,878 this is going to be our entry point 12726 09:06:40,258 --> 09:06:44,098 um let's start by setting up two State 12727 09:06:41,878 --> 09:06:45,782 values and let's start with shipping 12728 09:06:44,098 --> 09:06:48,300 which essentially is going to be our 12729 09:06:45,782 --> 09:06:51,660 Boolean value so here let's go with 12730 09:06:48,300 --> 09:06:52,918 const and then shipping and set 12731 09:06:51,660 --> 09:06:57,238 shipping 12732 09:06:52,918 --> 09:06:59,398 and we'll set it equal to use State and 12733 09:06:57,238 --> 09:07:02,578 by default it's going to be false and 12734 09:06:59,398 --> 09:07:04,078 then we want to navigate to our form and 12735 09:07:02,578 --> 09:07:07,500 we're looking 12736 09:07:04,078 --> 09:07:10,622 for this one the first form row where we 12737 09:07:07,500 --> 09:07:12,960 have the free shipping in the label and 12738 09:07:10,622 --> 09:07:15,898 in here let's set up the input first 12739 09:07:12,960 --> 09:07:19,378 it's not going to be equal to type text 12740 09:07:15,898 --> 09:07:22,200 we actually want to go with checkbox 12741 09:07:19,378 --> 09:07:24,058 and since Emmett right away spits out 12742 09:07:22,200 --> 09:07:26,578 this name one we might as well provide 12743 09:07:24,058 --> 09:07:29,578 that so we're not going to use it 12744 09:07:26,578 --> 09:07:32,040 in this example but normally if you 12745 09:07:29,578 --> 09:07:34,020 would you type whatever is the state 12746 09:07:32,040 --> 09:07:35,758 value over here and then when it comes 12747 09:07:34,020 --> 09:07:38,820 to Eddie same deal we're going to go 12748 09:07:35,758 --> 09:07:42,000 with shipping let me save and now notice 12749 09:07:38,820 --> 09:07:45,960 where I have our own little checkbox 12750 09:07:42,000 --> 09:07:49,738 over here after that let's set up the on 12751 09:07:45,960 --> 09:07:54,180 change and also let's create the 12752 09:07:49,738 --> 09:07:57,718 function now unlike the input the 12753 09:07:54,180 --> 09:08:00,180 typical text or email or password we 12754 09:07:57,718 --> 09:08:01,980 actually want to go with checked and we 12755 09:08:00,180 --> 09:08:04,438 want to set it equal to that state value 12756 09:08:01,980 --> 09:08:06,782 so unlike the value equals to the state 12757 09:08:04,438 --> 09:08:10,320 value now we go checked 12758 09:08:06,782 --> 09:08:12,480 so let's go here first checked and that 12759 09:08:10,320 --> 09:08:15,360 is equal to my state value which again 12760 09:08:12,480 --> 09:08:18,058 is a Boolean and then when it comes to 12761 09:08:15,360 --> 09:08:20,640 function first we need to create one so 12762 09:08:18,058 --> 09:08:22,258 let me go over here and in my case I'm 12763 09:08:20,640 --> 09:08:24,540 going to call this handle 12764 09:08:22,258 --> 09:08:27,058 shipping 12765 09:08:24,540 --> 09:08:28,738 so that's my function I'll be looking 12766 09:08:27,058 --> 09:08:32,340 for the event 12767 09:08:28,738 --> 09:08:35,300 object and as far as the logic 12768 09:08:32,340 --> 09:08:37,378 first let's just log 12769 09:08:35,300 --> 09:08:39,840 event.target.checked so that's the one 12770 09:08:37,378 --> 09:08:41,282 that we're going to use not event.target 12771 09:08:39,840 --> 09:08:42,960 that value 12772 09:08:41,282 --> 09:08:46,140 because notice we don't have the value 12773 09:08:42,960 --> 09:08:49,378 in here we have the checked so let's go 12774 09:08:46,140 --> 09:08:52,438 here let's say event dot Target and 12775 09:08:49,378 --> 09:08:54,300 checked and I'll right away use the 12776 09:08:52,438 --> 09:08:56,820 value that's coming back and I'll set it 12777 09:08:54,300 --> 09:08:59,520 equal to my shipping in the state so in 12778 09:08:56,820 --> 09:09:01,860 here I'll say set shipping and set it 12779 09:08:59,520 --> 09:09:03,660 equal to that value 12780 09:09:01,860 --> 09:09:07,320 and essentially it's going to be either 12781 09:09:03,660 --> 09:09:09,660 true or false depending whether you have 12782 09:09:07,320 --> 09:09:11,398 clicked on it or not 12783 09:09:09,660 --> 09:09:14,640 and the very last thing we want to do 12784 09:09:11,398 --> 09:09:15,960 over here is to set up on change and set 12785 09:09:14,640 --> 09:09:19,200 it equal 12786 09:09:15,960 --> 09:09:21,360 to the handle shipping let's save this 12787 09:09:19,200 --> 09:09:24,300 again we'll have this warning in the 12788 09:09:21,360 --> 09:09:26,282 console but basically you'll notice that 12789 09:09:24,300 --> 09:09:29,160 every time we click on the checkbox 12790 09:09:26,282 --> 09:09:31,500 we'll get back the value and we're 12791 09:09:29,160 --> 09:09:33,718 setting that in a state 12792 09:09:31,500 --> 09:09:36,000 and I guess the major gotcha about the 12793 09:09:33,718 --> 09:09:38,820 checkbox is that we want to use the 12794 09:09:36,000 --> 09:09:41,640 checked instead of the value and when it 12795 09:09:38,820 --> 09:09:44,578 comes to select input essentially it's a 12796 09:09:41,640 --> 09:09:47,218 list of options so normally you'll have 12797 09:09:44,578 --> 09:09:49,020 some kind of list again typically it's 12798 09:09:47,218 --> 09:09:52,558 going to be dynamic so it's going to be 12799 09:09:49,020 --> 09:09:55,558 coming from the API and you want to 12800 09:09:52,558 --> 09:09:59,040 render them inside of the select input 12801 09:09:55,558 --> 09:10:00,418 and you'll set them up as option element 12802 09:09:59,040 --> 09:10:02,640 and then you're going to State there's 12803 09:10:00,418 --> 09:10:05,340 going to be that one value that 12804 09:10:02,640 --> 09:10:06,418 represents whatever the user has 12805 09:10:05,340 --> 09:10:09,000 selected 12806 09:10:06,418 --> 09:10:12,238 so first we need to create a state value 12807 09:10:09,000 --> 09:10:14,700 and here just be careful because I named 12808 09:10:12,238 --> 09:10:17,700 the state value framework 12809 09:10:14,700 --> 09:10:19,200 and the latest Frameworks 12810 09:10:17,700 --> 09:10:21,660 and 12811 09:10:19,200 --> 09:10:23,700 you don't want to mix them basically 12812 09:10:21,660 --> 09:10:26,282 when we want to display the options we 12813 09:10:23,700 --> 09:10:28,320 want to iterate over Frameworks but when 12814 09:10:26,282 --> 09:10:30,238 we want to provide the value we want to 12815 09:10:28,320 --> 09:10:31,378 look for framework 12816 09:10:30,238 --> 09:10:33,840 and 12817 09:10:31,378 --> 09:10:37,078 I'll remind you that once we get there 12818 09:10:33,840 --> 09:10:40,020 so frame work and that is going to be 12819 09:10:37,078 --> 09:10:42,180 equal to set framework 12820 09:10:40,020 --> 09:10:46,200 so let me copy and paste and I just want 12821 09:10:42,180 --> 09:10:48,000 to change it to a uppercase and as far 12822 09:10:46,200 --> 09:10:50,340 as the default value this is totally up 12823 09:10:48,000 --> 09:10:52,500 to you I'm gonna go with react since 12824 09:10:50,340 --> 09:10:55,378 that's the first item here in this list 12825 09:10:52,500 --> 09:10:56,282 but of course you can set any of these 12826 09:10:55,378 --> 09:10:58,680 values 12827 09:10:56,282 --> 09:11:00,058 let's also create the function because 12828 09:10:58,680 --> 09:11:01,438 we'll need that 12829 09:11:00,058 --> 09:11:03,418 so for now 12830 09:11:01,438 --> 09:11:05,460 copy and paste so it needs to be the sub 12831 09:11:03,418 --> 09:11:08,578 we won't 12832 09:11:05,460 --> 09:11:10,622 actually use the checked one so of 12833 09:11:08,578 --> 09:11:12,898 course I can remove we won't set up the 12834 09:11:10,622 --> 09:11:14,938 logic for now we'll simply go with 12835 09:11:12,898 --> 09:11:18,540 handle and then let's call this frame 12836 09:11:14,938 --> 09:11:22,140 work let's save that and after that we 12837 09:11:18,540 --> 09:11:24,718 want to set up the select input where we 12838 09:11:22,140 --> 09:11:27,360 have the label for the framework so 12839 09:11:24,718 --> 09:11:30,300 let's go here with select again Emmett 12840 09:11:27,360 --> 09:11:32,520 is going to spit right away name and ID 12841 09:11:30,300 --> 09:11:35,758 so we might as well provide those values 12842 09:11:32,520 --> 09:11:38,460 so in here let's just write framework 12843 09:11:35,758 --> 09:11:41,098 so again this is very important we are 12844 09:11:38,460 --> 09:11:42,000 now referencing the state value not the 12845 09:11:41,098 --> 09:11:44,340 list 12846 09:11:42,000 --> 09:11:47,098 we'll work with the list in a second so 12847 09:11:44,340 --> 09:11:50,878 set it equal to framework 12848 09:11:47,098 --> 09:11:53,398 and as far as the options this is where 12849 09:11:50,878 --> 09:11:55,438 we want to iterate over the list so 12850 09:11:53,398 --> 09:11:58,558 first let's display them and then we'll 12851 09:11:55,438 --> 09:12:01,438 add the value as well as the on change 12852 09:11:58,558 --> 09:12:04,078 so let's start over here we effectively 12853 09:12:01,438 --> 09:12:06,860 want to iterate over the Frameworks so 12854 09:12:04,078 --> 09:12:11,160 we set up here the curlies we go with 12855 09:12:06,860 --> 09:12:15,418 Frameworks then dot map so we're mapping 12856 09:12:11,160 --> 09:12:17,578 over and then as far as the value 12857 09:12:15,418 --> 09:12:18,860 I'm going to reference this as a 12858 09:12:17,578 --> 09:12:21,418 framework 12859 09:12:18,860 --> 09:12:23,398 framework and as far as the return we 12860 09:12:21,418 --> 09:12:26,098 want to go with keyword and then we're 12861 09:12:23,398 --> 09:12:28,800 looking for the option 12862 09:12:26,098 --> 09:12:30,238 then since we have a list yes we still 12863 09:12:28,800 --> 09:12:32,282 need to provide a key 12864 09:12:30,238 --> 09:12:34,258 and in this case we don't have the Eddie 12865 09:12:32,282 --> 09:12:35,878 however those names are unique so I'm 12866 09:12:34,258 --> 09:12:37,378 going to go with framework so 12867 09:12:35,878 --> 09:12:39,718 essentially I'm referencing the 12868 09:12:37,378 --> 09:12:42,782 parameter and then inside the option 12869 09:12:39,718 --> 09:12:45,360 This is Where I Wanna showcase 12870 09:12:42,782 --> 09:12:46,918 one of those options let me save and 12871 09:12:45,360 --> 09:12:48,840 notice that everything is correct 12872 09:12:46,918 --> 09:12:50,878 however nothing is going to happen 12873 09:12:48,840 --> 09:12:54,480 so essentially yes we're changing this 12874 09:12:50,878 --> 09:12:56,700 but if we navigate to the big screen 12875 09:12:54,480 --> 09:12:57,782 you'll see that I'm not changing that 12876 09:12:56,700 --> 09:13:00,058 state value 12877 09:12:57,782 --> 09:13:03,238 because we haven't set up the logic yet 12878 09:13:00,058 --> 09:13:04,800 so technically this is view but of 12879 09:13:03,238 --> 09:13:08,520 course in the state I still have the 12880 09:13:04,800 --> 09:13:10,078 react and it's very similar to typical 12881 09:13:08,520 --> 09:13:12,238 input 12882 09:13:10,078 --> 09:13:15,300 so we don't need to go here we checked 12883 09:13:12,238 --> 09:13:18,840 that was only for the checkbox again we 12884 09:13:15,300 --> 09:13:21,300 go with value we go with frame work 12885 09:13:18,840 --> 09:13:22,460 very very important not Frameworks but 12886 09:13:21,300 --> 09:13:27,782 framework 12887 09:13:22,460 --> 09:13:29,098 and as far as the on change well we want 12888 09:13:27,782 --> 09:13:31,680 to provide 12889 09:13:29,098 --> 09:13:33,238 our handle framework correct let's 12890 09:13:31,680 --> 09:13:35,340 provide this function 12891 09:13:33,238 --> 09:13:38,098 and as far as the functionality we 12892 09:13:35,340 --> 09:13:41,000 simply want to go with set framework and 12893 09:13:38,098 --> 09:13:44,758 it's going to be located again in event 12894 09:13:41,000 --> 09:13:47,460 dot Target value so if you're wondering 12895 09:13:44,758 --> 09:13:50,218 can we pass directly the arrow function 12896 09:13:47,460 --> 09:13:51,540 like we have been doing yes absolutely I 12897 09:13:50,218 --> 09:13:54,300 just thought since we have handle 12898 09:13:51,540 --> 09:13:57,058 shipping here we might as well set up 12899 09:13:54,300 --> 09:14:00,000 another one so we go with framework and 12900 09:13:57,058 --> 09:14:02,520 then we'll look for event.target and 12901 09:14:00,000 --> 09:14:05,340 then the value let's save that and now 12902 09:14:02,520 --> 09:14:07,622 if I navigate to the big screen we 12903 09:14:05,340 --> 09:14:10,078 should see that we're also successfully 12904 09:14:07,622 --> 09:14:13,460 changing the state value so that's how 12905 09:14:10,078 --> 09:14:18,000 we can work with check boxes as well as 12906 09:14:13,460 --> 09:14:20,218 select inputs in react okay and as a 12907 09:14:18,000 --> 09:14:23,098 very last thing in this section I want 12908 09:14:20,218 --> 09:14:26,098 to show you a very Nifty alternative to 12909 09:14:23,098 --> 09:14:29,578 controlled inputs you see we can also 12910 09:14:26,098 --> 09:14:32,160 submit the form with uncontrolled inputs 12911 09:14:29,578 --> 09:14:35,398 basically without referencing the state 12912 09:14:32,160 --> 09:14:37,320 value now in the following section I'll 12913 09:14:35,398 --> 09:14:41,418 show you an example 12914 09:14:37,320 --> 09:14:44,820 of how we can do that using use ref hook 12915 09:14:41,418 --> 09:14:48,122 but in this video I'll show you how we 12916 09:14:44,820 --> 09:14:51,898 can accomplish the same task with form 12917 09:14:48,122 --> 09:14:54,960 data API which in my opinion is a better 12918 09:14:51,898 --> 09:14:58,320 approach if you have more than one input 12919 09:14:54,960 --> 09:15:01,738 as far as form data API it's coming from 12920 09:14:58,320 --> 09:15:04,918 vanilla.js and effectively 12921 09:15:01,738 --> 09:15:06,540 it's a interface that allows us to 12922 09:15:04,918 --> 09:15:10,378 construct 12923 09:15:06,540 --> 09:15:14,098 a set of key value pairs representing 12924 09:15:10,378 --> 09:15:16,200 the form fields and their values and if 12925 09:15:14,098 --> 09:15:18,540 this sounds somewhat funky don't worry 12926 09:15:16,200 --> 09:15:21,300 as we're going to be working through the 12927 09:15:18,540 --> 09:15:24,180 example you'll see what I mean now if 12928 09:15:21,300 --> 09:15:25,200 you're not familiar with form data API 12929 09:15:24,180 --> 09:15:27,480 or 12930 09:15:25,200 --> 09:15:30,300 you just want to get more info on it 12931 09:15:27,480 --> 09:15:34,500 please reference this JS nuggets video 12932 09:15:30,300 --> 09:15:37,980 where I cover warm data API in Greater 12933 09:15:34,500 --> 09:15:40,020 detail effectively form data API has 12934 09:15:37,980 --> 09:15:43,020 quite a few methods on it 12935 09:15:40,020 --> 09:15:46,200 and since I don't want this video to be 12936 09:15:43,020 --> 09:15:49,020 half an hour long I'll just show you the 12937 09:15:46,200 --> 09:15:52,438 most common ones so first let's just go 12938 09:15:49,020 --> 09:15:53,520 to app GSX notice over here I have the 12939 09:15:52,438 --> 09:15:57,418 starter 12940 09:15:53,520 --> 09:15:59,578 I'm looking for zero five form data and 12941 09:15:57,418 --> 09:16:02,218 once you navigate you'll see that we 12942 09:15:59,578 --> 09:16:03,598 have a state value so essentially it 12943 09:16:02,218 --> 09:16:06,782 doesn't do anything I'm just going to 12944 09:16:03,598 --> 09:16:08,820 use it to Showcase something we also 12945 09:16:06,782 --> 09:16:11,578 have right away handle submit on the 12946 09:16:08,820 --> 09:16:14,160 form and we prevent a default hopefully 12947 09:16:11,578 --> 09:16:18,300 we are clear why we need that and then 12948 09:16:14,160 --> 09:16:20,820 we have three inputs so we have the name 12949 09:16:18,300 --> 09:16:24,122 one the email as well as the password 12950 09:16:20,820 --> 09:16:27,058 and as you can see I messed up over here 12951 09:16:24,122 --> 09:16:30,660 that should be a password I already 12952 09:16:27,058 --> 09:16:33,300 provided the ID and I also provided the 12953 09:16:30,660 --> 09:16:35,398 names this is very important when you 12954 09:16:33,300 --> 09:16:37,438 work with form data it's a must to have 12955 09:16:35,398 --> 09:16:40,738 a name that 12956 09:16:37,438 --> 09:16:42,540 effectively represents that input so if 12957 09:16:40,738 --> 09:16:44,460 you do decide to work with form data 12958 09:16:42,540 --> 09:16:46,558 just don't forget about the name it's 12959 09:16:44,460 --> 09:16:48,718 very easy to do that and then your 12960 09:16:46,558 --> 09:16:50,640 functionality is not going to work 12961 09:16:48,718 --> 09:16:51,960 and as far as the setup let's start with 12962 09:16:50,640 --> 09:16:54,598 this 12963 09:16:51,960 --> 09:16:57,500 let's get on the same page that if I go 12964 09:16:54,598 --> 09:17:01,258 to handle submit and if I log not 12965 09:16:57,500 --> 09:17:02,578 event.target but if I log event dot 12966 09:17:01,258 --> 09:17:04,918 current Target 12967 09:17:02,578 --> 09:17:08,520 this will point to what 12968 09:17:04,918 --> 09:17:10,622 this will actually point to a form so if 12969 09:17:08,520 --> 09:17:14,040 I go to console and if I submit notice 12970 09:17:10,622 --> 09:17:16,622 this returns a form element 12971 09:17:14,040 --> 09:17:18,480 and as far as the difference so the 12972 09:17:16,622 --> 09:17:22,140 event.target 12973 09:17:18,480 --> 09:17:25,860 refers to the Dom element that triggers 12974 09:17:22,140 --> 09:17:29,398 an event however the current Target 12975 09:17:25,860 --> 09:17:33,180 refers to the Dom element that event 12976 09:17:29,398 --> 09:17:35,282 listener is listening on so in simple 12977 09:17:33,180 --> 09:17:37,680 English in our case 12978 09:17:35,282 --> 09:17:41,098 since the event 12979 09:17:37,680 --> 09:17:42,898 our on submit is set on a form 12980 09:17:41,098 --> 09:17:46,438 sure enough 12981 09:17:42,898 --> 09:17:49,078 event dot current Target returns are 12982 09:17:46,438 --> 09:17:51,738 form element now why is that useful 12983 09:17:49,078 --> 09:17:56,700 because in vanilla Jazz there's form 12984 09:17:51,738 --> 09:18:00,660 data API which is just a cool setup how 12985 09:17:56,700 --> 09:18:01,378 we can directly access the inputs we 12986 09:18:00,660 --> 09:18:05,758 have 12987 09:18:01,378 --> 09:18:08,578 as well as the values so in this case 12988 09:18:05,758 --> 09:18:13,020 I can go with const and I'll call this 12989 09:18:08,578 --> 09:18:16,918 form data and I'll set it equal to new 12990 09:18:13,020 --> 09:18:20,160 form data and one gotcha don't call the 12991 09:18:16,918 --> 09:18:22,378 component form data essentially you'll 12992 09:18:20,160 --> 09:18:24,418 have an error that's why I named this 12993 09:18:22,378 --> 09:18:26,820 uncontrolled inputs not form data 12994 09:18:24,418 --> 09:18:28,800 because essentially 12995 09:18:26,820 --> 09:18:31,800 react is going to think that you're 12996 09:18:28,800 --> 09:18:33,898 referencing the component and once we 12997 09:18:31,800 --> 09:18:36,000 have the new form data it's looking for 12998 09:18:33,898 --> 09:18:39,480 that form that's why we're going to go 12999 09:18:36,000 --> 09:18:43,258 with event dot current Target so we go 13000 09:18:39,480 --> 09:18:44,820 here with event and current Target and 13001 09:18:43,258 --> 09:18:46,500 let's just see what we get back so let 13002 09:18:44,820 --> 09:18:49,680 me move this sucker down 13003 09:18:46,500 --> 09:18:53,700 and I'm going to go with form data now 13004 09:18:49,680 --> 09:18:55,378 don't be surprised if you see Elvis in 13005 09:18:53,700 --> 09:18:57,540 the console so don't freak out 13006 09:18:55,378 --> 09:18:59,160 essentially everything is correct it 13007 09:18:57,540 --> 09:19:00,598 just works a little bit differently so 13008 09:18:59,160 --> 09:19:02,282 the first time I started working with 13009 09:19:00,598 --> 09:19:04,918 form data I was like well wait a minute 13010 09:19:02,282 --> 09:19:07,020 did I pass the correct 13011 09:19:04,918 --> 09:19:09,058 Target because I don't see anything 13012 09:19:07,020 --> 09:19:11,520 don't worry 13013 09:19:09,058 --> 09:19:15,480 in order to access the values 13014 09:19:11,520 --> 09:19:18,480 we have few approaches we can use the 13015 09:19:15,480 --> 09:19:19,500 get method on this form data so for 13016 09:19:18,480 --> 09:19:22,256 example 13017 09:19:19,500 --> 09:19:24,480 if I want to get the name 13018 09:19:22,256 --> 09:19:25,980 I'm going to go here with name I'm going 13019 09:19:24,480 --> 09:19:29,040 to set up some kind of variable we go 13020 09:19:25,980 --> 09:19:31,740 with form data and we go with get and 13021 09:19:29,040 --> 09:19:33,840 then whatever is the name so again 13022 09:19:31,740 --> 09:19:36,240 that's why it's important whatever is 13023 09:19:33,840 --> 09:19:38,340 this value over here so if I'm going to 13024 09:19:36,240 --> 09:19:40,200 go with name you can probably already 13025 09:19:38,340 --> 09:19:42,480 guess what we're going to be getting 13026 09:19:40,200 --> 09:19:45,360 back correct so let's try something else 13027 09:19:42,480 --> 09:19:48,416 why don't we try 13028 09:19:45,360 --> 09:19:50,520 let's say email I'm gonna go here email 13029 09:19:48,416 --> 09:19:53,220 and yes of course that input needs to be 13030 09:19:50,520 --> 09:19:55,800 there and if I log this 13031 09:19:53,220 --> 09:19:58,200 I'll actually see the value once we 13032 09:19:55,800 --> 09:20:02,820 submit the form so if I'll type here 13033 09:19:58,200 --> 09:20:06,120 John and Gmail once I submit check it 13034 09:20:02,820 --> 09:20:07,560 out I'll have this value over here so we 13035 09:20:06,120 --> 09:20:09,300 have a few options like I said I'm not 13036 09:20:07,560 --> 09:20:11,460 going to cover each and every method if 13037 09:20:09,300 --> 09:20:14,220 you want to find out more about form 13038 09:20:11,460 --> 09:20:16,436 data please reference that video but in 13039 09:20:14,220 --> 09:20:18,720 this instance what we can do one by one 13040 09:20:16,436 --> 09:20:20,756 we can grab the email password and name 13041 09:20:18,720 --> 09:20:23,460 yes that's definitely 13042 09:20:20,756 --> 09:20:25,740 one option however there's a little bit 13043 09:20:23,460 --> 09:20:29,640 better option where we can transform 13044 09:20:25,740 --> 09:20:31,860 this into an object directly and the way 13045 09:20:29,640 --> 09:20:34,436 we do that we use object Dot from 13046 09:20:31,860 --> 09:20:36,840 entries which again is 13047 09:20:34,436 --> 09:20:39,360 essentially a method that we get from 13048 09:20:36,840 --> 09:20:43,436 vanilla.js because what we need to keep 13049 09:20:39,360 --> 09:20:46,200 in mind that this form data actually is 13050 09:20:43,436 --> 09:20:48,780 an array of arrays and just so you don't 13051 09:20:46,200 --> 09:20:51,840 think that I'm making this up let me go 13052 09:20:48,780 --> 09:20:54,480 here where I have the log let me go with 13053 09:20:51,840 --> 09:20:58,140 spread operator I'm going to go dot dot 13054 09:20:54,480 --> 09:20:59,936 and then we're looking for form data Dot 13055 09:20:58,140 --> 09:21:02,880 and I'm going to go with entries just so 13056 09:20:59,936 --> 09:21:05,220 you can see both of them then let me 13057 09:21:02,880 --> 09:21:07,560 save with me refresh and I'll have to 13058 09:21:05,220 --> 09:21:09,840 provide the values again so in this case 13059 09:21:07,560 --> 09:21:13,520 I'm going to go John 13060 09:21:09,840 --> 09:21:13,520 John over here as well 13061 09:21:14,160 --> 09:21:18,660 and then lastly we want to go here with 13062 09:21:16,800 --> 09:21:22,256 some kind of password and once we click 13063 09:21:18,660 --> 09:21:25,500 like I said what we're getting back from 13064 09:21:22,256 --> 09:21:28,560 form data entries and as I said you can 13065 09:21:25,500 --> 09:21:30,540 find all these methods over here notice 13066 09:21:28,560 --> 09:21:32,340 it has quite a few of them that's why I 13067 09:21:30,540 --> 09:21:36,120 said that we're not going to cover all 13068 09:21:32,340 --> 09:21:38,880 of them I can spread out the entries and 13069 09:21:36,120 --> 09:21:40,740 I'll have this array of arrays so this 13070 09:21:38,880 --> 09:21:42,840 is going to be the name of the input and 13071 09:21:40,740 --> 09:21:45,240 then this is going to be the value 13072 09:21:42,840 --> 09:21:48,840 and even though this is nice 13073 09:21:45,240 --> 09:21:51,960 most likely you'll want the object for 13074 09:21:48,840 --> 09:21:55,620 example if you want to submit this data 13075 09:21:51,960 --> 09:21:58,140 to a server how we can do that well 13076 09:21:55,620 --> 09:22:00,120 first of all let me comment these ones 13077 09:21:58,140 --> 09:22:02,120 out as always 13078 09:22:00,120 --> 09:22:07,200 all this functionality is available 13079 09:22:02,120 --> 09:22:09,960 already in the readme and let me go here 13080 09:22:07,200 --> 09:22:12,900 with const and I'll call this new user 13081 09:22:09,960 --> 09:22:15,500 and I'll set it equal to object dot 13082 09:22:12,900 --> 09:22:17,936 again this is coming from vanilla.js 13083 09:22:15,500 --> 09:22:20,640 basically it's not provided by react 13084 09:22:17,936 --> 09:22:23,416 it's just JavaScript and effectively 13085 09:22:20,640 --> 09:22:26,460 object Dot from entries 13086 09:22:23,416 --> 09:22:27,300 turns an array of arranged data 13087 09:22:26,460 --> 09:22:31,560 structure 13088 09:22:27,300 --> 09:22:34,436 into a object with key value pairs 13089 09:22:31,560 --> 09:22:37,380 and you can find a vanilla.js code 13090 09:22:34,436 --> 09:22:40,860 example in readme and then here we pass 13091 09:22:37,380 --> 09:22:44,220 in the form data and what you'll see are 13092 09:22:40,860 --> 09:22:47,580 as a result we have this new user now so 13093 09:22:44,220 --> 09:22:50,640 once I click check it out now I have an 13094 09:22:47,580 --> 09:22:53,340 object with all of these values 13095 09:22:50,640 --> 09:22:55,740 and the kicker here is that I can send 13096 09:22:53,340 --> 09:22:58,500 it to a server I can do some kind of 13097 09:22:55,740 --> 09:23:01,740 functionality and all that cool stuff 13098 09:22:58,500 --> 09:23:04,200 again the main point of this video is 13099 09:23:01,740 --> 09:23:06,180 that we don't always have to set up 13100 09:23:04,200 --> 09:23:07,020 those controlled inputs if we don't want 13101 09:23:06,180 --> 09:23:10,320 to 13102 09:23:07,020 --> 09:23:12,000 for example we can use form data pass in 13103 09:23:10,320 --> 09:23:14,756 the form as long as we have the names 13104 09:23:12,000 --> 09:23:17,580 and all that we're good to go we can set 13105 09:23:14,756 --> 09:23:18,480 up the functionality here in the handle 13106 09:23:17,580 --> 09:23:20,160 submit 13107 09:23:18,480 --> 09:23:23,040 so please don't think that you always 13108 09:23:20,160 --> 09:23:26,340 always have to set up controlled inputs 13109 09:23:23,040 --> 09:23:28,380 it really depends on your preference and 13110 09:23:26,340 --> 09:23:30,120 pretty much the application you have in 13111 09:23:28,380 --> 09:23:32,400 some instances you'll do that in some 13112 09:23:30,120 --> 09:23:35,640 you'll be like nope I actually want to 13113 09:23:32,400 --> 09:23:38,820 go with phone data and lastly let's look 13114 09:23:35,640 --> 09:23:42,300 at one major gotcha so we did something 13115 09:23:38,820 --> 09:23:44,276 with this user data we're good to go now 13116 09:23:42,300 --> 09:23:45,480 we want to clear out the values 13117 09:23:44,276 --> 09:23:48,540 you see 13118 09:23:45,480 --> 09:23:50,160 if we just re-render those values that 13119 09:23:48,540 --> 09:23:51,776 are going to stay over here and that's 13120 09:23:50,160 --> 09:23:54,596 why we have over here the state value 13121 09:23:51,776 --> 09:23:56,880 the set value so just to Showcase that 13122 09:23:54,596 --> 09:23:59,820 I'm going to go to set value and I'm 13123 09:23:56,880 --> 09:24:03,300 going to go with Value Plus One 13124 09:23:59,820 --> 09:24:05,756 and essentially what you'll notice that 13125 09:24:03,300 --> 09:24:07,800 even though we re-render correctly 13126 09:24:05,756 --> 09:24:09,360 because remember we're updating the 13127 09:24:07,800 --> 09:24:11,640 state value 13128 09:24:09,360 --> 09:24:13,500 these are going to stay and as far as 13129 09:24:11,640 --> 09:24:14,820 the values just so we can save a little 13130 09:24:13,500 --> 09:24:16,500 bit of time I'm just going to type some 13131 09:24:14,820 --> 09:24:19,620 G brush ones 13132 09:24:16,500 --> 09:24:22,080 I don't think makes any sense to type 13133 09:24:19,620 --> 09:24:25,740 anything meaningful let's click and 13134 09:24:22,080 --> 09:24:27,540 notice we did update the state value 13135 09:24:25,740 --> 09:24:29,160 it's actually happening everything is 13136 09:24:27,540 --> 09:24:31,680 correct however 13137 09:24:29,160 --> 09:24:33,480 these ones stay in input and if you want 13138 09:24:31,680 --> 09:24:37,560 to clear the values you want to go with 13139 09:24:33,480 --> 09:24:40,740 event dot current Target and then the 13140 09:24:37,560 --> 09:24:43,916 method name is reset so we save this one 13141 09:24:40,740 --> 09:24:45,480 and now notice pretty much the moment I 13142 09:24:43,916 --> 09:24:47,460 will submit 13143 09:24:45,480 --> 09:24:49,700 basically I'll do something with the 13144 09:24:47,460 --> 09:24:53,520 data and as a quick sign out of course 13145 09:24:49,700 --> 09:24:54,960 you would check for empty values so in 13146 09:24:53,520 --> 09:24:58,200 that case either you would get them one 13147 09:24:54,960 --> 09:25:00,480 by one or in the vanilla.js video I 13148 09:24:58,200 --> 09:25:02,880 actually show you how to do that by 13149 09:25:00,480 --> 09:25:06,776 iterating over array which is just less 13150 09:25:02,880 --> 09:25:08,580 lines of code and then once we click on 13151 09:25:06,776 --> 09:25:09,900 submit notice how we nicely clear the 13152 09:25:08,580 --> 09:25:12,660 values and again 13153 09:25:09,900 --> 09:25:16,620 don't think that they always stay empty 13154 09:25:12,660 --> 09:25:20,160 again if we provide over here something 13155 09:25:16,620 --> 09:25:22,740 in the inputs notice again we'll have 13156 09:25:20,160 --> 09:25:26,580 the new values here as far as the new 13157 09:25:22,740 --> 09:25:29,936 user and we also nicely clear out the 13158 09:25:26,580 --> 09:25:32,580 inputs awesome and up next let's cover 13159 09:25:29,936 --> 09:25:35,820 use ref hook 13160 09:25:32,580 --> 09:25:38,160 it's a lot like use state 13161 09:25:35,820 --> 09:25:40,680 so it preserves the value between the 13162 09:25:38,160 --> 09:25:43,800 renders but the difference is that 13163 09:25:40,680 --> 09:25:46,500 updating userf does not trigger 13164 09:25:43,800 --> 09:25:49,256 re-render so remember when we work with 13165 09:25:46,500 --> 09:25:50,936 use state every time we 13166 09:25:49,256 --> 09:25:52,980 update the value 13167 09:25:50,936 --> 09:25:56,700 we trigger re-render 13168 09:25:52,980 --> 09:25:58,580 now it's not the case with user ref very 13169 09:25:56,700 --> 09:26:02,096 often use ref is used to access 13170 09:25:58,580 --> 09:26:04,860 downloads but there are some other Nifty 13171 09:26:02,096 --> 09:26:06,480 use cases as well of course it really 13172 09:26:04,860 --> 09:26:08,820 depends on the projects you're going to 13173 09:26:06,480 --> 09:26:09,900 be working on but at least in my 13174 09:26:08,820 --> 09:26:13,560 experience 13175 09:26:09,900 --> 09:26:15,180 since I don't use use ref to access form 13176 09:26:13,560 --> 09:26:18,416 inputs 13177 09:26:15,180 --> 09:26:21,480 use ref is not something I use in every 13178 09:26:18,416 --> 09:26:24,776 project but once in a while there's a 13179 09:26:21,480 --> 09:26:27,480 very specific use case where user f is 13180 09:26:24,776 --> 09:26:29,160 the right tool for the job we're going 13181 09:26:27,480 --> 09:26:32,160 to start just like with the other 13182 09:26:29,160 --> 09:26:35,820 examples where essentially we want to 13183 09:26:32,160 --> 09:26:40,256 look for zero seven user F star and then 13184 09:26:35,820 --> 09:26:42,540 zero one use ref Basics and 13185 09:26:40,256 --> 09:26:45,840 of course I want to import that in the 13186 09:26:42,540 --> 09:26:48,596 app jsx and I want to navigate to that 13187 09:26:45,840 --> 09:26:51,000 file and notice over here I already have 13188 09:26:48,596 --> 09:26:54,120 the state value I have handle submit 13189 09:26:51,000 --> 09:26:55,800 that just prevents the default and I 13190 09:26:54,120 --> 09:26:58,500 have the form 13191 09:26:55,800 --> 09:27:01,200 as well as the heading one 13192 09:26:58,500 --> 09:27:03,300 with the button and as I'm clicking I'm 13193 09:27:01,200 --> 09:27:06,300 increasing the value here and you'll see 13194 09:27:03,300 --> 09:27:08,040 why we have this kind of setup in a 13195 09:27:06,300 --> 09:27:10,860 second so essentially if we want to 13196 09:27:08,040 --> 09:27:13,080 start working with us ref just like the 13197 09:27:10,860 --> 09:27:15,060 other hooks we want to import that from 13198 09:27:13,080 --> 09:27:17,096 react and we want to come up with the 13199 09:27:15,060 --> 09:27:19,620 name so in this case we're not looking 13200 09:27:17,096 --> 09:27:21,840 for an array of values we're just 13201 09:27:19,620 --> 09:27:24,180 looking for one value so I'm going to go 13202 09:27:21,840 --> 09:27:26,460 here const and then we just come up with 13203 09:27:24,180 --> 09:27:29,220 a name so in my case I'm going to call 13204 09:27:26,460 --> 09:27:31,740 this container as a quick sound I 13205 09:27:29,220 --> 09:27:34,080 actually like to think of use ref value 13206 09:27:31,740 --> 09:27:37,500 as a container 13207 09:27:34,080 --> 09:27:40,140 then that's just my mental model and we 13208 09:27:37,500 --> 09:27:42,480 want to go here with use ref 13209 09:27:40,140 --> 09:27:44,220 so that's the hook and then we want to 13210 09:27:42,480 --> 09:27:45,840 pass in that default value 13211 09:27:44,220 --> 09:27:48,180 so in my case I'm going to start with 13212 09:27:45,840 --> 09:27:50,340 null but keep in mind you can pass here 13213 09:27:48,180 --> 09:27:53,160 whatever you want and then if we log 13214 09:27:50,340 --> 09:27:55,140 you'll notice something interesting 13215 09:27:53,160 --> 09:27:57,720 if I log the container 13216 09:27:55,140 --> 09:28:01,256 and if I take a look at the devtools 13217 09:27:57,720 --> 09:28:03,660 you'll see that it is an object and it 13218 09:28:01,256 --> 09:28:05,756 has the current property and the current 13219 09:28:03,660 --> 09:28:07,500 property is equal to that null so 13220 09:28:05,756 --> 09:28:08,880 current property is always going to be 13221 09:28:07,500 --> 09:28:11,580 equal to 13222 09:28:08,880 --> 09:28:14,820 whatever you sit here as a default value 13223 09:28:11,580 --> 09:28:16,800 and yes react behind the scenes is going 13224 09:28:14,820 --> 09:28:20,096 to set this one up as an object so 13225 09:28:16,800 --> 09:28:23,460 notice we did not do anything basically 13226 09:28:20,096 --> 09:28:25,916 we just set up the user ref and this is 13227 09:28:23,460 --> 09:28:28,740 what the react return now once we have 13228 09:28:25,916 --> 09:28:31,140 this one in place essentially 13229 09:28:28,740 --> 09:28:33,360 we have two ways how we can set this 13230 09:28:31,140 --> 09:28:34,980 value because of course you don't want 13231 09:28:33,360 --> 09:28:36,900 to always keep it as no 13232 09:28:34,980 --> 09:28:38,756 one way we can set it ourselves 13233 09:28:36,900 --> 09:28:40,680 basically using some kind of 13234 09:28:38,756 --> 09:28:43,080 functionality which is going to be 13235 09:28:40,680 --> 09:28:45,720 actually our second example or you can 13236 09:28:43,080 --> 09:28:49,560 use this container and set it equal to 13237 09:28:45,720 --> 09:28:52,500 any of the Dom nodes now in our example 13238 09:28:49,560 --> 09:28:54,840 yes we will use the input because it's 13239 09:28:52,500 --> 09:28:56,096 somewhat common but keep in mind you 13240 09:28:54,840 --> 09:28:58,860 don't have to 13241 09:28:56,096 --> 09:29:01,860 I mean you can set it equal to any Dom 13242 09:28:58,860 --> 09:29:04,140 nodes and if I remember correctly in the 13243 09:29:01,860 --> 09:29:07,740 project we're actually going to use the 13244 09:29:04,140 --> 09:29:10,140 navbar just so we have multiple examples 13245 09:29:07,740 --> 09:29:12,720 but yes this is pretty typical to set it 13246 09:29:10,140 --> 09:29:14,640 equal to an input if you remember when 13247 09:29:12,720 --> 09:29:18,840 we talked about controlled inputs and 13248 09:29:14,640 --> 09:29:21,180 form data API in the previous section I 13249 09:29:18,840 --> 09:29:23,400 mentioned that there's another way how 13250 09:29:21,180 --> 09:29:26,700 we can use uncontrolled inputs and 13251 09:29:23,400 --> 09:29:29,756 essentially that is using use ref hook 13252 09:29:26,700 --> 09:29:32,276 so once I'm done with my long speech and 13253 09:29:29,756 --> 09:29:34,380 all that here's what we want to do 13254 09:29:32,276 --> 09:29:36,540 we want to find the element again in my 13255 09:29:34,380 --> 09:29:39,000 case that is going to be the input and 13256 09:29:36,540 --> 09:29:41,640 we're going to go with the ref so that's 13257 09:29:39,000 --> 09:29:43,980 the attribute we want to use and we want 13258 09:29:41,640 --> 09:29:47,340 to set it equal to what well we want to 13259 09:29:43,980 --> 09:29:50,520 set it equal to ref container so 13260 09:29:47,340 --> 09:29:53,460 whatever you created with the user of 13261 09:29:50,520 --> 09:29:56,700 now we don't see anything because of 13262 09:29:53,460 --> 09:29:59,580 course this is essentially the initial 13263 09:29:56,700 --> 09:30:01,740 render correct so in order to see our 13264 09:29:59,580 --> 09:30:02,520 actual value we need to set up the user 13265 09:30:01,740 --> 09:30:04,080 effect 13266 09:30:02,520 --> 09:30:06,960 so in here I'm going to go with user 13267 09:30:04,080 --> 09:30:09,416 effect and then we'll pass in the 13268 09:30:06,960 --> 09:30:12,000 function now I don't care about the 13269 09:30:09,416 --> 09:30:15,840 dependency array I just want to showcase 13270 09:30:12,000 --> 09:30:17,096 that essentially once our initial render 13271 09:30:15,840 --> 09:30:19,500 takes place 13272 09:30:17,096 --> 09:30:22,080 we run this user fact and in there we'll 13273 09:30:19,500 --> 09:30:24,360 see a different picture so here we're 13274 09:30:22,080 --> 09:30:26,700 going to see error of container that 13275 09:30:24,360 --> 09:30:29,460 actually has this input again something 13276 09:30:26,700 --> 09:30:31,140 important to keep in mind that yes we 13277 09:30:29,460 --> 09:30:32,460 have two ways that we can set up the 13278 09:30:31,140 --> 09:30:34,980 user of 13279 09:30:32,460 --> 09:30:37,080 we can either set it equal to any of the 13280 09:30:34,980 --> 09:30:39,300 Dom nodes and the reason why we see that 13281 09:30:37,080 --> 09:30:42,060 right now because this runs after 13282 09:30:39,300 --> 09:30:43,916 initial render correct this one is 13283 09:30:42,060 --> 09:30:47,160 happening during the initial render 13284 09:30:43,916 --> 09:30:49,740 that's the difference and we can also 13285 09:30:47,160 --> 09:30:52,436 for example set it 13286 09:30:49,740 --> 09:30:55,140 ourself again that is going to be our 13287 09:30:52,436 --> 09:30:57,180 second example and once current is equal 13288 09:30:55,140 --> 09:31:00,180 to our Dom node 13289 09:30:57,180 --> 09:31:03,180 what can we do well pretty much anything 13290 09:31:00,180 --> 09:31:04,380 once we get the Dom node I mean sky is 13291 09:31:03,180 --> 09:31:06,240 the limit 13292 09:31:04,380 --> 09:31:08,756 for example 13293 09:31:06,240 --> 09:31:10,980 I can grab the value and this one of 13294 09:31:08,756 --> 09:31:13,380 course I'll do it in handle submit so 13295 09:31:10,980 --> 09:31:15,596 again this was just the Showcase that 13296 09:31:13,380 --> 09:31:18,240 after the initial render 13297 09:31:15,596 --> 09:31:20,640 now in the rough container we'll have 13298 09:31:18,240 --> 09:31:23,756 access to that input but we'll actually 13299 09:31:20,640 --> 09:31:27,480 use it over here in the handle submit so 13300 09:31:23,756 --> 09:31:31,680 in here effectively we want to grab the 13301 09:31:27,480 --> 09:31:34,020 value by using value property again this 13302 09:31:31,680 --> 09:31:36,596 is just coming from vanilla.js so if I'm 13303 09:31:34,020 --> 09:31:39,900 going to go here in the handle submit 13304 09:31:36,596 --> 09:31:42,120 and if I'm going to take a look at the 13305 09:31:39,900 --> 09:31:45,000 value it will actually give me whatever 13306 09:31:42,120 --> 09:31:48,240 is typed in the input so if I'm going to 13307 09:31:45,000 --> 09:31:50,400 go here with cons name and ref container 13308 09:31:48,240 --> 09:31:52,800 then current again I'm accessing the 13309 09:31:50,400 --> 09:31:54,000 property and since the property is the 13310 09:31:52,800 --> 09:31:56,360 input 13311 09:31:54,000 --> 09:32:00,000 basically I can run with value again 13312 09:31:56,360 --> 09:32:03,060 this is coming from an ljs you access 13313 09:32:00,000 --> 09:32:05,756 the Dom node and if it's an input you 13314 09:32:03,060 --> 09:32:07,380 can actually run dot value and 13315 09:32:05,756 --> 09:32:09,720 effectively this is just going to get us 13316 09:32:07,380 --> 09:32:11,580 whatever the user has typed in and then 13317 09:32:09,720 --> 09:32:13,740 again we can do something about it now 13318 09:32:11,580 --> 09:32:16,436 that's something in our case it's simply 13319 09:32:13,740 --> 09:32:19,320 going to be logging the name so notice 13320 09:32:16,436 --> 09:32:21,900 this essentially is no let me comment 13321 09:32:19,320 --> 09:32:24,300 out we have quite a few logs here but 13322 09:32:21,900 --> 09:32:26,820 then if I type something and if I press 13323 09:32:24,300 --> 09:32:30,256 submit check it out now I'm getting this 13324 09:32:26,820 --> 09:32:35,040 value so this is how we can set up again 13325 09:32:30,256 --> 09:32:37,436 uncontrolled inputs using use graph so 13326 09:32:35,040 --> 09:32:39,596 that's the first example we're going to 13327 09:32:37,436 --> 09:32:42,660 look at and in the following video we'll 13328 09:32:39,596 --> 09:32:46,020 take a look how we can use use ref 13329 09:32:42,660 --> 09:32:48,240 to avoid running some functionality on 13330 09:32:46,020 --> 09:32:51,000 the initial render and you'll see what 13331 09:32:48,240 --> 09:32:52,256 I'm talking about in a second okay and 13332 09:32:51,000 --> 09:32:56,340 now let's take a look at our second 13333 09:32:52,256 --> 09:33:00,120 example where we will change the value 13334 09:32:56,340 --> 09:33:02,460 of use ref and we're going to start just 13335 09:33:00,120 --> 09:33:06,596 like the first one where essentially I 13336 09:33:02,460 --> 09:33:08,820 want to create a value with user and in 13337 09:33:06,596 --> 09:33:12,060 this case I'm going to call is mounted 13338 09:33:08,820 --> 09:33:13,740 and I'll set it equal to use ref and 13339 09:33:12,060 --> 09:33:16,256 just like before 13340 09:33:13,740 --> 09:33:18,300 I want to provide some kind of default 13341 09:33:16,256 --> 09:33:21,240 value and in my case I'm going to go 13342 09:33:18,300 --> 09:33:23,880 with false again I probably don't need 13343 09:33:21,240 --> 09:33:26,756 to say that now is mounted essentially 13344 09:33:23,880 --> 09:33:30,300 is an object in there there's a current 13345 09:33:26,756 --> 09:33:33,360 property and that one is equal to a 13346 09:33:30,300 --> 09:33:36,360 false now I also probably don't need to 13347 09:33:33,360 --> 09:33:38,880 remind you that if I have here a huge 13348 09:33:36,360 --> 09:33:42,060 effect and if I add here 13349 09:33:38,880 --> 09:33:43,980 dependency array and if I provide here a 13350 09:33:42,060 --> 09:33:46,320 value basically 13351 09:33:43,980 --> 09:33:49,140 the state value over here every time 13352 09:33:46,320 --> 09:33:51,596 I'll click on a button guess what 13353 09:33:49,140 --> 09:33:53,400 essentially the functionality inside of 13354 09:33:51,596 --> 09:33:57,240 this use effect is going to run correct 13355 09:33:53,400 --> 09:34:00,120 so let's kill two birds at the same time 13356 09:33:57,240 --> 09:34:03,000 I'm gonna go with is mounted and let's 13357 09:34:00,120 --> 09:34:05,820 save and notice pretty much every time 13358 09:34:03,000 --> 09:34:08,820 I'll click I have this current is equal 13359 09:34:05,820 --> 09:34:11,460 to false and once we have established 13360 09:34:08,820 --> 09:34:13,140 this why don't we talk about a very 13361 09:34:11,460 --> 09:34:15,960 specific use case 13362 09:34:13,140 --> 09:34:19,860 what if I don't want to run certain 13363 09:34:15,960 --> 09:34:21,660 functionality after initial render so I 13364 09:34:19,860 --> 09:34:24,180 do want to run it 13365 09:34:21,660 --> 09:34:27,416 after that every time the value changes 13366 09:34:24,180 --> 09:34:29,096 but not on the initial render again this 13367 09:34:27,416 --> 09:34:31,500 is not probably something that you're 13368 09:34:29,096 --> 09:34:33,776 going to use in every project but here 13369 09:34:31,500 --> 09:34:36,660 and there this is 13370 09:34:33,776 --> 09:34:38,040 a Nifty solution where essentially you 13371 09:34:36,660 --> 09:34:41,040 can just avoid running some 13372 09:34:38,040 --> 09:34:43,080 functionality after that initial render 13373 09:34:41,040 --> 09:34:44,640 and the way we do that we essentially 13374 09:34:43,080 --> 09:34:46,800 check this value 13375 09:34:44,640 --> 09:34:50,220 and if this one is false 13376 09:34:46,800 --> 09:34:53,220 we set it equal to true 13377 09:34:50,220 --> 09:34:55,320 and then after that every time this 13378 09:34:53,220 --> 09:34:57,776 value changes this is equal already true 13379 09:34:55,320 --> 09:35:01,436 and then the functionality is going to 13380 09:34:57,776 --> 09:35:04,620 run now one gotcha though don't check is 13381 09:35:01,436 --> 09:35:05,700 mounted because this will always be an 13382 09:35:04,620 --> 09:35:08,460 object 13383 09:35:05,700 --> 09:35:10,560 so this will be true we need to check 13384 09:35:08,460 --> 09:35:12,480 for the current property and if you're 13385 09:35:10,560 --> 09:35:13,620 someone iffy on that let me showcase 13386 09:35:12,480 --> 09:35:17,040 what I'm talking about 13387 09:35:13,620 --> 09:35:19,320 so inside of the use effect first of all 13388 09:35:17,040 --> 09:35:21,540 I think I'm going to remove this one I 13389 09:35:19,320 --> 09:35:25,080 can set up the if condition and I can 13390 09:35:21,540 --> 09:35:29,040 say if is mounted current 13391 09:35:25,080 --> 09:35:31,500 is false then don't run basically return 13392 09:35:29,040 --> 09:35:36,120 from the use effect so in here I can say 13393 09:35:31,500 --> 09:35:39,240 is mounted and current again don't check 13394 09:35:36,120 --> 09:35:41,700 for is mounted because it is an object 13395 09:35:39,240 --> 09:35:43,560 so essentially 13396 09:35:41,700 --> 09:35:45,840 it's going to be true 13397 09:35:43,560 --> 09:35:48,660 I'm checking for the current property 13398 09:35:45,840 --> 09:35:50,936 which in the beginning is false and if 13399 09:35:48,660 --> 09:35:53,040 that's the case then I want to go with 13400 09:35:50,936 --> 09:35:55,256 is mounted so notice now I'm actually 13401 09:35:53,040 --> 09:35:57,720 changing that value myself 13402 09:35:55,256 --> 09:36:00,480 I'll set it equal to true and then I'm 13403 09:35:57,720 --> 09:36:03,540 going to return so true and then I 13404 09:36:00,480 --> 09:36:04,560 return and after that if condition I 13405 09:36:03,540 --> 09:36:07,020 actually want to set up that 13406 09:36:04,560 --> 09:36:09,540 functionality now again in my case that 13407 09:36:07,020 --> 09:36:14,400 functionality is simply going to be 13408 09:36:09,540 --> 09:36:16,560 log and then re-render like so but let's 13409 09:36:14,400 --> 09:36:19,500 take a look what is the result so notice 13410 09:36:16,560 --> 09:36:22,140 I refresh and there's nothing in the 13411 09:36:19,500 --> 09:36:24,660 console and I'll only see something in 13412 09:36:22,140 --> 09:36:26,580 the console once actually 13413 09:36:24,660 --> 09:36:29,220 I'm changing this value 13414 09:36:26,580 --> 09:36:32,936 so essentially what we're doing we're 13415 09:36:29,220 --> 09:36:35,400 avoiding to run whatever functionality 13416 09:36:32,936 --> 09:36:37,436 after initial render again not something 13417 09:36:35,400 --> 09:36:39,416 that you'll do in every project but once 13418 09:36:37,436 --> 09:36:41,460 in a while you might hit this use case 13419 09:36:39,416 --> 09:36:44,880 and lastly I just want to showcase how 13420 09:36:41,460 --> 09:36:47,160 we can set up nice Focus around our 13421 09:36:44,880 --> 09:36:49,320 input and since we're not triggering 13422 09:36:47,160 --> 09:36:52,380 re-render we won't have this infinite 13423 09:36:49,320 --> 09:36:55,080 Loop so I have rest container which 13424 09:36:52,380 --> 09:36:58,080 essentially is pointing to my input 13425 09:36:55,080 --> 09:37:00,300 correct so I can set up a use effect 13426 09:36:58,080 --> 09:37:02,936 instead of the use effect 13427 09:37:00,300 --> 09:37:04,200 I'm going to pass in the function and 13428 09:37:02,936 --> 09:37:06,720 then since I know that I'm accessing 13429 09:37:04,200 --> 09:37:09,240 container then current so that's my 13430 09:37:06,720 --> 09:37:11,820 input in vanilla.js we also have this 13431 09:37:09,240 --> 09:37:14,340 Focus we just invoke it again this is 13432 09:37:11,820 --> 09:37:16,080 not reacting this is vanilla.js and 13433 09:37:14,340 --> 09:37:18,596 check it out basically the moment I'll 13434 09:37:16,080 --> 09:37:21,776 refresh I'll have this Focus around the 13435 09:37:18,596 --> 09:37:24,180 input and what's really cool that since 13436 09:37:21,776 --> 09:37:26,756 use ref 13437 09:37:24,180 --> 09:37:29,520 does not trigger re-render essentially 13438 09:37:26,756 --> 09:37:32,460 you won't have this infinite Loop like 13439 09:37:29,520 --> 09:37:36,000 you would if for example you would be 13440 09:37:32,460 --> 09:37:39,120 using use effect without the dependence 13441 09:37:36,000 --> 09:37:42,000 array and then updating the state value 13442 09:37:39,120 --> 09:37:44,936 hopefully that is clear and we can move 13443 09:37:42,000 --> 09:37:47,220 on to the next topic okay and up next 13444 09:37:44,936 --> 09:37:51,540 let's see how we can create our own 13445 09:37:47,220 --> 09:37:52,880 custom Hooks and custom hooks are great 13446 09:37:51,540 --> 09:37:56,756 for 13447 09:37:52,880 --> 09:37:58,860 abstracting some functionality basically 13448 09:37:56,756 --> 09:38:01,916 to have less lines of code in the 13449 09:37:58,860 --> 09:38:05,040 component and as a result it's going to 13450 09:38:01,916 --> 09:38:07,800 be easier to manage our component and 13451 09:38:05,040 --> 09:38:09,960 also once we have custom hook 13452 09:38:07,800 --> 09:38:13,256 we can bring it from Project to project 13453 09:38:09,960 --> 09:38:16,500 which of course speeds up our workflow 13454 09:38:13,256 --> 09:38:18,776 our rule of thumb quite often use effect 13455 09:38:16,500 --> 09:38:20,880 logic is a great option for custom hook 13456 09:38:18,776 --> 09:38:24,120 and lastly 13457 09:38:20,880 --> 09:38:27,660 all the hooks rules apply to our custom 13458 09:38:24,120 --> 09:38:32,040 hooks for example they do need to start 13459 09:38:27,660 --> 09:38:35,220 with use for the setup in the app GSX 13460 09:38:32,040 --> 09:38:38,220 look for zero eight custom hooks star 13461 09:38:35,220 --> 09:38:40,256 and then zero one toggle so we'll start 13462 09:38:38,220 --> 09:38:43,256 with somewhat basic example and then 13463 09:38:40,256 --> 09:38:44,520 we'll work on the fetch one and once you 13464 09:38:43,256 --> 09:38:46,380 navigate here 13465 09:38:44,520 --> 09:38:49,560 in the component you can see that I'm 13466 09:38:46,380 --> 09:38:52,080 using your state I have show set show 13467 09:38:49,560 --> 09:38:54,000 default value is false and then every 13468 09:38:52,080 --> 09:38:56,880 time I click on a button what do you 13469 09:38:54,000 --> 09:38:58,500 know I'm toggling the component 13470 09:38:56,880 --> 09:39:01,560 and essentially 13471 09:38:58,500 --> 09:39:04,320 if I have for example three four five or 13472 09:39:01,560 --> 09:39:06,240 whatever components that have this kind 13473 09:39:04,320 --> 09:39:07,380 of functionality the toggle 13474 09:39:06,240 --> 09:39:11,596 functionality 13475 09:39:07,380 --> 09:39:14,400 I can set up a pretty simple custom hook 13476 09:39:11,596 --> 09:39:17,700 where first we're going to navigate to 13477 09:39:14,400 --> 09:39:20,880 the starter and go with new file and 13478 09:39:17,700 --> 09:39:22,800 let's go with use again this will have 13479 09:39:20,880 --> 09:39:25,140 to start with you so might as well right 13480 09:39:22,800 --> 09:39:27,596 away set it up and I'm gonna go with 13481 09:39:25,140 --> 09:39:30,120 toggle and in this case we can go with 13482 09:39:27,596 --> 09:39:32,460 JS now if you want of course you can set 13483 09:39:30,120 --> 09:39:34,916 up jsx but in my case I'm going to go 13484 09:39:32,460 --> 09:39:35,936 with Js and essentially the way it's 13485 09:39:34,916 --> 09:39:39,060 going to work 13486 09:39:35,936 --> 09:39:42,776 we'll just take this functionality and 13487 09:39:39,060 --> 09:39:45,480 return from that customer so first let 13488 09:39:42,776 --> 09:39:48,540 me grab this just so it's faster then 13489 09:39:45,480 --> 09:39:50,936 let's come up with the function again we 13490 09:39:48,540 --> 09:39:54,000 need to go to use and in this case I'm 13491 09:39:50,936 --> 09:39:55,256 going to go toggle here when I know that 13492 09:39:54,000 --> 09:39:57,776 I'm going to be looking for some kind of 13493 09:39:55,256 --> 09:40:00,900 default value that's why I'll use this 13494 09:39:57,776 --> 09:40:03,480 as a parameter I'll say default and then 13495 09:40:00,900 --> 09:40:07,020 value and of course I'll pass that value 13496 09:40:03,480 --> 09:40:09,000 when I invoke the custom hook 13497 09:40:07,020 --> 09:40:11,700 and in here as far as the logic it's 13498 09:40:09,000 --> 09:40:14,220 pretty much the same I want to have show 13499 09:40:11,700 --> 09:40:16,740 and set show and I want to pass this 13500 09:40:14,220 --> 09:40:19,436 default value so let's go back over here 13501 09:40:16,740 --> 09:40:22,500 and we'll cut it out 13502 09:40:19,436 --> 09:40:26,040 copy and paste and instead of false I'll 13503 09:40:22,500 --> 09:40:28,680 actually go default and then value now I 13504 09:40:26,040 --> 09:40:31,256 also want to set up the toggle function 13505 09:40:28,680 --> 09:40:33,720 the one that I'm invoking here and I'm 13506 09:40:31,256 --> 09:40:35,276 going to go with const toggle let me 13507 09:40:33,720 --> 09:40:37,880 close the sidebar 13508 09:40:35,276 --> 09:40:40,520 as far as the functionality same deal 13509 09:40:37,880 --> 09:40:43,916 basically I want to go with Seth show 13510 09:40:40,520 --> 09:40:47,276 and then the opposite value whatever it 13511 09:40:43,916 --> 09:40:49,080 is and then from this huge toggle we 13512 09:40:47,276 --> 09:40:51,960 have multiple options 13513 09:40:49,080 --> 09:40:54,180 if you want you can return an array so 13514 09:40:51,960 --> 09:40:56,220 you can go here and say return 13515 09:40:54,180 --> 09:40:58,680 and then the first value 13516 09:40:56,220 --> 09:41:01,020 can be show and then the second one is 13517 09:40:58,680 --> 09:41:03,360 toggle so notice basically we're making 13518 09:41:01,020 --> 09:41:05,460 a huge state 13519 09:41:03,360 --> 09:41:07,320 just a little bit more complex 13520 09:41:05,460 --> 09:41:09,300 so in here I also have the toggle 13521 09:41:07,320 --> 09:41:11,880 function so hopefully it's clear so we 13522 09:41:09,300 --> 09:41:13,740 have the state value but I'm just adding 13523 09:41:11,880 --> 09:41:16,080 this extra function 13524 09:41:13,740 --> 09:41:18,360 so now I'm returning this and then 13525 09:41:16,080 --> 09:41:20,640 backing the toggle 13526 09:41:18,360 --> 09:41:22,740 I can access it the same way I can go 13527 09:41:20,640 --> 09:41:25,320 use toggle and then the first value and 13528 09:41:22,740 --> 09:41:27,240 the second value now I actually prefer 13529 09:41:25,320 --> 09:41:29,820 turning this as an object 13530 09:41:27,240 --> 09:41:31,380 It just fits better my mental model 13531 09:41:29,820 --> 09:41:32,880 where I know that there's going to be 13532 09:41:31,380 --> 09:41:34,620 some properties there and I just want to 13533 09:41:32,880 --> 09:41:36,540 access them now 13534 09:41:34,620 --> 09:41:38,640 this is totally up to you 13535 09:41:36,540 --> 09:41:40,380 if you want you can set it up as an 13536 09:41:38,640 --> 09:41:42,120 array and then of course we want to 13537 09:41:40,380 --> 09:41:43,860 export that so I'm going to go here with 13538 09:41:42,120 --> 09:41:46,380 export default 13539 09:41:43,860 --> 09:41:48,960 and then we're looking for use and then 13540 09:41:46,380 --> 09:41:52,020 toggle that's what we're exporting 13541 09:41:48,960 --> 09:41:53,936 and now back in the toggle one instead 13542 09:41:52,020 --> 09:41:57,540 of State we don't need the State anymore 13543 09:41:53,936 --> 09:42:01,200 we want to import the use and then 13544 09:41:57,540 --> 09:42:03,540 toggle and I'll rely on my Auto Imports 13545 09:42:01,200 --> 09:42:04,680 remember we do need to provide the 13546 09:42:03,540 --> 09:42:07,380 default value 13547 09:42:04,680 --> 09:42:08,820 either that's true or false and just so 13548 09:42:07,380 --> 09:42:11,580 you can see that it works I'll actually 13549 09:42:08,820 --> 09:42:14,400 go through and then remember we are 13550 09:42:11,580 --> 09:42:16,320 returning the object correct and as far 13551 09:42:14,400 --> 09:42:19,020 as the properties what do we have over 13552 09:42:16,320 --> 09:42:22,500 there well we have show and we have the 13553 09:42:19,020 --> 09:42:24,240 toggle so now this stays the same this 13554 09:42:22,500 --> 09:42:25,380 doesn't change but here where I have the 13555 09:42:24,240 --> 09:42:28,436 on click 13556 09:42:25,380 --> 09:42:31,680 basically I have the toggle 13557 09:42:28,436 --> 09:42:33,120 notice the moment I save right away see 13558 09:42:31,680 --> 09:42:35,096 the component why well because the 13559 09:42:33,120 --> 09:42:37,500 default value is true and then the 13560 09:42:35,096 --> 09:42:40,680 functionality still works I purposely 13561 09:42:37,500 --> 09:42:42,000 picked a very basic example 13562 09:42:40,680 --> 09:42:44,400 of course 13563 09:42:42,000 --> 09:42:47,220 as you're looking at it you're like well 13564 09:42:44,400 --> 09:42:49,500 I mean are we really saving time again 13565 09:42:47,220 --> 09:42:51,300 it really depends if you have 10 13566 09:42:49,500 --> 09:42:53,480 components that use the same toggle 13567 09:42:51,300 --> 09:42:56,340 functionality yeah this is very Nifty 13568 09:42:53,480 --> 09:43:00,300 and in the following example you'll 13569 09:42:56,340 --> 09:43:03,360 truly see the power of the custom hooks 13570 09:43:00,300 --> 09:43:06,360 again this is just to showcase the main 13571 09:43:03,360 --> 09:43:08,400 setup yes we can use here use State yes 13572 09:43:06,360 --> 09:43:11,640 we can use use effect whatever hook you 13573 09:43:08,400 --> 09:43:15,240 want make sure that you start with use 13574 09:43:11,640 --> 09:43:17,400 and then when you actually invoke the 13575 09:43:15,240 --> 09:43:19,560 hook if you have some default value you 13576 09:43:17,400 --> 09:43:22,020 need to pass it in so for example if I 13577 09:43:19,560 --> 09:43:24,416 flip it to false notice now I don't see 13578 09:43:22,020 --> 09:43:27,596 the component on the initial render but 13579 09:43:24,416 --> 09:43:30,540 if I go back to true this is still going 13580 09:43:27,596 --> 09:43:33,416 to work so again I refresh and I'm good 13581 09:43:30,540 --> 09:43:35,520 to go so that's the basic setup and in 13582 09:43:33,416 --> 09:43:38,276 the following video we'll try to do the 13583 09:43:35,520 --> 09:43:41,096 same thing with our fetch functionality 13584 09:43:38,276 --> 09:43:44,640 okay and once we're familiar with the 13585 09:43:41,096 --> 09:43:48,300 fundamentals of custom Oops why don't we 13586 09:43:44,640 --> 09:43:49,320 cover more complex example and here's 13587 09:43:48,300 --> 09:43:55,140 the challenge 13588 09:43:49,320 --> 09:43:57,776 in the app jsx import 0 to fetch data so 13589 09:43:55,140 --> 09:44:00,480 it's in the same folder just a more 13590 09:43:57,776 --> 09:44:04,860 complex example and once you navigate 13591 09:44:00,480 --> 09:44:07,916 there remember this is essentially 13592 09:44:04,860 --> 09:44:08,936 a component that is fetching a GitHub 13593 09:44:07,916 --> 09:44:13,560 user 13594 09:44:08,936 --> 09:44:14,756 and now let's try to set up a custom 13595 09:44:13,560 --> 09:44:18,256 hook 13596 09:44:14,756 --> 09:44:22,800 so effectively we want to move away 13597 09:44:18,256 --> 09:44:25,080 pretty much most of the logic so try to 13598 09:44:22,800 --> 09:44:27,000 come up with that custom hook 13599 09:44:25,080 --> 09:44:29,936 and a hint 13600 09:44:27,000 --> 09:44:34,620 hooks should return is loading his error 13601 09:44:29,936 --> 09:44:36,840 and user and it should take URL as a 13602 09:44:34,620 --> 09:44:39,360 parameter so again if you're interested 13603 09:44:36,840 --> 09:44:41,880 try to set it up and once you're ready 13604 09:44:39,360 --> 09:44:42,776 to compare the results resume with the 13605 09:44:41,880 --> 09:44:45,240 video 13606 09:44:42,776 --> 09:44:49,860 effectively like I mentioned use effect 13607 09:44:45,240 --> 09:44:52,380 quite often is a good use case for the 13608 09:44:49,860 --> 09:44:54,540 custom hook notice all these lines of 13609 09:44:52,380 --> 09:44:57,840 code it would be nice if we could just 13610 09:44:54,540 --> 09:44:59,820 move it to a separate file and 13611 09:44:57,840 --> 09:45:02,340 effectively we'll have less lines of 13612 09:44:59,820 --> 09:45:05,040 code and it's going to be easier to 13613 09:45:02,340 --> 09:45:07,500 grasp the functionality once you come 13614 09:45:05,040 --> 09:45:10,140 back I don't know in two days three days 13615 09:45:07,500 --> 09:45:12,060 or I don't know a half a year 13616 09:45:10,140 --> 09:45:13,680 and the way we can do that we can set up 13617 09:45:12,060 --> 09:45:15,840 the custom hooks so let's navigate back 13618 09:45:13,680 --> 09:45:18,776 to the starter I'm going to go with use 13619 09:45:15,840 --> 09:45:21,120 and I'll set up two examples first one 13620 09:45:18,776 --> 09:45:22,740 is going to be use fetch person 13621 09:45:21,120 --> 09:45:24,360 this is going to be kind of a specific 13622 09:45:22,740 --> 09:45:26,936 case and then I'll show you how we can 13623 09:45:24,360 --> 09:45:30,416 make this more generic basically how we 13624 09:45:26,936 --> 09:45:33,320 can have a custom hook for any data so 13625 09:45:30,416 --> 09:45:36,776 let's start over here I'll say fetch 13626 09:45:33,320 --> 09:45:37,980 person.js and then let's create that 13627 09:45:36,776 --> 09:45:44,640 function first 13628 09:45:37,980 --> 09:45:46,680 so let's go here with const use fetch 13629 09:45:44,640 --> 09:45:48,360 and then user 13630 09:45:46,680 --> 09:45:50,880 or person sorry 13631 09:45:48,360 --> 09:45:54,120 I mean it can also be a user of course 13632 09:45:50,880 --> 09:45:57,480 correct then we want to set up the 13633 09:45:54,120 --> 09:45:59,096 function body and first let's just take 13634 09:45:57,480 --> 09:46:01,380 a look at our component 13635 09:45:59,096 --> 09:46:04,140 so I have is loading is error and user 13636 09:46:01,380 --> 09:46:07,020 all of these three state values can we 13637 09:46:04,140 --> 09:46:09,300 move them to a custom hook yes of course 13638 09:46:07,020 --> 09:46:10,436 because essentially What's Happening 13639 09:46:09,300 --> 09:46:13,200 Here 13640 09:46:10,436 --> 09:46:16,436 we just set up a use effect and 13641 09:46:13,200 --> 09:46:19,080 eventually we get back the user or of 13642 09:46:16,436 --> 09:46:21,900 course if there is an error 13643 09:46:19,080 --> 09:46:24,000 so let's try that first I guess let's 13644 09:46:21,900 --> 09:46:26,400 grab these Imports and I'll right away 13645 09:46:24,000 --> 09:46:27,840 basically cut them out we won't use them 13646 09:46:26,400 --> 09:46:29,520 in this file 13647 09:46:27,840 --> 09:46:32,340 set it over here 13648 09:46:29,520 --> 09:46:35,700 then we pretty much want to take the 13649 09:46:32,340 --> 09:46:38,580 whole thing yep it's that drastic so I 13650 09:46:35,700 --> 09:46:40,980 want to First Take These State values 13651 09:46:38,580 --> 09:46:41,820 set it up over here inside of the 13652 09:46:40,980 --> 09:46:44,580 function 13653 09:46:41,820 --> 09:46:47,580 and then we want to grab the use effect 13654 09:46:44,580 --> 09:46:49,020 so whole thing over here let's cut it 13655 09:46:47,580 --> 09:46:52,500 out as well 13656 09:46:49,020 --> 09:46:54,360 and copy and paste because this Hook is 13657 09:46:52,500 --> 09:46:55,916 going to be looking for that URL now of 13658 09:46:54,360 --> 09:46:58,740 course if you know that you're going to 13659 09:46:55,916 --> 09:47:03,480 be using the same URL you can hard code 13660 09:46:58,740 --> 09:47:06,240 in the use fetch person but if you want 13661 09:47:03,480 --> 09:47:09,240 to make it more Dynamic set up the 13662 09:47:06,240 --> 09:47:11,700 parameter so here I'll say whenever I 13663 09:47:09,240 --> 09:47:14,096 invoke use fetch person I'm going to be 13664 09:47:11,700 --> 09:47:16,860 looking for that URL and of course in 13665 09:47:14,096 --> 09:47:18,540 this case it's this getup one and then 13666 09:47:16,860 --> 09:47:21,180 we're pretty much done we just want to 13667 09:47:18,540 --> 09:47:23,820 return all of these three values yes 13668 09:47:21,180 --> 09:47:25,916 effectively it's that simple remember 13669 09:47:23,820 --> 09:47:28,916 the use toggle 13670 09:47:25,916 --> 09:47:32,700 in the component what did we do well we 13671 09:47:28,916 --> 09:47:34,740 invoked it correct so initially is 13672 09:47:32,700 --> 09:47:37,620 loading is going to be true 13673 09:47:34,740 --> 09:47:39,596 then we're going to invoke the use 13674 09:47:37,620 --> 09:47:42,776 effect only 13675 09:47:39,596 --> 09:47:45,120 after the initial render and here we 13676 09:47:42,776 --> 09:47:47,700 have all this logic where we'll grab the 13677 09:47:45,120 --> 09:47:50,460 URL which matches the parameter of 13678 09:47:47,700 --> 09:47:52,916 fetched the data if there's an error 13679 09:47:50,460 --> 09:47:54,360 will return now if not then we'll return 13680 09:47:52,916 --> 09:47:58,500 a user 13681 09:47:54,360 --> 09:48:00,480 so essentially yes it's that simple and 13682 09:47:58,500 --> 09:48:03,060 here let's just go with return 13683 09:48:00,480 --> 09:48:06,720 and we want to go with is loading again 13684 09:48:03,060 --> 09:48:08,820 if you want you can return an array but 13685 09:48:06,720 --> 09:48:11,880 especially if I have like three values 13686 09:48:08,820 --> 09:48:14,700 that's definitely a good use case for a 13687 09:48:11,880 --> 09:48:17,880 object in my opinion let's go here and 13688 09:48:14,700 --> 09:48:19,800 then we also want to return a user and 13689 09:48:17,880 --> 09:48:20,820 then we want to navigate back to fetch 13690 09:48:19,800 --> 09:48:22,680 data 13691 09:48:20,820 --> 09:48:25,436 and essentially in here we just want to 13692 09:48:22,680 --> 09:48:28,256 go to use fetch for some reason it's not 13693 09:48:25,436 --> 09:48:30,660 importing that's fine let's just pass in 13694 09:48:28,256 --> 09:48:33,180 the URL and remember this one is 13695 09:48:30,660 --> 09:48:36,000 returning three things it's loading his 13696 09:48:33,180 --> 09:48:39,060 error and if there is a user correct so 13697 09:48:36,000 --> 09:48:41,520 we go here we set it equal to and then 13698 09:48:39,060 --> 09:48:44,880 we just check for is loading 13699 09:48:41,520 --> 09:48:48,060 same goes for is error and then we have 13700 09:48:44,880 --> 09:48:50,640 the user and now million dollar question 13701 09:48:48,060 --> 09:48:54,500 which one is easier to read 13702 09:48:50,640 --> 09:48:57,360 if you have eu's fetch person or 13703 09:48:54,500 --> 09:48:59,400 all these lines of code and as I said 13704 09:48:57,360 --> 09:49:01,200 the reason why nothing worked actually I 13705 09:48:59,400 --> 09:49:03,776 didn't export that my bad so I'm going 13706 09:49:01,200 --> 09:49:08,096 to go here with export default 13707 09:49:03,776 --> 09:49:10,200 and then use fetch Versa let's do this 13708 09:49:08,096 --> 09:49:12,060 and then back in the fetch data of 13709 09:49:10,200 --> 09:49:15,360 course we want to import that 13710 09:49:12,060 --> 09:49:17,756 so I'm gonna go here with use Fetch and 13711 09:49:15,360 --> 09:49:20,096 I'll cheat a little bit where I'll grab 13712 09:49:17,756 --> 09:49:22,020 the import but I'll remove this line of 13713 09:49:20,096 --> 09:49:23,820 code because otherwise I'm going to get 13714 09:49:22,020 --> 09:49:27,060 the narrow so now everything should work 13715 09:49:23,820 --> 09:49:30,000 again notice we have nice loading one 13716 09:49:27,060 --> 09:49:33,540 if there's an error we'll get an r and 13717 09:49:30,000 --> 09:49:36,416 eventually we get the user so that's how 13718 09:49:33,540 --> 09:49:39,540 we can set up a custom hook with more 13719 09:49:36,416 --> 09:49:42,300 complex example and as a side note 13720 09:49:39,540 --> 09:49:45,840 whenever you start working with react 13721 09:49:42,300 --> 09:49:47,400 query or other fetching libraries the 13722 09:49:45,840 --> 09:49:48,776 setup is going to be extremely similar 13723 09:49:47,400 --> 09:49:50,700 that's why I thought that it's very 13724 09:49:48,776 --> 09:49:52,740 important for us to understand how 13725 09:49:50,700 --> 09:49:55,500 fetching works with use effect of course 13726 09:49:52,740 --> 09:49:56,580 those libraries do way more than just 13727 09:49:55,500 --> 09:49:59,040 this 13728 09:49:56,580 --> 09:50:01,020 they do caching and all kinds of cool 13729 09:49:59,040 --> 09:50:02,756 things but the idea is going to be the 13730 09:50:01,020 --> 09:50:05,160 same you're going to get some kind of 13731 09:50:02,756 --> 09:50:06,240 custom Hook from the library that hook 13732 09:50:05,160 --> 09:50:08,160 is going to be looking for some 13733 09:50:06,240 --> 09:50:10,916 parameters and of course it depends on 13734 09:50:08,160 --> 09:50:12,120 the library what parameters the hook is 13735 09:50:10,916 --> 09:50:14,400 looking for 13736 09:50:12,120 --> 09:50:18,596 and essentially you'll get back a bunch 13737 09:50:14,400 --> 09:50:20,936 of useful info about the request and you 13738 09:50:18,596 --> 09:50:25,020 can use that info to set up 13739 09:50:20,936 --> 09:50:27,960 conditions to access the data and all of 13740 09:50:25,020 --> 09:50:30,416 that cool stuff and lastly as far as the 13741 09:50:27,960 --> 09:50:33,840 custom hooks let me show you how we can 13742 09:50:30,416 --> 09:50:38,520 make our function more generic basically 13743 09:50:33,840 --> 09:50:41,220 our use fetch person because I mean 13744 09:50:38,520 --> 09:50:43,800 chances are we'll probably want to fetch 13745 09:50:41,220 --> 09:50:46,500 more than just the user for example we 13746 09:50:43,800 --> 09:50:48,776 want to fetch an array or something 13747 09:50:46,500 --> 09:50:50,040 along those lines so again the 13748 09:50:48,776 --> 09:50:52,080 functionality pretty much is going to 13749 09:50:50,040 --> 09:50:54,360 stay the same I'll just change some 13750 09:50:52,080 --> 09:50:57,180 names and what we're returning from the 13751 09:50:54,360 --> 09:51:00,720 function and as a result we'll be able 13752 09:50:57,180 --> 09:51:04,020 to use the custom fetch hook with pretty 13753 09:51:00,720 --> 09:51:06,240 much any data not just with a user 13754 09:51:04,020 --> 09:51:08,340 object so let's try this one out where 13755 09:51:06,240 --> 09:51:10,560 first I want to navigate to the star 13756 09:51:08,340 --> 09:51:13,916 I'll create a new file and I'll call 13757 09:51:10,560 --> 09:51:15,776 this use fetch pretty generic correct 13758 09:51:13,916 --> 09:51:16,860 then pretty much I want to take the 13759 09:51:15,776 --> 09:51:19,436 whole thing 13760 09:51:16,860 --> 09:51:23,160 one by one we'll just change some values 13761 09:51:19,436 --> 09:51:25,860 so copy and paste over here and instead 13762 09:51:23,160 --> 09:51:27,540 of user why don't we call this data I 13763 09:51:25,860 --> 09:51:31,256 think that's pretty generic correct so 13764 09:51:27,540 --> 09:51:35,340 data set data and as far as default 13765 09:51:31,256 --> 09:51:38,160 value I'll still keep it as a and L then 13766 09:51:35,340 --> 09:51:40,500 we want to change around the name it's 13767 09:51:38,160 --> 09:51:42,720 not going to be fetch user I'll actually 13768 09:51:40,500 --> 09:51:44,460 call this fetch data and I just need to 13769 09:51:42,720 --> 09:51:46,860 remind myself that pretty much 13770 09:51:44,460 --> 09:51:48,540 everywhere where I have fetch user I 13771 09:51:46,860 --> 09:51:52,256 want to also 13772 09:51:48,540 --> 09:51:56,096 rename it to a fetch data so page data 13773 09:51:52,256 --> 09:51:59,276 here and then we also invoke it now I 13774 09:51:56,096 --> 09:52:02,700 don't have user anymore I have the data 13775 09:51:59,276 --> 09:52:05,220 so instead of user from this hook I'm 13776 09:52:02,700 --> 09:52:06,480 returning a data and then lastly notice 13777 09:52:05,220 --> 09:52:08,220 over here 13778 09:52:06,480 --> 09:52:10,200 as far as the response I'm calling this 13779 09:52:08,220 --> 09:52:12,180 a user well 13780 09:52:10,200 --> 09:52:14,160 we can come up with response so that's 13781 09:52:12,180 --> 09:52:16,140 going to be pretty generic where I'm 13782 09:52:14,160 --> 09:52:19,916 going to go with set data and I'll set 13783 09:52:16,140 --> 09:52:23,160 it equal to respond now let's save it we 13784 09:52:19,916 --> 09:52:24,596 want to navigate to batch data and let 13785 09:52:23,160 --> 09:52:26,936 me double check yep 13786 09:52:24,596 --> 09:52:28,436 of course I have a mess up here it's not 13787 09:52:26,936 --> 09:52:32,880 fetch person 13788 09:52:28,436 --> 09:52:34,860 I'm gonna go with use and then fetch and 13789 09:52:32,880 --> 09:52:37,620 same over here now technically this 13790 09:52:34,860 --> 09:52:39,540 should work because the export is 13791 09:52:37,620 --> 09:52:42,240 default but 13792 09:52:39,540 --> 09:52:45,540 I'm gonna rename that and then back in 13793 09:52:42,240 --> 09:52:46,680 the fetch data instead of use person or 13794 09:52:45,540 --> 09:52:48,840 move this 13795 09:52:46,680 --> 09:52:51,120 and we want to set it from the scratch 13796 09:52:48,840 --> 09:52:53,160 where I'm gonna go with use and then 13797 09:52:51,120 --> 09:52:55,860 Fetch and we still want to pass in the 13798 09:52:53,160 --> 09:52:58,436 URL now you're probably wondering okay 13799 09:52:55,860 --> 09:53:01,140 what about the user since that's what 13800 09:52:58,436 --> 09:53:03,660 we're using in this component well we're 13801 09:53:01,140 --> 09:53:06,840 not getting back any more user this is 13802 09:53:03,660 --> 09:53:10,916 actually data so we have two options you 13803 09:53:06,840 --> 09:53:12,360 can remove the user and go with data but 13804 09:53:10,916 --> 09:53:14,756 then just keep in mind that everywhere 13805 09:53:12,360 --> 09:53:18,416 where you have reference to a user you 13806 09:53:14,756 --> 09:53:20,700 also need to use data or you can simply 13807 09:53:18,416 --> 09:53:23,276 add an alias again a JavaScript thing 13808 09:53:20,700 --> 09:53:26,400 where I'm saying yeah I want to grab the 13809 09:53:23,276 --> 09:53:28,560 data out of the use fetch however in 13810 09:53:26,400 --> 09:53:32,220 this component I'm going to call this a 13811 09:53:28,560 --> 09:53:35,160 user that's it notice everything still 13812 09:53:32,220 --> 09:53:37,436 works in this case we have the generic 13813 09:53:35,160 --> 09:53:39,720 one which effectively we can take from 13814 09:53:37,436 --> 09:53:43,200 Project to project and then every time 13815 09:53:39,720 --> 09:53:47,040 we want to get some data we just invoke 13816 09:53:43,200 --> 09:53:48,596 the use fetch pass in the URL and if 13817 09:53:47,040 --> 09:53:50,220 everything is corrected we have no 13818 09:53:48,596 --> 09:53:52,800 errors 13819 09:53:50,220 --> 09:53:55,980 once we're done loading we'll get back 13820 09:53:52,800 --> 09:53:59,400 the data and up next let's discuss 13821 09:53:55,980 --> 09:54:02,520 context API and we're going to start 13822 09:53:59,400 --> 09:54:07,256 actually with the challenge where I want 13823 09:54:02,520 --> 09:54:09,960 you to see the pain of prop Drilling and 13824 09:54:07,256 --> 09:54:12,360 the challenge is following 13825 09:54:09,960 --> 09:54:16,680 first you want to navigate to the star 13826 09:54:12,360 --> 09:54:21,060 in zero nine context API and create 13827 09:54:16,680 --> 09:54:23,580 three components create nav bar jsx 13828 09:54:21,060 --> 09:54:27,180 then nav links which you want to Nest 13829 09:54:23,580 --> 09:54:28,500 inside of the number and then user 13830 09:54:27,180 --> 09:54:31,200 container 13831 09:54:28,500 --> 09:54:33,776 which you want to Nest inside of the nav 13832 09:54:31,200 --> 09:54:36,776 links one so yes I don't want you to 13833 09:54:33,776 --> 09:54:39,300 Nest use your container and nav links 13834 09:54:36,776 --> 09:54:41,700 inside of the navbar no our use case is 13835 09:54:39,300 --> 09:54:44,160 going to be the navbar component which 13836 09:54:41,700 --> 09:54:47,700 basically is our parent then the child 13837 09:54:44,160 --> 09:54:50,756 is nav links and then the user container 13838 09:54:47,700 --> 09:54:53,160 is nested inside of the navic so 13839 09:54:50,756 --> 09:54:56,040 essentially we have this 13840 09:54:53,160 --> 09:54:58,916 three level structure now after that 13841 09:54:56,040 --> 09:55:02,276 once you have the basic setup in place I 13842 09:54:58,916 --> 09:55:03,596 want you to import navbar.jsx in the app 13843 09:55:02,276 --> 09:55:07,320 jsx 13844 09:55:03,596 --> 09:55:10,256 now as far as my solution I'm going to 13845 09:55:07,320 --> 09:55:13,020 remove the container just so it looks 13846 09:55:10,256 --> 09:55:14,700 like a nav bar because I have all that 13847 09:55:13,020 --> 09:55:17,700 padding in the container and all that 13848 09:55:14,700 --> 09:55:20,520 and it doesn't look very good if I place 13849 09:55:17,700 --> 09:55:23,400 that nav bar but you don't have to do 13850 09:55:20,520 --> 09:55:25,916 that and also don't worry about the CSS 13851 09:55:23,400 --> 09:55:28,916 the main goal of this challenge is to 13852 09:55:25,916 --> 09:55:31,320 work on a logic if your example doesn't 13853 09:55:28,916 --> 09:55:33,840 look exactly like mine don't worry about 13854 09:55:31,320 --> 09:55:35,756 it again we're just focusing on the 13855 09:55:33,840 --> 09:55:37,500 functionality so once you have that 13856 09:55:35,756 --> 09:55:39,776 nested structure 13857 09:55:37,500 --> 09:55:43,436 then in the nav bar I want you to set up 13858 09:55:39,776 --> 09:55:45,660 a user State value the default is going 13859 09:55:43,436 --> 09:55:46,860 to be an object with some kind of name 13860 09:55:45,660 --> 09:55:48,596 property 13861 09:55:46,860 --> 09:55:51,540 again whatever you want to place over 13862 09:55:48,596 --> 09:55:54,416 here as well as the logout function 13863 09:55:51,540 --> 09:55:55,380 that sets the user back to no 13864 09:55:54,416 --> 09:55:58,200 no 13865 09:55:55,380 --> 09:56:00,840 I want to pass both of them down to a 13866 09:55:58,200 --> 09:56:03,840 user container and just to give you a 13867 09:56:00,840 --> 09:56:07,080 hint yes you will have to pass those 13868 09:56:03,840 --> 09:56:09,380 values through the nav links now in the 13869 09:56:07,080 --> 09:56:13,860 user container I want you to display 13870 09:56:09,380 --> 09:56:16,256 user as well as the button and then once 13871 09:56:13,860 --> 09:56:18,596 the user clicks the button I want to set 13872 09:56:16,256 --> 09:56:19,620 it equal to null now there's an extra 13873 09:56:18,596 --> 09:56:22,980 challenge 13874 09:56:19,620 --> 09:56:26,640 you can also use conditional rendering 13875 09:56:22,980 --> 09:56:29,160 and basically if the user is null then 13876 09:56:26,640 --> 09:56:31,680 you can display please login notice over 13877 09:56:29,160 --> 09:56:34,020 here that's the final example but again 13878 09:56:31,680 --> 09:56:36,776 this is an extra challenge our main 13879 09:56:34,020 --> 09:56:40,020 focus is to get both of these values 13880 09:56:36,776 --> 09:56:42,300 down from the nav bar to a user 13881 09:56:40,020 --> 09:56:43,680 component and once you're ready to 13882 09:56:42,300 --> 09:56:46,680 compare the results 13883 09:56:43,680 --> 09:56:48,540 resume with the videos Okay so let's 13884 09:56:46,680 --> 09:56:52,020 start working on the challenge 13885 09:56:48,540 --> 09:56:54,540 first I want to navigate to that folder 13886 09:56:52,020 --> 09:56:56,936 so again we're looking for context we're 13887 09:56:54,540 --> 09:56:59,640 not looking for the final one 13888 09:56:56,936 --> 09:57:02,220 we'll start working the star and we want 13889 09:56:59,640 --> 09:57:06,180 to set up those components let's start 13890 09:57:02,220 --> 09:57:08,700 with the main one so nav bar jsx let's 13891 09:57:06,180 --> 09:57:10,380 set up the component okay awesome and 13892 09:57:08,700 --> 09:57:14,460 then we want to do the same thing for 13893 09:57:10,380 --> 09:57:16,020 the nav links as well as the 13894 09:57:14,460 --> 09:57:19,200 what was the name 13895 09:57:16,020 --> 09:57:22,020 I believe they named it user container 13896 09:57:19,200 --> 09:57:23,700 yep that's the one so let's continue I'm 13897 09:57:22,020 --> 09:57:27,776 going to go with nav 13898 09:57:23,700 --> 09:57:30,776 links jsx again create the component 13899 09:57:27,776 --> 09:57:33,596 okay awesome and then lastly we want to 13900 09:57:30,776 --> 09:57:36,060 create that user container user 13901 09:57:33,596 --> 09:57:39,120 container jsx again 13902 09:57:36,060 --> 09:57:43,080 set up the component let's save now 13903 09:57:39,120 --> 09:57:47,040 remember we have nested structure so 13904 09:57:43,080 --> 09:57:51,000 I want to go to navbar and First grab 13905 09:57:47,040 --> 09:57:52,800 the nav links so in the nav bar I will 13906 09:57:51,000 --> 09:57:56,340 add a class here 13907 09:57:52,800 --> 09:57:57,660 of navbar just for style so I'll say 13908 09:57:56,340 --> 09:58:00,840 here nav 13909 09:57:57,660 --> 09:58:04,080 let's add the CSS and the class is 13910 09:58:00,840 --> 09:58:07,080 navbar and then in here inside of the 13911 09:58:04,080 --> 09:58:08,700 nav bar I'll have the heading 5 with 13912 09:58:07,080 --> 09:58:11,756 some kind of logo 13913 09:58:08,700 --> 09:58:13,916 so in this case I'll go with context API 13914 09:58:11,756 --> 09:58:17,276 and then I also want to grab those 13915 09:58:13,916 --> 09:58:20,276 navlicks so let's set up that component 13916 09:58:17,276 --> 09:58:22,980 and we want to import them like so let's 13917 09:58:20,276 --> 09:58:25,560 save now I don't see anything because of 13918 09:58:22,980 --> 09:58:28,560 course in here I'm importing the 13919 09:58:25,560 --> 09:58:29,756 complete one so now I right away want to 13920 09:58:28,560 --> 09:58:34,140 go I guess 13921 09:58:29,756 --> 09:58:35,820 two hey app jsx and when I grab the nav 13922 09:58:34,140 --> 09:58:36,960 bar from the star 13923 09:58:35,820 --> 09:58:38,936 so again 13924 09:58:36,960 --> 09:58:41,700 we'll have two of them and like I said 13925 09:58:38,936 --> 09:58:43,980 I'm not placing anything inside of the 13926 09:58:41,700 --> 09:58:46,620 container and since I still want to 13927 09:58:43,980 --> 09:58:49,080 showcase the final one I think I'm gonna 13928 09:58:46,620 --> 09:58:51,776 go with the react fragment 13929 09:58:49,080 --> 09:58:55,140 so I'm gonna go here with star 13930 09:58:51,776 --> 09:58:57,840 then it's not coming from the final it's 13931 09:58:55,140 --> 09:59:00,660 actually coming from the Star and then 13932 09:58:57,840 --> 09:59:03,596 let me set up that fragment first 13933 09:59:00,660 --> 09:59:05,700 and then inside of the fragment we'll 13934 09:59:03,596 --> 09:59:07,980 place a final so that's going to be on 13935 09:59:05,700 --> 09:59:10,680 the top and then we'll have the star 13936 09:59:07,980 --> 09:59:12,540 let's save it and I actually get the 13937 09:59:10,680 --> 09:59:15,416 narrow because 13938 09:59:12,540 --> 09:59:17,756 in the final one I'm looking for context 13939 09:59:15,416 --> 09:59:19,740 and of course the moment there is no 13940 09:59:17,756 --> 09:59:21,720 context now if you're wondering about 13941 09:59:19,740 --> 09:59:23,460 the folder structure notice the final 13942 09:59:21,720 --> 09:59:25,380 one there's going to be two examples 13943 09:59:23,460 --> 09:59:26,936 there's going to be a prop drilling 13944 09:59:25,380 --> 09:59:29,880 which is the one that we're going to be 13945 09:59:26,936 --> 09:59:31,860 working on right now so if you ever want 13946 09:59:29,880 --> 09:59:33,720 to compare the results look for prop 13947 09:59:31,860 --> 09:59:36,360 Drilling and then there's going to be 13948 09:59:33,720 --> 09:59:40,320 the context one so that's going to be 13949 09:59:36,360 --> 09:59:42,540 our complete example with context API so 13950 09:59:40,320 --> 09:59:44,700 in here I need to change the path where 13951 09:59:42,540 --> 09:59:47,220 it's not going to be context it's 13952 09:59:44,700 --> 09:59:49,680 actually going to be a starter and then 13953 09:59:47,220 --> 09:59:51,120 nav bar let me save it this should do it 13954 09:59:49,680 --> 09:59:54,960 and now notice 13955 09:59:51,120 --> 09:59:58,020 so I have context API that's my logo and 13956 09:59:54,960 --> 10:00:01,200 I have the nav links so up next I want 13957 09:59:58,020 --> 10:00:04,800 to go to a now links and I want to set 13958 10:00:01,200 --> 10:00:07,740 up the logic as well and in here it's 13959 10:00:04,800 --> 10:00:10,140 going to be a div with nav container and 13960 10:00:07,740 --> 10:00:12,300 then inside of it we'll have an old list 13961 10:00:10,140 --> 10:00:14,820 with nav links and then the user 13962 10:00:12,300 --> 10:00:17,756 container so I guess let's start by 13963 10:00:14,820 --> 10:00:21,240 setting up that div we do want to add a 13964 10:00:17,756 --> 10:00:23,820 class of nav container 13965 10:00:21,240 --> 10:00:25,680 and then let's go with that on our list 13966 10:00:23,820 --> 10:00:28,256 so this is where we'll display those 13967 10:00:25,680 --> 10:00:32,160 links so an ordered list and let's call 13968 10:00:28,256 --> 10:00:35,040 this nav links and in here list item and 13969 10:00:32,160 --> 10:00:36,960 I'm just going to go with an href and 13970 10:00:35,040 --> 10:00:39,300 for time being I'm just going to pass in 13971 10:00:36,960 --> 10:00:43,256 the hashtag so we're not going to go 13972 10:00:39,300 --> 10:00:46,140 anywhere and then let's copy and paste 13973 10:00:43,256 --> 10:00:49,380 and this one is going to be about 13974 10:00:46,140 --> 10:00:51,776 now after the unordered list I'm gonna 13975 10:00:49,380 --> 10:00:54,480 display that user container so again 13976 10:00:51,776 --> 10:00:57,060 let's use the Auto Imports let's grab 13977 10:00:54,480 --> 10:00:59,640 the user container and now notice 13978 10:00:57,060 --> 10:01:03,000 I have the links and I also have the 13979 10:00:59,640 --> 10:01:04,500 user container and before we set up any 13980 10:01:03,000 --> 10:01:07,860 kind of logic 13981 10:01:04,500 --> 10:01:10,620 in the user container I want to set up 13982 10:01:07,860 --> 10:01:13,020 those values in the navbar and 13983 10:01:10,620 --> 10:01:15,660 effectively I want to set up the state 13984 10:01:13,020 --> 10:01:18,480 value the user and I also want to get 13985 10:01:15,660 --> 10:01:20,820 the logout one so let's go over here 13986 10:01:18,480 --> 10:01:22,620 let me close the sidebar I'm going to go 13987 10:01:20,820 --> 10:01:25,380 with use 13988 10:01:22,620 --> 10:01:27,660 State like I said the default value is 13989 10:01:25,380 --> 10:01:30,840 going to be that object in my case the 13990 10:01:27,660 --> 10:01:34,560 name will be Bob so name is equal to a 13991 10:01:30,840 --> 10:01:36,900 bob now what are we getting back well 13992 10:01:34,560 --> 10:01:41,820 we want to get back the array 13993 10:01:36,900 --> 10:01:44,220 the user and set user now we also want 13994 10:01:41,820 --> 10:01:46,680 to set up right away what the logger one 13995 10:01:44,220 --> 10:01:49,740 so const logout and as far as the 13996 10:01:46,680 --> 10:01:53,096 function in here we want to go with set 13997 10:01:49,740 --> 10:01:56,160 user and set it back to no 13998 10:01:53,096 --> 10:02:00,660 and like I said the main 13999 10:01:56,160 --> 10:02:03,660 challenge here is to get both user a log 14000 10:02:00,660 --> 10:02:07,020 out down to a user container 14001 10:02:03,660 --> 10:02:09,240 so not just the nav links but the user 14002 10:02:07,020 --> 10:02:11,700 container how we can do that well since 14003 10:02:09,240 --> 10:02:14,160 we can only pass props down 14004 10:02:11,700 --> 10:02:16,800 we go to in nav links and we come up 14005 10:02:14,160 --> 10:02:20,520 with a prop so I'm going to go with user 14006 10:02:16,800 --> 10:02:22,080 is equal to user and then log out is 14007 10:02:20,520 --> 10:02:25,020 equal to a logout 14008 10:02:22,080 --> 10:02:28,080 let's save that now we want to go to a 14009 10:02:25,020 --> 10:02:30,660 nav links and essentially yes even 14010 10:02:28,080 --> 10:02:33,360 though we're not using any of these 14011 10:02:30,660 --> 10:02:35,400 props in the nav links component we'll 14012 10:02:33,360 --> 10:02:39,720 have to grab them and we'll have to pass 14013 10:02:35,400 --> 10:02:41,580 them down to a user container so it's 14014 10:02:39,720 --> 10:02:44,096 not an official name but effectively 14015 10:02:41,580 --> 10:02:45,720 it's called prop drilling where you're 14016 10:02:44,096 --> 10:02:47,400 passing 14017 10:02:45,720 --> 10:02:49,560 through the components that are actually 14018 10:02:47,400 --> 10:02:53,040 not using those props because again we 14019 10:02:49,560 --> 10:02:55,140 can only pass props down and as you can 14020 10:02:53,040 --> 10:02:57,000 see in this case we only have what three 14021 10:02:55,140 --> 10:03:00,240 levels basically we have parent child 14022 10:02:57,000 --> 10:03:02,756 and let's call it grandchild but it's 14023 10:03:00,240 --> 10:03:06,300 pretty normal in the application to have 14024 10:03:02,756 --> 10:03:08,276 five six or whatever levels so probably 14025 10:03:06,300 --> 10:03:11,276 your next question is are we gonna have 14026 10:03:08,276 --> 10:03:13,860 to do that well yeah if we're not using 14027 10:03:11,276 --> 10:03:15,596 the state libraries or the context API 14028 10:03:13,860 --> 10:03:17,580 which we're going to cover in the 14029 10:03:15,596 --> 10:03:20,700 following videos essentially that's the 14030 10:03:17,580 --> 10:03:22,620 setup there's no way around it so let's 14031 10:03:20,700 --> 10:03:24,900 go back over here let's say that I'm 14032 10:03:22,620 --> 10:03:27,960 going to be looking for a user and a log 14033 10:03:24,900 --> 10:03:32,040 out and yep the same deal I want to go 14034 10:03:27,960 --> 10:03:33,840 with user is equal to a user and I 14035 10:03:32,040 --> 10:03:34,916 probably don't have to say that of 14036 10:03:33,840 --> 10:03:37,020 course you can name this prop 14037 10:03:34,916 --> 10:03:39,300 differently but I just find it 14038 10:03:37,020 --> 10:03:43,080 convenient if we go with the same name 14039 10:03:39,300 --> 10:03:45,660 so I'm gonna go logout is equal to a log 14040 10:03:43,080 --> 10:03:48,240 out and then in the user container this 14041 10:03:45,660 --> 10:03:52,080 is finally where we're going to use them 14042 10:03:48,240 --> 10:03:54,776 so again same deal we go with user and 14043 10:03:52,080 --> 10:03:58,140 we want to set up the logo and now let's 14044 10:03:54,776 --> 10:04:00,180 work on pretty straightforward jsx where 14045 10:03:58,140 --> 10:04:04,980 I'm going to set up a div I'm going to 14046 10:04:00,180 --> 10:04:07,140 add a class name of user container and 14047 10:04:04,980 --> 10:04:09,900 then inside of it for now I'm not gonna 14048 10:04:07,140 --> 10:04:13,140 render anything conditionally I'll just 14049 10:04:09,900 --> 10:04:15,540 say hey grab me the user and also set up 14050 10:04:13,140 --> 10:04:17,340 a logout button so I'm going to have a 14051 10:04:15,540 --> 10:04:21,660 paragraph with hello 14052 10:04:17,340 --> 10:04:24,660 their comma then let's grab the user and 14053 10:04:21,660 --> 10:04:26,820 remember the property his name and then 14054 10:04:24,660 --> 10:04:28,620 right after the paragraph We will set up 14055 10:04:26,820 --> 10:04:30,120 a button 14056 10:04:28,620 --> 10:04:32,160 so button 14057 10:04:30,120 --> 10:04:34,860 class name 14058 10:04:32,160 --> 10:04:36,000 is going to be a button and then on 14059 10:04:34,860 --> 10:04:39,416 click 14060 10:04:36,000 --> 10:04:42,300 it's going to be equal to our logar one 14061 10:04:39,416 --> 10:04:45,240 so log out here and as far as the text 14062 10:04:42,300 --> 10:04:48,720 let's just go with logout let's save and 14063 10:04:45,240 --> 10:04:51,480 notice I have Bob and once I click I 14064 10:04:48,720 --> 10:04:53,400 actually set this equal to null now 14065 10:04:51,480 --> 10:04:56,640 we're going to have an error here 14066 10:04:53,400 --> 10:04:58,740 because once the user 14067 10:04:56,640 --> 10:05:02,096 essentially is null then of course we 14068 10:04:58,740 --> 10:05:05,936 cannot access the name property and in 14069 10:05:02,096 --> 10:05:08,520 order to avoid this error remember we 14070 10:05:05,936 --> 10:05:12,360 have the optional chaining where I can 14071 10:05:08,520 --> 10:05:14,756 say Hey listen if the user exists then 14072 10:05:12,360 --> 10:05:16,740 grab the name if not then just return 14073 10:05:14,756 --> 10:05:19,200 undefined so no others 14074 10:05:16,740 --> 10:05:21,120 once I click hello there and basically I 14075 10:05:19,200 --> 10:05:23,936 have nothing there because the value is 14076 10:05:21,120 --> 10:05:25,916 undefined and I also want to add to 14077 10:05:23,936 --> 10:05:27,660 uppercase just to Showcase that we can 14078 10:05:25,916 --> 10:05:30,240 use multiple 14079 10:05:27,660 --> 10:05:31,680 optional training operators and I'm 14080 10:05:30,240 --> 10:05:32,936 gonna go with two 14081 10:05:31,680 --> 10:05:36,776 upper 14082 10:05:32,936 --> 10:05:38,640 and case here let's save it so I'm 14083 10:05:36,776 --> 10:05:41,340 saying if there's a user grab the name 14084 10:05:38,640 --> 10:05:44,096 if there's a name then set it to an 14085 10:05:41,340 --> 10:05:46,080 uppercase so now once I refresh notice I 14086 10:05:44,096 --> 10:05:48,300 have Bob and then again once I click I 14087 10:05:46,080 --> 10:05:51,060 have nothing and then lastly let's set 14088 10:05:48,300 --> 10:05:54,180 up that conditional rendering where 14089 10:05:51,060 --> 10:05:57,416 if the user exists then of course I want 14090 10:05:54,180 --> 10:05:59,820 to provide the paragraph and the button 14091 10:05:57,416 --> 10:06:02,756 however if there's no user I simply want 14092 10:05:59,820 --> 10:06:05,520 to go with please login and for that 14093 10:06:02,756 --> 10:06:08,460 I'll first set up a paragraph I'll say 14094 10:06:05,520 --> 10:06:11,520 please log in and then above both of 14095 10:06:08,460 --> 10:06:13,500 them for now I'm gonna go user and 14096 10:06:11,520 --> 10:06:15,120 remember we go with question mark and 14097 10:06:13,500 --> 10:06:18,720 then we have two options 14098 10:06:15,120 --> 10:06:21,120 so first if it's true we return 14099 10:06:18,720 --> 10:06:23,096 something and then if it's false so 14100 10:06:21,120 --> 10:06:25,320 let's start here by cutting this out but 14101 10:06:23,096 --> 10:06:26,520 there's another gotcha if you copy and 14102 10:06:25,320 --> 10:06:29,340 paste there's actually going to be an 14103 10:06:26,520 --> 10:06:32,460 error because notice we have effectively 14104 10:06:29,340 --> 10:06:33,776 two adjacent elements and we can only 14105 10:06:32,460 --> 10:06:36,180 return one 14106 10:06:33,776 --> 10:06:40,500 now the way around that is again either 14107 10:06:36,180 --> 10:06:43,080 to set up a div or we can go with a 14108 10:06:40,500 --> 10:06:44,820 fragment copy and paste and now 14109 10:06:43,080 --> 10:06:47,580 everything works and then in here since 14110 10:06:44,820 --> 10:06:50,820 I'm returning only one cut it out and I 14111 10:06:47,580 --> 10:06:54,060 can set it up so now notice since the 14112 10:06:50,820 --> 10:06:56,096 user is no I have login 14113 10:06:54,060 --> 10:06:57,900 however once I have the user I have 14114 10:06:56,096 --> 10:07:01,500 there hello there Bob and once I click 14115 10:06:57,900 --> 10:07:03,596 now essentially I log out the user now 14116 10:07:01,500 --> 10:07:05,040 pretty much most of the functionality we 14117 10:07:03,596 --> 10:07:08,520 already covered 14118 10:07:05,040 --> 10:07:10,620 at some point during the fundamentals 14119 10:07:08,520 --> 10:07:12,480 and tutorial like I mentioned already 14120 10:07:10,620 --> 10:07:15,240 previously the main reason for this 14121 10:07:12,480 --> 10:07:16,740 challenge was to give you a first-hand 14122 10:07:15,240 --> 10:07:20,340 experience 14123 10:07:16,740 --> 10:07:21,180 of such nested structure so if you do 14124 10:07:20,340 --> 10:07:24,180 have 14125 10:07:21,180 --> 10:07:26,756 a component a parent 14126 10:07:24,180 --> 10:07:28,500 and you have the nested structure where 14127 10:07:26,756 --> 10:07:31,916 you have some kind of child and that 14128 10:07:28,500 --> 10:07:34,020 child has child on its own and you want 14129 10:07:31,916 --> 10:07:36,256 to pass something down from the parent 14130 10:07:34,020 --> 10:07:39,596 to let's say 14131 10:07:36,256 --> 10:07:42,120 grandparent let's call it that then you 14132 10:07:39,596 --> 10:07:45,300 will have to pass the props 14133 10:07:42,120 --> 10:07:47,936 through all of the components even 14134 10:07:45,300 --> 10:07:50,580 though technically this component is not 14135 10:07:47,936 --> 10:07:52,320 using any of these two props all right 14136 10:07:50,580 --> 10:07:55,756 and now let's take a look how we can use 14137 10:07:52,320 --> 10:08:00,060 context API which is provided by react 14138 10:07:55,756 --> 10:08:02,160 to essentially solve our issue like I 14139 10:08:00,060 --> 10:08:06,900 mentioned already in the previous video 14140 10:08:02,160 --> 10:08:10,020 if you want to see the exact example we 14141 10:08:06,900 --> 10:08:11,520 just completed look for prop drilling so 14142 10:08:10,020 --> 10:08:13,500 in the context one 14143 10:08:11,520 --> 10:08:16,256 you'll actually see 14144 10:08:13,500 --> 10:08:18,480 the setup which we're going to work on 14145 10:08:16,256 --> 10:08:20,520 right now now the reason why I'm telling 14146 10:08:18,480 --> 10:08:23,096 you that because I'll remove essentially 14147 10:08:20,520 --> 10:08:26,040 the user and logout from the nav links 14148 10:08:23,096 --> 10:08:27,540 as well as the nav bar and user 14149 10:08:26,040 --> 10:08:29,756 container so we'll start from the 14150 10:08:27,540 --> 10:08:31,800 scratch keep in mind that if you ever 14151 10:08:29,756 --> 10:08:33,900 need a reference it's right over here in 14152 10:08:31,800 --> 10:08:35,400 the final so one by one let's remove 14153 10:08:33,900 --> 10:08:37,620 them because that's not how we're going 14154 10:08:35,400 --> 10:08:38,340 to pass them down so let's remove over 14155 10:08:37,620 --> 10:08:39,900 here 14156 10:08:38,340 --> 10:08:41,880 let's save 14157 10:08:39,900 --> 10:08:43,200 then we want to do the same thing in the 14158 10:08:41,880 --> 10:08:45,240 now links 14159 10:08:43,200 --> 10:08:46,380 effectively we're looking for a way to 14160 10:08:45,240 --> 10:08:50,520 bypass 14161 10:08:46,380 --> 10:08:53,756 this one or multiple components that we 14162 10:08:50,520 --> 10:08:56,220 have sitting in between the main one the 14163 10:08:53,756 --> 10:08:58,380 parent and then whichever component 14164 10:08:56,220 --> 10:09:01,436 wants to use those values so let's 14165 10:08:58,380 --> 10:09:04,140 remove this and we'll probably have a 14166 10:09:01,436 --> 10:09:06,000 narrow right now so you know what 14167 10:09:04,140 --> 10:09:08,460 let me just 14168 10:09:06,000 --> 10:09:10,916 return something else for now again 14169 10:09:08,460 --> 10:09:13,560 we'll remove this I just don't want 14170 10:09:10,916 --> 10:09:15,180 react to start looking for the user 14171 10:09:13,560 --> 10:09:17,640 value and all that let's just go with 14172 10:09:15,180 --> 10:09:19,740 the return and we'll go with hello world 14173 10:09:17,640 --> 10:09:23,040 let's save it we should be good to go 14174 10:09:19,740 --> 10:09:26,880 now how we can pass those values 14175 10:09:23,040 --> 10:09:29,276 down without doing the prop drilling 14176 10:09:26,880 --> 10:09:32,276 well like I said we need to use context 14177 10:09:29,276 --> 10:09:35,220 API now there's multiple ways how again 14178 10:09:32,276 --> 10:09:37,860 we can invoke this if you have react 14179 10:09:35,220 --> 10:09:40,380 imported you can go and react and then 14180 10:09:37,860 --> 10:09:43,020 create context but again you'll have to 14181 10:09:40,380 --> 10:09:44,640 import react of course notice once I 14182 10:09:43,020 --> 10:09:47,520 save I'll have big fat error because 14183 10:09:44,640 --> 10:09:51,060 react is not defined just like with huge 14184 10:09:47,520 --> 10:09:52,916 state or we can right away import that 14185 10:09:51,060 --> 10:09:56,276 correct so since we already have used 14186 10:09:52,916 --> 10:10:00,180 State we can go to create context 14187 10:09:56,276 --> 10:10:03,480 and essentially in the parent we want to 14188 10:10:00,180 --> 10:10:05,580 invoke that create context and this is 14189 10:10:03,480 --> 10:10:08,880 going to return two things it's going to 14190 10:10:05,580 --> 10:10:11,820 return a provider component and a 14191 10:10:08,880 --> 10:10:14,640 consumer component however 14192 10:10:11,820 --> 10:10:17,520 since there is a huge context hook 14193 10:10:14,640 --> 10:10:20,520 I mean there's really no need 14194 10:10:17,520 --> 10:10:24,240 for the consumer however we will use the 14195 10:10:20,520 --> 10:10:27,300 provider so first I want to go above the 14196 10:10:24,240 --> 10:10:30,480 nav bar and I'll right away export since 14197 10:10:27,300 --> 10:10:32,340 eventually we will import the context 14198 10:10:30,480 --> 10:10:34,080 I'm about to create in the user 14199 10:10:32,340 --> 10:10:37,740 container that's why I right away go 14200 10:10:34,080 --> 10:10:39,900 export and then const and come up with a 14201 10:10:37,740 --> 10:10:42,360 name and since we're working in the nav 14202 10:10:39,900 --> 10:10:45,480 bar I mean kind of makes sense 14203 10:10:42,360 --> 10:10:47,276 if I named the snap bar context that one 14204 10:10:45,480 --> 10:10:49,320 is going to be equal to create context 14205 10:10:47,276 --> 10:10:50,580 we can pass in the default value but 14206 10:10:49,320 --> 10:10:52,980 we're not going to do that and 14207 10:10:50,580 --> 10:10:56,040 essentially once we invoke this 14208 10:10:52,980 --> 10:10:57,840 we get back bunch of useful stuff and we 14209 10:10:56,040 --> 10:11:00,900 can log that one here 14210 10:10:57,840 --> 10:11:02,756 so if we go here with context we should 14211 10:11:00,900 --> 10:11:04,740 see a bunch of cool things in a console 14212 10:11:02,756 --> 10:11:06,776 let me just 14213 10:11:04,740 --> 10:11:08,220 clean it out notice 14214 10:11:06,776 --> 10:11:09,800 essentially this is what we're getting 14215 10:11:08,220 --> 10:11:13,436 back but 14216 10:11:09,800 --> 10:11:15,120 our main interest is in this provider 14217 10:11:13,436 --> 10:11:16,740 that's what we definitely want to get 14218 10:11:15,120 --> 10:11:20,460 again 14219 10:11:16,740 --> 10:11:23,340 we used to use consumer but now use 14220 10:11:20,460 --> 10:11:26,400 context hook which we're about to cover 14221 10:11:23,340 --> 10:11:28,860 actually replaced it so there's no need 14222 10:11:26,400 --> 10:11:30,776 for it anymore and basically if we go 14223 10:11:28,860 --> 10:11:33,300 here with DOT notice 14224 10:11:30,776 --> 10:11:34,916 I have these options and the provider is 14225 10:11:33,300 --> 10:11:37,916 what we're looking for 14226 10:11:34,916 --> 10:11:42,360 so we want to grab that provider and go 14227 10:11:37,916 --> 10:11:45,180 to a main parent basically the component 14228 10:11:42,360 --> 10:11:47,960 which is going to provide those values 14229 10:11:45,180 --> 10:11:52,200 and we want to wrap the return 14230 10:11:47,960 --> 10:11:54,120 in that provider so we go to our nav bar 14231 10:11:52,200 --> 10:11:57,240 and let's say not I think I can just 14232 10:11:54,120 --> 10:12:00,000 move this and then we want to wrap our 14233 10:11:57,240 --> 10:12:02,936 nav in the navbar context that's the 14234 10:12:00,000 --> 10:12:06,596 name Dot and then the provider now 14235 10:12:02,936 --> 10:12:08,160 what's really cool that provider has a 14236 10:12:06,596 --> 10:12:10,200 value prop 14237 10:12:08,160 --> 10:12:13,740 and essentially whatever you'll pass 14238 10:12:10,200 --> 10:12:16,380 over here you'll have access anywhere in 14239 10:12:13,740 --> 10:12:18,240 that tree again we have three components 14240 10:12:16,380 --> 10:12:21,840 but please keep in mind you can have a 14241 10:12:18,240 --> 10:12:24,240 setup with 15 components so anywhere in 14242 10:12:21,840 --> 10:12:26,640 that component tree you'll have access 14243 10:12:24,240 --> 10:12:29,160 to this value now 14244 10:12:26,640 --> 10:12:30,776 you can simply go here with hello and 14245 10:12:29,160 --> 10:12:33,000 you'll be able to pass it 14246 10:12:30,776 --> 10:12:35,096 however as you're looking at it you're 14247 10:12:33,000 --> 10:12:37,500 probably thinking what's the use case of 14248 10:12:35,096 --> 10:12:39,480 hello I mean it's nice that I can access 14249 10:12:37,500 --> 10:12:41,220 it in the user container but that's not 14250 10:12:39,480 --> 10:12:42,776 what I'm looking for and you're 14251 10:12:41,220 --> 10:12:45,060 absolutely correct 14252 10:12:42,776 --> 10:12:48,240 so here's what we can do we can turn 14253 10:12:45,060 --> 10:12:49,916 this value into an object so please keep 14254 10:12:48,240 --> 10:12:52,620 in mind this is not a special syntax 14255 10:12:49,916 --> 10:12:55,320 again I'm just simply going back to a 14256 10:12:52,620 --> 10:12:56,160 JavaScript land and I'll set it up as an 14257 10:12:55,320 --> 10:12:58,320 object 14258 10:12:56,160 --> 10:13:00,720 that's it this is not a special Syntax 14259 10:12:58,320 --> 10:13:02,756 for double curlies I keep getting these 14260 10:13:00,720 --> 10:13:05,160 questions in the course q a 14261 10:13:02,756 --> 10:13:08,340 so I really want to stress that I simply 14262 10:13:05,160 --> 10:13:10,320 want to set up an object and if you 14263 10:13:08,340 --> 10:13:12,900 want to make it more readable 14264 10:13:10,320 --> 10:13:14,820 you can set it up over here you can 14265 10:13:12,900 --> 10:13:16,860 create an object and then pass it 14266 10:13:14,820 --> 10:13:18,900 directly here in a value if that's 14267 10:13:16,860 --> 10:13:21,300 easier for you to read 14268 10:13:18,900 --> 10:13:24,180 I'm not gonna do that but 14269 10:13:21,300 --> 10:13:26,580 just an option so I have an object and 14270 10:13:24,180 --> 10:13:28,320 now notice effectively I can pass both 14271 10:13:26,580 --> 10:13:30,540 of them down here in the subject as 14272 10:13:28,320 --> 10:13:33,540 properties I can say user is equal to 14273 10:13:30,540 --> 10:13:36,540 user and logout is equal to a log out 14274 10:13:33,540 --> 10:13:39,060 let's save that and now we need to go to 14275 10:13:36,540 --> 10:13:41,756 a user container and notice how we're 14276 10:13:39,060 --> 10:13:44,340 nicely bypassing the nav links and we 14277 10:13:41,756 --> 10:13:47,300 want to grab those values and for that 14278 10:13:44,340 --> 10:13:50,220 we'll need two things we'll need a 14279 10:13:47,300 --> 10:13:52,860 navbar context that's why we're 14280 10:13:50,220 --> 10:13:56,936 exporting and also 14281 10:13:52,860 --> 10:14:00,180 we'll need a huge context Hook from 14282 10:13:56,936 --> 10:14:01,320 the react so let's navigate to a user 14283 10:14:00,180 --> 10:14:03,840 container 14284 10:14:01,320 --> 10:14:07,256 and in here let's import both of those 14285 10:14:03,840 --> 10:14:08,660 things the use contacts as well as nav 14286 10:14:07,256 --> 10:14:11,580 bar context 14287 10:14:08,660 --> 10:14:14,096 so I'm gonna go for now above the return 14288 10:14:11,580 --> 10:14:17,220 again I don't want to get the error and 14289 10:14:14,096 --> 10:14:19,200 essentially we want to go with use 14290 10:14:17,220 --> 10:14:22,436 context talk which is a special hook 14291 10:14:19,200 --> 10:14:24,300 which is looking for that context again 14292 10:14:22,436 --> 10:14:26,340 this is coming from react 14293 10:14:24,300 --> 10:14:27,120 just like you state use effect and all 14294 10:14:26,340 --> 10:14:30,300 that 14295 10:14:27,120 --> 10:14:33,000 and we want to pass in that nav bar 14296 10:14:30,300 --> 10:14:35,880 context or whatever context you have 14297 10:14:33,000 --> 10:14:38,580 so the one that's in the parent whatever 14298 10:14:35,880 --> 10:14:40,140 you create over here you export and then 14299 10:14:38,580 --> 10:14:43,080 the use context is going to be looking 14300 10:14:40,140 --> 10:14:45,900 for that so here I can go with navbar 14301 10:14:43,080 --> 10:14:48,480 and context and since my Auto Imports 14302 10:14:45,900 --> 10:14:49,980 are working we're good to go now what is 14303 10:14:48,480 --> 10:14:52,620 this returning well 14304 10:14:49,980 --> 10:14:55,620 let's go back to your navbar what are we 14305 10:14:52,620 --> 10:14:58,140 setting up over here an object correct 14306 10:14:55,620 --> 10:15:00,416 so let's try this one out I'm gonna come 14307 10:14:58,140 --> 10:15:03,480 up with a variable and I'll set it equal 14308 10:15:00,416 --> 10:15:06,596 to a value and you'll notice nicely in 14309 10:15:03,480 --> 10:15:09,900 the console that we have access to that 14310 10:15:06,596 --> 10:15:12,660 object so every user with the name 14311 10:15:09,900 --> 10:15:16,140 property as well as the logo 14312 10:15:12,660 --> 10:15:20,700 so in my jsx I can either go with Dot 14313 10:15:16,140 --> 10:15:24,000 and then grab the logout or user or we 14314 10:15:20,700 --> 10:15:26,340 can do the structuring right so instead 14315 10:15:24,000 --> 10:15:28,080 of grabbing the value I'll say yep I 14316 10:15:26,340 --> 10:15:30,840 know for sure that I'm getting back the 14317 10:15:28,080 --> 10:15:33,120 object so let's go here with user and 14318 10:15:30,840 --> 10:15:35,580 then the logout and of course I simply 14319 10:15:33,120 --> 10:15:38,640 want to return hello world and 14320 10:15:35,580 --> 10:15:41,820 everything is going to work like peaches 14321 10:15:38,640 --> 10:15:45,020 and again the main goal here is to 14322 10:15:41,820 --> 10:15:47,756 bypass the navx notice 14323 10:15:45,020 --> 10:15:49,560 networks is not getting any props it's 14324 10:15:47,756 --> 10:15:52,140 not passing any props 14325 10:15:49,560 --> 10:15:54,776 I'm getting everything directly here in 14326 10:15:52,140 --> 10:15:57,776 the user container and my functionality 14327 10:15:54,776 --> 10:16:00,480 still works and since we just covered 14328 10:15:57,776 --> 10:16:02,700 custom hooks why don't we put our 14329 10:16:00,480 --> 10:16:05,640 knowledge to a good use 14330 10:16:02,700 --> 10:16:08,096 so notice in the user container 14331 10:16:05,640 --> 10:16:10,980 we are getting used context and we're 14332 10:16:08,096 --> 10:16:13,436 getting that nav bar context and 14333 10:16:10,980 --> 10:16:16,020 essentially in any component all 14334 10:16:13,436 --> 10:16:17,880 throughout your application I mean in 14335 10:16:16,020 --> 10:16:20,756 this case of course it needs to be 14336 10:16:17,880 --> 10:16:22,560 nested inside of the nav bar but in the 14337 10:16:20,756 --> 10:16:24,776 following videos I'll show you 14338 10:16:22,560 --> 10:16:27,240 how we can set up a global context 14339 10:16:24,776 --> 10:16:29,580 hopefully that is clear pretty much 14340 10:16:27,240 --> 10:16:31,800 anywhere where I want to use that yes 14341 10:16:29,580 --> 10:16:34,256 I'll have to grab these two things I'll 14342 10:16:31,800 --> 10:16:36,540 have to grab the use context as well as 14343 10:16:34,256 --> 10:16:39,960 the navbar context 14344 10:16:36,540 --> 10:16:40,916 now this is a good use case for a custom 14345 10:16:39,960 --> 10:16:43,200 hook 14346 10:16:40,916 --> 10:16:45,360 so instead of these two Imports I can 14347 10:16:43,200 --> 10:16:46,436 set up one custom hook and then just 14348 10:16:45,360 --> 10:16:49,380 import that 14349 10:16:46,436 --> 10:16:51,900 so back in the nav bar 14350 10:16:49,380 --> 10:16:54,000 I first want to come up with the name in 14351 10:16:51,900 --> 10:16:57,540 my case I'm going to call this use app 14352 10:16:54,000 --> 10:17:01,140 context but sky is the limit so let me 14353 10:16:57,540 --> 10:17:03,776 add here custom hook and then 14354 10:17:01,140 --> 10:17:07,436 effectively we want to export it because 14355 10:17:03,776 --> 10:17:09,416 same deal we will use it so const and 14356 10:17:07,436 --> 10:17:13,436 then call it use 14357 10:17:09,416 --> 10:17:14,880 so notice we still using the used so 14358 10:17:13,436 --> 10:17:17,880 that doesn't change 14359 10:17:14,880 --> 10:17:21,000 otherwise the stuff is not going to work 14360 10:17:17,880 --> 10:17:23,400 use app context that's my function and 14361 10:17:21,000 --> 10:17:26,340 what do I want to do from this function 14362 10:17:23,400 --> 10:17:28,256 I simply want to return use context 14363 10:17:26,340 --> 10:17:30,720 which I'm importing over here 14364 10:17:28,256 --> 10:17:34,256 and the nav bar context 14365 10:17:30,720 --> 10:17:36,720 so effectively this line over here so 14366 10:17:34,256 --> 10:17:40,200 first let me navigate back I'll remove 14367 10:17:36,720 --> 10:17:42,480 both of them from this user container 14368 10:17:40,200 --> 10:17:45,240 just so you can see that I'm not messing 14369 10:17:42,480 --> 10:17:47,756 with you let me go to a nav bar 14370 10:17:45,240 --> 10:17:50,400 copy and paste we don't of course need a 14371 10:17:47,756 --> 10:17:54,480 nav bar context but we will need this 14372 10:17:50,400 --> 10:17:56,880 use context in the enable one and as far 14373 10:17:54,480 --> 10:17:59,220 as the logic we just want to invoke this 14374 10:17:56,880 --> 10:18:01,320 used context once and we want to provide 14375 10:17:59,220 --> 10:18:04,080 the nav bar context so I'm going to go 14376 10:18:01,320 --> 10:18:06,480 with use context and then I'll provide 14377 10:18:04,080 --> 10:18:08,700 nav bar context so now I want to save 14378 10:18:06,480 --> 10:18:12,180 and again keep in mind we're exporting 14379 10:18:08,700 --> 10:18:15,840 this so now in the user container I can 14380 10:18:12,180 --> 10:18:18,900 replace this used context with use and 14381 10:18:15,840 --> 10:18:21,416 then app context now please keep in mind 14382 10:18:18,900 --> 10:18:24,416 that there's also one in the final 14383 10:18:21,416 --> 10:18:27,180 so don't grab that one just set up this 14384 10:18:24,416 --> 10:18:28,500 one over here let's save it and then 14385 10:18:27,180 --> 10:18:31,620 notice how 14386 10:18:28,500 --> 10:18:32,820 our functionality still works and if 14387 10:18:31,620 --> 10:18:35,700 you're looking at it and you're like 14388 10:18:32,820 --> 10:18:39,120 well this just saved us one line of code 14389 10:18:35,700 --> 10:18:40,740 keep in mind when you use context 14390 10:18:39,120 --> 10:18:43,436 globally against something that we're 14391 10:18:40,740 --> 10:18:45,240 going to cover in the next video 14392 10:18:43,436 --> 10:18:48,480 you'll be accessing 14393 10:18:45,240 --> 10:18:52,256 that context quite a few times and then 14394 10:18:48,480 --> 10:18:55,800 it's much easier to just use one hook 14395 10:18:52,256 --> 10:18:58,140 instead of importing the use context as 14396 10:18:55,800 --> 10:19:00,540 well as that particular context each and 14397 10:18:58,140 --> 10:19:03,840 every time you want to access it in the 14398 10:19:00,540 --> 10:19:06,720 component okay now let's see how we can 14399 10:19:03,840 --> 10:19:09,660 set up a global context basically how we 14400 10:19:06,720 --> 10:19:12,540 can provide some data to all components 14401 10:19:09,660 --> 10:19:14,936 in our application now for this one I 14402 10:19:12,540 --> 10:19:16,680 will create a new application just 14403 10:19:14,936 --> 10:19:19,740 because I want to set up everything from 14404 10:19:16,680 --> 10:19:21,480 scratch and if you want to access my 14405 10:19:19,740 --> 10:19:24,000 final application basically the 14406 10:19:21,480 --> 10:19:27,060 application I'm about to create 14407 10:19:24,000 --> 10:19:31,380 I will add it to the main repo 14408 10:19:27,060 --> 10:19:33,000 in the Z assets folder so as I'm 14409 10:19:31,380 --> 10:19:35,640 recording this video of course that 14410 10:19:33,000 --> 10:19:37,740 folder does not exist but by the time 14411 10:19:35,640 --> 10:19:40,320 you're watching the video just go to the 14412 10:19:37,740 --> 10:19:42,840 main repo effective way where you got 14413 10:19:40,320 --> 10:19:45,180 the advanced react and then the last 14414 10:19:42,840 --> 10:19:47,756 folder should be there and the name is 14415 10:19:45,180 --> 10:19:50,276 going to be Z assets and in there you'll 14416 10:19:47,756 --> 10:19:52,980 find this application now if you're 14417 10:19:50,276 --> 10:19:56,040 interested you can also try to set it up 14418 10:19:52,980 --> 10:19:58,200 yourself the global context and here are 14419 10:19:56,040 --> 10:20:00,900 the steps I'm going to create a new 14420 10:19:58,200 --> 10:20:03,720 application with feet and we're going to 14421 10:20:00,900 --> 10:20:06,300 go with npm create and blah blah blah 14422 10:20:03,720 --> 10:20:09,180 blah blah then we'll install 14423 10:20:06,300 --> 10:20:12,360 dependencies and run npm run Dev 14424 10:20:09,180 --> 10:20:16,916 to spin up the dev server in the source 14425 10:20:12,360 --> 10:20:19,680 create a context jsx file and set up two 14426 10:20:16,916 --> 10:20:20,880 things a global context with create 14427 10:20:19,680 --> 10:20:23,700 context 14428 10:20:20,880 --> 10:20:28,436 and a component which I'm going to name 14429 10:20:23,700 --> 10:20:31,640 app context with one state value now 14430 10:20:28,436 --> 10:20:35,640 from the app context return a 14431 10:20:31,640 --> 10:20:38,580 globalcontext DOT provider the component 14432 10:20:35,640 --> 10:20:42,540 and then wrap the entire application 14433 10:20:38,580 --> 10:20:44,700 which we're gonna do in the main jsx and 14434 10:20:42,540 --> 10:20:47,700 there's going to be a gotcha effectively 14435 10:20:44,700 --> 10:20:50,220 we'll have to jog our memory on the 14436 10:20:47,700 --> 10:20:53,096 children prop so if you're struggling to 14437 10:20:50,220 --> 10:20:55,500 set this up this is totally fine now 14438 10:20:53,096 --> 10:20:57,060 once we have wrapped our entire 14439 10:20:55,500 --> 10:21:00,060 application 14440 10:20:57,060 --> 10:21:01,680 then we want to set up a custom hook so 14441 10:21:00,060 --> 10:21:02,640 we don't have to import two of those 14442 10:21:01,680 --> 10:21:05,520 things 14443 10:21:02,640 --> 10:21:09,000 when we want to access the context and 14444 10:21:05,520 --> 10:21:11,040 then just try it in app GSX there's 14445 10:21:09,000 --> 10:21:14,460 going to be app.jsx and in there just 14446 10:21:11,040 --> 10:21:16,860 try to lock whatever you have in the 14447 10:21:14,460 --> 10:21:18,900 value prop in the global context 14448 10:21:16,860 --> 10:21:21,596 provider and if you can see some value 14449 10:21:18,900 --> 10:21:24,480 that means that it's going to work 14450 10:21:21,596 --> 10:21:27,840 everywhere in our application 14451 10:21:24,480 --> 10:21:30,480 so let's get cracking and 14452 10:21:27,840 --> 10:21:33,416 I think I'm just going to copy and paste 14453 10:21:30,480 --> 10:21:35,276 there's no need for you to watch me how 14454 10:21:33,416 --> 10:21:36,240 I tap this so I'm just going to go with 14455 10:21:35,276 --> 10:21:38,040 City 14456 10:21:36,240 --> 10:21:39,480 bus stop 14457 10:21:38,040 --> 10:21:42,620 and let me 14458 10:21:39,480 --> 10:21:47,160 massively zoom in copy and paste again 14459 10:21:42,620 --> 10:21:48,540 npm create beat latest and Global 14460 10:21:47,160 --> 10:21:49,620 context that's the name of the 14461 10:21:48,540 --> 10:21:53,096 application 14462 10:21:49,620 --> 10:21:55,620 Dash Dash or hyphen hyphen space dash 14463 10:21:53,096 --> 10:21:59,160 dash template and then we want to go 14464 10:21:55,620 --> 10:22:01,436 with react one so once that is done I'm 14465 10:21:59,160 --> 10:22:04,080 going to open up a new window 14466 10:22:01,436 --> 10:22:05,700 because I might want to showcase 14467 10:22:04,080 --> 10:22:09,060 something and read me 14468 10:22:05,700 --> 10:22:11,040 and this is again totally optional but 14469 10:22:09,060 --> 10:22:13,500 I'll send them side by side 14470 10:22:11,040 --> 10:22:16,620 so there's my browser 14471 10:22:13,500 --> 10:22:18,660 that's my text error let me open up the 14472 10:22:16,620 --> 10:22:22,380 integrated terminal we want to go with 14473 10:22:18,660 --> 10:22:23,820 npm install and we also want to go with 14474 10:22:22,380 --> 10:22:26,400 npm 14475 10:22:23,820 --> 10:22:28,680 start or I'm sorry it's not create react 14476 10:22:26,400 --> 10:22:31,680 app we're going to go with npm run Dev 14477 10:22:28,680 --> 10:22:34,256 and I know I already have set this quite 14478 10:22:31,680 --> 10:22:37,580 a few times but if both of those 14479 10:22:34,256 --> 10:22:40,800 commands don't work one after another 14480 10:22:37,580 --> 10:22:43,560 just run them separately result is going 14481 10:22:40,800 --> 10:22:46,680 to be exactly the same and once I open 14482 10:22:43,560 --> 10:22:49,320 up 5173 14483 10:22:46,680 --> 10:22:52,916 this is what we should see on the screen 14484 10:22:49,320 --> 10:22:55,020 and since we're now familiar with the 14485 10:22:52,916 --> 10:22:57,776 use state 14486 10:22:55,020 --> 10:23:00,776 this shouldn't look very foreign we have 14487 10:22:57,776 --> 10:23:03,120 count set count your state and what do 14488 10:23:00,776 --> 10:23:05,700 you know every time I click on a button 14489 10:23:03,120 --> 10:23:07,800 I'm increasing the account 14490 10:23:05,700 --> 10:23:10,620 so now let's set up that Global context 14491 10:23:07,800 --> 10:23:13,080 please do that in the source so don't do 14492 10:23:10,620 --> 10:23:15,416 it in a public or the node modules let's 14493 10:23:13,080 --> 10:23:17,520 create a new file here and I'm going to 14494 10:23:15,416 --> 10:23:19,256 call this context 14495 10:23:17,520 --> 10:23:23,276 a new file 14496 10:23:19,256 --> 10:23:25,620 let's go with context and jsx 14497 10:23:23,276 --> 10:23:26,880 and first I guess let's set up that 14498 10:23:25,620 --> 10:23:30,060 Global one 14499 10:23:26,880 --> 10:23:31,380 so first let's go with const I'm not 14500 10:23:30,060 --> 10:23:34,436 going to export that because there's 14501 10:23:31,380 --> 10:23:36,596 going to be a global hook basically a 14502 10:23:34,436 --> 10:23:37,560 custom hook that's gonna set up 14503 10:23:36,596 --> 10:23:39,960 everything 14504 10:23:37,560 --> 10:23:43,080 so I'm gonna go with global context and 14505 10:23:39,960 --> 10:23:44,880 that is equal to create context and we 14506 10:23:43,080 --> 10:23:46,800 just want to invoke that that's the 14507 10:23:44,880 --> 10:23:48,480 first step after that we want to set up 14508 10:23:46,800 --> 10:23:52,740 the component which we're going to 14509 10:23:48,480 --> 10:23:55,740 return from this file and I'm going to 14510 10:23:52,740 --> 10:23:57,840 call this app context that's going to be 14511 10:23:55,740 --> 10:24:00,416 my function here 14512 10:23:57,840 --> 10:24:02,936 as far as what we're returning well 14513 10:24:00,416 --> 10:24:06,360 we're going to return a global context 14514 10:24:02,936 --> 10:24:08,400 dot provider correct so let's go with 14515 10:24:06,360 --> 10:24:10,560 return keyword 14516 10:24:08,400 --> 10:24:12,900 and then let's set up that Global 14517 10:24:10,560 --> 10:24:15,120 context dot provider 14518 10:24:12,900 --> 10:24:16,380 and for now we don't have anything in 14519 10:24:15,120 --> 10:24:18,960 there so we're not going to set the 14520 10:24:16,380 --> 10:24:21,840 value and as far as 14521 10:24:18,960 --> 10:24:24,776 what are we gonna set up in between the 14522 10:24:21,840 --> 10:24:26,520 component tags well that's the gotcha 14523 10:24:24,776 --> 10:24:29,700 that we're going to discuss in a second 14524 10:24:26,520 --> 10:24:31,140 so for now don't return anything and of 14525 10:24:29,700 --> 10:24:33,180 course we do want to export that so 14526 10:24:31,140 --> 10:24:37,256 export default 14527 10:24:33,180 --> 10:24:38,640 and we're looking for app app context 14528 10:24:37,256 --> 10:24:41,096 okay good 14529 10:24:38,640 --> 10:24:41,936 before we set up the value why don't we 14530 10:24:41,096 --> 10:24:44,700 also 14531 10:24:41,936 --> 10:24:46,860 create the actual State value in my case 14532 10:24:44,700 --> 10:24:50,820 simply going to be name 14533 10:24:46,860 --> 10:24:54,060 and set name and as far as default value 14534 10:24:50,820 --> 10:24:55,500 I'm going to go with Peter okay good and 14535 10:24:54,060 --> 10:24:57,960 then remember 14536 10:24:55,500 --> 10:25:01,020 there is a special prop on this provider 14537 10:24:57,960 --> 10:25:03,900 and the name was value I do want to pass 14538 10:25:01,020 --> 10:25:05,040 in the object in here and inside of the 14539 10:25:03,900 --> 10:25:08,936 object there's going to be a name 14540 10:25:05,040 --> 10:25:11,756 property and also a set name property 14541 10:25:08,936 --> 10:25:15,000 save that now both of them are 14542 10:25:11,756 --> 10:25:17,340 effectively passed down but of course we 14543 10:25:15,000 --> 10:25:20,460 haven't wrapped our entire application 14544 10:25:17,340 --> 10:25:23,096 correct and as I said I don't know 14545 10:25:20,460 --> 10:25:27,300 whether I covered this before but 14546 10:25:23,096 --> 10:25:29,040 in es6 essentially we can do the 14547 10:25:27,300 --> 10:25:31,500 shortcut so if you're looking at your 14548 10:25:29,040 --> 10:25:34,256 like I don't understand how are we 14549 10:25:31,500 --> 10:25:37,200 passing here name equals the name 14550 10:25:34,256 --> 10:25:39,840 well if we have this kind of setup 14551 10:25:37,200 --> 10:25:43,500 where the property name is basically 14552 10:25:39,840 --> 10:25:47,400 equal to a variable and the same over 14553 10:25:43,500 --> 10:25:50,040 here so set name is equal to set name we 14554 10:25:47,400 --> 10:25:51,416 can shorten this up again if you have 14555 10:25:50,040 --> 10:25:53,756 this kind of setup 14556 10:25:51,416 --> 10:25:55,436 then essentially you can shorten this up 14557 10:25:53,756 --> 10:25:58,200 and just type one 14558 10:25:55,436 --> 10:26:00,416 in this case name and set name okay good 14559 10:25:58,200 --> 10:26:04,436 we have covered that so now let's 14560 10:26:00,416 --> 10:26:06,380 navigate to the main GSX because we want 14561 10:26:04,436 --> 10:26:09,240 to wrap our entire application 14562 10:26:06,380 --> 10:26:12,596 effectively our app component 14563 10:26:09,240 --> 10:26:13,860 and it's located over here so unlike the 14564 10:26:12,596 --> 10:26:17,160 nav bar 14565 10:26:13,860 --> 10:26:20,276 where effectively we imported a nav 14566 10:26:17,160 --> 10:26:23,820 links and then the user container inside 14567 10:26:20,276 --> 10:26:25,560 of the knowledge now we want to wrap our 14568 10:26:23,820 --> 10:26:28,020 entire application 14569 10:26:25,560 --> 10:26:30,960 so we're not going to import for example 14570 10:26:28,020 --> 10:26:33,840 app over here no we want to go to 14571 10:26:30,960 --> 10:26:36,120 main.jsx we want to import our app 14572 10:26:33,840 --> 10:26:39,360 context 14573 10:26:36,120 --> 10:26:40,860 so the import is in place and now this 14574 10:26:39,360 --> 10:26:43,380 is really up to you if you don't want 14575 10:26:40,860 --> 10:26:45,596 the strict mode if you don't want those 14576 10:26:43,380 --> 10:26:48,776 extra logs and all that you can simply 14577 10:26:45,596 --> 10:26:51,960 remove it or just place the app context 14578 10:26:48,776 --> 10:26:53,460 within the strict mode just please don't 14579 10:26:51,960 --> 10:26:55,936 wrap the 14580 10:26:53,460 --> 10:26:58,800 react trig mode in the app context 14581 10:26:55,936 --> 10:27:01,256 that's not going to make sense so we 14582 10:26:58,800 --> 10:27:03,596 want to go here we want to set up a App 14583 10:27:01,256 --> 10:27:05,820 context and yes I'm basically creating a 14584 10:27:03,596 --> 10:27:08,340 new component and I'll just remove this 14585 10:27:05,820 --> 10:27:10,560 one over here so now we're good to go 14586 10:27:08,340 --> 10:27:12,660 but there's one gotcha the moment we 14587 10:27:10,560 --> 10:27:13,620 save we're going to see nothing on the 14588 10:27:12,660 --> 10:27:15,416 screen 14589 10:27:13,620 --> 10:27:17,756 uh if you're wondering why is that 14590 10:27:15,416 --> 10:27:21,660 happening well remember long long long 14591 10:27:17,756 --> 10:27:23,820 long long long long time ago we talked 14592 10:27:21,660 --> 10:27:27,300 about the children prop I believe it was 14593 10:27:23,820 --> 10:27:29,220 all the way back in the fundamentals 14594 10:27:27,300 --> 10:27:30,840 and at the moment if we take a look at 14595 10:27:29,220 --> 10:27:32,756 the context notice we have nothing in 14596 10:27:30,840 --> 10:27:34,560 here so of course I can type here 14597 10:27:32,756 --> 10:27:36,416 context and everything is going to be 14598 10:27:34,560 --> 10:27:38,580 fine it's going to be displayed on the 14599 10:27:36,416 --> 10:27:42,776 screen but essentially that's not the 14600 10:27:38,580 --> 10:27:45,360 goal so I want to grab that app 14601 10:27:42,776 --> 10:27:46,740 component and then since all my 14602 10:27:45,360 --> 10:27:48,360 components are going to meet over here 14603 10:27:46,740 --> 10:27:52,320 in the app 14604 10:27:48,360 --> 10:27:54,776 I wanna wrap it in the provider and as a 14605 10:27:52,320 --> 10:27:57,416 result all my components 14606 10:27:54,776 --> 10:27:59,160 are gonna have access to in this case 14607 10:27:57,416 --> 10:28:01,320 name and set name 14608 10:27:59,160 --> 10:28:03,416 how we can do that well I kind of 14609 10:28:01,320 --> 10:28:06,960 already gave you a hint 14610 10:28:03,416 --> 10:28:08,580 since app right now is within the 14611 10:28:06,960 --> 10:28:11,220 component tags 14612 10:28:08,580 --> 10:28:12,720 now we want to go back to context and 14613 10:28:11,220 --> 10:28:15,540 essentially we want to access the 14614 10:28:12,720 --> 10:28:17,040 children we have few ways we can go here 14615 10:28:15,540 --> 10:28:19,916 with props 14616 10:28:17,040 --> 10:28:23,096 and then instead of the silly context 14617 10:28:19,916 --> 10:28:27,300 I can set up correlate and we can go 14618 10:28:23,096 --> 10:28:29,460 with props Dot children or 14619 10:28:27,300 --> 10:28:31,860 of course we can just shorten this up 14620 10:28:29,460 --> 10:28:33,480 and set up the structuring and as you 14621 10:28:31,860 --> 10:28:35,580 can see the moment I save everything is 14622 10:28:33,480 --> 10:28:36,360 correct so now everything is going to 14623 10:28:35,580 --> 10:28:38,820 work 14624 10:28:36,360 --> 10:28:40,320 again this is totally up to you if you 14625 10:28:38,820 --> 10:28:42,360 want to go with props 14626 10:28:40,320 --> 10:28:46,436 in my case I'm going to go children here 14627 10:28:42,360 --> 10:28:50,160 and then I can remove the props as well 14628 10:28:46,436 --> 10:28:52,560 before we go to app.jsx Let's also set 14629 10:28:50,160 --> 10:28:54,480 up that custom hook 14630 10:28:52,560 --> 10:28:58,560 in this case I'm going to export it 14631 10:28:54,480 --> 10:29:01,500 right away and I'll call it use global 14632 10:28:58,560 --> 10:29:03,180 context that's my custom hook as far as 14633 10:29:01,500 --> 10:29:07,140 the functionality 14634 10:29:03,180 --> 10:29:09,840 I want to invoke use context so please 14635 10:29:07,140 --> 10:29:12,360 be careful most likely vs code will try 14636 10:29:09,840 --> 10:29:13,740 to invoke the same one the use Global 14637 10:29:12,360 --> 10:29:15,596 context we don't want to do that 14638 10:29:13,740 --> 10:29:17,580 essentially we want to go with use 14639 10:29:15,596 --> 10:29:19,980 context which is coming 14640 10:29:17,580 --> 10:29:21,900 from react again this is a special hook 14641 10:29:19,980 --> 10:29:24,840 that is looking for one thing and one 14642 10:29:21,900 --> 10:29:27,660 thing only it's looking for the context 14643 10:29:24,840 --> 10:29:30,000 you want access to in this case it's 14644 10:29:27,660 --> 10:29:34,080 going to be a global context since we're 14645 10:29:30,000 --> 10:29:37,800 exporting this I can nicely hop over to 14646 10:29:34,080 --> 10:29:39,360 app jsx I know that I'm getting back the 14647 10:29:37,800 --> 10:29:41,640 object correct so I'm just going to go 14648 10:29:39,360 --> 10:29:43,620 here const and the structure it right 14649 10:29:41,640 --> 10:29:45,720 away I'm not going to look for set name 14650 10:29:43,620 --> 10:29:47,820 hopefully it's clear that I'll have 14651 10:29:45,720 --> 10:29:51,000 access to both of them and that is going 14652 10:29:47,820 --> 10:29:53,096 to be equal to my use Global context and 14653 10:29:51,000 --> 10:29:54,776 I just invoke it and just to Showcase 14654 10:29:53,096 --> 10:29:58,140 that everything works 14655 10:29:54,776 --> 10:29:59,040 I'm going to log name so if I go right 14656 10:29:58,140 --> 10:30:02,340 now 14657 10:29:59,040 --> 10:30:05,340 to my devtools I'll see Peter which 14658 10:30:02,340 --> 10:30:08,460 means that any component that is going 14659 10:30:05,340 --> 10:30:09,840 to be in the app where basically all our 14660 10:30:08,460 --> 10:30:12,300 components meet 14661 10:30:09,840 --> 10:30:15,120 it will have access and not only those 14662 10:30:12,300 --> 10:30:17,276 components but if I have any components 14663 10:30:15,120 --> 10:30:19,320 inside of those components 14664 10:30:17,276 --> 10:30:22,560 so hopefully that is clear this is how 14665 10:30:19,320 --> 10:30:24,960 we can set up a global context using 14666 10:30:22,560 --> 10:30:28,200 context API and also hopefully you see 14667 10:30:24,960 --> 10:30:30,300 how useful is our custom hook where now 14668 10:30:28,200 --> 10:30:32,756 we don't need to do those two Imports in 14669 10:30:30,300 --> 10:30:35,520 every file essentially we just grab the 14670 10:30:32,756 --> 10:30:39,720 use Global context and we're good to go 14671 10:30:35,520 --> 10:30:42,540 okay and next let's cover use reducer 14672 10:30:39,720 --> 10:30:46,320 hook and essentially you could think of 14673 10:30:42,540 --> 10:30:49,380 user doucher as a light version of Redux 14674 10:30:46,320 --> 10:30:52,380 one of the most famous State Management 14675 10:30:49,380 --> 10:30:55,256 libraries out there so here's the deal 14676 10:30:52,380 --> 10:30:57,060 as your application grows in size it's 14677 10:30:55,256 --> 10:30:59,720 going to be very hard to manage 14678 10:30:57,060 --> 10:31:01,620 everything with just use State 14679 10:30:59,720 --> 10:31:03,960 especially if you have multiple 14680 10:31:01,620 --> 10:31:06,120 developers working on the project 14681 10:31:03,960 --> 10:31:09,240 that's where State Management libraries 14682 10:31:06,120 --> 10:31:12,120 like Redux come into play since they 14683 10:31:09,240 --> 10:31:15,180 provide much needed structure and set of 14684 10:31:12,120 --> 10:31:19,500 rules which in a perfect world leads to 14685 10:31:15,180 --> 10:31:22,080 less bugs and easier code management 14686 10:31:19,500 --> 10:31:24,960 the problem with such libraries is that 14687 10:31:22,080 --> 10:31:25,980 they require quite a bit of boilerplate 14688 10:31:24,960 --> 10:31:28,680 code 14689 10:31:25,980 --> 10:31:32,276 and some time to get used to the 14690 10:31:28,680 --> 10:31:35,460 terminology so react released use 14691 10:31:32,276 --> 10:31:38,580 reducer hook and I like to think of use 14692 10:31:35,460 --> 10:31:42,776 reducer as a middle ground 14693 10:31:38,580 --> 10:31:45,840 between using full-blown State Library 14694 10:31:42,776 --> 10:31:48,360 and just using huge state 14695 10:31:45,840 --> 10:31:51,540 now a few more things before we begin 14696 10:31:48,360 --> 10:31:55,020 exploring user reducer first 14697 10:31:51,540 --> 10:31:58,320 don't get frustrated if some of this 14698 10:31:55,020 --> 10:32:00,660 stuff looks foreign and weird 14699 10:31:58,320 --> 10:32:03,840 It's Perfectly Normal 14700 10:32:00,660 --> 10:32:06,660 it simply takes time and few projects to 14701 10:32:03,840 --> 10:32:09,180 get used to use reducer or any State 14702 10:32:06,660 --> 10:32:11,700 Management library for that matter 14703 10:32:09,180 --> 10:32:14,360 second because I have to introduce quite 14704 10:32:11,700 --> 10:32:18,060 a few new weird sounding terms like 14705 10:32:14,360 --> 10:32:20,580 dispatch and reducer and the action I 14706 10:32:18,060 --> 10:32:21,776 purposely picked the example we already 14707 10:32:20,580 --> 10:32:23,880 worked on 14708 10:32:21,776 --> 10:32:25,200 and we'll just refactor it to use 14709 10:32:23,880 --> 10:32:27,060 reducer 14710 10:32:25,200 --> 10:32:30,480 hopefully that way it's going to be 14711 10:32:27,060 --> 10:32:33,360 easier to grasp all of the funky use 14712 10:32:30,480 --> 10:32:35,580 reducer terminology and lastly please 14713 10:32:33,360 --> 10:32:39,120 don't think that from now on you only 14714 10:32:35,580 --> 10:32:42,660 have to use use reducer or even more 14715 10:32:39,120 --> 10:32:47,160 drastic refactor all your existing apps 14716 10:32:42,660 --> 10:32:50,220 to use reducer it's a tool that has a 14717 10:32:47,160 --> 10:32:51,960 very specific purpose and as you work on 14718 10:32:50,220 --> 10:32:53,936 the bigger project you'll definitely 14719 10:32:51,960 --> 10:32:57,060 notice when there's need for it 14720 10:32:53,936 --> 10:32:59,936 basically when it's a good time to 14721 10:32:57,060 --> 10:33:02,580 implement use reducer or completely 14722 10:32:59,936 --> 10:33:07,256 switch your app to State Management 14723 10:33:02,580 --> 10:33:09,500 Library like Redux toolkit okay and as 14724 10:33:07,256 --> 10:33:13,200 far as the setup we're looking for 14725 10:33:09,500 --> 10:33:15,840 folder 10 use reducer we want to grab 14726 10:33:13,200 --> 10:33:18,120 the star and we're looking for use 14727 10:33:15,840 --> 10:33:20,460 reducer and essentially if you take a 14728 10:33:18,120 --> 10:33:24,840 look at the file and folder structure 14729 10:33:20,460 --> 10:33:27,540 you'll see that we have only one file so 14730 10:33:24,840 --> 10:33:30,660 I want you to grab that starter in the 14731 10:33:27,540 --> 10:33:33,180 app GSX for now I have 14732 10:33:30,660 --> 10:33:35,276 commented it out because I want to 14733 10:33:33,180 --> 10:33:37,320 showcase something so essentially grab 14734 10:33:35,276 --> 10:33:38,460 the starter and if you navigate to the 14735 10:33:37,320 --> 10:33:41,700 file you'll 14736 10:33:38,460 --> 10:33:43,436 notice that it's one of the projects we 14737 10:33:41,700 --> 10:33:47,040 worked on before 14738 10:33:43,436 --> 10:33:51,060 and effectively we cover that in New 14739 10:33:47,040 --> 10:33:53,640 State when we talked about the arrays so 14740 10:33:51,060 --> 10:33:55,916 this is the same exact example where 14741 10:33:53,640 --> 10:33:59,276 we're importing the datum 14742 10:33:55,916 --> 10:34:02,880 we set this using use state so that's 14743 10:33:59,276 --> 10:34:05,160 our default one and then we display all 14744 10:34:02,880 --> 10:34:09,540 the people and then we have an option to 14745 10:34:05,160 --> 10:34:11,400 remove specific person or clear playlist 14746 10:34:09,540 --> 10:34:13,500 like I said I purposely picked this 14747 10:34:11,400 --> 10:34:17,460 example because I think it's going to be 14748 10:34:13,500 --> 10:34:20,700 easier to grasp all of the use reducer 14749 10:34:17,460 --> 10:34:22,980 Concepts since you don't need to worry 14750 10:34:20,700 --> 10:34:27,060 about this functionality we already 14751 10:34:22,980 --> 10:34:29,096 covered it before and let's start with a 14752 10:34:27,060 --> 10:34:32,040 tiny challenge 14753 10:34:29,096 --> 10:34:34,140 I want you to add to this one a reset 14754 10:34:32,040 --> 10:34:35,220 functionality now don't worry we're not 14755 10:34:34,140 --> 10:34:38,040 going to do that right away with 14756 10:34:35,220 --> 10:34:39,416 userducer we'll simply do it by just 14757 10:34:38,040 --> 10:34:43,436 adding a function 14758 10:34:39,416 --> 10:34:47,096 and in that function essentially I want 14759 10:34:43,436 --> 10:34:49,320 to set back the state to an array and 14760 10:34:47,096 --> 10:34:51,060 once you have the function I want you to 14761 10:34:49,320 --> 10:34:53,640 set up a button 14762 10:34:51,060 --> 10:34:56,880 and once the user clicks the button then 14763 10:34:53,640 --> 10:34:59,580 we set basically the empty array to all 14764 10:34:56,880 --> 10:35:03,000 of the items and I want you to render 14765 10:34:59,580 --> 10:35:05,520 that button conditionally a hint you'll 14766 10:35:03,000 --> 10:35:07,560 most likely use the ternary operator so 14767 10:35:05,520 --> 10:35:09,180 notice two options either I remove them 14768 10:35:07,560 --> 10:35:11,756 one by one 14769 10:35:09,180 --> 10:35:14,460 and once I have the clear list I have 14770 10:35:11,756 --> 10:35:16,916 now reset button which essentially sets 14771 10:35:14,460 --> 10:35:19,860 it back to all of the items or 14772 10:35:16,916 --> 10:35:22,200 we can simply clear it and same deal the 14773 10:35:19,860 --> 10:35:25,080 moment I have empty list I want to have 14774 10:35:22,200 --> 10:35:29,700 a reset button which essentially sets it 14775 10:35:25,080 --> 10:35:31,860 back to our default people all right so 14776 10:35:29,700 --> 10:35:34,320 first let's work on that and then we'll 14777 10:35:31,860 --> 10:35:35,460 Implement user reducer essentially I 14778 10:35:34,320 --> 10:35:36,720 just want to add a little bit more 14779 10:35:35,460 --> 10:35:38,276 functionality 14780 10:35:36,720 --> 10:35:40,800 just so 14781 10:35:38,276 --> 10:35:45,300 it's more interesting when we cover use 14782 10:35:40,800 --> 10:35:47,756 reducer okay I'm done with my big speech 14783 10:35:45,300 --> 10:35:49,680 let me navigate here use reducer 14784 10:35:47,756 --> 10:35:52,080 hopefully I'm in the correct one yep 14785 10:35:49,680 --> 10:35:54,300 it's in the star and essentially let me 14786 10:35:52,080 --> 10:35:55,916 just set up the function I think it's 14787 10:35:54,300 --> 10:35:58,320 going to be faster if I just copy and 14788 10:35:55,916 --> 10:36:01,320 paste I'm looking for the function name 14789 10:35:58,320 --> 10:36:03,660 which usually is the most 14790 10:36:01,320 --> 10:36:06,840 hardest thing in programming 14791 10:36:03,660 --> 10:36:09,060 so let's go here with a reset list and 14792 10:36:06,840 --> 10:36:10,200 instead of empty array what do I need to 14793 10:36:09,060 --> 10:36:12,900 do well 14794 10:36:10,200 --> 10:36:15,660 I already have the data correct so I 14795 10:36:12,900 --> 10:36:18,660 just pass here this is an empty one this 14796 10:36:15,660 --> 10:36:21,720 just resets to 14797 10:36:18,660 --> 10:36:24,180 our original array and then we want to 14798 10:36:21,720 --> 10:36:27,900 keep on moving Keep On Moving I have 14799 10:36:24,180 --> 10:36:30,060 this button now I need to set up a 14800 10:36:27,900 --> 10:36:31,200 functionality where once the list is 14801 10:36:30,060 --> 10:36:34,020 empty 14802 10:36:31,200 --> 10:36:36,720 I'll display one button and if I have 14803 10:36:34,020 --> 10:36:40,020 some values then I'll have the clear one 14804 10:36:36,720 --> 10:36:42,360 how do we do that well we go here with 14805 10:36:40,020 --> 10:36:45,720 the curlies and what is going to be our 14806 10:36:42,360 --> 10:36:47,936 condition people and length correct let 14807 10:36:45,720 --> 10:36:50,580 me double check it that's my state value 14808 10:36:47,936 --> 10:36:51,720 so don't set this as data that's a 14809 10:36:50,580 --> 10:36:53,880 little bit different that's our original 14810 10:36:51,720 --> 10:36:57,120 array and here we're looking for people 14811 10:36:53,880 --> 10:36:59,096 and I'll say if it's less than one I 14812 10:36:57,120 --> 10:37:02,096 have two options what is my first one 14813 10:36:59,096 --> 10:37:04,980 well that's a reset one correct because 14814 10:37:02,096 --> 10:37:07,800 this is the one that 14815 10:37:04,980 --> 10:37:10,020 is displayed if it's less than one then 14816 10:37:07,800 --> 10:37:12,840 I want to reset and I'll work on that 14817 10:37:10,020 --> 10:37:15,480 one in a second and now let me check if 14818 10:37:12,840 --> 10:37:18,000 this is false basically if I have some 14819 10:37:15,480 --> 10:37:20,520 items I want to display this button so 14820 10:37:18,000 --> 10:37:23,640 let me cut this one out okay good that's 14821 10:37:20,520 --> 10:37:26,520 my first one and basically I'll use the 14822 10:37:23,640 --> 10:37:28,436 same structure so just copy and paste 14823 10:37:26,520 --> 10:37:32,460 over here I just need to change some 14824 10:37:28,436 --> 10:37:34,320 values around where the text inside of 14825 10:37:32,460 --> 10:37:36,596 the button is not going to be clear it's 14826 10:37:34,320 --> 10:37:38,936 going to be reset and instead of 14827 10:37:36,596 --> 10:37:43,620 clearing the list what do we need to do 14828 10:37:38,936 --> 10:37:46,256 yep correct we need to go with reset so 14829 10:37:43,620 --> 10:37:47,936 now check it out again two options I 14830 10:37:46,256 --> 10:37:50,756 think I'll pick this one just because 14831 10:37:47,936 --> 10:37:53,276 it's faster and notice now I can reset 14832 10:37:50,756 --> 10:37:55,256 now of course I'm showing this because I 14833 10:37:53,276 --> 10:37:56,580 already have the complete application in 14834 10:37:55,256 --> 10:37:59,480 my app.js 14835 10:37:56,580 --> 10:38:02,460 my apologies notice here I have the star 14836 10:37:59,480 --> 10:38:04,916 I will I guess remove the final there's 14837 10:38:02,460 --> 10:38:07,020 no need hopefully I didn't make some 14838 10:38:04,916 --> 10:38:09,900 egregious errors 14839 10:38:07,020 --> 10:38:11,520 you save notice that the result is 14840 10:38:09,900 --> 10:38:12,480 exactly the same so that's already a 14841 10:38:11,520 --> 10:38:15,720 good start 14842 10:38:12,480 --> 10:38:18,060 I can remove yep I can reset and I can 14843 10:38:15,720 --> 10:38:20,160 also clear so that was the first 14844 10:38:18,060 --> 10:38:23,340 Challenge and up next we'll get to know 14845 10:38:20,160 --> 10:38:26,880 use reducer all right so now let's 14846 10:38:23,340 --> 10:38:29,220 refactor our application to use reducer 14847 10:38:26,880 --> 10:38:30,720 since there are going to be multiple 14848 10:38:29,220 --> 10:38:32,340 steps of course we're not going to do 14849 10:38:30,720 --> 10:38:35,276 that in one video 14850 10:38:32,340 --> 10:38:37,680 but let's just do a basic setup and 14851 10:38:35,276 --> 10:38:41,220 arrive out of the get-go I can say that 14852 10:38:37,680 --> 10:38:42,900 most likely this video is going to be 14853 10:38:41,220 --> 10:38:45,256 the most difficult one 14854 10:38:42,900 --> 10:38:49,200 because unfortunately I'll have to throw 14855 10:38:45,256 --> 10:38:51,416 multiple terms at you right out of the 14856 10:38:49,200 --> 10:38:54,960 gate unfortunately there's no other way 14857 10:38:51,416 --> 10:38:56,820 in order just to create that setup 14858 10:38:54,960 --> 10:38:59,400 I need to provide you values and yes 14859 10:38:56,820 --> 10:39:01,080 they have funky names so my apologies if 14860 10:38:59,400 --> 10:39:03,480 something sounds weird my best 14861 10:39:01,080 --> 10:39:07,020 suggestion is just to re-watch the video 14862 10:39:03,480 --> 10:39:09,660 a few times and hopefully by doing so 14863 10:39:07,020 --> 10:39:12,120 you'll understand the stuff better now 14864 10:39:09,660 --> 10:39:14,460 eventually we'll remove this one but 14865 10:39:12,120 --> 10:39:17,096 since I don't want to have some massive 14866 10:39:14,460 --> 10:39:19,800 errors on the screen since I think it 14867 10:39:17,096 --> 10:39:21,720 might get distracting for now I'll just 14868 10:39:19,800 --> 10:39:24,540 leave this one over here again 14869 10:39:21,720 --> 10:39:26,640 eventually this is going to be gone what 14870 10:39:24,540 --> 10:39:28,560 I do want to do for now is just comment 14871 10:39:26,640 --> 10:39:31,680 out this functionality because we'll 14872 10:39:28,560 --> 10:39:34,200 move this one to a reducer a function 14873 10:39:31,680 --> 10:39:37,080 we're about to create so it's not going 14874 10:39:34,200 --> 10:39:39,360 to be sitting here anyway and I don't 14875 10:39:37,080 --> 10:39:41,880 want that functionality to run so let's 14876 10:39:39,360 --> 10:39:44,040 just comment this one out so as the user 14877 10:39:41,880 --> 10:39:47,640 clicks any of the buttons nothing should 14878 10:39:44,040 --> 10:39:50,160 happen that's exactly what I want so in 14879 10:39:47,640 --> 10:39:54,720 order to get started with use reducer 14880 10:39:50,160 --> 10:39:56,640 first we need to grab be hook correct so 14881 10:39:54,720 --> 10:39:58,200 we go over here now of course we can do 14882 10:39:56,640 --> 10:39:59,700 the Auto Import and all that but why 14883 10:39:58,200 --> 10:40:01,916 don't we change this around and just say 14884 10:39:59,700 --> 10:40:04,740 use reducer again all of this is coming 14885 10:40:01,916 --> 10:40:06,540 from react now 14886 10:40:04,740 --> 10:40:07,620 when it comes to you state what do we 14887 10:40:06,540 --> 10:40:10,860 pass in 14888 10:40:07,620 --> 10:40:13,380 we pass in the default State correct now 14889 10:40:10,860 --> 10:40:15,720 with use reducer 14890 10:40:13,380 --> 10:40:18,240 it's a little bit more complex we need 14891 10:40:15,720 --> 10:40:22,140 to pass in two things we need to pass in 14892 10:40:18,240 --> 10:40:23,240 a default State and we need to provide a 14893 10:40:22,140 --> 10:40:25,740 reducer 14894 10:40:23,240 --> 10:40:28,320 effectively a function 14895 10:40:25,740 --> 10:40:30,180 that is going to manipulate the state 14896 10:40:28,320 --> 10:40:33,180 like I said 14897 10:40:30,180 --> 10:40:34,916 right away many funky terms now keep in 14898 10:40:33,180 --> 10:40:37,140 mind that it's just a convention to call 14899 10:40:34,916 --> 10:40:40,500 this reducer you can call this banana 14900 10:40:37,140 --> 10:40:42,900 pudding it's totally fine but yes most 14901 10:40:40,500 --> 10:40:44,160 likely you'll always see the reducer so 14902 10:40:42,900 --> 10:40:46,800 let's start working on that I'm going to 14903 10:40:44,160 --> 10:40:48,596 go to use reducer I invoke the hook and 14904 10:40:46,800 --> 10:40:50,160 yes eventually we'll set up the values 14905 10:40:48,596 --> 10:40:52,080 that we're getting back for now let's 14906 10:40:50,160 --> 10:40:55,500 just worry what we need to pass in like 14907 10:40:52,080 --> 10:40:56,880 I said two things a reducer function and 14908 10:40:55,500 --> 10:40:59,160 a default state 14909 10:40:56,880 --> 10:41:00,120 so first let's come up with that 14910 10:40:59,160 --> 10:41:02,460 function 14911 10:41:00,120 --> 10:41:04,380 I'm going to go to constant you simply 14912 10:41:02,460 --> 10:41:06,840 want to create a empty Arrow function 14913 10:41:04,380 --> 10:41:08,400 that's all you need to do the hardest 14914 10:41:06,840 --> 10:41:10,320 part probably is going to be coming up 14915 10:41:08,400 --> 10:41:12,900 with the name that's why I'll stick with 14916 10:41:10,320 --> 10:41:16,080 convention and I'll just say producer 14917 10:41:12,900 --> 10:41:18,360 not a vegan food truck so reducer here 14918 10:41:16,080 --> 10:41:20,400 and for now let's just try it with empty 14919 10:41:18,360 --> 10:41:22,860 one I'm not sure maybe we'll right away 14920 10:41:20,400 --> 10:41:24,596 have to pass in the state but let me 14921 10:41:22,860 --> 10:41:26,640 just try with an empty one so this is an 14922 10:41:24,596 --> 10:41:29,756 empty function that's our reducer this 14923 10:41:26,640 --> 10:41:32,040 is where we'll control essentially our 14924 10:41:29,756 --> 10:41:33,840 entire state so we need to pass this one 14925 10:41:32,040 --> 10:41:36,560 first so then 14926 10:41:33,840 --> 10:41:39,416 we need to pass in the default State Now 14927 10:41:36,560 --> 10:41:41,180 default State can be anything however in 14928 10:41:39,416 --> 10:41:45,360 my case it's just going to be an object 14929 10:41:41,180 --> 10:41:48,360 with a people property which is going to 14930 10:41:45,360 --> 10:41:50,520 be equal to the data so pretty much 14931 10:41:48,360 --> 10:41:54,596 whatever we have over here 14932 10:41:50,520 --> 10:41:56,580 but yes normally you have multiple 14933 10:41:54,596 --> 10:41:59,096 things in there you can have is the 14934 10:41:56,580 --> 10:42:01,680 modal open have you fetched the data is 14935 10:41:59,096 --> 10:42:02,340 it loading and all that kind of stuff 14936 10:42:01,680 --> 10:42:04,020 um 14937 10:42:02,340 --> 10:42:06,000 while we were working on a project 14938 10:42:04,020 --> 10:42:09,360 you'll definitely see that for now let's 14939 10:42:06,000 --> 10:42:11,580 just start very simply and create that 14940 10:42:09,360 --> 10:42:14,400 default state so I'm going to go here 14941 10:42:11,580 --> 10:42:16,680 with const and you know what I think I'm 14942 10:42:14,400 --> 10:42:18,480 just going to move this one up because 14943 10:42:16,680 --> 10:42:19,436 pretty much once we set up that default 14944 10:42:18,480 --> 10:42:21,540 state 14945 10:42:19,436 --> 10:42:24,900 there's not going to be much work in 14946 10:42:21,540 --> 10:42:26,520 there but reducer yes we'll do quite a 14947 10:42:24,900 --> 10:42:29,276 few things in there so let me just move 14948 10:42:26,520 --> 10:42:31,080 this one up default state so that's the 14949 10:42:29,276 --> 10:42:32,276 second thing you'll always always need 14950 10:42:31,080 --> 10:42:34,800 to provide 14951 10:42:32,276 --> 10:42:37,380 and of course keep in mind one thing 14952 10:42:34,800 --> 10:42:39,480 just because initially you add people 14953 10:42:37,380 --> 10:42:41,936 doesn't mean that 14954 10:42:39,480 --> 10:42:44,400 you cannot add later for example is 14955 10:42:41,936 --> 10:42:46,436 loading so as your application grows 14956 10:42:44,400 --> 10:42:48,120 essentially you just keep adding these 14957 10:42:46,436 --> 10:42:50,880 items in a default state 14958 10:42:48,120 --> 10:42:53,640 that's how it works so don't worry that 14959 10:42:50,880 --> 10:42:55,256 pretty much prior to setting up your 14960 10:42:53,640 --> 10:42:57,480 whole application you need to right away 14961 10:42:55,256 --> 10:42:58,980 come up with the state no keep in mind 14962 10:42:57,480 --> 10:43:00,960 you can keep adding things that's 14963 10:42:58,980 --> 10:43:02,160 perfectly fine and all of them 14964 10:43:00,960 --> 10:43:04,800 eventually 14965 10:43:02,160 --> 10:43:06,776 will be over here as our default state 14966 10:43:04,800 --> 10:43:08,580 so let's pass it 14967 10:43:06,776 --> 10:43:10,560 I'm gonna go with default State and 14968 10:43:08,580 --> 10:43:14,400 we're pretty much done with this part 14969 10:43:10,560 --> 10:43:16,800 so now let's move on the other side of 14970 10:43:14,400 --> 10:43:19,436 the equation over here 14971 10:43:16,800 --> 10:43:23,040 remember would you state what did we get 14972 10:43:19,436 --> 10:43:26,756 back eight people and set people right 14973 10:43:23,040 --> 10:43:28,500 so a state value and a function it's 14974 10:43:26,756 --> 10:43:31,080 very similar 14975 10:43:28,500 --> 10:43:33,720 with use reducer however 14976 10:43:31,080 --> 10:43:36,000 we're getting back a state and we're 14977 10:43:33,720 --> 10:43:38,460 getting back a dispatch so the main idea 14978 10:43:36,000 --> 10:43:39,660 is the same but the implementation is a 14979 10:43:38,460 --> 10:43:42,120 little bit different so let's try this 14980 10:43:39,660 --> 10:43:45,240 one out I'm gonna go with const yes it's 14981 10:43:42,120 --> 10:43:46,380 still an array but this time it's not 14982 10:43:45,240 --> 10:43:49,320 just 14983 10:43:46,380 --> 10:43:51,960 one state value this is our entire state 14984 10:43:49,320 --> 10:43:53,820 so if I'll have 50 values in here yep 14985 10:43:51,960 --> 10:43:56,400 it's going to be represented here in the 14986 10:43:53,820 --> 10:43:57,720 state now again in my case I only have 14987 10:43:56,400 --> 10:44:00,480 people 14988 10:43:57,720 --> 10:44:02,756 but I want to stress one more time 14989 10:44:00,480 --> 10:44:04,256 you can have a bunch of values and all 14990 10:44:02,756 --> 10:44:05,276 of them will be represented here with 14991 10:44:04,256 --> 10:44:07,740 the state 14992 10:44:05,276 --> 10:44:09,596 now since this is an object how we can 14993 10:44:07,740 --> 10:44:12,120 access things in our application well 14994 10:44:09,596 --> 10:44:15,000 state DOT State DOT state DOT hopefully 14995 10:44:12,120 --> 10:44:17,220 that's clear now the second thing is the 14996 10:44:15,000 --> 10:44:20,160 dispatch so we're going to go here with 14997 10:44:17,220 --> 10:44:21,596 dispatch again this is a naming 14998 10:44:20,160 --> 10:44:25,916 convention 14999 10:44:21,596 --> 10:44:29,640 a bobo is also a nice alternative 15000 10:44:25,916 --> 10:44:31,680 and yes this function is updating the 15001 10:44:29,640 --> 10:44:33,060 state but here's the biggest gotcha 15002 10:44:31,680 --> 10:44:36,240 there's the biggest difference between 15003 10:44:33,060 --> 10:44:39,360 you state and the use reducer when we 15004 10:44:36,240 --> 10:44:40,500 talk about dispatch we'll have to do 15005 10:44:39,360 --> 10:44:43,800 this 15006 10:44:40,500 --> 10:44:46,200 somewhat funny uh syntax where let me 15007 10:44:43,800 --> 10:44:48,740 keep moving and show you that so when it 15008 10:44:46,200 --> 10:44:51,960 comes to dispatch we need to do this 15009 10:44:48,740 --> 10:44:54,960 we'll basically go dispatch will invoke 15010 10:44:51,960 --> 10:44:56,880 it like so and then we'll pass the 15011 10:44:54,960 --> 10:44:58,860 typing now all of that is coming up I'm 15012 10:44:56,880 --> 10:45:01,320 just showing you that essentially the 15013 10:44:58,860 --> 10:45:03,776 area is the same yes we are updating the 15014 10:45:01,320 --> 10:45:04,800 state however this is not happening 15015 10:45:03,776 --> 10:45:07,560 directly 15016 10:45:04,800 --> 10:45:09,776 so remember with set people or any of 15017 10:45:07,560 --> 10:45:12,960 the set functions in each state 15018 10:45:09,776 --> 10:45:15,360 we just invoke them and this immediately 15019 10:45:12,960 --> 10:45:17,640 changed the value of people that's not 15020 10:45:15,360 --> 10:45:19,500 how it works with user reducer so that's 15021 10:45:17,640 --> 10:45:21,660 where that structure comes into play you 15022 10:45:19,500 --> 10:45:24,360 cannot just willy-nilly come in here and 15023 10:45:21,660 --> 10:45:27,540 start updating the state no what you'll 15024 10:45:24,360 --> 10:45:30,596 do you'll dispatch you'll pass in the 15025 10:45:27,540 --> 10:45:32,756 action basically what you want to do and 15026 10:45:30,596 --> 10:45:34,916 then it's gonna go through the reducer 15027 10:45:32,756 --> 10:45:37,320 and then whatever we get back here from 15028 10:45:34,916 --> 10:45:40,256 the reducer it's going to be our new 15029 10:45:37,320 --> 10:45:44,220 state so yes multiple 15030 10:45:40,256 --> 10:45:46,020 steps and multiple new terms like I said 15031 10:45:44,220 --> 10:45:48,840 this is probably going to be the hardest 15032 10:45:46,020 --> 10:45:50,640 video because I just have to cover these 15033 10:45:48,840 --> 10:45:53,040 things unfortunately there's no way I 15034 10:45:50,640 --> 10:45:55,256 can show you a use reducer setup without 15035 10:45:53,040 --> 10:45:57,416 talking about them that's not going to 15036 10:45:55,256 --> 10:45:59,820 make sense so let me try this I might 15037 10:45:57,416 --> 10:46:02,460 need to pass in state I kind of want to 15038 10:45:59,820 --> 10:46:04,860 leave it for later videos but if I'll 15039 10:46:02,460 --> 10:46:07,380 get some bugs of course we'll have to at 15040 10:46:04,860 --> 10:46:09,660 least talk about it a little bit so this 15041 10:46:07,380 --> 10:46:11,096 is going to be our initial setup now we 15042 10:46:09,660 --> 10:46:13,800 can bravely 15043 10:46:11,096 --> 10:46:16,740 take this one out so we won't need it 15044 10:46:13,800 --> 10:46:17,756 anymore that's it we can remove it and 15045 10:46:16,740 --> 10:46:20,460 then remember 15046 10:46:17,756 --> 10:46:22,380 we have people now do we have a state 15047 10:46:20,460 --> 10:46:25,320 value now well 15048 10:46:22,380 --> 10:46:28,020 not really right so what do we have we 15049 10:46:25,320 --> 10:46:31,740 have state and in there I have the 15050 10:46:28,020 --> 10:46:34,256 people so let's keep on moving and you 15051 10:46:31,740 --> 10:46:35,460 know what I'll just log so you can see 15052 10:46:34,256 --> 10:46:37,980 the state 15053 10:46:35,460 --> 10:46:39,840 but since again I don't want to have 15054 10:46:37,980 --> 10:46:41,936 some unnecessary errors I can tell you 15055 10:46:39,840 --> 10:46:44,160 right away that in a state we have the 15056 10:46:41,936 --> 10:46:46,680 people and now instead of accessing the 15057 10:46:44,160 --> 10:46:48,240 people directly we're grabbing the state 15058 10:46:46,680 --> 10:46:51,300 so here's the deal 15059 10:46:48,240 --> 10:46:54,720 as far as access yes we still do the 15060 10:46:51,300 --> 10:46:57,000 Good Old State value and then since this 15061 10:46:54,720 --> 10:47:00,000 is an object we're grabbing some 15062 10:46:57,000 --> 10:47:01,436 particular property but the update is 15063 10:47:00,000 --> 10:47:02,936 not going to be like this you're not 15064 10:47:01,436 --> 10:47:05,400 going to go hey 15065 10:47:02,936 --> 10:47:07,436 um let's update the people already in 15066 10:47:05,400 --> 10:47:10,020 the state let's create new people and 15067 10:47:07,436 --> 10:47:12,000 set state DOT people equal to that no 15068 10:47:10,020 --> 10:47:13,916 that's not how it's going to work we'll 15069 10:47:12,000 --> 10:47:16,020 have to use the dispatch it's going to 15070 10:47:13,916 --> 10:47:18,960 go through the reducer and all that 15071 10:47:16,020 --> 10:47:21,960 shebang so let's keep on moving at him 15072 10:47:18,960 --> 10:47:24,180 here I also have people and I just want 15073 10:47:21,960 --> 10:47:26,880 to add State again let me see whether 15074 10:47:24,180 --> 10:47:31,080 we're getting back the error if we don't 15075 10:47:26,880 --> 10:47:32,880 return the state we might so let me save 15076 10:47:31,080 --> 10:47:35,220 and if everything is correct then I will 15077 10:47:32,880 --> 10:47:37,500 talk about these suckers in next video 15078 10:47:35,220 --> 10:47:39,900 the state and action that we need to 15079 10:47:37,500 --> 10:47:42,180 pass in and up everything 15080 10:47:39,900 --> 10:47:44,700 seems to be working and check it out 15081 10:47:42,180 --> 10:47:47,460 so this is my state object 15082 10:47:44,700 --> 10:47:49,916 and this is my array correct State 15083 10:47:47,460 --> 10:47:54,360 object I have people and now everywhere 15084 10:47:49,916 --> 10:47:56,820 in my application I basically grab these 15085 10:47:54,360 --> 10:47:59,340 values whatever they are again in our 15086 10:47:56,820 --> 10:48:01,380 case it's just a people but there can be 15087 10:47:59,340 --> 10:48:05,276 more things in there so that's the 15088 10:48:01,380 --> 10:48:06,960 general setup for use reducer and in the 15089 10:48:05,276 --> 10:48:08,880 following videos of course we'll start 15090 10:48:06,960 --> 10:48:10,560 talking about how we can update this 15091 10:48:08,880 --> 10:48:12,720 state because it's nice to have a 15092 10:48:10,560 --> 10:48:15,840 default state but obviously we want to 15093 10:48:12,720 --> 10:48:18,596 implement some functionality okay we 15094 10:48:15,840 --> 10:48:21,120 have the initial setup in place up next 15095 10:48:18,596 --> 10:48:24,540 let's talk about how we can Implement 15096 10:48:21,120 --> 10:48:27,060 some functionality and like I already 15097 10:48:24,540 --> 10:48:29,640 probably mentioned 10 000 times in a 15098 10:48:27,060 --> 10:48:33,540 previous video essentially when it comes 15099 10:48:29,640 --> 10:48:36,000 to use reducer or again since it's a 15100 10:48:33,540 --> 10:48:38,936 Redux light effectively the same 15101 10:48:36,000 --> 10:48:41,880 functionality Works in Redux you want to 15102 10:48:38,936 --> 10:48:43,560 dispatch something called action and 15103 10:48:41,880 --> 10:48:46,740 that action is going to be handled here 15104 10:48:43,560 --> 10:48:48,900 in the reducer and then whatever gets 15105 10:48:46,740 --> 10:48:51,720 returned from 15106 10:48:48,900 --> 10:48:53,880 the reducer is going to be the new state 15107 10:48:51,720 --> 10:48:55,916 so here's the deal by default this 15108 10:48:53,880 --> 10:48:58,620 function whatever we said over here 15109 10:48:55,916 --> 10:49:02,520 whether that is a vegan food truck or 15110 10:48:58,620 --> 10:49:06,120 banana pudding or Bobo whatever it gets 15111 10:49:02,520 --> 10:49:08,700 two things it gets state so that is the 15112 10:49:06,120 --> 10:49:11,276 state right before the update and the 15113 10:49:08,700 --> 10:49:14,276 action basically what are we trying to 15114 10:49:11,276 --> 10:49:17,276 do and since those are parameters we 15115 10:49:14,276 --> 10:49:18,900 simply go State again just a convention 15116 10:49:17,276 --> 10:49:21,416 and an action 15117 10:49:18,900 --> 10:49:22,800 okay hopefully we're clear on that and 15118 10:49:21,416 --> 10:49:25,620 now let's see how we can change 15119 10:49:22,800 --> 10:49:27,660 something in that state basically how we 15120 10:49:25,620 --> 10:49:30,120 can Implement some functionality 15121 10:49:27,660 --> 10:49:33,060 because what we need to understand it 15122 10:49:30,120 --> 10:49:35,580 was the same deal over here when we for 15123 10:49:33,060 --> 10:49:38,220 example remove the item we just change 15124 10:49:35,580 --> 10:49:40,560 the state value and essentially we want 15125 10:49:38,220 --> 10:49:42,840 to do it over here as well we just don't 15126 10:49:40,560 --> 10:49:46,080 want to do it directly you want to do it 15127 10:49:42,840 --> 10:49:47,936 through the reducer and you can pick any 15128 10:49:46,080 --> 10:49:50,700 of the functions I think I'll start with 15129 10:49:47,936 --> 10:49:53,820 clear list because in my opinion it's 15130 10:49:50,700 --> 10:49:55,380 going to be the easiest one to grasp and 15131 10:49:53,820 --> 10:49:56,880 essentially if we want to update 15132 10:49:55,380 --> 10:50:00,240 something if we want to change that 15133 10:49:56,880 --> 10:50:03,000 state value we go here with dispatch 15134 10:50:00,240 --> 10:50:06,660 and like I said we have this somewhat 15135 10:50:03,000 --> 10:50:08,460 funky syntax where we invoked this patch 15136 10:50:06,660 --> 10:50:10,256 but dispatch is looking for an object 15137 10:50:08,460 --> 10:50:13,020 and we must must must must must must 15138 10:50:10,256 --> 10:50:15,900 must must must must must provide type 15139 10:50:13,020 --> 10:50:17,700 over here type property yes you just 15140 10:50:15,900 --> 10:50:18,660 have to do that I didn't make that one 15141 10:50:17,700 --> 10:50:21,776 up 15142 10:50:18,660 --> 10:50:25,860 and as far as the type it's equal to a 15143 10:50:21,776 --> 10:50:28,020 string so I can say here do something 15144 10:50:25,860 --> 10:50:30,240 or I can type whatever string value I 15145 10:50:28,020 --> 10:50:33,000 want and I'll handle that in the reducer 15146 10:50:30,240 --> 10:50:34,800 now a convention 15147 10:50:33,000 --> 10:50:38,040 is to use 15148 10:50:34,800 --> 10:50:40,740 all uppercase and actually assign a 15149 10:50:38,040 --> 10:50:42,360 variable to this one I'll show you in a 15150 10:50:40,740 --> 10:50:44,520 separate video why we want to assign a 15151 10:50:42,360 --> 10:50:46,916 variable for now let's just stick with 15152 10:50:44,520 --> 10:50:48,416 convention and what are we doing over 15153 10:50:46,916 --> 10:50:51,060 here 15154 10:50:48,416 --> 10:50:54,120 we are clearing the list right so yes 15155 10:50:51,060 --> 10:50:56,040 essentially I would go with clear 15156 10:50:54,120 --> 10:50:58,800 and then list 15157 10:50:56,040 --> 10:51:01,980 this is just a convention you can write 15158 10:50:58,800 --> 10:51:04,140 it in camel case you can write it all in 15159 10:51:01,980 --> 10:51:06,360 lower case that's totally up to you just 15160 10:51:04,140 --> 10:51:09,416 remember that you'll have to handle that 15161 10:51:06,360 --> 10:51:11,880 here in the reducer so whichever option 15162 10:51:09,416 --> 10:51:13,740 you pick just stick with it otherwise 15163 10:51:11,880 --> 10:51:15,540 it's going to be very confusing and 15164 10:51:13,740 --> 10:51:17,340 don't be surprised if you are working on 15165 10:51:15,540 --> 10:51:19,740 a bigger project with other developers 15166 10:51:17,340 --> 10:51:21,540 and you'll see this syntax yes this is 15167 10:51:19,740 --> 10:51:24,840 pretty common so essentially what I'm 15168 10:51:21,540 --> 10:51:27,480 saying I want to clear a list so now 15169 10:51:24,840 --> 10:51:29,936 dispatch is like okay reducer now you 15170 10:51:27,480 --> 10:51:33,180 handle that I did my work 15171 10:51:29,936 --> 10:51:34,140 I dispatched an action so back in the 15172 10:51:33,180 --> 10:51:36,120 reducer 15173 10:51:34,140 --> 10:51:38,640 like I said we have two things 15174 10:51:36,120 --> 10:51:40,916 we have the state and we have the action 15175 10:51:38,640 --> 10:51:42,776 so state is going to be the state before 15176 10:51:40,916 --> 10:51:45,720 the update so once we're done with this 15177 10:51:42,776 --> 10:51:49,256 function there's going to be a new state 15178 10:51:45,720 --> 10:51:52,080 value please keep that in mind and the 15179 10:51:49,256 --> 10:51:54,720 way we handle that let's just First Lock 15180 10:51:52,080 --> 10:51:56,640 two things state or you know what let's 15181 10:51:54,720 --> 10:51:59,640 start with an action just to Showcase 15182 10:51:56,640 --> 10:52:03,120 what we have so notice here right so I 15183 10:51:59,640 --> 10:52:05,580 have my array now once I click there's a 15184 10:52:03,120 --> 10:52:08,220 big fat error and we'll talk about it 15185 10:52:05,580 --> 10:52:10,740 why we have nothing on the screen but 15186 10:52:08,220 --> 10:52:14,580 for now let's just focus on this one so 15187 10:52:10,740 --> 10:52:17,040 notice here I have the clear list in 15188 10:52:14,580 --> 10:52:20,936 line 9. so this is what I'm getting 15189 10:52:17,040 --> 10:52:22,916 that's my action now why we have this 15190 10:52:20,936 --> 10:52:25,256 whole thing not just type well because 15191 10:52:22,916 --> 10:52:27,240 we can provide more data in here and 15192 10:52:25,256 --> 10:52:29,160 you'll see that in a second once 15193 10:52:27,240 --> 10:52:31,916 essentially we need to remove a person 15194 10:52:29,160 --> 10:52:34,140 so we'll add more things over here but 15195 10:52:31,916 --> 10:52:36,060 if we just log this action this is going 15196 10:52:34,140 --> 10:52:39,240 to be the object that we're passing in 15197 10:52:36,060 --> 10:52:41,220 now what are we interested in well we 15198 10:52:39,240 --> 10:52:43,436 want to get the type correct because 15199 10:52:41,220 --> 10:52:45,180 we'll do something with that type that's 15200 10:52:43,436 --> 10:52:46,020 the one that screams hey I want to do 15201 10:52:45,180 --> 10:52:47,160 something 15202 10:52:46,020 --> 10:52:50,820 now 15203 10:52:47,160 --> 10:52:53,820 why do we have this massive error well 15204 10:52:50,820 --> 10:52:55,680 whatever we're going to return from the 15205 10:52:53,820 --> 10:52:58,080 reducer 15206 10:52:55,680 --> 10:53:00,416 is going to be that new state value so 15207 10:52:58,080 --> 10:53:04,020 notice this line 27 here 15208 10:53:00,416 --> 10:53:06,900 our state why is it undefined 15209 10:53:04,020 --> 10:53:09,840 well because reducer is basically a 15210 10:53:06,900 --> 10:53:11,460 function by default returns undefined so 15211 10:53:09,840 --> 10:53:13,560 this underscores the point whatever 15212 10:53:11,460 --> 10:53:16,140 you'll return here from this reducer 15213 10:53:13,560 --> 10:53:18,080 will be that state value so check it out 15214 10:53:16,140 --> 10:53:22,020 I can go here with return 15215 10:53:18,080 --> 10:53:24,540 and I can say shake 15216 10:53:22,020 --> 10:53:27,120 and bake 15217 10:53:24,540 --> 10:53:30,060 now I'll still most likely have the 15218 10:53:27,120 --> 10:53:31,620 error once I click the clear again once 15219 10:53:30,060 --> 10:53:33,596 I save technically everything is fine 15220 10:53:31,620 --> 10:53:35,880 but check it out I'll have the same 15221 10:53:33,596 --> 10:53:37,916 error and again we can scroll down and 15222 10:53:35,880 --> 10:53:41,096 we can see that nothing works why 15223 10:53:37,916 --> 10:53:43,256 because again line 28 15224 10:53:41,096 --> 10:53:46,080 shake and bake 15225 10:53:43,256 --> 10:53:47,400 is my people property somewhere in the 15226 10:53:46,080 --> 10:53:51,000 shake and bake 15227 10:53:47,400 --> 10:53:53,096 and of course the answer is no so 15228 10:53:51,000 --> 10:53:55,140 here's what we want to do first of all 15229 10:53:53,096 --> 10:53:56,820 we'll talk about the default returns in 15230 10:53:55,140 --> 10:53:58,916 a second that for now let me just get 15231 10:53:56,820 --> 10:54:01,436 rid of it and I'll also remove the 15232 10:53:58,916 --> 10:54:04,500 console log here in the reducer 15233 10:54:01,436 --> 10:54:06,120 essentially one by one will check for 15234 10:54:04,500 --> 10:54:07,740 these actions yes 15235 10:54:06,120 --> 10:54:10,436 if you're wondering do we have to do 15236 10:54:07,740 --> 10:54:13,860 this manually that's correct that's the 15237 10:54:10,436 --> 10:54:16,916 setup we go here with if now quite often 15238 10:54:13,860 --> 10:54:18,416 you'll see this switch operator used I 15239 10:54:16,916 --> 10:54:21,000 actually don't like that approach again 15240 10:54:18,416 --> 10:54:24,120 switch is an alternative to a bunch of 15241 10:54:21,000 --> 10:54:26,756 if statements I actually find if 15242 10:54:24,120 --> 10:54:29,040 statements more readable so we go here 15243 10:54:26,756 --> 10:54:31,860 with if and then I want to check for 15244 10:54:29,040 --> 10:54:34,680 action and remember the type is the 15245 10:54:31,860 --> 10:54:36,776 property correct so we go with type and 15246 10:54:34,680 --> 10:54:39,776 I want to check whether it is equal to 15247 10:54:36,776 --> 10:54:42,480 now what am I passing here clear list so 15248 10:54:39,776 --> 10:54:45,300 what do I need to set here same deal yep 15249 10:54:42,480 --> 10:54:47,936 that's correct so we'll take it here 15250 10:54:45,300 --> 10:54:50,520 copy and paste and that's why I'm saying 15251 10:54:47,936 --> 10:54:52,916 you can come up with whatever string 15252 10:54:50,520 --> 10:54:55,256 value you want over here just make sure 15253 10:54:52,916 --> 10:54:57,360 that you use the same one in the reducer 15254 10:54:55,256 --> 10:54:59,400 otherwise it's not going to work and 15255 10:54:57,360 --> 10:55:02,756 then we need to set up the functionality 15256 10:54:59,400 --> 10:55:04,680 so whatever we'll return from this is 15257 10:55:02,756 --> 10:55:06,840 gonna be my new state value 15258 10:55:04,680 --> 10:55:08,880 so how our functionality is going to 15259 10:55:06,840 --> 10:55:11,756 look like well we'll go with the return 15260 10:55:08,880 --> 10:55:15,180 because we want to return something and 15261 10:55:11,756 --> 10:55:18,360 essentially I want to spread out the 15262 10:55:15,180 --> 10:55:22,020 current state values why well because 15263 10:55:18,360 --> 10:55:24,540 even though I'm clearing the list keep 15264 10:55:22,020 --> 10:55:27,300 in mind that if I have is loading over 15265 10:55:24,540 --> 10:55:29,820 here and let's set it equal to false 15266 10:55:27,300 --> 10:55:32,756 what do you think is going to happen if 15267 10:55:29,820 --> 10:55:35,096 I'll just return empty array yes as far 15268 10:55:32,756 --> 10:55:37,560 as the people array my functionality is 15269 10:55:35,096 --> 10:55:40,020 going to work but keep in mind 15270 10:55:37,560 --> 10:55:42,900 if I'm not going to include this is 15271 10:55:40,020 --> 10:55:46,080 loading then essentially your reducer is 15272 10:55:42,900 --> 10:55:48,240 going to return a new state and that is 15273 10:55:46,080 --> 10:55:50,400 loading is going to be gone 15274 10:55:48,240 --> 10:55:52,980 so just like previously 15275 10:55:50,400 --> 10:55:54,120 effectively we want to spread out or 15276 10:55:52,980 --> 10:55:57,360 copy 15277 10:55:54,120 --> 10:55:59,756 the current state values so this is the 15278 10:55:57,360 --> 10:56:02,040 current state before this update and we 15279 10:55:59,756 --> 10:56:02,936 want to spread them out we want to copy 15280 10:56:02,040 --> 10:56:05,520 them 15281 10:56:02,936 --> 10:56:07,800 so now we're also passing this is 15282 10:56:05,520 --> 10:56:10,740 loading otherwise it's going to be gone 15283 10:56:07,800 --> 10:56:12,840 and then we want to overwrite then I 15284 10:56:10,740 --> 10:56:15,416 want to go with okay what is going to be 15285 10:56:12,840 --> 10:56:17,640 the new value of the people well we 15286 10:56:15,416 --> 10:56:19,860 wanna set it equal to an empty array 15287 10:56:17,640 --> 10:56:23,096 correct so we go here with people 15288 10:56:19,860 --> 10:56:25,020 and check it out it's not different 15289 10:56:23,096 --> 10:56:27,180 to what we have already in a clear list 15290 10:56:25,020 --> 10:56:29,276 we're just doing that in reducer and I 15291 10:56:27,180 --> 10:56:32,400 really want to stress that point that's 15292 10:56:29,276 --> 10:56:36,000 why I picked this particular example so 15293 10:56:32,400 --> 10:56:38,340 now let me save it let me clear out the 15294 10:56:36,000 --> 10:56:40,860 console app a bunch of errors in there 15295 10:56:38,340 --> 10:56:41,756 and now everything should work where 15296 10:56:40,860 --> 10:56:45,000 again 15297 10:56:41,756 --> 10:56:49,080 in clear list we dispatch a specific 15298 10:56:45,000 --> 10:56:50,220 action called clear list and then in the 15299 10:56:49,080 --> 10:56:53,756 reducer 15300 10:56:50,220 --> 10:56:57,060 we handle that I check for Action type 15301 10:56:53,756 --> 10:56:58,740 I grab the current state whatever it is 15302 10:56:57,060 --> 10:57:00,900 of course now we have the default one 15303 10:56:58,740 --> 10:57:02,520 but keep in mind that as we add more 15304 10:57:00,900 --> 10:57:04,140 functionality of course it's going to 15305 10:57:02,520 --> 10:57:07,020 update itself 15306 10:57:04,140 --> 10:57:08,700 so the state 15307 10:57:07,020 --> 10:57:11,700 is going to be the current state before 15308 10:57:08,700 --> 10:57:13,740 that particular action and then we 15309 10:57:11,700 --> 10:57:16,020 return a new value 15310 10:57:13,740 --> 10:57:19,160 this is our new state this is what we'll 15311 10:57:16,020 --> 10:57:21,960 see so now let me clear and check it out 15312 10:57:19,160 --> 10:57:26,520 notice everything works here 15313 10:57:21,960 --> 10:57:28,080 so there was no error and notice this is 15314 10:57:26,520 --> 10:57:30,360 my new state now 15315 10:57:28,080 --> 10:57:33,120 so initially I had four items and now I 15316 10:57:30,360 --> 10:57:36,360 have zero however notice how nicely we 15317 10:57:33,120 --> 10:57:40,380 still keep the is loading so we did not 15318 10:57:36,360 --> 10:57:43,436 lose this one over here so essentially 15319 10:57:40,380 --> 10:57:45,660 that's the general setup when we want to 15320 10:57:43,436 --> 10:57:47,040 update something in the state with 15321 10:57:45,660 --> 10:57:48,960 dispatch 15322 10:57:47,040 --> 10:57:51,540 we need to provide a type which is going 15323 10:57:48,960 --> 10:57:53,160 to be our action we need to tell what 15324 10:57:51,540 --> 10:57:55,980 we're going to do 15325 10:57:53,160 --> 10:57:58,140 and then in the reducer we handle that 15326 10:57:55,980 --> 10:58:00,776 type and whatever we return from the 15327 10:57:58,140 --> 10:58:03,720 reducer is going to be our new state 15328 10:58:00,776 --> 10:58:05,936 value okay and before we set up rest of 15329 10:58:03,720 --> 10:58:08,820 the functionality let's just talk about 15330 10:58:05,936 --> 10:58:11,820 the conventions and first let's start 15331 10:58:08,820 --> 10:58:15,660 with actions like I already previously 15332 10:58:11,820 --> 10:58:20,460 mentioned it can be any string you want 15333 10:58:15,660 --> 10:58:23,580 but in order to avoid some weird typos a 15334 10:58:20,460 --> 10:58:24,660 convention is to set this one up as a 15335 10:58:23,580 --> 10:58:27,480 variable 15336 10:58:24,660 --> 10:58:30,000 and then you're just avoiding some silly 15337 10:58:27,480 --> 10:58:33,060 bugs just to Showcase that if we go back 15338 10:58:30,000 --> 10:58:35,400 to our application notice in here we 15339 10:58:33,060 --> 10:58:39,916 have clear list but what if in the 15340 10:58:35,400 --> 10:58:42,840 reducer I mistyped and I said clear list 15341 10:58:39,916 --> 10:58:44,276 now do these ones match 15342 10:58:42,840 --> 10:58:46,860 and 15343 10:58:44,276 --> 10:58:49,500 the answer of course is no so now what 15344 10:58:46,860 --> 10:58:52,140 happens when I clear the list well I'll 15345 10:58:49,500 --> 10:58:55,080 have big fat error because 15346 10:58:52,140 --> 10:58:57,480 we are right now returning undefined 15347 10:58:55,080 --> 10:59:00,416 that's the default one since 15348 10:58:57,480 --> 10:59:04,020 we're not hitting this condition correct 15349 10:59:00,416 --> 10:59:05,520 and a way around that is effectively to 15350 10:59:04,020 --> 10:59:08,220 set up variables 15351 10:59:05,520 --> 10:59:10,800 that have the same name so if you have 15352 10:59:08,220 --> 10:59:14,220 clear list yes the variable name is also 15353 10:59:10,800 --> 10:59:16,020 going to be clear list so 15354 10:59:14,220 --> 10:59:19,500 why don't we right away set up those 15355 10:59:16,020 --> 10:59:21,900 variables we have only three pieces of 15356 10:59:19,500 --> 10:59:24,240 functionality so it's going to be pretty 15357 10:59:21,900 --> 10:59:26,936 quick I'm gonna go with clear list and 15358 10:59:24,240 --> 10:59:29,820 the other one is equal to my clear and 15359 10:59:26,936 --> 10:59:31,980 underscore list then I want to do the 15360 10:59:29,820 --> 10:59:35,340 same for reset something we're going to 15361 10:59:31,980 --> 10:59:38,820 work on in the next video so reset list 15362 10:59:35,340 --> 10:59:41,580 now that one is equal to reset and 15363 10:59:38,820 --> 10:59:44,820 underscore list and then lastly I have 15364 10:59:41,580 --> 10:59:48,000 remove item so again const and remove 15365 10:59:44,820 --> 10:59:51,120 item and at the very end of this section 15366 10:59:48,000 --> 10:59:54,596 will actually set it up in a separate 15367 10:59:51,120 --> 10:59:57,000 file and we'll import it because that is 15368 10:59:54,596 --> 10:59:59,040 a pretty typical setup since you're not 15369 10:59:57,000 --> 11:00:02,520 going to handle everything in just one 15370 10:59:59,040 --> 11:00:05,400 file so remove underscore and then item 15371 11:00:02,520 --> 11:00:08,096 and now where I'm checking in the 15372 11:00:05,400 --> 11:00:10,980 reducer as well as in the clear list 15373 11:00:08,096 --> 11:00:14,160 where I'm dispatching instead of passing 15374 11:00:10,980 --> 11:00:18,180 this string directly I go with clear 15375 11:00:14,160 --> 11:00:20,900 list that's my variable which holds the 15376 11:00:18,180 --> 11:00:25,080 string so this just 15377 11:00:20,900 --> 11:00:27,480 omits some weird bugs let's save this 15378 11:00:25,080 --> 11:00:30,180 let's first double check 15379 11:00:27,480 --> 11:00:33,020 we don't want any bugs here yep we can 15380 11:00:30,180 --> 11:00:36,540 nicely do so and now let's talk about 15381 11:00:33,020 --> 11:00:37,800 the state that we're returning from 15382 11:00:36,540 --> 11:00:41,400 reducer 15383 11:00:37,800 --> 11:00:43,560 you say what if I go to a reset list 15384 11:00:41,400 --> 11:00:44,276 something we're going to work on in a 15385 11:00:43,560 --> 11:00:47,756 second 15386 11:00:44,276 --> 11:00:50,460 and just pass some random stuff now this 15387 11:00:47,756 --> 11:00:53,340 could be because I just haven't created 15388 11:00:50,460 --> 11:00:56,580 yet the functionality in the reducer to 15389 11:00:53,340 --> 11:00:58,080 handle that dispatch or I just for some 15390 11:00:56,580 --> 11:01:00,300 weird reason access to your wrong 15391 11:00:58,080 --> 11:01:03,060 variable what if I do something along 15392 11:01:00,300 --> 11:01:06,776 the lines of type and then something 15393 11:01:03,060 --> 11:01:10,080 again we'll have big fat error correct 15394 11:01:06,776 --> 11:01:12,120 because we're not handling it here and 15395 11:01:10,080 --> 11:01:14,700 essentially we have two approaches just 15396 11:01:12,120 --> 11:01:18,300 to Showcase bam this is what we'll get 15397 11:01:14,700 --> 11:01:21,720 we only have clear list over here and 15398 11:01:18,300 --> 11:01:24,120 you can always return each state like so 15399 11:01:21,720 --> 11:01:26,756 so I have bunch of conditions I'll check 15400 11:01:24,120 --> 11:01:30,000 for reset list I'll check for remove 15401 11:01:26,756 --> 11:01:33,300 item and all that now if 15402 11:01:30,000 --> 11:01:36,240 the action type does not match any of my 15403 11:01:33,300 --> 11:01:39,060 conditions I'll simply return H state 15404 11:01:36,240 --> 11:01:41,220 so notice a clear that should definitely 15405 11:01:39,060 --> 11:01:43,916 work and once I reset technically 15406 11:01:41,220 --> 11:01:44,936 nothing happens I'm just returning a 15407 11:01:43,916 --> 11:01:46,980 state 15408 11:01:44,936 --> 11:01:50,160 which was right before the update and 15409 11:01:46,980 --> 11:01:52,980 since we did not perform any update I 15410 11:01:50,160 --> 11:01:56,700 just returned State and call it a day 15411 11:01:52,980 --> 11:01:59,460 now this is a valid approach however 15412 11:01:56,700 --> 11:02:00,540 how are we going to know if we have a 15413 11:01:59,460 --> 11:02:02,880 bug 15414 11:02:00,540 --> 11:02:05,936 so alternatively you can actually throw 15415 11:02:02,880 --> 11:02:08,040 error and this is very useful because it 15416 11:02:05,936 --> 11:02:10,080 right away tells you like something's 15417 11:02:08,040 --> 11:02:12,840 wrong with your application you need to 15418 11:02:10,080 --> 11:02:14,340 check it and the way we do that and 15419 11:02:12,840 --> 11:02:16,256 I think I'm just going to leave this one 15420 11:02:14,340 --> 11:02:19,620 for your reference just in case so 15421 11:02:16,256 --> 11:02:22,740 return State and the way we throw the 15422 11:02:19,620 --> 11:02:25,140 error we go through new error again this 15423 11:02:22,740 --> 11:02:27,480 is a JavaScript thing we're not making 15424 11:02:25,140 --> 11:02:29,580 this up just because we are in react 15425 11:02:27,480 --> 11:02:31,436 then I'll set up a template string and 15426 11:02:29,580 --> 11:02:36,776 you'll see in a second why so I'll say 15427 11:02:31,436 --> 11:02:41,220 no matching and essentially I'll look 15428 11:02:36,776 --> 11:02:44,520 for the action type action and then type 15429 11:02:41,220 --> 11:02:49,380 so access it and I'll just add here a 15430 11:02:44,520 --> 11:02:51,540 text action and type let's save this and 15431 11:02:49,380 --> 11:02:54,240 like I already said probably 15432 11:02:51,540 --> 11:02:57,360 twenty thousand times effectively this 15433 11:02:54,240 --> 11:03:01,140 just helps me if I see this I know that 15434 11:02:57,360 --> 11:03:03,360 listen somewhere I'm dispatching an 15435 11:03:01,140 --> 11:03:06,120 action that I'm not handling it that's 15436 11:03:03,360 --> 11:03:07,800 very useful notice reset bam and at 15437 11:03:06,120 --> 11:03:10,980 least I can go to console and I can be 15438 11:03:07,800 --> 11:03:13,320 like no matching something action type 15439 11:03:10,980 --> 11:03:16,860 so let me double check 15440 11:03:13,320 --> 11:03:19,080 the action that I'm dispatching so 15441 11:03:16,860 --> 11:03:22,380 essentially those are the conventions 15442 11:03:19,080 --> 11:03:25,936 when it comes to actions as well as how 15443 11:03:22,380 --> 11:03:29,040 we handle an action that we're not 15444 11:03:25,936 --> 11:03:30,596 catching in the reducer all right and 15445 11:03:29,040 --> 11:03:34,020 once we're familiar with the 15446 11:03:30,596 --> 11:03:35,400 fundamentals now let's work on a small 15447 11:03:34,020 --> 11:03:38,776 challenge where 15448 11:03:35,400 --> 11:03:42,180 you can try to reset the list yourself 15449 11:03:38,776 --> 11:03:45,120 so set up a dispatch and handle the 15450 11:03:42,180 --> 11:03:48,120 action in the reducer and once you're 15451 11:03:45,120 --> 11:03:50,096 ready to compare the results resume with 15452 11:03:48,120 --> 11:03:52,740 the video so 15453 11:03:50,096 --> 11:03:56,400 I'm going to navigate to use reducer 15454 11:03:52,740 --> 11:03:58,436 basically I'm looking for reset the list 15455 11:03:56,400 --> 11:04:01,200 instead of something 15456 11:03:58,436 --> 11:04:04,980 I'll use my variable and what is that 15457 11:04:01,200 --> 11:04:07,916 well that is reset list awesome 15458 11:04:04,980 --> 11:04:09,840 I'm pretty sure I can remove this one as 15459 11:04:07,916 --> 11:04:13,256 well as the previous one here 15460 11:04:09,840 --> 11:04:15,660 and then let's just scroll up and inside 15461 11:04:13,256 --> 11:04:18,480 of the reducer right after my clear list 15462 11:04:15,660 --> 11:04:21,240 I'll set up the reset one now please 15463 11:04:18,480 --> 11:04:24,720 keep in mind in this case order does not 15464 11:04:21,240 --> 11:04:28,740 matter if I place it before or after 15465 11:04:24,720 --> 11:04:30,840 clear list I simply want to check again 15466 11:04:28,740 --> 11:04:32,400 for Action I think in order to speed 15467 11:04:30,840 --> 11:04:34,860 this up I'll just copy and paste we're 15468 11:04:32,400 --> 11:04:37,800 not checking for Clear list we want to 15469 11:04:34,860 --> 11:04:39,900 go with reset so now notice how nice it 15470 11:04:37,800 --> 11:04:42,120 is that we have that variable which 15471 11:04:39,900 --> 11:04:44,040 right away grabs me in that string 15472 11:04:42,120 --> 11:04:47,460 and instead of 15473 11:04:44,040 --> 11:04:50,040 returning empty I'll just set it equal 15474 11:04:47,460 --> 11:04:52,620 to data that's all we have to do again 15475 11:04:50,040 --> 11:04:55,256 let's refresh so we don't have 15476 11:04:52,620 --> 11:04:56,276 the errors in the console and let's just 15477 11:04:55,256 --> 11:04:57,416 try it out 15478 11:04:56,276 --> 11:05:00,360 yep 15479 11:04:57,416 --> 11:05:04,080 it's empty and once we click on reset we 15480 11:05:00,360 --> 11:05:05,460 set it back to data array okay and we 15481 11:05:04,080 --> 11:05:07,860 also want to do that with a remove 15482 11:05:05,460 --> 11:05:10,500 person our last piece of functionality 15483 11:05:07,860 --> 11:05:11,700 and again if you want to set it up 15484 11:05:10,500 --> 11:05:14,460 yourself 15485 11:05:11,700 --> 11:05:19,200 set up a dispatch and then handle that 15486 11:05:14,460 --> 11:05:20,460 action in the reducer a tiny hint as far 15487 11:05:19,200 --> 11:05:23,160 as ID 15488 11:05:20,460 --> 11:05:26,096 you want to pass it as an extra property 15489 11:05:23,160 --> 11:05:29,640 in the object the one that you're 15490 11:05:26,096 --> 11:05:32,000 passing into a dispatch so let's get 15491 11:05:29,640 --> 11:05:35,160 cracking I'm going to navigate to use 15492 11:05:32,000 --> 11:05:38,276 reducer I'm looking for a remove item 15493 11:05:35,160 --> 11:05:40,740 and here we want to go with this patch 15494 11:05:38,276 --> 11:05:43,560 we want to invoke it we want to pass in 15495 11:05:40,740 --> 11:05:47,276 the type and in this case I'm looking 15496 11:05:43,560 --> 11:05:50,160 for remove item comma and you can 15497 11:05:47,276 --> 11:05:52,980 definitely pass here the ID like so you 15498 11:05:50,160 --> 11:05:55,436 can go with ID equals to any because 15499 11:05:52,980 --> 11:05:58,320 keep in mind that if you don't pass the 15500 11:05:55,436 --> 11:06:01,560 ID how are you gonna know which item you 15501 11:05:58,320 --> 11:06:02,880 want to remove I mean remove item okay 15502 11:06:01,560 --> 11:06:05,040 but which one 15503 11:06:02,880 --> 11:06:07,200 so you can set up the condition you can 15504 11:06:05,040 --> 11:06:09,000 even return a state but it's not going 15505 11:06:07,200 --> 11:06:12,416 to make sense if you don't have access 15506 11:06:09,000 --> 11:06:16,080 to that specific ID 15507 11:06:12,416 --> 11:06:18,480 and you can pass it like so ID equals to 15508 11:06:16,080 --> 11:06:20,756 NAD however the convention is to call 15509 11:06:18,480 --> 11:06:23,520 this property a payload 15510 11:06:20,756 --> 11:06:26,096 set it equal to an object even though 15511 11:06:23,520 --> 11:06:29,700 you're passing in one property yep set 15512 11:06:26,096 --> 11:06:32,160 it up as an object and then pass the ID 15513 11:06:29,700 --> 11:06:34,140 the name whatever so let me just 15514 11:06:32,160 --> 11:06:36,720 showcase the 15515 11:06:34,140 --> 11:06:39,240 could mention we go with payload that's 15516 11:06:36,720 --> 11:06:42,000 our second property it is an object yep 15517 11:06:39,240 --> 11:06:43,620 and then in here we go with ID and then 15518 11:06:42,000 --> 11:06:46,380 equals to an ID 15519 11:06:43,620 --> 11:06:48,960 so now let's navigate up 15520 11:06:46,380 --> 11:06:50,040 and in the reducer right after reset 15521 11:06:48,960 --> 11:06:52,436 list 15522 11:06:50,040 --> 11:06:55,980 I want to come up with another condition 15523 11:06:52,436 --> 11:06:59,096 I'll say if action type and in this case 15524 11:06:55,980 --> 11:07:01,800 I'm looking for remove item if that is 15525 11:06:59,096 --> 11:07:03,596 the case what do I want to do well first 15526 11:07:01,800 --> 11:07:07,140 let's just start by returning a state 15527 11:07:03,596 --> 11:07:09,360 and I want to showcase where we have 15528 11:07:07,140 --> 11:07:11,276 that ID right now so I'm going to go log 15529 11:07:09,360 --> 11:07:14,276 and then I'm just going to look for 15530 11:07:11,276 --> 11:07:17,040 Action let me save and then you can 15531 11:07:14,276 --> 11:07:19,800 click on any of them and check it out 15532 11:07:17,040 --> 11:07:22,200 you'll see here remove item and then 15533 11:07:19,800 --> 11:07:24,776 you'll see the payload so in order to 15534 11:07:22,200 --> 11:07:28,500 grab that ID what do we need to do well 15535 11:07:24,776 --> 11:07:31,256 first of all my spelling and notice this 15536 11:07:28,500 --> 11:07:34,560 is even with the extension so just 15537 11:07:31,256 --> 11:07:37,320 imagine how well air quotes I'm doing 15538 11:07:34,560 --> 11:07:39,960 without it so this is a payload my bad 15539 11:07:37,320 --> 11:07:43,200 and let me save it again basically we 15540 11:07:39,960 --> 11:07:45,840 want to go with payload and then Dot 15541 11:07:43,200 --> 11:07:48,416 and then the ID that's what we have to 15542 11:07:45,840 --> 11:07:50,000 do so type is remove item and then they 15543 11:07:48,416 --> 11:07:53,880 payload over here 15544 11:07:50,000 --> 11:07:56,340 so now let's navigate to that condition 15545 11:07:53,880 --> 11:07:59,640 and the setup is going to be pretty much 15546 11:07:56,340 --> 11:08:01,860 the same I'm gonna go with people filter 15547 11:07:59,640 --> 11:08:04,560 right so of course I'll have to access 15548 11:08:01,860 --> 11:08:08,520 it using state I'm going to get back to 15549 11:08:04,560 --> 11:08:12,060 new people and in here instead of ID 15550 11:08:08,520 --> 11:08:14,340 I'll say action payload and ID and of 15551 11:08:12,060 --> 11:08:17,640 course I can destructure it so let me 15552 11:08:14,340 --> 11:08:20,220 take this one up there's no need to set 15553 11:08:17,640 --> 11:08:21,660 it from the scratch again the difference 15554 11:08:20,220 --> 11:08:23,756 right now is that 15555 11:08:21,660 --> 11:08:26,756 people is in a state 15556 11:08:23,756 --> 11:08:29,520 so we go here with state DOT people and 15557 11:08:26,756 --> 11:08:33,660 the second thing this ID is located in 15558 11:08:29,520 --> 11:08:36,540 the action dot payload Dot and then the 15559 11:08:33,660 --> 11:08:39,540 Eddy and like I just said before of 15560 11:08:36,540 --> 11:08:42,840 course you can destructure it above you 15561 11:08:39,540 --> 11:08:45,840 can look for ID which is located in the 15562 11:08:42,840 --> 11:08:49,080 action payload and as far as what we're 15563 11:08:45,840 --> 11:08:51,480 returning well instead of State we're 15564 11:08:49,080 --> 11:08:54,180 going to go with dot dot state so we 15565 11:08:51,480 --> 11:08:57,060 return all of the properties and then 15566 11:08:54,180 --> 11:09:00,300 let's set people 15567 11:08:57,060 --> 11:09:02,096 equal to our new array which is going to 15568 11:09:00,300 --> 11:09:04,680 be new people 15569 11:09:02,096 --> 11:09:07,256 and that's it that's all we have to do 15570 11:09:04,680 --> 11:09:08,820 now again let's be clear we have a bunch 15571 11:09:07,256 --> 11:09:11,756 of things in here 15572 11:09:08,820 --> 11:09:14,220 doesn't look like I have any more logs 15573 11:09:11,756 --> 11:09:17,880 pretty sure I can remove this one and 15574 11:09:14,220 --> 11:09:20,220 our functionality should work check it 15575 11:09:17,880 --> 11:09:23,520 out I have the list I can clear it I can 15576 11:09:20,220 --> 11:09:26,340 reset and I can move them one by one we 15577 11:09:23,520 --> 11:09:30,416 get to an empty array and we set it back 15578 11:09:26,340 --> 11:09:33,596 to I believe four items okay and lastly 15579 11:09:30,416 --> 11:09:35,096 in the use reducer section I want to 15580 11:09:33,596 --> 11:09:38,936 show you 15581 11:09:35,096 --> 11:09:41,040 the approach we use as our application 15582 11:09:38,936 --> 11:09:44,240 grows basically 15583 11:09:41,040 --> 11:09:47,880 as we're getting more and more actions 15584 11:09:44,240 --> 11:09:50,040 and we handle them in the reducer 15585 11:09:47,880 --> 11:09:53,096 it's not going to be 15586 11:09:50,040 --> 11:09:55,560 easy to keep it in one file as you can 15587 11:09:53,096 --> 11:09:57,660 see it's getting hard to read and 15588 11:09:55,560 --> 11:09:59,936 therefore we just essentially split it 15589 11:09:57,660 --> 11:10:01,560 up of course in our case we have only 15590 11:09:59,936 --> 11:10:05,220 three actions 15591 11:10:01,560 --> 11:10:08,096 so this is only going to make sense if 15592 11:10:05,220 --> 11:10:09,776 you're getting more actions but still 15593 11:10:08,096 --> 11:10:12,416 the structure is going to be exactly the 15594 11:10:09,776 --> 11:10:15,660 same and if you want you can work on it 15595 11:10:12,416 --> 11:10:19,200 yourself essentially create a new file 15596 11:10:15,660 --> 11:10:20,580 I'm going to call this actions JS and in 15597 11:10:19,200 --> 11:10:23,220 this case since we're not going to be 15598 11:10:20,580 --> 11:10:26,720 creating a component we can get away 15599 11:10:23,220 --> 11:10:29,276 with just JS I think this is too much 15600 11:10:26,720 --> 11:10:32,276 and then you want to set up all the 15601 11:10:29,276 --> 11:10:34,380 actions from the use reducer in there 15602 11:10:32,276 --> 11:10:38,276 and you want to export them 15603 11:10:34,380 --> 11:10:41,880 and of course you want to import them in 15604 11:10:38,276 --> 11:10:43,500 the user Edition since you'll still need 15605 11:10:41,880 --> 11:10:46,860 to use them you just want to keep them 15606 11:10:43,500 --> 11:10:50,880 in a separate place and as soon as this 15607 11:10:46,860 --> 11:10:52,980 is not reducer this is actions like so 15608 11:10:50,880 --> 11:10:54,360 then you also want to do the same thing 15609 11:10:52,980 --> 11:10:56,840 with the reducer 15610 11:10:54,360 --> 11:10:59,936 copy and paste the logic in that file 15611 11:10:56,840 --> 11:11:01,436 and then keep in mind that you'll still 15612 11:10:59,936 --> 11:11:04,256 need an actions 15613 11:11:01,436 --> 11:11:08,096 so yes you'll have import for actions in 15614 11:11:04,256 --> 11:11:11,820 two places in use reducer as well as the 15615 11:11:08,096 --> 11:11:14,880 reducer JS and then lastly from the 15616 11:11:11,820 --> 11:11:18,840 reducer JS export reducer and then 15617 11:11:14,880 --> 11:11:21,540 import it in the use reducer 15618 11:11:18,840 --> 11:11:25,680 so let's get cracking I'm gonna go with 15619 11:11:21,540 --> 11:11:26,936 new file I'll call this actions Js 15620 11:11:25,680 --> 11:11:29,540 beautiful 15621 11:11:26,936 --> 11:11:33,120 and then the second one is going to be 15622 11:11:29,540 --> 11:11:35,700 reducer JS as far as the actions I think 15623 11:11:33,120 --> 11:11:38,220 it's going to be more straightforward 15624 11:11:35,700 --> 11:11:39,596 where in the reducer I just want to grab 15625 11:11:38,220 --> 11:11:42,840 them here 15626 11:11:39,596 --> 11:11:44,756 copy and paste and we simply want to add 15627 11:11:42,840 --> 11:11:49,020 those exports 15628 11:11:44,756 --> 11:11:52,140 since we'll use them in the use reducer 15629 11:11:49,020 --> 11:11:52,980 as well as the reducer so now if we go 15630 11:11:52,140 --> 11:11:55,800 back 15631 11:11:52,980 --> 11:11:58,800 to a use reducer 15632 11:11:55,800 --> 11:12:00,900 we want to go above the default state 15633 11:11:58,800 --> 11:12:04,256 remember we have a bunch of things right 15634 11:12:00,900 --> 11:12:06,776 now coming from the file and one by one 15635 11:12:04,256 --> 11:12:09,960 we can just access them so clear list 15636 11:12:06,776 --> 11:12:13,320 first one then reset 15637 11:12:09,960 --> 11:12:15,480 list and then lastly we have remove item 15638 11:12:13,320 --> 11:12:18,060 so all of them are coming from the 15639 11:12:15,480 --> 11:12:20,700 actions and if everything is correct 15640 11:12:18,060 --> 11:12:22,620 then the functionality is still going to 15641 11:12:20,700 --> 11:12:24,960 work so that's the first step we 15642 11:12:22,620 --> 11:12:28,080 separate the actions now let's do the 15643 11:12:24,960 --> 11:12:29,400 same thing with reducer and just so we 15644 11:12:28,080 --> 11:12:31,436 can speed this up I can tell you right 15645 11:12:29,400 --> 11:12:34,080 away that we'll need those actions in 15646 11:12:31,436 --> 11:12:36,000 the reducer so copy and paste yep that's 15647 11:12:34,080 --> 11:12:38,520 going to be the setup pretty much for 15648 11:12:36,000 --> 11:12:40,320 every action that you have you'll need 15649 11:12:38,520 --> 11:12:43,860 to import it most likely in two places 15650 11:12:40,320 --> 11:12:46,820 where you dispatch it and also in the 15651 11:12:43,860 --> 11:12:51,060 reducer that's quite typical setup 15652 11:12:46,820 --> 11:12:54,060 so let's go with our reducer right now 15653 11:12:51,060 --> 11:12:56,756 take this one out 15654 11:12:54,060 --> 11:12:59,640 copy and paste and then we want to 15655 11:12:56,756 --> 11:13:03,120 export so export default 15656 11:12:59,640 --> 11:13:06,596 and then reducer beautiful let's save 15657 11:13:03,120 --> 11:13:10,020 that and then back in the user educer we 15658 11:13:06,596 --> 11:13:12,000 also want to import the reducer and I'm 15659 11:13:10,020 --> 11:13:14,460 gonna try to do the Auto Import let's 15660 11:13:12,000 --> 11:13:17,460 see whether that works yep so I can 15661 11:13:14,460 --> 11:13:20,040 nicely grab my reducer and like I just 15662 11:13:17,460 --> 11:13:23,520 said if everything still works 15663 11:13:20,040 --> 11:13:26,096 which by the way it doesn't data is not 15664 11:13:23,520 --> 11:13:28,080 defined ah you see something really 15665 11:13:26,096 --> 11:13:30,660 interesting that I forgot to mention of 15666 11:13:28,080 --> 11:13:33,180 course we also want to grab the data in 15667 11:13:30,660 --> 11:13:37,256 the reducer right now since we're using 15668 11:13:33,180 --> 11:13:38,756 it over here so let me add that one to a 15669 11:13:37,256 --> 11:13:42,960 readme 15670 11:13:38,756 --> 11:13:45,596 where I have the reducer import actions 15671 11:13:42,960 --> 11:13:49,080 import data 15672 11:13:45,596 --> 11:13:52,200 so let's go back to I guess user user 15673 11:13:49,080 --> 11:13:53,820 first let's copy this one since it's in 15674 11:13:52,200 --> 11:13:55,560 the same folder we don't need to change 15675 11:13:53,820 --> 11:13:58,320 the path 15676 11:13:55,560 --> 11:14:00,300 simply you want to copy and paste and 15677 11:13:58,320 --> 11:14:01,436 now everything should work so let's try 15678 11:14:00,300 --> 11:14:04,800 this one out 15679 11:14:01,436 --> 11:14:06,960 I can clear I can reset and I can remove 15680 11:14:04,800 --> 11:14:09,596 them one by one 15681 11:14:06,960 --> 11:14:12,060 so those are the fundamentals of user 15682 11:14:09,596 --> 11:14:15,660 reducer and now you're ready to move on 15683 11:14:12,060 --> 11:14:18,416 to the next topic okay and up next let's 15684 11:14:15,660 --> 11:14:21,416 see how we can test and improve the 15685 11:14:18,416 --> 11:14:24,840 performance of our react applications 15686 11:14:21,416 --> 11:14:28,560 now before we begin few disclaimers 15687 11:14:24,840 --> 11:14:30,776 react as fast by default so all the 15688 11:14:28,560 --> 11:14:32,040 techniques we're going to discuss in the 15689 11:14:30,776 --> 11:14:35,160 following videos 15690 11:14:32,040 --> 11:14:38,220 make way more sense when you are working 15691 11:14:35,160 --> 11:14:41,096 on a bigger application not a small 15692 11:14:38,220 --> 11:14:43,436 to-do list type of application 15693 11:14:41,096 --> 11:14:45,776 also please keep in mind that I 15694 11:14:43,436 --> 11:14:49,080 purposely picked simple 15695 11:14:45,776 --> 11:14:52,560 and straightforward examples just so we 15696 11:14:49,080 --> 11:14:55,380 can spend all of our time and energy on 15697 11:14:52,560 --> 11:14:57,660 the performance topics okay and at the 15698 11:14:55,380 --> 11:15:01,200 very beginning of our react performance 15699 11:14:57,660 --> 11:15:05,160 Journey let's see what exact issue we're 15700 11:15:01,200 --> 11:15:08,820 trying to solve so first in the app jsx 15701 11:15:05,160 --> 11:15:13,460 navigate to this folder so tutorial 11 15702 11:15:08,820 --> 11:15:16,740 performance starter 0 1 and Lower State 15703 11:15:13,460 --> 11:15:19,380 and once you get there you'll see that 15704 11:15:16,740 --> 11:15:22,256 it's pretty typical application I have 15705 11:15:19,380 --> 11:15:25,380 here index GSX this is where 15706 11:15:22,256 --> 11:15:28,200 my components meet in this case it's 15707 11:15:25,380 --> 11:15:30,916 just a list and also there's a person 15708 11:15:28,200 --> 11:15:34,200 component now in the 15709 11:15:30,916 --> 11:15:35,096 index.jsx I have two State values one is 15710 11:15:34,200 --> 11:15:37,860 people 15711 11:15:35,096 --> 11:15:41,340 which is using data which is coming from 15712 11:15:37,860 --> 11:15:43,500 our data file as the default one 15713 11:15:41,340 --> 11:15:47,160 and I also have the count 15714 11:15:43,500 --> 11:15:50,936 and the initial value is zero so at top 15715 11:15:47,160 --> 11:15:53,756 I have the button and below it I have a 15716 11:15:50,936 --> 11:15:55,080 list component where I'm passing the 15717 11:15:53,756 --> 11:15:57,480 people prop 15718 11:15:55,080 --> 11:15:59,700 you can probably already guess that in a 15719 11:15:57,480 --> 11:16:03,300 list I'm just going to iterate over it 15720 11:15:59,700 --> 11:16:06,000 and return a person component and in a 15721 11:16:03,300 --> 11:16:09,960 person component I simply render a name 15722 11:16:06,000 --> 11:16:13,580 and in here I have a console log that's 15723 11:16:09,960 --> 11:16:16,860 it I mean it's not a magical application 15724 11:16:13,580 --> 11:16:19,500 so here's what I want to show you though 15725 11:16:16,860 --> 11:16:22,860 what's really interesting that every 15726 11:16:19,500 --> 11:16:25,340 time I will click on a button and I'll 15727 11:16:22,860 --> 11:16:30,480 increase the count 15728 11:16:25,340 --> 11:16:31,740 my person component will re-render so at 15729 11:16:30,480 --> 11:16:33,300 the moment you're looking at the console 15730 11:16:31,740 --> 11:16:35,580 you can see okay I have four renders 15731 11:16:33,300 --> 11:16:38,040 well that makes sense I have four people 15732 11:16:35,580 --> 11:16:38,880 in the array I can clearly see that on 15733 11:16:38,040 --> 11:16:42,240 the screen 15734 11:16:38,880 --> 11:16:45,840 however what we probably don't expect is 15735 11:16:42,240 --> 11:16:49,436 this notice every time I click 15736 11:16:45,840 --> 11:16:50,756 I'm getting more renders now is it the 15737 11:16:49,436 --> 11:16:53,700 end of the world in this application 15738 11:16:50,756 --> 11:16:56,220 absolutely not again these are just 15739 11:16:53,700 --> 11:16:58,860 example applications where we're trying 15740 11:16:56,220 --> 11:17:01,200 to take a look at the issue and what 15741 11:16:58,860 --> 11:17:04,140 Solutions we have however 15742 11:17:01,200 --> 11:17:05,640 in a bigger application if you have a 15743 11:17:04,140 --> 11:17:09,300 component tree of 15744 11:17:05,640 --> 11:17:11,756 whatever 60 components yeah that is an 15745 11:17:09,300 --> 11:17:13,916 issue if you're basically re-rendering 15746 11:17:11,756 --> 11:17:16,860 for nothing you're just changing one 15747 11:17:13,916 --> 11:17:18,000 value here in the Lower State component 15748 11:17:16,860 --> 11:17:19,980 and then 15749 11:17:18,000 --> 11:17:24,000 your entire tree 15750 11:17:19,980 --> 11:17:25,800 constantly just re-renders now before we 15751 11:17:24,000 --> 11:17:27,596 take a look at the tools and solutions 15752 11:17:25,800 --> 11:17:31,200 and all that let's discuss why is that 15753 11:17:27,596 --> 11:17:33,720 happening so if we navigate back to a 15754 11:17:31,200 --> 11:17:36,660 readme file you'll see that 15755 11:17:33,720 --> 11:17:37,500 when we covered re-renders I mentioned 15756 11:17:36,660 --> 11:17:40,860 this 15757 11:17:37,500 --> 11:17:44,276 component re-renders when the state 15758 11:17:40,860 --> 11:17:47,220 or props change correct 15759 11:17:44,276 --> 11:17:48,360 but if you take a look at the setup 15760 11:17:47,220 --> 11:17:51,120 right now you 15761 11:17:48,360 --> 11:17:53,880 are probably wondering okay but nothing 15762 11:17:51,120 --> 11:17:56,580 changes over here so the people stay the 15763 11:17:53,880 --> 11:17:59,340 same as well as the person stays the 15764 11:17:56,580 --> 11:18:00,060 same right whatever I'm passing in the 15765 11:17:59,340 --> 11:18:01,860 list 15766 11:18:00,060 --> 11:18:03,360 it's not changing it's pretty much the 15767 11:18:01,860 --> 11:18:04,916 same thing I'm just passing in that 15768 11:18:03,360 --> 11:18:08,096 person object 15769 11:18:04,916 --> 11:18:10,916 so there's another reason why components 15770 11:18:08,096 --> 11:18:14,160 re-render and that is when the parent 15771 11:18:10,916 --> 11:18:17,400 element re-renders so what's happening 15772 11:18:14,160 --> 11:18:20,220 here in the index.jsx every time 15773 11:18:17,400 --> 11:18:22,080 we update the count what do we do we 15774 11:18:20,220 --> 11:18:24,900 trigger re-render correct we cover that 15775 11:18:22,080 --> 11:18:25,916 when we talked about use State the set 15776 11:18:24,900 --> 11:18:27,776 function 15777 11:18:25,916 --> 11:18:30,480 and what happens 15778 11:18:27,776 --> 11:18:31,860 the list which is a child also 15779 11:18:30,480 --> 11:18:33,720 re-renders 15780 11:18:31,860 --> 11:18:36,540 once the list re-renders what's 15781 11:18:33,720 --> 11:18:37,980 happening with person component it also 15782 11:18:36,540 --> 11:18:40,140 re-renders 15783 11:18:37,980 --> 11:18:42,120 and before we 15784 11:18:40,140 --> 11:18:44,520 take a look at the tools 15785 11:18:42,120 --> 11:18:46,436 let me also quickly cover something that 15786 11:18:44,520 --> 11:18:50,096 has been popping up 15787 11:18:46,436 --> 11:18:53,040 in a course q a and essential is this 15788 11:18:50,096 --> 11:18:55,740 setting up a use effect is not going to 15789 11:18:53,040 --> 11:18:57,480 solve our issue so let me navigate here 15790 11:18:55,740 --> 11:18:59,220 notice this use effect 15791 11:18:57,480 --> 11:19:01,916 let me 15792 11:18:59,220 --> 11:19:04,740 save and refresh yep I have now two 15793 11:19:01,916 --> 11:19:07,256 console logs four for render and four 15794 11:19:04,740 --> 11:19:10,140 for this log inside of the user effect 15795 11:19:07,256 --> 11:19:13,200 and technically as I'm clicking I only 15796 11:19:10,140 --> 11:19:15,776 see the render however 15797 11:19:13,200 --> 11:19:18,180 this doesn't stop the component from 15798 11:19:15,776 --> 11:19:20,340 re-rendering essentially the only thing 15799 11:19:18,180 --> 11:19:22,500 we're doing we're invoking whatever 15800 11:19:20,340 --> 11:19:25,140 functionality we have here in the user 15801 11:19:22,500 --> 11:19:26,460 effect when the initial render takes 15802 11:19:25,140 --> 11:19:27,480 place 15803 11:19:26,460 --> 11:19:30,596 but 15804 11:19:27,480 --> 11:19:32,160 the component itself still keeps 15805 11:19:30,596 --> 11:19:34,560 re-rendering 15806 11:19:32,160 --> 11:19:36,360 so yes it's a solution if you have some 15807 11:19:34,560 --> 11:19:38,756 kind of function that you don't wanna 15808 11:19:36,360 --> 11:19:41,160 trigger each and every time the 15809 11:19:38,756 --> 11:19:43,080 component re-renders however it's not 15810 11:19:41,160 --> 11:19:46,620 going to solve the issue of person 15811 11:19:43,080 --> 11:19:49,560 component re-rendering just because the 15812 11:19:46,620 --> 11:19:52,200 parent component re-rendered and if you 15813 11:19:49,560 --> 11:19:54,300 still don't believe me in the next video 15814 11:19:52,200 --> 11:19:57,060 we'll cover tools and then you'll 15815 11:19:54,300 --> 11:19:59,220 definitely see what I'm talking about 15816 11:19:57,060 --> 11:20:01,500 and before we take a look at our first 15817 11:19:59,220 --> 11:20:04,980 solution let's see how we can utilize 15818 11:20:01,500 --> 11:20:07,980 react Dev tools to measure the 15819 11:20:04,980 --> 11:20:10,380 performance of our application needless 15820 11:20:07,980 --> 11:20:12,720 to say that my assumption is that you 15821 11:20:10,380 --> 11:20:15,540 already have installed the react Dev 15822 11:20:12,720 --> 11:20:18,060 tools so if you haven't yet please 15823 11:20:15,540 --> 11:20:21,360 reference the react fundamentals part 15824 11:20:18,060 --> 11:20:24,240 where I covered the install 15825 11:20:21,360 --> 11:20:27,060 step by step and once we install the 15826 11:20:24,240 --> 11:20:30,300 tools we'll get these nice tabs 15827 11:20:27,060 --> 11:20:32,820 in the devtools the components and the 15828 11:20:30,300 --> 11:20:34,740 profiler and throughout the course we 15829 11:20:32,820 --> 11:20:36,416 have been looking at the components 15830 11:20:34,740 --> 11:20:39,240 however in this section we'll spend a 15831 11:20:36,416 --> 11:20:41,640 lot of time in the profile and before we 15832 11:20:39,240 --> 11:20:44,276 navigate to the profiler let's also 15833 11:20:41,640 --> 11:20:46,256 discuss something very important if I 15834 11:20:44,276 --> 11:20:49,500 navigate through my extensions 15835 11:20:46,256 --> 11:20:52,140 while I'm developing I can see that the 15836 11:20:49,500 --> 11:20:55,020 react developer tools have that red icon 15837 11:20:52,140 --> 11:20:57,000 and once I click on it notice it right 15838 11:20:55,020 --> 11:21:00,660 away tells me that the page is using 15839 11:20:57,000 --> 11:21:03,060 development build of react and something 15840 11:21:00,660 --> 11:21:05,936 important to keep in mind 15841 11:21:03,060 --> 11:21:07,140 by default while we're developing our 15842 11:21:05,936 --> 11:21:09,960 application is going to be slower 15843 11:21:07,140 --> 11:21:12,840 because react is doing 15844 11:21:09,960 --> 11:21:15,960 extra checks so don't be surprised if 15845 11:21:12,840 --> 11:21:18,180 your development application is slower 15846 11:21:15,960 --> 11:21:20,400 again that is happening for a reason and 15847 11:21:18,180 --> 11:21:22,140 that is happening by default so for 15848 11:21:20,400 --> 11:21:24,540 example if I navigate to one of the 15849 11:21:22,140 --> 11:21:27,720 projects we worked on during the course 15850 11:21:24,540 --> 11:21:30,240 and notice this is a hosted URL and if 15851 11:21:27,720 --> 11:21:32,400 I'll check I'll see that this one is 15852 11:21:30,240 --> 11:21:35,520 actually blue and this is also something 15853 11:21:32,400 --> 11:21:37,340 very important to keep in mind you don't 15854 11:21:35,520 --> 11:21:40,380 want to ship your development 15855 11:21:37,340 --> 11:21:42,180 application to production 15856 11:21:40,380 --> 11:21:43,916 so you don't want to host your 15857 11:21:42,180 --> 11:21:46,320 development application 15858 11:21:43,916 --> 11:21:47,220 well I will because it's not meant for 15859 11:21:46,320 --> 11:21:50,160 production 15860 11:21:47,220 --> 11:21:53,460 so whenever you want to push this up to 15861 11:21:50,160 --> 11:21:56,700 production make sure that is actual 15862 11:21:53,460 --> 11:21:59,096 production ready application hopefully I 15863 11:21:56,700 --> 11:22:00,360 make myself clear and once we navigate 15864 11:21:59,096 --> 11:22:04,080 back 15865 11:22:00,360 --> 11:22:06,776 now let's go to a profiler and before we 15866 11:22:04,080 --> 11:22:09,300 even look at anything let me just show 15867 11:22:06,776 --> 11:22:11,936 you my setup so we're all on the same 15868 11:22:09,300 --> 11:22:13,140 page so once I click over here notice as 15869 11:22:11,936 --> 11:22:15,776 far as the general 15870 11:22:13,140 --> 11:22:17,756 I'm using the Dark theme but of course 15871 11:22:15,776 --> 11:22:18,776 you can switch to light or Auto or 15872 11:22:17,756 --> 11:22:21,240 whatever 15873 11:22:18,776 --> 11:22:22,620 then as far as density I went with 15874 11:22:21,240 --> 11:22:25,200 comfortable I just like that they're 15875 11:22:22,620 --> 11:22:27,720 spanning all across and this one is very 15876 11:22:25,200 --> 11:22:30,180 Nifty this checkbox and you'll see in a 15877 11:22:27,720 --> 11:22:32,756 second why basically every time I'll do 15878 11:22:30,180 --> 11:22:36,900 something it's just gonna highlight the 15879 11:22:32,756 --> 11:22:39,540 components that are re-rendering then we 15880 11:22:36,900 --> 11:22:42,540 have debugging in here I have first two 15881 11:22:39,540 --> 11:22:44,756 check boxes and then when it comes to 15882 11:22:42,540 --> 11:22:48,416 components I'm just expanding the tree 15883 11:22:44,756 --> 11:22:50,460 by default and I don't think I changed 15884 11:22:48,416 --> 11:22:53,220 anything in here I have this setup and 15885 11:22:50,460 --> 11:22:55,860 then for the profiler I just have record 15886 11:22:53,220 --> 11:22:58,740 y each component rendered while 15887 11:22:55,860 --> 11:23:00,360 profiling and essentially if we want to 15888 11:22:58,740 --> 11:23:03,480 take a look what's happening in our 15889 11:23:00,360 --> 11:23:06,180 application we just go to this button 15890 11:23:03,480 --> 11:23:08,276 notice the start profiling it's going to 15891 11:23:06,180 --> 11:23:10,740 turn red and then start doing something 15892 11:23:08,276 --> 11:23:11,700 in the application and like I mentioned 15893 11:23:10,740 --> 11:23:14,400 before 15894 11:23:11,700 --> 11:23:15,840 I like that option where it highlights 15895 11:23:14,400 --> 11:23:18,960 pretty much 15896 11:23:15,840 --> 11:23:20,160 all the components that are re-rendering 15897 11:23:18,960 --> 11:23:22,680 and 15898 11:23:20,160 --> 11:23:24,840 I'm going to use this ranked I just 15899 11:23:22,680 --> 11:23:27,060 prefer this setup better but of course 15900 11:23:24,840 --> 11:23:30,540 you can also use this one 15901 11:23:27,060 --> 11:23:33,480 and effectively you can see that okay my 15902 11:23:30,540 --> 11:23:36,840 application loads right I expect all of 15903 11:23:33,480 --> 11:23:40,500 them to render then on every button 15904 11:23:36,840 --> 11:23:43,680 click notice all of my components 15905 11:23:40,500 --> 11:23:47,460 re-rendered and it even nicely tells me 15906 11:23:43,680 --> 11:23:49,256 why the parent component rendered 15907 11:23:47,460 --> 11:23:51,720 so what happens 15908 11:23:49,256 --> 11:23:54,776 all the items in my list are 15909 11:23:51,720 --> 11:23:57,300 re-rendering pretty much I clicked 13 15910 11:23:54,776 --> 11:23:59,276 times and same scenario now of course 15911 11:23:57,300 --> 11:24:01,320 the times are going to be different and 15912 11:23:59,276 --> 11:24:03,660 all that but that's not the main point 15913 11:24:01,320 --> 11:24:07,080 the main point is 15914 11:24:03,660 --> 11:24:10,620 that all my components are re-rendering 15915 11:24:07,080 --> 11:24:13,020 just because I'm changing one value in 15916 11:24:10,620 --> 11:24:16,680 the state and in the upcoming videos 15917 11:24:13,020 --> 11:24:19,200 we'll cover possible solutions to such 15918 11:24:16,680 --> 11:24:22,916 problem okay and what's our first 15919 11:24:19,200 --> 11:24:23,756 possible solution well we can lower the 15920 11:24:22,916 --> 11:24:25,680 state 15921 11:24:23,756 --> 11:24:29,756 in other words 15922 11:24:25,680 --> 11:24:31,916 I could just move the state variable as 15923 11:24:29,756 --> 11:24:34,980 well as the button to a separate 15924 11:24:31,916 --> 11:24:37,800 component let's think about it do we 15925 11:24:34,980 --> 11:24:40,620 really need this count over here in the 15926 11:24:37,800 --> 11:24:42,060 Lower State I mean I'm just showing it 15927 11:24:40,620 --> 11:24:45,120 in a button 15928 11:24:42,060 --> 11:24:48,596 so simply I could create a different 15929 11:24:45,120 --> 11:24:50,520 component let's call it counter move all 15930 11:24:48,596 --> 11:24:51,960 of the logic over there 15931 11:24:50,520 --> 11:24:55,320 and then 15932 11:24:51,960 --> 11:24:58,200 just import that in the Lower State and 15933 11:24:55,320 --> 11:25:00,540 as a result yes we will be updating that 15934 11:24:58,200 --> 11:25:04,080 state value but that is not going to 15935 11:25:00,540 --> 11:25:06,540 trigger re-renders in the Lower State so 15936 11:25:04,080 --> 11:25:09,000 let's try this one out when navigate to 15937 11:25:06,540 --> 11:25:13,020 the star we want to create a new file 15938 11:25:09,000 --> 11:25:15,000 and we'll call this counter jsx and once 15939 11:25:13,020 --> 11:25:17,580 I'm here I'm just going to create that 15940 11:25:15,000 --> 11:25:21,360 new component and then basically we want 15941 11:25:17,580 --> 11:25:24,300 to jump back to index.jsx and grab the 15942 11:25:21,360 --> 11:25:26,580 values I guess let's start with our 15943 11:25:24,300 --> 11:25:28,860 return and in this case I can just cut 15944 11:25:26,580 --> 11:25:30,776 everything out but that is not going to 15945 11:25:28,860 --> 11:25:32,640 be the case with the import so make sure 15946 11:25:30,776 --> 11:25:35,096 that you leave that use stately imported 15947 11:25:32,640 --> 11:25:37,560 because in the index we're still using 15948 11:25:35,096 --> 11:25:41,220 it correct so this one just copy 15949 11:25:37,560 --> 11:25:43,680 let me set it up above the counter 15950 11:25:41,220 --> 11:25:45,840 let's go over here copy and paste and 15951 11:25:43,680 --> 11:25:46,916 then in here I just want to grab this 15952 11:25:45,840 --> 11:25:49,500 count one 15953 11:25:46,916 --> 11:25:51,900 so this one again we're cutting it out 15954 11:25:49,500 --> 11:25:54,240 set it up over here in the counter and 15955 11:25:51,900 --> 11:25:56,580 then lastly we wanna 15956 11:25:54,240 --> 11:25:59,720 go back to the index and we want to set 15957 11:25:56,580 --> 11:26:03,360 up that counter component so counter 15958 11:25:59,720 --> 11:26:05,276 component let's save that and now let's 15959 11:26:03,360 --> 11:26:07,080 use the dev tools that we covered in the 15960 11:26:05,276 --> 11:26:09,900 previous video so now let me navigate 15961 11:26:07,080 --> 11:26:13,256 back Let me refresh 15962 11:26:09,900 --> 11:26:16,140 that's always a good starting point and 15963 11:26:13,256 --> 11:26:18,240 then let me again record notice I'm 15964 11:26:16,140 --> 11:26:21,000 starting profiling and I can right away 15965 11:26:18,240 --> 11:26:23,400 see that only my box 15966 11:26:21,000 --> 11:26:25,800 is being highlighted which essentially 15967 11:26:23,400 --> 11:26:28,680 is a good thing that means that only 15968 11:26:25,800 --> 11:26:32,040 this counter component is re-rendering 15969 11:26:28,680 --> 11:26:34,560 our not the rest of the list so now let 15970 11:26:32,040 --> 11:26:37,680 me stop over here and notice all this 15971 11:26:34,560 --> 11:26:40,980 time only the counter is rendering right 15972 11:26:37,680 --> 11:26:42,540 so that's my initial render and then all 15973 11:26:40,980 --> 11:26:45,060 of these times I'm clicking on a button 15974 11:26:42,540 --> 11:26:46,980 only the counter is basically 15975 11:26:45,060 --> 11:26:48,240 re-rendering why well because State 15976 11:26:46,980 --> 11:26:51,660 change notice 15977 11:26:48,240 --> 11:26:54,960 why this rendered well hook one changed 15978 11:26:51,660 --> 11:26:57,120 which effectively is our name now 15979 11:26:54,960 --> 11:26:59,160 million dollar question are you going to 15980 11:26:57,120 --> 11:27:00,840 be able to do that in all the instances 15981 11:26:59,160 --> 11:27:03,120 of course not 15982 11:27:00,840 --> 11:27:06,416 but this is definitely something I want 15983 11:27:03,120 --> 11:27:10,380 you to consider before you grab the 15984 11:27:06,416 --> 11:27:12,000 memos and use callbacks and all that 15985 11:27:10,380 --> 11:27:15,960 they're going to be instances where you 15986 11:27:12,000 --> 11:27:18,416 can simply just split up the logic 15987 11:27:15,960 --> 11:27:20,220 so instead of jamming everything in one 15988 11:27:18,416 --> 11:27:23,880 component and then having those 15989 11:27:20,220 --> 11:27:25,256 unnecessary re-renders essentially the 15990 11:27:23,880 --> 11:27:29,160 moment you move 15991 11:27:25,256 --> 11:27:30,596 the logic to a separate component that's 15992 11:27:29,160 --> 11:27:34,500 not going to be the case anymore so 15993 11:27:30,596 --> 11:27:37,380 notice now I only have 400 and I still 15994 11:27:34,500 --> 11:27:40,436 have of course the use effect code so 15995 11:27:37,380 --> 11:27:44,580 that's the first solution to our issue 15996 11:27:40,436 --> 11:27:46,140 we can simply lower the state right so 15997 11:27:44,580 --> 11:27:49,380 we can just set up separate component 15998 11:27:46,140 --> 11:27:50,460 and add all of the logic in there so 15999 11:27:49,380 --> 11:27:53,640 that way we're not going to be 16000 11:27:50,460 --> 11:27:56,096 triggering these unnecessary re-renders 16001 11:27:53,640 --> 11:27:58,616 okay and once we're familiar with the 16002 11:27:56,096 --> 11:28:02,640 concept now let's work on a small 16003 11:27:58,616 --> 11:28:06,000 challenge so first in the app jsx change 16004 11:28:02,640 --> 11:28:08,820 the folder so now you wanna get the 16005 11:28:06,000 --> 11:28:11,060 starter from the second one and once you 16006 11:28:08,820 --> 11:28:14,580 navigate there you'll see that there is 16007 11:28:11,060 --> 11:28:16,916 index.jsx there's also a list so the 16008 11:28:14,580 --> 11:28:19,380 same deal and the person 16009 11:28:16,916 --> 11:28:21,840 now the logic works just fine if I go 16010 11:28:19,380 --> 11:28:25,140 here and say Bob it's going to be nicely 16011 11:28:21,840 --> 11:28:29,040 added to the list or notice as I'm 16012 11:28:25,140 --> 11:28:32,520 typing stuff in the input all of my 16013 11:28:29,040 --> 11:28:35,220 components again are having that 16014 11:28:32,520 --> 11:28:36,776 flashing border around them why well 16015 11:28:35,220 --> 11:28:39,000 because we are re-rendering those 16016 11:28:36,776 --> 11:28:41,220 components so I want you to fix that 16017 11:28:39,000 --> 11:28:44,480 just like we covered in the previous 16018 11:28:41,220 --> 11:28:47,460 video so navigate to 16019 11:28:44,480 --> 11:28:50,220 index.jsx and the side 16020 11:28:47,460 --> 11:28:54,300 what do you need to move to a separate 16021 11:28:50,220 --> 11:28:57,776 component to avoid this issue and I can 16022 11:28:54,300 --> 11:29:00,776 give you a tiny hint once you set up the 16023 11:28:57,776 --> 11:29:03,300 entire functionality you'll also need to 16024 11:29:00,776 --> 11:29:04,860 fix the ad person so that's the only 16025 11:29:03,300 --> 11:29:07,380 place where you'll actually have to 16026 11:29:04,860 --> 11:29:10,080 change your functionality a little bit 16027 11:29:07,380 --> 11:29:12,000 so try working on that and then once 16028 11:29:10,080 --> 11:29:15,000 you're ready to compare the results 16029 11:29:12,000 --> 11:29:17,640 resume with the video 16030 11:29:15,000 --> 11:29:19,860 so first let me just navigate over there 16031 11:29:17,640 --> 11:29:22,740 that's my index let me just double check 16032 11:29:19,860 --> 11:29:24,960 that's the correct index one 16033 11:29:22,740 --> 11:29:27,180 and once we're here let's just establish 16034 11:29:24,960 --> 11:29:29,756 why is this happening well I have the 16035 11:29:27,180 --> 11:29:31,860 Forum correct I have the value and on 16036 11:29:29,756 --> 11:29:35,220 change and in there 16037 11:29:31,860 --> 11:29:37,860 I have a name State value 16038 11:29:35,220 --> 11:29:39,840 so every time I'm going to be updating 16039 11:29:37,860 --> 11:29:42,660 that state value 16040 11:29:39,840 --> 11:29:45,436 I will trigger what a re-render 16041 11:29:42,660 --> 11:29:48,960 so what happens with a list and a person 16042 11:29:45,436 --> 11:29:51,180 they also get re-rendered so first we 16043 11:29:48,960 --> 11:29:53,340 always need to look what is causing the 16044 11:29:51,180 --> 11:29:55,740 issue in this case it is the name one 16045 11:29:53,340 --> 11:29:59,096 now million dollar question can remove 16046 11:29:55,740 --> 11:30:01,020 name into a separate component basically 16047 11:29:59,096 --> 11:30:03,180 can I set up a form in a separate 16048 11:30:01,020 --> 11:30:04,080 component and of course the answer is 16049 11:30:03,180 --> 11:30:05,520 yes 16050 11:30:04,080 --> 11:30:07,436 I'll just have to move some 16051 11:30:05,520 --> 11:30:10,800 functionality as well 16052 11:30:07,436 --> 11:30:14,820 and lastly I will have to pass in the 16053 11:30:10,800 --> 11:30:16,916 add person down to a form so this one 16054 11:30:14,820 --> 11:30:19,680 we still want to keep it here because 16055 11:30:16,916 --> 11:30:21,840 we're working with the list 16056 11:30:19,680 --> 11:30:24,776 however we'll have to provide a 16057 11:30:21,840 --> 11:30:27,960 parameter because add person won't have 16058 11:30:24,776 --> 11:30:29,400 access to the name State value and 16059 11:30:27,960 --> 11:30:33,180 you'll see in a second what I mean so 16060 11:30:29,400 --> 11:30:36,660 first we find what is causing the issue 16061 11:30:33,180 --> 11:30:38,580 it's this name State value and now we 16062 11:30:36,660 --> 11:30:39,776 just want to set it up in a separate 16063 11:30:38,580 --> 11:30:42,000 component 16064 11:30:39,776 --> 11:30:44,276 so let's navigate here I'm going to go 16065 11:30:42,000 --> 11:30:46,140 with new file we want to go with form 16066 11:30:44,276 --> 11:30:48,360 jsx 16067 11:30:46,140 --> 11:30:51,060 then here let's create that component 16068 11:30:48,360 --> 11:30:53,700 that's my Forum I'm exporting all that 16069 11:30:51,060 --> 11:30:55,800 is awesome and then one by one let's 16070 11:30:53,700 --> 11:30:58,256 grab everything that we need so I'll 16071 11:30:55,800 --> 11:31:00,596 start here with my return so we won't 16072 11:30:58,256 --> 11:31:03,416 need it over here cut it out 16073 11:31:00,596 --> 11:31:05,880 copy and paste in the form okay awesome 16074 11:31:03,416 --> 11:31:07,860 then as far as the logic 16075 11:31:05,880 --> 11:31:08,700 like I just said add person will stay 16076 11:31:07,860 --> 11:31:10,616 here 16077 11:31:08,700 --> 11:31:13,380 I mean I'm working with the list makes 16078 11:31:10,616 --> 11:31:16,320 sense for this function to be in the 16079 11:31:13,380 --> 11:31:19,680 Lower State component however I do want 16080 11:31:16,320 --> 11:31:22,380 to grab this one and I'll submit 16081 11:31:19,680 --> 11:31:25,380 and I also want to set up that state 16082 11:31:22,380 --> 11:31:29,096 value correct so let's cut this one out 16083 11:31:25,380 --> 11:31:30,480 and we will also need that use state so 16084 11:31:29,096 --> 11:31:33,300 back 16085 11:31:30,480 --> 11:31:36,116 in my index I just want to copy this one 16086 11:31:33,300 --> 11:31:38,756 so I will navigate here copy and paste 16087 11:31:36,116 --> 11:31:41,880 and now everything is nice now the only 16088 11:31:38,756 --> 11:31:44,400 thing that's missing is that add person 16089 11:31:41,880 --> 11:31:47,480 however like I already 16090 11:31:44,400 --> 11:31:49,740 said I believe two times now add person 16091 11:31:47,480 --> 11:31:52,380 does not have access to this name 16092 11:31:49,740 --> 11:31:54,596 anymore so what do we do well we just 16093 11:31:52,380 --> 11:31:57,660 pass it as a parameter we just say name 16094 11:31:54,596 --> 11:31:59,756 here that's it and you nicely saw how it 16095 11:31:57,660 --> 11:32:02,276 was crossed out essentially add person 16096 11:31:59,756 --> 11:32:06,116 doesn't have access to the name unless 16097 11:32:02,276 --> 11:32:09,000 we provide that as a parameter so in 16098 11:32:06,116 --> 11:32:11,220 here we want to go first with the form 16099 11:32:09,000 --> 11:32:14,276 so form component 16100 11:32:11,220 --> 11:32:16,740 like so we grab the form and we want to 16101 11:32:14,276 --> 11:32:20,400 add that person correct so add person is 16102 11:32:16,740 --> 11:32:22,256 equal to add person we pass the prop we 16103 11:32:20,400 --> 11:32:23,700 can save it here now it's going to 16104 11:32:22,256 --> 11:32:24,416 complain and all that don't worry about 16105 11:32:23,700 --> 11:32:26,340 it 16106 11:32:24,416 --> 11:32:29,160 back in the form 16107 11:32:26,340 --> 11:32:30,660 you want to grab add person and now 16108 11:32:29,160 --> 11:32:33,596 everything is fine we just need to pass 16109 11:32:30,660 --> 11:32:36,840 in the name so we go here with the name 16110 11:32:33,596 --> 11:32:38,220 that's it that's all we have to do and 16111 11:32:36,840 --> 11:32:41,160 essentially 16112 11:32:38,220 --> 11:32:45,776 I want to clean out my console now 16113 11:32:41,160 --> 11:32:48,240 notice how nicely as I'm typing only my 16114 11:32:45,776 --> 11:32:50,520 form lights up why well because we're 16115 11:32:48,240 --> 11:32:52,980 not triggering those re-renders anymore 16116 11:32:50,520 --> 11:32:55,560 that's it we're only working with the 16117 11:32:52,980 --> 11:32:58,500 form and of course once we submit yeah 16118 11:32:55,560 --> 11:33:00,776 of course we're updating the entire list 16119 11:32:58,500 --> 11:33:03,720 so there's going to be a re-render 16120 11:33:00,776 --> 11:33:05,640 that's exactly what we want so once we 16121 11:33:03,720 --> 11:33:09,300 create that new list I do want to 16122 11:33:05,640 --> 11:33:11,820 re-render the list as well as the person 16123 11:33:09,300 --> 11:33:16,140 but I don't want to do that while I'm 16124 11:33:11,820 --> 11:33:18,180 typing stuff in the input okay so we 16125 11:33:16,140 --> 11:33:19,740 know how we can push the state down 16126 11:33:18,180 --> 11:33:22,916 awesome 16127 11:33:19,740 --> 11:33:25,436 but it's not going to be possible in 16128 11:33:22,916 --> 11:33:28,740 every instance so there's going to be 16129 11:33:25,436 --> 11:33:30,480 times when yes you basically need to 16130 11:33:28,740 --> 11:33:33,596 have that state value 16131 11:33:30,480 --> 11:33:35,880 but you still wanna 16132 11:33:33,596 --> 11:33:39,300 avoid those re-renders 16133 11:33:35,880 --> 11:33:42,180 and in order to do that we can use a 16134 11:33:39,300 --> 11:33:45,000 react memo function now please don't 16135 11:33:42,180 --> 11:33:47,400 confuse it with use memo which we're 16136 11:33:45,000 --> 11:33:49,860 going to cover in few videos 16137 11:33:47,400 --> 11:33:52,500 essentially this is a memo function that 16138 11:33:49,860 --> 11:33:56,400 is coming from react I want you to start 16139 11:33:52,500 --> 11:33:59,276 by navigating to this folder so zero 16140 11:33:56,400 --> 11:34:01,860 three hooks in the app 16141 11:33:59,276 --> 11:34:04,740 jsx and essentially once you navigate 16142 11:34:01,860 --> 11:34:08,936 here you'll see that we have our first 16143 11:34:04,740 --> 11:34:11,520 example where as I'm clicking on a 16144 11:34:08,936 --> 11:34:12,660 button I'm increasing the count which in 16145 11:34:11,520 --> 11:34:15,980 turn 16146 11:34:12,660 --> 11:34:19,380 re-renders the component which in turn 16147 11:34:15,980 --> 11:34:23,520 re-renders the list as well as the 16148 11:34:19,380 --> 11:34:25,980 person component and if I need to have 16149 11:34:23,520 --> 11:34:28,200 this state value here there's nothing I 16150 11:34:25,980 --> 11:34:30,840 can do I cannot just move it to a 16151 11:34:28,200 --> 11:34:33,840 separate component we can 16152 11:34:30,840 --> 11:34:36,900 pass the component the list component in 16153 11:34:33,840 --> 11:34:40,140 this case through the memo one basically 16154 11:34:36,900 --> 11:34:42,480 we go and look at the tree of our 16155 11:34:40,140 --> 11:34:45,900 components and essentially you decide 16156 11:34:42,480 --> 11:34:47,220 that hey if I stop this list from 16157 11:34:45,900 --> 11:34:49,380 re-rendering 16158 11:34:47,220 --> 11:34:51,480 what's going to happen well the person 16159 11:34:49,380 --> 11:34:54,000 also won't re-render 16160 11:34:51,480 --> 11:34:57,416 and the way we do that we simply 16161 11:34:54,000 --> 11:35:00,116 navigate to a specific component in this 16162 11:34:57,416 --> 11:35:02,460 case again it's a list so you would look 16163 11:35:00,116 --> 11:35:05,880 at the component tree and then we want 16164 11:35:02,460 --> 11:35:09,480 to get that memo function from react 16165 11:35:05,880 --> 11:35:12,480 as with the rest of the functions we 16166 11:35:09,480 --> 11:35:15,480 have few flavors we can go the react dot 16167 11:35:12,480 --> 11:35:17,160 memo and then pass the component let me 16168 11:35:15,480 --> 11:35:21,480 add here component 16169 11:35:17,160 --> 11:35:24,660 so component here or I can just grab the 16170 11:35:21,480 --> 11:35:28,320 memo function and effectively 16171 11:35:24,660 --> 11:35:30,240 it returns a memoized component in 16172 11:35:28,320 --> 11:35:32,936 simple English it just means that now 16173 11:35:30,240 --> 11:35:35,936 the component will check whether the 16174 11:35:32,936 --> 11:35:38,340 props have changed and if they haven't 16175 11:35:35,936 --> 11:35:40,560 which again is happening right now with 16176 11:35:38,340 --> 11:35:42,960 our people the people prop is not 16177 11:35:40,560 --> 11:35:46,256 changing we're just re-rendering the 16178 11:35:42,960 --> 11:35:48,300 Lower State component and if that's the 16179 11:35:46,256 --> 11:35:51,416 case if the props haven't changed well 16180 11:35:48,300 --> 11:35:53,160 then the component won't re-render let 16181 11:35:51,416 --> 11:35:55,200 me go to a list 16182 11:35:53,160 --> 11:35:57,480 so now let me go above my list and I'm 16183 11:35:55,200 --> 11:36:00,900 going to go with memo and that is coming 16184 11:35:57,480 --> 11:36:05,160 from the react and effectively where we 16185 11:36:00,900 --> 11:36:06,540 have the export we simply want to wrap 16186 11:36:05,160 --> 11:36:09,240 the list 16187 11:36:06,540 --> 11:36:11,700 in the memo so essentially we invoke the 16188 11:36:09,240 --> 11:36:15,300 memo and we pass down the list 16189 11:36:11,700 --> 11:36:19,200 and as a result notice once I refresh 16190 11:36:15,300 --> 11:36:21,116 only the component the parent component 16191 11:36:19,200 --> 11:36:24,416 lights up because that's where we're 16192 11:36:21,116 --> 11:36:26,220 setting up those State values and if you 16193 11:36:24,416 --> 11:36:28,800 don't believe me let me navigate to the 16194 11:36:26,220 --> 11:36:31,080 bigger browser window I will refresh and 16195 11:36:28,800 --> 11:36:34,560 all that let's start from the scratch 16196 11:36:31,080 --> 11:36:36,416 and check it out notice over here I'm 16197 11:36:34,560 --> 11:36:40,020 increasing the count all of that is good 16198 11:36:36,416 --> 11:36:44,400 once I stop notice only the Lower State 16199 11:36:40,020 --> 11:36:46,740 we rendered so rest of them the list and 16200 11:36:44,400 --> 11:36:50,580 each person component they didn't 16201 11:36:46,740 --> 11:36:53,340 re-render because props did not change 16202 11:36:50,580 --> 11:36:55,380 so essentially lists stayed the same and 16203 11:36:53,340 --> 11:36:57,720 as a result we did not trigger those 16204 11:36:55,380 --> 11:37:00,720 unnecessary re-renders 16205 11:36:57,720 --> 11:37:02,756 okay and once we have established that 16206 11:37:00,720 --> 11:37:06,116 memo function is awesome 16207 11:37:02,756 --> 11:37:09,660 now let me throw you a mind grenade what 16208 11:37:06,116 --> 11:37:11,460 if we have a remove person function 16209 11:37:09,660 --> 11:37:13,500 and keep in mind 16210 11:37:11,460 --> 11:37:15,540 it totally makes sense 16211 11:37:13,500 --> 11:37:18,840 to set this function in this component 16212 11:37:15,540 --> 11:37:20,880 why well because the list is here 16213 11:37:18,840 --> 11:37:22,140 so let's set up this function remove 16214 11:37:20,880 --> 11:37:23,640 person 16215 11:37:22,140 --> 11:37:26,460 pretty much the logic is going to be 16216 11:37:23,640 --> 11:37:27,480 following we'll grab the ID and we'll 16217 11:37:26,460 --> 11:37:30,480 only 16218 11:37:27,480 --> 11:37:32,400 return items whose ID does not match and 16219 11:37:30,480 --> 11:37:35,276 then we'll set 16220 11:37:32,400 --> 11:37:37,020 the array the people are equal to that 16221 11:37:35,276 --> 11:37:38,820 new value and then we want to pass it 16222 11:37:37,020 --> 11:37:42,240 down because of course I want to invoke 16223 11:37:38,820 --> 11:37:44,700 it in the person component now in my 16224 11:37:42,240 --> 11:37:47,160 case I will copy and paste 16225 11:37:44,700 --> 11:37:49,140 since we have done that quite a few 16226 11:37:47,160 --> 11:37:51,416 times during the course I really don't 16227 11:37:49,140 --> 11:37:54,540 see the point of setting this up from 16228 11:37:51,416 --> 11:37:57,776 the scratch so let me go back to the 16229 11:37:54,540 --> 11:38:00,540 next copy and paste and then we want to 16230 11:37:57,776 --> 11:38:02,880 pass it down to a person like I said so 16231 11:38:00,540 --> 11:38:06,900 we're gonna go the remote person is 16232 11:38:02,880 --> 11:38:08,936 equal to remove person okay beautiful 16233 11:38:06,900 --> 11:38:10,560 then I'll copy and paste right away 16234 11:38:08,936 --> 11:38:12,776 because we want to do the same thing in 16235 11:38:10,560 --> 11:38:14,580 the list we just need to remember that 16236 11:38:12,776 --> 11:38:17,640 we need to structure it here as well of 16237 11:38:14,580 --> 11:38:19,500 course so remove person and then let me 16238 11:38:17,640 --> 11:38:23,040 save the index 16239 11:38:19,500 --> 11:38:24,776 and then back in the person I want to 16240 11:38:23,040 --> 11:38:28,380 actually grab two things 16241 11:38:24,776 --> 11:38:31,320 I want to grab the remove person 16242 11:38:28,380 --> 11:38:33,240 and I also want to grab the ID and then 16243 11:38:31,320 --> 11:38:35,340 right after the heading four I'm gonna 16244 11:38:33,240 --> 11:38:37,740 go with button I'm not gonna set up any 16245 11:38:35,340 --> 11:38:40,560 classes you can definitely do so if you 16246 11:38:37,740 --> 11:38:42,776 want I'm just gonna go with on click and 16247 11:38:40,560 --> 11:38:45,000 this is going to be equal to my arrow 16248 11:38:42,776 --> 11:38:49,320 function and here let's go with remove 16249 11:38:45,000 --> 11:38:52,980 person and let's pass in the ID 16250 11:38:49,320 --> 11:38:54,596 now everything is awesome and by the way 16251 11:38:52,980 --> 11:38:56,640 everything is not awesome because I did 16252 11:38:54,596 --> 11:38:59,460 forget to add some kind of value here 16253 11:38:56,640 --> 11:39:01,560 and I'll just say your room move so now 16254 11:38:59,460 --> 11:39:03,720 everything is awesome however we'll 16255 11:39:01,560 --> 11:39:08,040 probably notice something interesting 16256 11:39:03,720 --> 11:39:10,200 every time I'll click on remove 16257 11:39:08,040 --> 11:39:12,540 I'll be re-rendering so this is 16258 11:39:10,200 --> 11:39:13,980 something we expect okay we're changing 16259 11:39:12,540 --> 11:39:17,756 the list so of course it's going to 16260 11:39:13,980 --> 11:39:20,276 re-render okay but what about this 16261 11:39:17,756 --> 11:39:22,200 what about count 16262 11:39:20,276 --> 11:39:24,416 we have a memo right 16263 11:39:22,200 --> 11:39:26,880 so this shouldn't be happening 16264 11:39:24,416 --> 11:39:28,140 I mean we established in the previous 16265 11:39:26,880 --> 11:39:32,040 video 16266 11:39:28,140 --> 11:39:34,860 that memo is checking for those props 16267 11:39:32,040 --> 11:39:37,500 and if they haven't changed 16268 11:39:34,860 --> 11:39:38,936 well then the component doesn't 16269 11:39:37,500 --> 11:39:41,520 re-render 16270 11:39:38,936 --> 11:39:43,616 and we saw that in a previous video that 16271 11:39:41,520 --> 11:39:47,000 was the case however we add this 16272 11:39:43,616 --> 11:39:50,040 function and now even without going to 16273 11:39:47,000 --> 11:39:53,096 the performance tab I can clearly see 16274 11:39:50,040 --> 11:39:55,256 that these ones are also re-rendering 16275 11:39:53,096 --> 11:39:57,960 so why is that happening 16276 11:39:55,256 --> 11:40:00,180 you see memo is doing its job 16277 11:39:57,960 --> 11:40:01,740 everything is fine with the memo we 16278 11:40:00,180 --> 11:40:03,900 don't need to pass some kind of extra 16279 11:40:01,740 --> 11:40:06,000 argument or anything like that 16280 11:40:03,900 --> 11:40:09,360 the problem is this function 16281 11:40:06,000 --> 11:40:12,416 if you remember all the way back when we 16282 11:40:09,360 --> 11:40:14,756 covered them let me find this 16283 11:40:12,416 --> 11:40:16,560 use effect the first one code example 16284 11:40:14,756 --> 11:40:18,000 what did I say 16285 11:40:16,560 --> 11:40:19,680 I said that 16286 11:40:18,000 --> 11:40:22,436 this function 16287 11:40:19,680 --> 11:40:24,660 is going to be created from the scratch 16288 11:40:22,436 --> 11:40:26,936 correct and it's also going to be 16289 11:40:24,660 --> 11:40:28,916 invoked if you're invoking it here so 16290 11:40:26,936 --> 11:40:31,380 every time the component is going to 16291 11:40:28,916 --> 11:40:32,936 re-render two things are happening this 16292 11:40:31,380 --> 11:40:36,660 function gets created from the scratch 16293 11:40:32,936 --> 11:40:39,540 so in our case let's remove person and 16294 11:40:36,660 --> 11:40:42,000 in here we also invoked it now we're not 16295 11:40:39,540 --> 11:40:45,060 doing that in the 16296 11:40:42,000 --> 11:40:47,400 component here but we are creating this 16297 11:40:45,060 --> 11:40:50,700 function from scratch every time 16298 11:40:47,400 --> 11:40:52,140 the state gets updated 16299 11:40:50,700 --> 11:40:55,020 and here's the thing 16300 11:40:52,140 --> 11:40:58,080 as far as JavaScript is concerned 16301 11:40:55,020 --> 11:41:01,140 this prop has changed so every time we 16302 11:40:58,080 --> 11:41:03,000 click on Account button we re-render the 16303 11:41:01,140 --> 11:41:05,640 component correct 16304 11:41:03,000 --> 11:41:07,740 and every time we render 16305 11:41:05,640 --> 11:41:09,900 we create that function from the scratch 16306 11:41:07,740 --> 11:41:13,080 be a remove person 16307 11:41:09,900 --> 11:41:14,936 so now in the list component yes this 16308 11:41:13,080 --> 11:41:16,256 value is not changing this one pretty 16309 11:41:14,936 --> 11:41:19,380 much stays the same 16310 11:41:16,256 --> 11:41:22,020 so memo fix that however 16311 11:41:19,380 --> 11:41:24,360 when it comes to remove person 16312 11:41:22,020 --> 11:41:26,400 since it gets created from the scratch 16313 11:41:24,360 --> 11:41:28,916 each and every time 16314 11:41:26,400 --> 11:41:31,860 JavaScript things that yeah the prop 16315 11:41:28,916 --> 11:41:36,360 changed and we can actually go to a 16316 11:41:31,860 --> 11:41:40,916 devtools and we can see that so let me 16317 11:41:36,360 --> 11:41:42,720 first go to a profiler record and now 16318 11:41:40,916 --> 11:41:45,060 check it out I'll click I can clearly 16319 11:41:42,720 --> 11:41:47,936 see that they are rendering and we'll 16320 11:41:45,060 --> 11:41:50,460 nicely see that the reason for that is 16321 11:41:47,936 --> 11:41:53,936 because props changed 16322 11:41:50,460 --> 11:41:56,520 so notice it even says the remove person 16323 11:41:53,936 --> 11:41:58,680 changed that's why the list is 16324 11:41:56,520 --> 11:41:59,756 re-rendering even though it's the same 16325 11:41:58,680 --> 11:42:02,820 function 16326 11:41:59,756 --> 11:42:06,116 and therefore in the next video we'll 16327 11:42:02,820 --> 11:42:09,000 cover a huge callback hook which 16328 11:42:06,116 --> 11:42:12,480 actually helps us to solve this issue 16329 11:42:09,000 --> 11:42:15,560 okay so how we can fix this mind grenade 16330 11:42:12,480 --> 11:42:19,616 issue well we can use use callback hook 16331 11:42:15,560 --> 11:42:22,616 which in react allows us to memorize a 16332 11:42:19,616 --> 11:42:25,380 function or basically remember the 16333 11:42:22,616 --> 11:42:27,060 function it takes two arguments the 16334 11:42:25,380 --> 11:42:29,040 first one is the function you want to 16335 11:42:27,060 --> 11:42:31,680 memorize so of course in our case that 16336 11:42:29,040 --> 11:42:34,860 is going to be remove person and the 16337 11:42:31,680 --> 11:42:37,680 second one is the array of dependencies 16338 11:42:34,860 --> 11:42:39,660 and we're already familiar with array of 16339 11:42:37,680 --> 11:42:44,340 dependencies because we have worked with 16340 11:42:39,660 --> 11:42:45,540 it when we used use effect hook correct 16341 11:42:44,340 --> 11:42:48,660 so 16342 11:42:45,540 --> 11:42:52,140 it only creates that function if the 16343 11:42:48,660 --> 11:42:55,020 value in dependency array changes 16344 11:42:52,140 --> 11:42:57,116 so for example if it's empty if it's 16345 11:42:55,020 --> 11:42:59,520 nothing there then of course it's only 16346 11:42:57,116 --> 11:43:01,560 going to create that function the first 16347 11:42:59,520 --> 11:43:05,580 time the component 16348 11:43:01,560 --> 11:43:07,500 renders basically when the initial Mount 16349 11:43:05,580 --> 11:43:09,800 takes place so let's try this one out 16350 11:43:07,500 --> 11:43:13,436 we're going to navigate to 16351 11:43:09,800 --> 11:43:16,756 index.js and as I know I suggest pausing 16352 11:43:13,436 --> 11:43:20,096 the video and try to set it up yourself 16353 11:43:16,756 --> 11:43:22,860 in the readme you can find the 16354 11:43:20,096 --> 11:43:25,860 syntax that you need to use just try to 16355 11:43:22,860 --> 11:43:28,140 implement this in our component and 16356 11:43:25,860 --> 11:43:31,916 effectively we want to grab the hook 16357 11:43:28,140 --> 11:43:33,596 it's called euge callback and then where 16358 11:43:31,916 --> 11:43:37,140 we have the function 16359 11:43:33,596 --> 11:43:39,480 now I want to pass this function into a 16360 11:43:37,140 --> 11:43:41,880 used callback so that's going to be my 16361 11:43:39,480 --> 11:43:44,640 first argument and the second one is 16362 11:43:41,880 --> 11:43:46,436 going to be that dependency array so 16363 11:43:44,640 --> 11:43:49,800 first let me navigate here let me push 16364 11:43:46,436 --> 11:43:51,840 this one down and also use callback and 16365 11:43:49,800 --> 11:43:53,880 first I'll just pass empty dependency 16366 11:43:51,840 --> 11:43:55,140 array just so you can see one major 16367 11:43:53,880 --> 11:43:57,480 gotcha 16368 11:43:55,140 --> 11:43:59,340 so now this is empty now this is 16369 11:43:57,480 --> 11:44:02,096 happening only 16370 11:43:59,340 --> 11:44:05,040 when the component mounts let me cut 16371 11:44:02,096 --> 11:44:06,840 this one out and we pass this one in so 16372 11:44:05,040 --> 11:44:08,160 it still removes the person all of that 16373 11:44:06,840 --> 11:44:10,740 is good 16374 11:44:08,160 --> 11:44:14,520 is just now we're creating it 16375 11:44:10,740 --> 11:44:16,560 only once only the first time when the 16376 11:44:14,520 --> 11:44:19,756 component renders so now check it out 16377 11:44:16,560 --> 11:44:22,320 once I click count I don't have those 16378 11:44:19,756 --> 11:44:24,416 unnecessary re-randers and we can 16379 11:44:22,320 --> 11:44:26,756 actually double check that if we again 16380 11:44:24,416 --> 11:44:28,380 navigate to the profiler 16381 11:44:26,756 --> 11:44:30,800 record 16382 11:44:28,380 --> 11:44:34,616 over here looks like I need to refresh 16383 11:44:30,800 --> 11:44:38,580 yep I start recording and notice 16384 11:44:34,616 --> 11:44:40,320 only the component re-renders not the 16385 11:44:38,580 --> 11:44:43,320 person one so let me stop it over here 16386 11:44:40,320 --> 11:44:44,756 and check it out everything is awesome 16387 11:44:43,320 --> 11:44:48,000 however 16388 11:44:44,756 --> 11:44:51,180 I said that there's a gotcha so 16389 11:44:48,000 --> 11:44:53,700 everything is beautiful use callback 16390 11:44:51,180 --> 11:44:55,560 creates this function only once but 16391 11:44:53,700 --> 11:44:57,480 you'll notice that our functionality 16392 11:44:55,560 --> 11:45:00,180 doesn't quite work 16393 11:44:57,480 --> 11:45:02,580 notice I basically removed one person 16394 11:45:00,180 --> 11:45:04,680 after that I get stuck and just keep 16395 11:45:02,580 --> 11:45:07,080 flipping these values and the reason for 16396 11:45:04,680 --> 11:45:08,756 that is following you see when we create 16397 11:45:07,080 --> 11:45:10,080 this function 16398 11:45:08,756 --> 11:45:13,256 basically 16399 11:45:10,080 --> 11:45:16,800 this is the list that we're working with 16400 11:45:13,256 --> 11:45:19,560 so once we remove that first person we 16401 11:45:16,800 --> 11:45:22,616 actually change that value correct 16402 11:45:19,560 --> 11:45:24,960 initial Aid was 4 and now it's three 16403 11:45:22,616 --> 11:45:27,660 but we still keep using that old value 16404 11:45:24,960 --> 11:45:30,540 and I think it's going to be 16405 11:45:27,660 --> 11:45:33,540 very useful if we log it so let me go 16406 11:45:30,540 --> 11:45:34,860 with console.log and let me set up 16407 11:45:33,540 --> 11:45:37,680 people and you'll see what I'm talking 16408 11:45:34,860 --> 11:45:39,776 about so let me refresh over here 16409 11:45:37,680 --> 11:45:41,580 function gets created okay I don't see 16410 11:45:39,776 --> 11:45:44,520 anything because I'm not invoking it but 16411 11:45:41,580 --> 11:45:46,860 check it out once I click I have four 16412 11:45:44,520 --> 11:45:49,616 items over here right but then the next 16413 11:45:46,860 --> 11:45:51,960 time I still have the four items and 16414 11:45:49,616 --> 11:45:53,820 that's where the issue is functionality 16415 11:45:51,960 --> 11:45:56,220 is not going to work because pretty much 16416 11:45:53,820 --> 11:45:58,500 all the time just working with the 16417 11:45:56,220 --> 11:46:00,300 initial array but since we're moving 16418 11:45:58,500 --> 11:46:02,880 items of course 16419 11:46:00,300 --> 11:46:06,000 our array is changing the people State 16420 11:46:02,880 --> 11:46:09,900 value so long story short this is why we 16421 11:46:06,000 --> 11:46:12,416 want to pass the people as dependency so 16422 11:46:09,900 --> 11:46:15,596 pretty much every time 16423 11:46:12,416 --> 11:46:18,300 will change the people array we also 16424 11:46:15,596 --> 11:46:20,520 want to generate this function from the 16425 11:46:18,300 --> 11:46:22,436 scratch so there's going to be instances 16426 11:46:20,520 --> 11:46:24,900 where you just create this once 16427 11:46:22,436 --> 11:46:26,460 basically you'll have empty dependence 16428 11:46:24,900 --> 11:46:28,860 here and yes there's going to be 16429 11:46:26,460 --> 11:46:30,360 instances where you do need to provide 16430 11:46:28,860 --> 11:46:32,400 some various independency array 16431 11:46:30,360 --> 11:46:35,276 otherwise as you can see our 16432 11:46:32,400 --> 11:46:38,520 functionality went bananas so now let me 16433 11:46:35,276 --> 11:46:40,200 save and let's test it out notice our 16434 11:46:38,520 --> 11:46:41,640 functionality works and yes we do 16435 11:46:40,200 --> 11:46:44,700 re-render the 16436 11:46:41,640 --> 11:46:47,580 person that's totally expected because 16437 11:46:44,700 --> 11:46:49,140 we're actually updating the people State 16438 11:46:47,580 --> 11:46:50,580 value however 16439 11:46:49,140 --> 11:46:51,900 the person is not going to be 16440 11:46:50,580 --> 11:46:54,740 re-rendering 16441 11:46:51,900 --> 11:46:57,000 if we're clicking on account so now 16442 11:46:54,740 --> 11:46:59,460 notice as I'm clicking on account 16443 11:46:57,000 --> 11:47:00,900 everything stays the same and if you 16444 11:46:59,460 --> 11:47:03,960 still don't believe me 16445 11:47:00,900 --> 11:47:05,460 we can go back to our favorite profiler 16446 11:47:03,960 --> 11:47:09,180 record 16447 11:47:05,460 --> 11:47:13,080 and check it out I only have that main 16448 11:47:09,180 --> 11:47:15,180 component over here I won't have the 16449 11:47:13,080 --> 11:47:17,936 person re-rendering just because I'm 16450 11:47:15,180 --> 11:47:20,820 updating the account value again we did 16451 11:47:17,936 --> 11:47:22,680 that with use callback hook which takes 16452 11:47:20,820 --> 11:47:24,840 two arguments first we want to pass in 16453 11:47:22,680 --> 11:47:27,000 the function we want to memorize or 16454 11:47:24,840 --> 11:47:30,540 essentially remember and then the second 16455 11:47:27,000 --> 11:47:32,700 one is the dependency all right okay and 16456 11:47:30,540 --> 11:47:33,660 once we're familiar with use callback 16457 11:47:32,700 --> 11:47:36,720 hook 16458 11:47:33,660 --> 11:47:40,616 now let's see a pretty typical use case 16459 11:47:36,720 --> 11:47:41,936 for it and for that we're actually going 16460 11:47:40,616 --> 11:47:44,340 to navigate away 16461 11:47:41,936 --> 11:47:46,800 from the performance one 16462 11:47:44,340 --> 11:47:49,020 and we're looking for the final one in 16463 11:47:46,800 --> 11:47:51,480 the use effect technically this video is 16464 11:47:49,020 --> 11:47:54,240 optional if you're not interested you 16465 11:47:51,480 --> 11:47:56,936 can just watch and don't switch the 16466 11:47:54,240 --> 11:48:01,500 directories so essentially remember when 16467 11:47:56,936 --> 11:48:06,180 we fetch the users in the user fact zero 16468 11:48:01,500 --> 11:48:09,960 four fetch data I did mention that 16469 11:48:06,180 --> 11:48:13,320 essentially we don't want to set the 16470 11:48:09,960 --> 11:48:16,740 fetch data outside of the use effect 16471 11:48:13,320 --> 11:48:20,580 and then pass it in dependency array 16472 11:48:16,740 --> 11:48:22,200 which Veet doesn't complain about but if 16473 11:48:20,580 --> 11:48:25,980 you're working with create react app 16474 11:48:22,200 --> 11:48:27,540 you'll see this es lint warning now I'm 16475 11:48:25,980 --> 11:48:29,460 not going to place it in dependency 16476 11:48:27,540 --> 11:48:31,980 array because I don't wanna 16477 11:48:29,460 --> 11:48:35,096 basically run out of my requests however 16478 11:48:31,980 --> 11:48:37,380 I will cut this one out from the use 16479 11:48:35,096 --> 11:48:39,060 effect let me copy and paste so that's 16480 11:48:37,380 --> 11:48:42,000 my fetch data 16481 11:48:39,060 --> 11:48:45,060 notice once I run everything is still 16482 11:48:42,000 --> 11:48:47,520 awesome everything still works but like 16483 11:48:45,060 --> 11:48:49,916 I just said normally with create react 16484 11:48:47,520 --> 11:48:52,320 app you'll get that warning in the 16485 11:48:49,916 --> 11:48:53,580 console now that is not an error but it 16486 11:48:52,320 --> 11:48:55,200 is a warning 16487 11:48:53,580 --> 11:48:58,560 the reason why we don't want to pass 16488 11:48:55,200 --> 11:49:02,936 fetch data directly in the user effect 16489 11:48:58,560 --> 11:49:04,980 same deal because this one gets created 16490 11:49:02,936 --> 11:49:07,860 from the scratch so essentially you'll 16491 11:49:04,980 --> 11:49:09,960 have that influent Loop however since 16492 11:49:07,860 --> 11:49:11,880 now we know how to work with use 16493 11:49:09,960 --> 11:49:14,820 callback here's what we can do 16494 11:49:11,880 --> 11:49:17,460 I can go here with comma use callback 16495 11:49:14,820 --> 11:49:19,140 and essentially just set up this 16496 11:49:17,460 --> 11:49:22,256 functionality over here 16497 11:49:19,140 --> 11:49:25,080 so let's try this one out I'm going to 16498 11:49:22,256 --> 11:49:27,360 go to use callback in this case it's an 16499 11:49:25,080 --> 11:49:29,460 empty dependency array so I don't want 16500 11:49:27,360 --> 11:49:32,880 to pass anything in here cut this one 16501 11:49:29,460 --> 11:49:35,040 copy and paste and we're good to go let 16502 11:49:32,880 --> 11:49:36,360 me save it notice the functionality 16503 11:49:35,040 --> 11:49:40,080 still works 16504 11:49:36,360 --> 11:49:42,596 and in this case I can nicely add fetch 16505 11:49:40,080 --> 11:49:45,480 data to dependency array because now 16506 11:49:42,596 --> 11:49:46,916 this fetch data will only be generated 16507 11:49:45,480 --> 11:49:50,640 once 16508 11:49:46,916 --> 11:49:53,276 when the component mounts during the 16509 11:49:50,640 --> 11:49:56,096 initial render and while we're on a 16510 11:49:53,276 --> 11:50:00,416 topic of news callback why don't we also 16511 11:49:56,096 --> 11:50:03,900 cover use memo which is very similar the 16512 11:50:00,416 --> 11:50:06,116 difference is that use memo memorizes 16513 11:50:03,900 --> 11:50:07,916 the value that we're getting back from 16514 11:50:06,116 --> 11:50:11,520 the function so essentially again it 16515 11:50:07,916 --> 11:50:13,860 remembers that value now in this example 16516 11:50:11,520 --> 11:50:17,880 I'll just show you with slow function 16517 11:50:13,860 --> 11:50:22,020 but later on of course we'll use it to 16518 11:50:17,880 --> 11:50:24,360 debounce some functionality so in one of 16519 11:50:22,020 --> 11:50:27,116 the complex projects essentially there's 16520 11:50:24,360 --> 11:50:30,360 going to be a input and we'll use use 16521 11:50:27,116 --> 11:50:32,276 memo to the bounce some functionality 16522 11:50:30,360 --> 11:50:35,460 basically to run some functionality 16523 11:50:32,276 --> 11:50:38,096 later now why am I telling you that just 16524 11:50:35,460 --> 11:50:40,800 so you don't think that we can only use 16525 11:50:38,096 --> 11:50:42,960 for the example I'm about to show you so 16526 11:50:40,800 --> 11:50:44,936 use memoize is actually very cool you 16527 11:50:42,960 --> 11:50:46,860 can do a bunch of interesting stuff 16528 11:50:44,936 --> 11:50:51,720 again in our case we'll just take a look 16529 11:50:46,860 --> 11:50:55,020 look at the slow function example so 16530 11:50:51,720 --> 11:50:59,220 the way use memo works is very similar 16531 11:50:55,020 --> 11:51:00,840 to use callback we get use memo and then 16532 11:50:59,220 --> 11:51:02,520 again notice 16533 11:51:00,840 --> 11:51:03,960 it's looking for two arguments first one 16534 11:51:02,520 --> 11:51:05,520 is going to be the function however in 16535 11:51:03,960 --> 11:51:06,660 this case we're returning something from 16536 11:51:05,520 --> 11:51:08,400 the function 16537 11:51:06,660 --> 11:51:10,980 and then the second one is going to be 16538 11:51:08,400 --> 11:51:13,616 the dependency array now in our case in 16539 11:51:10,980 --> 11:51:16,800 our example we won't have any values in 16540 11:51:13,616 --> 11:51:20,700 dependency array it's only going to run 16541 11:51:16,800 --> 11:51:23,460 once now as far as our example 16542 11:51:20,700 --> 11:51:25,500 let's imagine that in our project 16543 11:51:23,460 --> 11:51:27,840 we have some kind of slow function 16544 11:51:25,500 --> 11:51:32,580 that's doing some kind of calculation 16545 11:51:27,840 --> 11:51:35,880 now I will simulate that by setting up a 16546 11:51:32,580 --> 11:51:37,680 very ugly for Loop so please don't judge 16547 11:51:35,880 --> 11:51:39,660 me on this one so essentially let's 16548 11:51:37,680 --> 11:51:41,820 create a function I'll call this slow 16549 11:51:39,660 --> 11:51:44,700 function.js in there set up this 16550 11:51:41,820 --> 11:51:47,880 function again simply there's a value 16551 11:51:44,700 --> 11:51:50,040 that I'm setting up a for Loop which I 16552 11:51:47,880 --> 11:51:53,700 haven't done probably in four years or 16553 11:51:50,040 --> 11:51:56,060 so and then once I'm done with this long 16554 11:51:53,700 --> 11:51:57,840 for loop I just return a value 16555 11:51:56,060 --> 11:52:00,240 effectively it's just going to take 16556 11:51:57,840 --> 11:52:02,640 quite some time to generate that value 16557 11:52:00,240 --> 11:52:04,800 so this function returns a value and in 16558 11:52:02,640 --> 11:52:07,500 the index I want to import the slow 16559 11:52:04,800 --> 11:52:09,540 function I want to invoke it and I want 16560 11:52:07,500 --> 11:52:12,840 to set it equal to value and you'll see 16561 11:52:09,540 --> 11:52:15,416 the result so let's navigate to the 16562 11:52:12,840 --> 11:52:19,080 hooks like I said in my case I'll call 16563 11:52:15,416 --> 11:52:20,880 this slow function and as I said if you 16564 11:52:19,080 --> 11:52:22,980 don't want to work on this example you 16565 11:52:20,880 --> 11:52:25,800 don't have to you can just see the 16566 11:52:22,980 --> 11:52:30,360 result and I'm pretty sure you'll get 16567 11:52:25,800 --> 11:52:33,660 the main gist of use memo so let's go to 16568 11:52:30,360 --> 11:52:35,520 tutorial let me grab this I don't think 16569 11:52:33,660 --> 11:52:37,140 there's a need to create it from the 16570 11:52:35,520 --> 11:52:38,700 scratch 16571 11:52:37,140 --> 11:52:40,080 now I want to make sure that I'm going 16572 11:52:38,700 --> 11:52:42,960 to start 16573 11:52:40,080 --> 11:52:45,720 yep that's my function then we want to 16574 11:52:42,960 --> 11:52:49,256 navigate back to index we want to import 16575 11:52:45,720 --> 11:52:51,300 that function and I'm going to rely on 16576 11:52:49,256 --> 11:52:54,180 Auto Import and I'll right away set it 16577 11:52:51,300 --> 11:52:56,640 equal to a value so say value is equal 16578 11:52:54,180 --> 11:52:59,640 to and then slow function yep we invoke 16579 11:52:56,640 --> 11:53:02,640 it we don't need to pass anything and I 16580 11:52:59,640 --> 11:53:06,060 simply want to log the value 16581 11:53:02,640 --> 11:53:07,916 so let me go with console log 16582 11:53:06,060 --> 11:53:10,500 and we're looking for the value let me 16583 11:53:07,916 --> 11:53:13,080 save and again same deal 16584 11:53:10,500 --> 11:53:14,220 pretty much every time I'll click on 16585 11:53:13,080 --> 11:53:15,720 account 16586 11:53:14,220 --> 11:53:19,020 it's gonna take 16587 11:53:15,720 --> 11:53:21,540 time for my component to render you'll 16588 11:53:19,020 --> 11:53:23,520 see that it's definitely slower 16589 11:53:21,540 --> 11:53:25,560 if you would have for example some kind 16590 11:53:23,520 --> 11:53:27,480 of input and if you're typing it's going 16591 11:53:25,560 --> 11:53:30,540 to be with delay 16592 11:53:27,480 --> 11:53:32,756 so let me just showcase that I'm going 16593 11:53:30,540 --> 11:53:34,080 to go to button and I'll click like six 16594 11:53:32,756 --> 11:53:37,140 seven times 16595 11:53:34,080 --> 11:53:39,480 and you'll notice that the value is not 16596 11:53:37,140 --> 11:53:42,720 updating right away why well because 16597 11:53:39,480 --> 11:53:44,880 again we're changing the state value we 16598 11:53:42,720 --> 11:53:47,040 are re-rendering and what do we do in 16599 11:53:44,880 --> 11:53:49,740 each render we run this slow function 16600 11:53:47,040 --> 11:53:53,040 and since our slow function is really 16601 11:53:49,740 --> 11:53:56,340 slow well that's why it takes ages for 16602 11:53:53,040 --> 11:54:00,116 me to see that count of nine and this is 16603 11:53:56,340 --> 11:54:03,596 where the use memo hook can definitely 16604 11:54:00,116 --> 11:54:06,180 help us again the syntax is extremely 16605 11:54:03,596 --> 11:54:10,020 similar to use callback we can go with 16606 11:54:06,180 --> 11:54:10,860 use memo we import the hook and now I 16607 11:54:10,020 --> 11:54:13,140 wanna 16608 11:54:10,860 --> 11:54:15,840 set up this slow function in the used 16609 11:54:13,140 --> 11:54:18,840 memo and as a result you'll see that the 16610 11:54:15,840 --> 11:54:22,436 slow function is only going to run once 16611 11:54:18,840 --> 11:54:25,020 when our initial render takes place so 16612 11:54:22,436 --> 11:54:26,756 let's go to user memo again two 16613 11:54:25,020 --> 11:54:30,180 arguments in this case I'll pass in 16614 11:54:26,756 --> 11:54:33,416 empty dependency array then 16615 11:54:30,180 --> 11:54:35,880 I wanna grab my slow function but again 16616 11:54:33,416 --> 11:54:38,160 this needs to return a value so let me 16617 11:54:35,880 --> 11:54:40,800 cut this one out I'll pass here the 16618 11:54:38,160 --> 11:54:43,616 arrow function like so and then we'll 16619 11:54:40,800 --> 11:54:46,560 invoke slow function and now you'll 16620 11:54:43,616 --> 11:54:48,960 notice that once we refresh yep the 16621 11:54:46,560 --> 11:54:52,256 initial render takes a little bit of 16622 11:54:48,960 --> 11:54:54,776 time but after that everything's cool 16623 11:54:52,256 --> 11:54:57,596 so unless I provide some kind of value 16624 11:54:54,776 --> 11:55:01,080 in this dependency array 16625 11:54:57,596 --> 11:55:02,756 this initial value the one that takes 16626 11:55:01,080 --> 11:55:05,700 really long time 16627 11:55:02,756 --> 11:55:07,980 for the slow function to calculate 16628 11:55:05,700 --> 11:55:09,900 it's only going to run once 16629 11:55:07,980 --> 11:55:12,060 I'm only going to get that value on 16630 11:55:09,900 --> 11:55:14,460 initial render and that's it since the 16631 11:55:12,060 --> 11:55:17,340 value is not changing essentially I'm 16632 11:55:14,460 --> 11:55:21,416 not slowing down my component every time 16633 11:55:17,340 --> 11:55:24,596 a state update takes place with react 18 16634 11:55:21,416 --> 11:55:26,820 we also got use transition hook 16635 11:55:24,596 --> 11:55:30,360 which we can use to treat some 16636 11:55:26,820 --> 11:55:32,936 functionality as less urgent so imagine 16637 11:55:30,360 --> 11:55:35,700 this scenario in your app 16638 11:55:32,936 --> 11:55:38,640 there is some kind of user interaction 16639 11:55:35,700 --> 11:55:41,880 which triggers some heavy computation 16640 11:55:38,640 --> 11:55:45,116 for example user types something in the 16641 11:55:41,880 --> 11:55:48,240 input and based on the input value you 16642 11:55:45,116 --> 11:55:50,700 filter I don't know 20 000 items 16643 11:55:48,240 --> 11:55:53,640 here's the thing while the computation 16644 11:55:50,700 --> 11:55:55,140 is taking place it will actually block 16645 11:55:53,640 --> 11:55:57,720 the UI 16646 11:55:55,140 --> 11:56:01,200 basically our app will be less 16647 11:55:57,720 --> 11:56:03,840 responsive and as you can imagine that's 16648 11:56:01,200 --> 11:56:07,680 not a good thing ask yourself 16649 11:56:03,840 --> 11:56:11,640 as a user what's more annoying waiting 16650 11:56:07,680 --> 11:56:13,800 for some data to arrive or tirelessly 16651 11:56:11,640 --> 11:56:17,756 clicking on the button 16652 11:56:13,800 --> 11:56:20,880 so use transition just allows us to Mark 16653 11:56:17,756 --> 11:56:23,276 certain functionality as less urgent 16654 11:56:20,880 --> 11:56:24,540 which in turn prevents the whole UI 16655 11:56:23,276 --> 11:56:26,820 blocking thing 16656 11:56:24,540 --> 11:56:30,180 with that said like I already mentioned 16657 11:56:26,820 --> 11:56:33,000 before react is Fast by default 16658 11:56:30,180 --> 11:56:35,700 so even though this is a very cool 16659 11:56:33,000 --> 11:56:38,880 addition to the library it has very 16660 11:56:35,700 --> 11:56:41,400 specific use cases and most likely it's 16661 11:56:38,880 --> 11:56:44,096 not something you'll use immediately in 16662 11:56:41,400 --> 11:56:46,680 all your applications and once we're 16663 11:56:44,096 --> 11:56:49,380 familiar with the general concept now 16664 11:56:46,680 --> 11:56:52,616 let's take it out for a test drive 16665 11:56:49,380 --> 11:56:55,860 in this video we will heavily rely on 16666 11:56:52,616 --> 11:56:58,140 array Dot from method so if you're not 16667 11:56:55,860 --> 11:57:00,900 familiar with it or you need to jog your 16668 11:56:58,140 --> 11:57:03,060 memory please utilize this JavaScript 16669 11:57:00,900 --> 11:57:05,340 nuggets video where I cover everything 16670 11:57:03,060 --> 11:57:09,000 in great detail 16671 11:57:05,340 --> 11:57:11,936 as far as the location in the app.jsx we 16672 11:57:09,000 --> 11:57:17,276 want to grab the starter 16673 11:57:11,936 --> 11:57:20,460 from zero four react 18. again zero for 16674 11:57:17,276 --> 11:57:22,140 react 18. this is what we should see on 16675 11:57:20,460 --> 11:57:24,840 the screen basically there's going to be 16676 11:57:22,140 --> 11:57:27,596 an input and we'll have some items below 16677 11:57:24,840 --> 11:57:30,180 and if we navigate to the file 16678 11:57:27,596 --> 11:57:32,160 you'll see that I have two State values 16679 11:57:30,180 --> 11:57:34,256 I have one for text so that's for the 16680 11:57:32,160 --> 11:57:37,560 input and I also have one for items 16681 11:57:34,256 --> 11:57:39,180 which initially is just empty array so 16682 11:57:37,560 --> 11:57:41,700 in here I have the form everything is 16683 11:57:39,180 --> 11:57:44,276 awesome I have the handle change I also 16684 11:57:41,700 --> 11:57:45,660 have heading 4 right below the form so 16685 11:57:44,276 --> 11:57:47,640 this is where we are displaying the 16686 11:57:45,660 --> 11:57:49,560 items at the moment they're empty 16687 11:57:47,640 --> 11:57:52,080 there's nothing there and as far as 16688 11:57:49,560 --> 11:57:54,240 handle change well I have set text I'm 16689 11:57:52,080 --> 11:57:55,436 just grabbing the value all of this is 16690 11:57:54,240 --> 11:57:58,936 awesome 16691 11:57:55,436 --> 11:58:01,256 however let's imagine that 16692 11:57:58,936 --> 11:58:02,460 I attempt to do something very 16693 11:58:01,256 --> 11:58:06,000 interesting 16694 11:58:02,460 --> 11:58:09,540 first I'll create new items 16695 11:58:06,000 --> 11:58:13,436 using array.com and I want to construct 16696 11:58:09,540 --> 11:58:15,660 a very big array therefore I provide 16697 11:58:13,436 --> 11:58:17,040 here an object with a length property of 16698 11:58:15,660 --> 11:58:18,960 5 16699 11:58:17,040 --> 11:58:21,060 000 and then the second one is the 16700 11:58:18,960 --> 11:58:24,060 Callback function which gets invoked for 16701 11:58:21,060 --> 11:58:26,520 every item now I don't care about the 16702 11:58:24,060 --> 11:58:28,140 actual item I do care about the index 16703 11:58:26,520 --> 11:58:30,180 and yes this is the case where I'll 16704 11:58:28,140 --> 11:58:32,096 cheat a little bit and I'll set up the 16705 11:58:30,180 --> 11:58:34,080 key as the index 16706 11:58:32,096 --> 11:58:35,580 now inside of it I also want to return 16707 11:58:34,080 --> 11:58:38,776 an image 16708 11:58:35,580 --> 11:58:41,400 which essentially is that nice and cute 16709 11:58:38,776 --> 11:58:44,276 wheat SVG 16710 11:58:41,400 --> 11:58:45,776 so remember the public folder I talked 16711 11:58:44,276 --> 11:58:48,116 about at the very very beginning in 16712 11:58:45,776 --> 11:58:50,040 react fundamentals since it's in the 16713 11:58:48,116 --> 11:58:54,116 public I can just go forward slash and 16714 11:58:50,040 --> 11:58:58,500 beat SVG then once I create that array 16715 11:58:54,116 --> 11:59:02,040 the 5000 items basically just a bunch of 16716 11:58:58,500 --> 11:59:06,900 wheat SVG images I want to set it equal 16717 11:59:02,040 --> 11:59:10,800 to my new items and if everything is 16718 11:59:06,900 --> 11:59:13,560 still fast we can also slow down the CPU 16719 11:59:10,800 --> 11:59:15,660 because it's one thing to work in 16720 11:59:13,560 --> 11:59:17,520 development with a fast computer but 16721 11:59:15,660 --> 11:59:19,256 it's totally different if you're 16722 11:59:17,520 --> 11:59:21,960 somewhere out there with Spotty Wi-Fi 16723 11:59:19,256 --> 11:59:24,776 struggling with I don't know iPhone 3 or 16724 11:59:21,960 --> 11:59:26,400 whatever so the way we can slow down the 16725 11:59:24,776 --> 11:59:28,020 CPU so again we're not talking about the 16726 11:59:26,400 --> 11:59:30,360 network we can also do it with a network 16727 11:59:28,020 --> 11:59:33,776 in this case I want to slow down the CPU 16728 11:59:30,360 --> 11:59:35,820 we go with performant insights let me 16729 11:59:33,776 --> 11:59:38,400 make this sucker bigger 16730 11:59:35,820 --> 11:59:40,800 and then notice here it says no 16731 11:59:38,400 --> 11:59:43,380 throttling I can actually go with CPU 16732 11:59:40,800 --> 11:59:45,540 and I can make it I don't know four 16733 11:59:43,380 --> 11:59:48,300 times slower something along those lines 16734 11:59:45,540 --> 11:59:50,936 now let me make this one again smaller 16735 11:59:48,300 --> 11:59:54,300 and you'll notice something interesting 16736 11:59:50,936 --> 11:59:56,340 since I'm running this pretty much on 16737 11:59:54,300 --> 11:59:58,680 every keystroke 16738 11:59:56,340 --> 12:00:00,660 you'll actually notice that my input is 16739 11:59:58,680 --> 12:00:03,180 going to be somewhat slow so I haven't 16740 12:00:00,660 --> 12:00:04,560 saved the file yet so notice I can 16741 12:00:03,180 --> 12:00:07,860 nicely type and everything is awesome 16742 12:00:04,560 --> 12:00:10,916 however the moment I save the file and 16743 12:00:07,860 --> 12:00:14,096 the moment I create those 5 000 items 16744 12:00:10,916 --> 12:00:17,520 every time user 16745 12:00:14,096 --> 12:00:19,616 presses something in the input you'll 16746 12:00:17,520 --> 12:00:22,256 right away notice that it's very slow so 16747 12:00:19,616 --> 12:00:25,080 I typed few characters and notice how 16748 12:00:22,256 --> 12:00:27,060 long it took for them to show on screen 16749 12:00:25,080 --> 12:00:28,980 and it's going to be a little bit faster 16750 12:00:27,060 --> 12:00:30,480 once we already have all the items and 16751 12:00:28,980 --> 12:00:33,480 all that since of course we're not 16752 12:00:30,480 --> 12:00:35,160 changing the path but hopefully you can 16753 12:00:33,480 --> 12:00:39,000 see the issue 16754 12:00:35,160 --> 12:00:41,340 so we need to somehow fix this where I 16755 12:00:39,000 --> 12:00:42,360 still want a fast response here in the 16756 12:00:41,340 --> 12:00:45,720 input 16757 12:00:42,360 --> 12:00:48,720 and then this one the new items well 16758 12:00:45,720 --> 12:00:50,880 that can happen in the background so 16759 12:00:48,720 --> 12:00:54,596 let's try this one out where I want to 16760 12:00:50,880 --> 12:00:56,936 go to import and I want to grab use and 16761 12:00:54,596 --> 12:00:59,460 then transition so that's the hook I'm 16762 12:00:56,936 --> 12:01:01,560 looking for then I want to go right 16763 12:00:59,460 --> 12:01:03,660 after the items and I want to invoke it 16764 12:01:01,560 --> 12:01:06,480 and it's getting back to you things 16765 12:01:03,660 --> 12:01:07,980 it's getting back is bending and the 16766 12:01:06,480 --> 12:01:11,700 start transition 16767 12:01:07,980 --> 12:01:13,436 so the slow function this one over here 16768 12:01:11,700 --> 12:01:16,140 or functionality 16769 12:01:13,436 --> 12:01:19,020 maybe be more precise we want to place 16770 12:01:16,140 --> 12:01:20,460 that in the start transition and then is 16771 12:01:19,020 --> 12:01:22,320 pending we'll use 16772 12:01:20,460 --> 12:01:24,300 effectively to Showcase that something 16773 12:01:22,320 --> 12:01:28,140 has happened so we're gonna go here with 16774 12:01:24,300 --> 12:01:31,220 const and then is and pending 16775 12:01:28,140 --> 12:01:34,200 and then comma and we'll say start 16776 12:01:31,220 --> 12:01:37,800 transition now like I said that is equal 16777 12:01:34,200 --> 12:01:40,740 to my use transition and I think 16778 12:01:37,800 --> 12:01:43,740 I'll make this one smaller it's somewhat 16779 12:01:40,740 --> 12:01:47,040 annoying here so yep we invoke them then 16780 12:01:43,740 --> 12:01:50,820 let's go to a handle change 16781 12:01:47,040 --> 12:01:52,860 and let's set up that start transition 16782 12:01:50,820 --> 12:01:54,900 and now let's pass both of these things 16783 12:01:52,860 --> 12:01:56,756 inside of it now we do need to set up 16784 12:01:54,900 --> 12:01:59,096 the function here first 16785 12:01:56,756 --> 12:02:01,980 so let's do that let's grab these two 16786 12:01:59,096 --> 12:02:05,820 suckers so the 16787 12:02:01,980 --> 12:02:09,240 array with 5000 items and the set new 16788 12:02:05,820 --> 12:02:12,840 items good and now let's scroll down a 16789 12:02:09,240 --> 12:02:15,540 little bit and where we have the items I 16790 12:02:12,840 --> 12:02:18,900 actually want to look for the is pending 16791 12:02:15,540 --> 12:02:21,000 the value of spending and then if we're 16792 12:02:18,900 --> 12:02:23,700 loading basically if it's true then 16793 12:02:21,000 --> 12:02:26,460 we'll display loading dot dot and once 16794 12:02:23,700 --> 12:02:29,596 we're good to go then we'll display the 16795 12:02:26,460 --> 12:02:32,580 items so right before D heading four 16796 12:02:29,596 --> 12:02:35,640 let's open up the curlies I'll go with 16797 12:02:32,580 --> 12:02:38,220 is pending and I'm gonna go with the new 16798 12:02:35,640 --> 12:02:40,200 operator so if it's true what do I want 16799 12:02:38,220 --> 12:02:43,520 to showcase let me go here with heading 16800 12:02:40,200 --> 12:02:47,096 four and then loading dot okay awesome 16801 12:02:43,520 --> 12:02:49,436 and then if it's false basically once 16802 12:02:47,096 --> 12:02:52,200 we're done then we want to display those 16803 12:02:49,436 --> 12:02:55,020 items so let me cut this one out copy 16804 12:02:52,200 --> 12:02:57,480 and paste and once I save you'll notice 16805 12:02:55,020 --> 12:02:59,640 something interesting that now the 16806 12:02:57,480 --> 12:03:01,616 response in the input is going to be 16807 12:02:59,640 --> 12:03:04,860 much faster and notice we have this 16808 12:03:01,616 --> 12:03:08,220 loading so essentially something we want 16809 12:03:04,860 --> 12:03:09,360 to keep fast which is the response to 16810 12:03:08,220 --> 12:03:13,020 our user 16811 12:03:09,360 --> 12:03:15,360 is still going to happen fast however 16812 12:03:13,020 --> 12:03:17,640 the slow functionality 16813 12:03:15,360 --> 12:03:20,040 is going to happen in the background 16814 12:03:17,640 --> 12:03:22,436 okay and now let's see how we can do 16815 12:03:20,040 --> 12:03:25,436 code splitting in react 16816 12:03:22,436 --> 12:03:29,040 which in turn will allow us to 16817 12:03:25,436 --> 12:03:30,180 progressively load or lazy load our 16818 12:03:29,040 --> 12:03:32,880 application 16819 12:03:30,180 --> 12:03:35,936 before we take a look at the code let's 16820 12:03:32,880 --> 12:03:37,916 discuss the main benefits of such 16821 12:03:35,936 --> 12:03:41,160 approach first 16822 12:03:37,916 --> 12:03:44,460 improve performance by splitting up a 16823 12:03:41,160 --> 12:03:47,936 code into smaller more manageable chunks 16824 12:03:44,460 --> 12:03:50,936 we can reduce the size of the initial 16825 12:03:47,936 --> 12:03:54,720 JavaScript payload that needs to be 16826 12:03:50,936 --> 12:03:58,980 loaded this results in Faster load times 16827 12:03:54,720 --> 12:04:03,000 and improved performance especially on 16828 12:03:58,980 --> 12:04:05,580 slow networks or low end devices 16829 12:04:03,000 --> 12:04:07,800 second better user experience 16830 12:04:05,580 --> 12:04:10,560 with code splitting only the essential 16831 12:04:07,800 --> 12:04:13,436 code needed for the initial render of 16832 12:04:10,560 --> 12:04:16,740 your application is loaded the remaining 16833 12:04:13,436 --> 12:04:18,720 code is loaded as the user interacts 16834 12:04:16,740 --> 12:04:22,800 with your application 16835 12:04:18,720 --> 12:04:24,240 leading to a smoother less blocking user 16836 12:04:22,800 --> 12:04:26,340 experience 16837 12:04:24,240 --> 12:04:30,000 you see especially when it comes to 16838 12:04:26,340 --> 12:04:31,616 bigger projects not all the pages and 16839 12:04:30,000 --> 12:04:35,040 components are equal 16840 12:04:31,616 --> 12:04:38,220 meaning there are certain resources that 16841 12:04:35,040 --> 12:04:40,320 are used More Often by the users for 16842 12:04:38,220 --> 12:04:42,660 example typically 16843 12:04:40,320 --> 12:04:43,800 which page do you think gets more 16844 12:04:42,660 --> 12:04:46,680 traffic 16845 12:04:43,800 --> 12:04:49,080 the homepage or the contact page 16846 12:04:46,680 --> 12:04:52,860 so million dollar question 16847 12:04:49,080 --> 12:04:55,860 if some resources are used less often 16848 12:04:52,860 --> 12:04:59,400 than the other ones does it make sense 16849 12:04:55,860 --> 12:05:02,276 to jam all of that code when we 16850 12:04:59,400 --> 12:05:05,040 initially ship our application to the 16851 12:05:02,276 --> 12:05:06,596 browser because keep in mind the more 16852 12:05:05,040 --> 12:05:09,596 code we send 16853 12:05:06,596 --> 12:05:12,416 the more time it will take for the 16854 12:05:09,596 --> 12:05:15,776 browser to compile it which in turn will 16855 12:05:12,416 --> 12:05:17,880 affect how fast the user can interact 16856 12:05:15,776 --> 12:05:20,936 with our application 16857 12:05:17,880 --> 12:05:24,900 so wouldn't it be nice if we could 16858 12:05:20,936 --> 12:05:27,060 prioritize the important resources over 16859 12:05:24,900 --> 12:05:30,080 the less important ones 16860 12:05:27,060 --> 12:05:32,880 in order to Lazy load our components or 16861 12:05:30,080 --> 12:05:36,180 progressively load our application we'll 16862 12:05:32,880 --> 12:05:39,180 use a tool called suspense now suspense 16863 12:05:36,180 --> 12:05:43,200 is a series of apis and it actually has 16864 12:05:39,180 --> 12:05:45,000 been around for quite some time but at 16865 12:05:43,200 --> 12:05:47,400 this point in time only the code 16866 12:05:45,000 --> 12:05:51,300 splitting feature which we're about to 16867 12:05:47,400 --> 12:05:54,000 cover is fully supported other ones are 16868 12:05:51,300 --> 12:05:56,520 still experimental including data 16869 12:05:54,000 --> 12:05:59,700 fetching and therefore we won't discuss 16870 12:05:56,520 --> 12:06:02,400 them at this time lastly in our example 16871 12:05:59,700 --> 12:06:05,640 we'll progressively load a big component 16872 12:06:02,400 --> 12:06:08,756 when the user clicks the button 16873 12:06:05,640 --> 12:06:12,116 however a more realistic scenario is to 16874 12:06:08,756 --> 12:06:14,520 Lazy load our component when we have 16875 12:06:12,116 --> 12:06:17,756 multiple Pages which is something we'll 16876 12:06:14,520 --> 12:06:21,240 cover when we discuss routing in react 16877 12:06:17,756 --> 12:06:24,060 so this is just general info and we will 16878 12:06:21,240 --> 12:06:26,340 revisit this topic later on in the 16879 12:06:24,060 --> 12:06:27,776 course okay and now let's see suspense 16880 12:06:26,340 --> 12:06:29,936 in action 16881 12:06:27,776 --> 12:06:31,616 for that we want to navigate back to the 16882 12:06:29,936 --> 12:06:35,276 same folder 16883 12:06:31,616 --> 12:06:38,820 the zero four react 18. and notice over 16884 12:06:35,276 --> 12:06:40,140 there I created a slow component and 16885 12:06:38,820 --> 12:06:41,880 essentially the idea is going to be 16886 12:06:40,140 --> 12:06:44,936 exactly the same as in the previous 16887 12:06:41,880 --> 12:06:48,180 setup where I want to create 5000 items 16888 12:06:44,936 --> 12:06:51,300 remember those are images and then 16889 12:06:48,180 --> 12:06:53,936 in the component I'll use use State I'll 16890 12:06:51,300 --> 12:06:55,740 set this one up as default value and 16891 12:06:53,936 --> 12:06:58,916 essentially I just want to render them 16892 12:06:55,740 --> 12:07:01,200 on a screen so now I want to go back to 16893 12:06:58,916 --> 12:07:04,140 index.jsx 16894 12:07:01,200 --> 12:07:05,936 and effectively I'm not going to touch 16895 12:07:04,140 --> 12:07:07,080 any of this logic I just want to 16896 12:07:05,936 --> 12:07:11,400 showcase something 16897 12:07:07,080 --> 12:07:13,500 so I'm going to go below this is pending 16898 12:07:11,400 --> 12:07:16,020 and I'll just showcase the slow 16899 12:07:13,500 --> 12:07:18,300 component and as I said you know I'll 16900 12:07:16,020 --> 12:07:20,640 just refresh just so we don't have two 16901 12:07:18,300 --> 12:07:23,340 of them on the screen so still within a 16902 12:07:20,640 --> 12:07:27,116 section I want to go with slow component 16903 12:07:23,340 --> 12:07:30,000 let's save that we should eventually see 16904 12:07:27,116 --> 12:07:32,096 the component on the screen so let me 16905 12:07:30,000 --> 12:07:35,580 refresh notice it took a little bit of 16906 12:07:32,096 --> 12:07:38,400 time but eventually we see those 5000 16907 12:07:35,580 --> 12:07:40,616 items again the idea is exactly the same 16908 12:07:38,400 --> 12:07:42,776 this time we're not 16909 12:07:40,616 --> 12:07:46,020 getting them from the input 16910 12:07:42,776 --> 12:07:47,936 we are importing the component and of 16911 12:07:46,020 --> 12:07:50,160 course the reason why everything is 16912 12:07:47,936 --> 12:07:54,116 happening so slow is because I'm still 16913 12:07:50,160 --> 12:07:56,400 throttling the CPU now 16914 12:07:54,116 --> 12:07:57,980 what's also interesting 16915 12:07:56,400 --> 12:08:01,616 if I navigate to the bigger screen 16916 12:07:57,980 --> 12:08:04,380 you'll notice in the network tab 16917 12:08:01,616 --> 12:08:06,300 more specifically if we look for Js 16918 12:08:04,380 --> 12:08:09,776 since we are right away 16919 12:08:06,300 --> 12:08:13,320 displaying the slow component we are 16920 12:08:09,776 --> 12:08:15,540 also importing the JavaScript code 16921 12:08:13,320 --> 12:08:19,616 correct but what if we have a different 16922 12:08:15,540 --> 12:08:21,960 setup what if I create a state value 16923 12:08:19,616 --> 12:08:23,040 I'm going to call this show 16924 12:08:21,960 --> 12:08:27,416 and 16925 12:08:23,040 --> 12:08:29,460 set show and I'll set it equal to let's 16926 12:08:27,416 --> 12:08:31,340 say the default one will be false so 16927 12:08:29,460 --> 12:08:34,680 that's my Boolean value 16928 12:08:31,340 --> 12:08:37,800 then I'll set up a button that toggles 16929 12:08:34,680 --> 12:08:39,840 it so right above the component 16930 12:08:37,800 --> 12:08:42,840 I'm going to go with my button 16931 12:08:39,840 --> 12:08:44,700 I'll set up on click 16932 12:08:42,840 --> 12:08:48,416 let's invoke here 16933 12:08:44,700 --> 12:08:50,340 the set show so set show and we'll set 16934 12:08:48,416 --> 12:08:53,936 it equal to the opposite value whatever 16935 12:08:50,340 --> 12:08:56,340 it is in a state and let's just call 16936 12:08:53,936 --> 12:08:59,520 this toggle now in this case I do want 16937 12:08:56,340 --> 12:09:01,560 to add a class so class name is equal to 16938 12:08:59,520 --> 12:09:05,096 BTN 16939 12:09:01,560 --> 12:09:07,680 and then when it comes to slow component 16940 12:09:05,096 --> 12:09:10,560 I want to go to show 16941 12:09:07,680 --> 12:09:11,580 and only if the value is true I want to 16942 12:09:10,560 --> 12:09:13,380 display it 16943 12:09:11,580 --> 12:09:16,256 so let's go here 16944 12:09:13,380 --> 12:09:17,640 let's save again let me navigate to the 16945 12:09:16,256 --> 12:09:19,500 big screen 16946 12:09:17,640 --> 12:09:21,960 and notice 16947 12:09:19,500 --> 12:09:23,756 even though I'm displaying this 16948 12:09:21,960 --> 12:09:26,460 component only 16949 12:09:23,756 --> 12:09:28,320 when the value is true 16950 12:09:26,460 --> 12:09:30,960 I'm still importing 16951 12:09:28,320 --> 12:09:33,360 right out of the gate right so I'm still 16952 12:09:30,960 --> 12:09:35,096 grabbing that JavaScript logic even 16953 12:09:33,360 --> 12:09:36,720 though there's no guarantee that the 16954 12:09:35,096 --> 12:09:39,660 user will click the button 16955 12:09:36,720 --> 12:09:42,480 and essentially it would be better if we 16956 12:09:39,660 --> 12:09:44,936 only import this logic when we actually 16957 12:09:42,480 --> 12:09:48,116 need to display that component correct 16958 12:09:44,936 --> 12:09:50,580 so instead of importing everything on 16959 12:09:48,116 --> 12:09:52,680 our initial render which of course is 16960 12:09:50,580 --> 12:09:55,436 going to add the loading time we only 16961 12:09:52,680 --> 12:09:58,500 want to import that slow component when 16962 12:09:55,436 --> 12:10:01,500 the user actually wants to work with it 16963 12:09:58,500 --> 12:10:04,500 in this case clicks the toggle button so 16964 12:10:01,500 --> 12:10:07,200 how we can do that well we need to First 16965 12:10:04,500 --> 12:10:09,540 navigate up and we'll need to grab two 16966 12:10:07,200 --> 12:10:11,640 things we'll need to grab suspense 16967 12:10:09,540 --> 12:10:13,916 components so this is a component that 16968 12:10:11,640 --> 12:10:17,416 react provides and we also need to go 16969 12:10:13,916 --> 12:10:20,880 with lazy then we have this somewhat 16970 12:10:17,416 --> 12:10:22,680 interesting setup where we want to go 16971 12:10:20,880 --> 12:10:24,300 with const we need to come up with an 16972 12:10:22,680 --> 12:10:27,540 iron in my case it's still going to be 16973 12:10:24,300 --> 12:10:31,560 slow component and that is equal to lazy 16974 12:10:27,540 --> 12:10:34,380 so invoke this then we provide a 16975 12:10:31,560 --> 12:10:37,500 function and then we go with import so 16976 12:10:34,380 --> 12:10:38,820 as you can see now we're importing this 16977 12:10:37,500 --> 12:10:40,980 dynamically 16978 12:10:38,820 --> 12:10:42,776 so we go with forward slash and of 16979 12:10:40,980 --> 12:10:45,416 course the path is still the same so now 16980 12:10:42,776 --> 12:10:47,640 I want to remove this one the straight 16981 12:10:45,416 --> 12:10:50,340 up import and then let's keep on moving 16982 12:10:47,640 --> 12:10:53,220 and now we want to wrap 16983 12:10:50,340 --> 12:10:55,740 our component 16984 12:10:53,220 --> 12:10:57,776 in the suspense 16985 12:10:55,740 --> 12:11:00,900 and we also want to provide some kind of 16986 12:10:57,776 --> 12:11:03,900 fallback so I'm gonna go 16987 12:11:00,900 --> 12:11:05,180 and I'll wrap the slow component in the 16988 12:11:03,900 --> 12:11:09,116 suspense 16989 12:11:05,180 --> 12:11:11,700 component then pullback prop basically 16990 12:11:09,116 --> 12:11:14,040 what do we want to display when we're 16991 12:11:11,700 --> 12:11:15,680 loading and I'll just cheat I'll grab 16992 12:11:14,040 --> 12:11:19,916 this value over here 16993 12:11:15,680 --> 12:11:22,320 and after that we want to place the 16994 12:11:19,916 --> 12:11:24,180 slow component inside of the suspense 16995 12:11:22,320 --> 12:11:26,040 and now notice something interesting 16996 12:11:24,180 --> 12:11:29,160 when I navigate to the big screen and 16997 12:11:26,040 --> 12:11:32,520 refresh you won't see anywhere the 16998 12:11:29,160 --> 12:11:33,416 import for the slow component JavaScript 16999 12:11:32,520 --> 12:11:36,596 code 17000 12:11:33,416 --> 12:11:38,700 only once I click here notice then I'm 17001 12:11:36,596 --> 12:11:41,820 importing which again is really really 17002 12:11:38,700 --> 12:11:45,060 awesome because it allows us to decrease 17003 12:11:41,820 --> 12:11:46,980 our initial load time 17004 12:11:45,060 --> 12:11:49,880 since we're not grabbing all of the 17005 12:11:46,980 --> 12:11:53,400 JavaScript code and only if the user 17006 12:11:49,880 --> 12:11:55,916 decides to interact with the component 17007 12:11:53,400 --> 12:11:58,256 only then we import so essentially this 17008 12:11:55,916 --> 12:12:00,660 is how we can lazy load our components 17009 12:11:58,256 --> 12:12:02,820 or in other words we only import the 17010 12:12:00,660 --> 12:12:05,276 functionality when there is a need for 17011 12:12:02,820 --> 12:12:09,616 it One Last Thing Before I Let You Go 17012 12:12:05,276 --> 12:12:12,060 typically we will wrap our entire return 17013 12:12:09,616 --> 12:12:15,180 in suspense 17014 12:12:12,060 --> 12:12:16,380 since that way we can lazy load multiple 17015 12:12:15,180 --> 12:12:19,800 components 17016 12:12:16,380 --> 12:12:20,640 now the syntax and the result is the 17017 12:12:19,800 --> 12:12:24,360 same 17018 12:12:20,640 --> 12:12:26,720 we will still provide fallback prop and 17019 12:12:24,360 --> 12:12:30,240 for all of the components we import 17020 12:12:26,720 --> 12:12:34,800 progressively the fallback value in this 17021 12:12:30,240 --> 12:12:37,200 case the loading text will be displayed 17022 12:12:34,800 --> 12:12:40,020 the reason why I wrapped only the slow 17023 12:12:37,200 --> 12:12:42,776 component in the previous example was 17024 12:12:40,020 --> 12:12:46,436 just to underscore which component we'll 17025 12:12:42,776 --> 12:12:49,200 get that fallback value all right up 17026 12:12:46,436 --> 12:12:51,540 next let's cover what is Redux toolkit 17027 12:12:49,200 --> 12:12:53,400 and why it's so useful 17028 12:12:51,540 --> 12:12:54,416 you see when it comes to bigger 17029 12:12:53,400 --> 12:12:55,916 applications 17030 12:12:54,416 --> 12:12:58,916 especially 17031 12:12:55,916 --> 12:13:01,380 if they're built by a team of Developers 17032 12:12:58,916 --> 12:13:04,860 one of the biggest challenges is State 17033 12:13:01,380 --> 12:13:08,160 Management yes context API is very 17034 12:13:04,860 --> 12:13:10,740 useful tool but it clearly has its 17035 12:13:08,160 --> 12:13:14,160 limits especially when we talk about big 17036 12:13:10,740 --> 12:13:16,740 applications with tons of features 17037 12:13:14,160 --> 12:13:19,200 and in order to solve the issue over the 17038 12:13:16,740 --> 12:13:22,740 time various State Management libraries 17039 12:13:19,200 --> 12:13:25,680 have been introduced by the community 17040 12:13:22,740 --> 12:13:28,140 and without a doubt for very very very 17041 12:13:25,680 --> 12:13:31,740 long time one of the most popular state 17042 12:13:28,140 --> 12:13:34,740 libraries has been Redux now before we 17043 12:13:31,740 --> 12:13:36,660 continue let me stress something Redux 17044 12:13:34,740 --> 12:13:40,740 is not part of official react again 17045 12:13:36,660 --> 12:13:44,520 Redux is not part of official react even 17046 12:13:40,740 --> 12:13:45,596 though they are used together very often 17047 12:13:44,520 --> 12:13:48,180 now 17048 12:13:45,596 --> 12:13:51,720 even though Redux solves a lot of 17049 12:13:48,180 --> 12:13:54,900 problems it also introduces some new 17050 12:13:51,720 --> 12:13:57,860 headaches in short it requires bunch of 17051 12:13:54,900 --> 12:14:00,840 annoying boilerplate and manual setup 17052 12:13:57,860 --> 12:14:03,416 especially when it comes to Advanced 17053 12:14:00,840 --> 12:14:06,596 features since you'll need to install 17054 12:14:03,416 --> 12:14:09,660 more libraries and add some more config 17055 12:14:06,596 --> 12:14:11,820 setup which over time especially if you 17056 12:14:09,660 --> 12:14:14,756 have to do it for every project just 17057 12:14:11,820 --> 12:14:18,000 becomes annoying and this is where Redux 17058 12:14:14,756 --> 12:14:20,360 toolkit comes into play so creators of 17059 12:14:18,000 --> 12:14:23,096 Redux notice the general need for 17060 12:14:20,360 --> 12:14:25,800 opinionated approach while setting up 17061 12:14:23,096 --> 12:14:28,500 Redux applications and as a result they 17062 12:14:25,800 --> 12:14:30,660 came up with Redux toolkit which 17063 12:14:28,500 --> 12:14:33,720 effectively is Redux with batteries 17064 12:14:30,660 --> 12:14:36,776 basically it means that all the popular 17065 12:14:33,720 --> 12:14:40,140 extra libraries and setups are built in 17066 12:14:36,776 --> 12:14:43,436 and do not require extra setup 17067 12:14:40,140 --> 12:14:44,340 as a result there is no time consuming 17068 12:14:43,436 --> 12:14:48,000 setup 17069 12:14:44,340 --> 12:14:49,740 and over time it speeds up our workflow 17070 12:14:48,000 --> 12:14:52,916 tremendously 17071 12:14:49,740 --> 12:14:56,700 so with Redux toolkit we can have our 17072 12:14:52,916 --> 12:15:00,720 cake and eat it too meaning we get all 17073 12:14:56,700 --> 12:15:03,300 of the benefits of Redux without any of 17074 12:15:00,720 --> 12:15:05,700 the manual labor and as always we'll 17075 12:15:03,300 --> 12:15:08,220 start with extensive tutorial where 17076 12:15:05,700 --> 12:15:11,096 we'll cover all of the major building 17077 12:15:08,220 --> 12:15:13,980 blocks of Redux toolkit during tutorial 17078 12:15:11,096 --> 12:15:16,320 we'll build this card application and in 17079 12:15:13,980 --> 12:15:19,560 the process we'll see all of the cool 17080 12:15:16,320 --> 12:15:23,040 Redux toolkit features in action so 17081 12:15:19,560 --> 12:15:25,860 things like store slice reducers action 17082 12:15:23,040 --> 12:15:28,080 creators and of course create async 17083 12:15:25,860 --> 12:15:30,840 thunk which handles a synchronous 17084 12:15:28,080 --> 12:15:34,080 functionality in order to follow you 17085 12:15:30,840 --> 12:15:37,616 will need a star and you can find it in 17086 12:15:34,080 --> 12:15:41,220 this GitHub repo so look for my profile 17087 12:15:37,616 --> 12:15:44,756 Jones milgar and the actual repo name is 17088 12:15:41,220 --> 12:15:46,980 Redux toolkit tutorial then the name of 17089 12:15:44,756 --> 12:15:50,880 the repo that you're looking for is 17090 12:15:46,980 --> 12:15:52,740 Redux toolkit tutorial and once you get 17091 12:15:50,880 --> 12:15:54,180 here just pick the option that makes the 17092 12:15:52,740 --> 12:15:55,980 most sense to you 17093 12:15:54,180 --> 12:15:59,460 I think in my case I'm gonna go for 17094 12:15:55,980 --> 12:16:02,160 download zip option then 17095 12:15:59,460 --> 12:16:05,820 I want to crack it open 17096 12:16:02,160 --> 12:16:07,020 and in the folder you'll find two more 17097 12:16:05,820 --> 12:16:09,720 folders 17098 12:16:07,020 --> 12:16:12,000 the final as well as the starter and 17099 12:16:09,720 --> 12:16:14,096 needless to say that in the final one 17100 12:16:12,000 --> 12:16:16,860 you'll find the complete source code and 17101 12:16:14,096 --> 12:16:20,276 then in a star is where we'll do all of 17102 12:16:16,860 --> 12:16:22,080 our work so let me take this sucker out 17103 12:16:20,276 --> 12:16:25,256 let me open up 17104 12:16:22,080 --> 12:16:28,380 the text editor Visual Studio code 17105 12:16:25,256 --> 12:16:31,916 oh right away set it side by side now 17106 12:16:28,380 --> 12:16:34,740 this is a standard react application I 17107 12:16:31,916 --> 12:16:37,200 used create react app the only 17108 12:16:34,740 --> 12:16:40,256 difference is that I removed some 17109 12:16:37,200 --> 12:16:42,660 boilerplate code and I also add some 17110 12:16:40,256 --> 12:16:47,040 resources so in here what we want to do 17111 12:16:42,660 --> 12:16:48,660 is run npm install and also npm start so 17112 12:16:47,040 --> 12:16:53,400 npm install 17113 12:16:48,660 --> 12:16:54,776 and PM start and there is an app so at 17114 12:16:53,400 --> 12:16:57,720 the moment notice there's only having 17115 12:16:54,776 --> 12:17:00,240 two we have some car items which we'll 17116 12:16:57,720 --> 12:17:01,980 use in the beginning as our data and 17117 12:17:00,240 --> 12:17:03,960 also a few icons 17118 12:17:01,980 --> 12:17:08,300 I'll discuss them a little bit later 17119 12:17:03,960 --> 12:17:12,660 index CSS all our CSS and also of course 17120 12:17:08,300 --> 12:17:15,116 index.js so once you get the repo once 17121 12:17:12,660 --> 12:17:18,416 you get the star install all the 17122 12:17:15,116 --> 12:17:22,560 dependencies Run npm start you should 17123 12:17:18,416 --> 12:17:25,320 see in the browser they hear into with I 17124 12:17:22,560 --> 12:17:27,776 believe it was Redux toolkit and if you 17125 12:17:25,320 --> 12:17:31,200 do we're in good shape and we can 17126 12:17:27,776 --> 12:17:35,220 proceed to The Next Step a quick update 17127 12:17:31,200 --> 12:17:37,500 I upgraded this project to react 18. 17128 12:17:35,220 --> 12:17:41,400 which also means that in the index.js 17129 12:17:37,500 --> 12:17:43,740 you'll find create root method instead 17130 12:17:41,400 --> 12:17:45,776 of react Dom render 17131 12:17:43,740 --> 12:17:46,860 which was used when I recorded this 17132 12:17:45,776 --> 12:17:49,436 project 17133 12:17:46,860 --> 12:17:52,740 now let me be very clear 17134 12:17:49,436 --> 12:17:55,256 all of the upcoming content is still 17135 12:17:52,740 --> 12:17:58,380 valid and up to date 17136 12:17:55,256 --> 12:18:00,360 only few lines of code in the index.js 17137 12:17:58,380 --> 12:18:03,596 are different 17138 12:18:00,360 --> 12:18:06,960 since the latest react version uses 17139 12:18:03,596 --> 12:18:10,740 create root method instead of react Dom 17140 12:18:06,960 --> 12:18:13,020 render and yes in a few videos when we 17141 12:18:10,740 --> 12:18:16,380 set up Redux store 17142 12:18:13,020 --> 12:18:21,060 we still want to wrap the app component 17143 12:18:16,380 --> 12:18:24,776 in the provider just like I showcase 17144 12:18:21,060 --> 12:18:27,660 during the videos so again let me repeat 17145 12:18:24,776 --> 12:18:30,300 all of the upcoming content is still 17146 12:18:27,660 --> 12:18:34,740 valid and up to date the only difference 17147 12:18:30,300 --> 12:18:39,116 is the react version now this app uses 17148 12:18:34,740 --> 12:18:42,180 react 18. in the star you'll also find a 17149 12:18:39,116 --> 12:18:44,640 readme file where I laid out all the 17150 12:18:42,180 --> 12:18:48,180 steps we're going to take as well as 17151 12:18:44,640 --> 12:18:51,776 some useful links and first if you want 17152 12:18:48,180 --> 12:18:53,936 to find more info about Redux token you 17153 12:18:51,776 --> 12:18:57,776 can always utilize this link so this one 17154 12:18:53,936 --> 12:19:00,660 goes directly to their docs and we have 17155 12:18:57,776 --> 12:19:03,360 few ways that we can add Redux toolkit 17156 12:19:00,660 --> 12:19:05,040 to our project first we can go with 17157 12:19:03,360 --> 12:19:07,800 straight up template 17158 12:19:05,040 --> 12:19:09,720 so in that case instead of going npx 17159 12:19:07,800 --> 12:19:12,840 create react app and then the app name 17160 12:19:09,720 --> 12:19:16,020 we also add this hyphen hyphen and then 17161 12:19:12,840 --> 12:19:19,020 template and Redux now from my 17162 12:19:16,020 --> 12:19:21,840 experience they add way too much 17163 12:19:19,020 --> 12:19:25,740 boilerplate so essentially I always 17164 12:19:21,840 --> 12:19:28,560 avoid that and in general I just go with 17165 12:19:25,740 --> 12:19:31,560 the Redux toolkit so I set up the 17166 12:19:28,560 --> 12:19:34,740 application let's say with create react 17167 12:19:31,560 --> 12:19:38,460 app and then install the toolkit 17168 12:19:34,740 --> 12:19:42,900 separately and we do that with npm 17169 12:19:38,460 --> 12:19:46,080 install and then add Redux JS and then 17170 12:19:42,900 --> 12:19:48,000 forward slash toolkit and also We'll add 17171 12:19:46,080 --> 12:19:50,400 react Redux 17172 12:19:48,000 --> 12:19:52,380 and I'll talk about it in a second now 17173 12:19:50,400 --> 12:19:53,520 let me just quickly mention that if for 17174 12:19:52,380 --> 12:19:56,096 some reason 17175 12:19:53,520 --> 12:19:59,400 you get some issues when you just run 17176 12:19:56,096 --> 12:20:01,740 MPX create react app and again it's not 17177 12:19:59,400 --> 12:20:04,740 specific to this template one if 17178 12:20:01,740 --> 12:20:07,680 something goes bananas with npx you can 17179 12:20:04,740 --> 12:20:10,380 always try to go with create react app 17180 12:20:07,680 --> 12:20:14,220 and then the latest one so this is just 17181 12:20:10,380 --> 12:20:15,800 a side note now back to the toolkit so 17182 12:20:14,220 --> 12:20:19,500 when we install 17183 12:20:15,800 --> 12:20:22,560 reduxjs toolkit we actually install fuel 17184 12:20:19,500 --> 12:20:25,500 libraries so we install the Redux which 17185 12:20:22,560 --> 12:20:27,776 is going to be our core Library we also 17186 12:20:25,500 --> 12:20:32,040 install Emer which is going to allow us 17187 12:20:27,776 --> 12:20:35,160 to mutate the state we install Redux 17188 12:20:32,040 --> 12:20:38,400 thunk which will handle the async 17189 12:20:35,160 --> 12:20:42,540 actions and also we install reselect 17190 12:20:38,400 --> 12:20:44,820 which will simplify the reducer setup so 17191 12:20:42,540 --> 12:20:47,096 these things of course will only make 17192 12:20:44,820 --> 12:20:49,436 sense if you already have worked with 17193 12:20:47,096 --> 12:20:51,416 the Redux so this is already provided 17194 12:20:49,436 --> 12:20:53,340 for us and of course we'll see all of 17195 12:20:51,416 --> 12:20:56,460 those things in action and if you didn't 17196 12:20:53,340 --> 12:20:59,400 work with the Redux before don't worry 17197 12:20:56,460 --> 12:21:02,460 of course I will explain all of them and 17198 12:20:59,400 --> 12:21:05,276 also as an extra we get right away Redux 17199 12:21:02,460 --> 12:21:07,860 devtools and also combined reducers 17200 12:21:05,276 --> 12:21:09,776 right from the get-go again if you 17201 12:21:07,860 --> 12:21:12,116 haven't worked with Redux I totally 17202 12:21:09,776 --> 12:21:14,220 understand if none of those things make 17203 12:21:12,116 --> 12:21:17,640 sense but don't worry we'll talk about 17204 12:21:14,220 --> 12:21:21,500 them later now why we also install this 17205 12:21:17,640 --> 12:21:25,320 package the react Redux well you see 17206 12:21:21,500 --> 12:21:30,300 Redux can be used with any front-end 17207 12:21:25,320 --> 12:21:32,936 framework it's not specific to react in 17208 12:21:30,300 --> 12:21:35,400 order to connect our application to the 17209 12:21:32,936 --> 12:21:38,400 Redux this is where we'll use the 17210 12:21:35,400 --> 12:21:41,096 package by the name of react Redux now 17211 12:21:38,400 --> 12:21:43,740 needless to say that of course if you'll 17212 12:21:41,096 --> 12:21:47,400 take a look at the packages on 17213 12:21:43,740 --> 12:21:49,436 you will see the both packages you'll 17214 12:21:47,400 --> 12:21:52,020 see the toolkit as well as the react 17215 12:21:49,436 --> 12:21:54,180 Redux but just so you don't think that 17216 12:21:52,020 --> 12:21:57,900 there is some funny business going on 17217 12:21:54,180 --> 12:22:01,200 let me copy this line of code let me 17218 12:21:57,900 --> 12:22:04,200 install both packages and once the 17219 12:22:01,200 --> 12:22:07,740 install is complete we can start setting 17220 12:22:04,200 --> 12:22:10,680 up our Redux store so the install is 17221 12:22:07,740 --> 12:22:13,436 done we go with npm start and if we take 17222 12:22:10,680 --> 12:22:17,936 a look at the package Json you will find 17223 12:22:13,436 --> 12:22:19,256 the toolkit as well as react reduct and 17224 12:22:17,936 --> 12:22:22,256 with this in place now we can start 17225 12:22:19,256 --> 12:22:24,840 setting up our application with Redux 17226 12:22:22,256 --> 12:22:27,720 toolkit okay and once we have installed 17227 12:22:24,840 --> 12:22:31,340 all dependencies up next I want to set 17228 12:22:27,720 --> 12:22:35,460 up the store and you can think of store 17229 12:22:31,340 --> 12:22:37,800 as the entire state for your application 17230 12:22:35,460 --> 12:22:41,220 so remember previously we used context 17231 12:22:37,800 --> 12:22:44,220 API now in this case we'll use toolkit 17232 12:22:41,220 --> 12:22:46,616 and we'll set up the store and the 17233 12:22:44,220 --> 12:22:49,800 syntax is following where first we want 17234 12:22:46,616 --> 12:22:52,916 to create store.js somewhere in the 17235 12:22:49,800 --> 12:22:56,160 source we want to import configure store 17236 12:22:52,916 --> 12:22:57,840 function from the toolkit and yes in 17237 12:22:56,160 --> 12:23:00,060 order to speed this up I'll just 17238 12:22:57,840 --> 12:23:01,200 reference it as toolkit please keep in 17239 12:23:00,060 --> 12:23:04,080 mind that of course I'm talking about 17240 12:23:01,200 --> 12:23:05,276 the Redux token and then in this 17241 12:23:04,080 --> 12:23:08,936 function 17242 12:23:05,276 --> 12:23:11,660 we want to pass in the object and in the 17243 12:23:08,936 --> 12:23:14,400 object there is going to be a reducer 17244 12:23:11,660 --> 12:23:17,220 property which is going to be object 17245 12:23:14,400 --> 12:23:20,820 itself and then in here we'll set up our 17246 12:23:17,220 --> 12:23:24,776 features and I fully understand that I 17247 12:23:20,820 --> 12:23:26,580 mean this can look somewhat fuzzy don't 17248 12:23:24,776 --> 12:23:28,616 worry as we start setting up the 17249 12:23:26,580 --> 12:23:31,560 features you'll see everything in action 17250 12:23:28,616 --> 12:23:34,380 so for the time being just bear with me 17251 12:23:31,560 --> 12:23:36,360 and then once we have the store we want 17252 12:23:34,380 --> 12:23:39,000 to export that and this is the case 17253 12:23:36,360 --> 12:23:40,740 where you can export this as default 17254 12:23:39,000 --> 12:23:43,616 so the difference of course is just 17255 12:23:40,740 --> 12:23:46,200 export default and then the store or you 17256 12:23:43,616 --> 12:23:47,720 can go with name export and then we want 17257 12:23:46,200 --> 12:23:52,200 to go back to 17258 12:23:47,720 --> 12:23:55,980 index.js we want to import store from 17259 12:23:52,200 --> 12:23:58,680 the store file as well as the provider 17260 12:23:55,980 --> 12:24:02,040 from react Redux like I said this is the 17261 12:23:58,680 --> 12:24:06,000 library that connects Redux store or 17262 12:24:02,040 --> 12:24:08,040 Redux to our application and similarly 17263 12:24:06,000 --> 12:24:11,640 to context API 17264 12:24:08,040 --> 12:24:13,800 we want to wrap our entire application 17265 12:24:11,640 --> 12:24:16,680 so we're going to grab this provider 17266 12:24:13,800 --> 12:24:20,160 there's a store prop and we just want to 17267 12:24:16,680 --> 12:24:24,300 pass in the store coming from the store 17268 12:24:20,160 --> 12:24:28,140 file so let's get cracking first 17269 12:24:24,300 --> 12:24:31,680 I'm gonna go with store Js 17270 12:24:28,140 --> 12:24:34,680 in here let's import configure store so 17271 12:24:31,680 --> 12:24:36,840 import we're looking for configure store 17272 12:24:34,680 --> 12:24:38,756 notice right away I have suggestion for 17273 12:24:36,840 --> 12:24:41,700 that and then let's set up that store 17274 12:24:38,756 --> 12:24:43,560 like I said at the moment it might look 17275 12:24:41,700 --> 12:24:45,660 somewhat funky the moment we'll set up 17276 12:24:43,560 --> 12:24:48,240 some features it will make sense so 17277 12:24:45,660 --> 12:24:50,400 let's go here with const and of course 17278 12:24:48,240 --> 12:24:52,680 I'm adding export as well I'm going to 17279 12:24:50,400 --> 12:24:54,840 store that is equal to configure store 17280 12:24:52,680 --> 12:24:57,596 let's pass in the object let's set up 17281 12:24:54,840 --> 12:24:59,580 the reducer is an object itself and for 17282 12:24:57,596 --> 12:25:01,436 time being it's going to be empty and 17283 12:24:59,580 --> 12:25:04,140 most likely we'll get the warning in a 17284 12:25:01,436 --> 12:25:06,240 console don't worry about it again 17285 12:25:04,140 --> 12:25:09,660 all of this functionality is coming up 17286 12:25:06,240 --> 12:25:12,720 and then let's navigate to index.js 17287 12:25:09,660 --> 12:25:15,900 Let's import both of the things let's go 17288 12:25:12,720 --> 12:25:17,756 with import then store 17289 12:25:15,900 --> 12:25:20,040 um this is coming from the store file of 17290 12:25:17,756 --> 12:25:22,680 course and also we want to get that 17291 12:25:20,040 --> 12:25:25,140 provider so import and then we're 17292 12:25:22,680 --> 12:25:28,680 looking for provider now that is coming 17293 12:25:25,140 --> 12:25:31,020 from react Redux and now let's wrap our 17294 12:25:28,680 --> 12:25:34,140 entire application so let's say provider 17295 12:25:31,020 --> 12:25:37,680 here let's close it out let's wrap our 17296 12:25:34,140 --> 12:25:40,560 app and then we want to set up a store 17297 12:25:37,680 --> 12:25:41,460 prop and we want to set it equal to our 17298 12:25:40,560 --> 12:25:44,820 store 17299 12:25:41,460 --> 12:25:47,400 and if nothing breaks we are heading in 17300 12:25:44,820 --> 12:25:50,580 the right direction so then we can set 17301 12:25:47,400 --> 12:25:52,916 up the slice again like I said 17302 12:25:50,580 --> 12:25:56,580 if you see this warning don't worry 17303 12:25:52,916 --> 12:25:58,916 we'll fix it in a second beautiful and 17304 12:25:56,580 --> 12:26:02,460 once we have the store in place now 17305 12:25:58,916 --> 12:26:04,616 let's set up the slice and I fully 17306 12:26:02,460 --> 12:26:06,960 understand that this name is totally 17307 12:26:04,616 --> 12:26:08,820 funky you're like what slice are you 17308 12:26:06,960 --> 12:26:12,840 talking about and I want you to think of 17309 12:26:08,820 --> 12:26:14,880 slice as the feature of your application 17310 12:26:12,840 --> 12:26:16,800 so if we take a look at the complete 17311 12:26:14,880 --> 12:26:18,660 project you'll notice that yes we have 17312 12:26:16,800 --> 12:26:21,840 the card functionality 17313 12:26:18,660 --> 12:26:25,140 but we also have the model 17314 12:26:21,840 --> 12:26:27,660 and you can think of it as two features 17315 12:26:25,140 --> 12:26:30,540 our application so we have one feature 17316 12:26:27,660 --> 12:26:33,840 for the model and the second one is 17317 12:26:30,540 --> 12:26:36,660 going to be for our card and of course 17318 12:26:33,840 --> 12:26:38,936 the bigger our application 17319 12:26:36,660 --> 12:26:42,660 the more features we're going to have 17320 12:26:38,936 --> 12:26:45,540 and in the Redux toolkit land it is 17321 12:26:42,660 --> 12:26:49,160 called slice now in order to set up 17322 12:26:45,540 --> 12:26:52,256 slice a common convention is to set up 17323 12:26:49,160 --> 12:26:54,840 features folder but of course naming is 17324 12:26:52,256 --> 12:26:56,756 always up to you but then whatever is 17325 12:26:54,840 --> 12:26:58,200 going to be the name of the feature so 17326 12:26:56,756 --> 12:27:00,960 in my case it's going to be card that's 17327 12:26:58,200 --> 12:27:04,256 why I'll set up a card folder and then 17328 12:27:00,960 --> 12:27:05,880 we want to set up a file again like I 17329 12:27:04,256 --> 12:27:08,040 keep mentioning the naming is up to you 17330 12:27:05,880 --> 12:27:11,700 but again typically you'll go with 17331 12:27:08,040 --> 12:27:12,960 whatever feature and then slice JS now 17332 12:27:11,700 --> 12:27:15,900 in there 17333 12:27:12,960 --> 12:27:17,700 you'll want to get create slash and 17334 12:27:15,900 --> 12:27:20,520 don't worry about this one that create 17335 12:27:17,700 --> 12:27:23,460 async thunk let's say not I'll remove it 17336 12:27:20,520 --> 12:27:27,000 worry about that later and then we want 17337 12:27:23,460 --> 12:27:29,460 to invoke that function create slash we 17338 12:27:27,000 --> 12:27:30,840 want to give it a name again it's 17339 12:27:29,460 --> 12:27:32,820 totally up to you but in my case I'm 17340 12:27:30,840 --> 12:27:35,160 going to go with card and then we have 17341 12:27:32,820 --> 12:27:37,800 initial State Property 17342 12:27:35,160 --> 12:27:39,660 where we just set up whatever state we 17343 12:27:37,800 --> 12:27:41,700 want in my case I'll set it up as a 17344 12:27:39,660 --> 12:27:44,400 separate object and in here I'm going to 17345 12:27:41,700 --> 12:27:46,256 go with cart items with an amount so how 17346 12:27:44,400 --> 12:27:48,840 many items I have in a cart what is the 17347 12:27:46,256 --> 12:27:50,580 card total both of them zero and then is 17348 12:27:48,840 --> 12:27:54,000 loading true because eventually we will 17349 12:27:50,580 --> 12:27:55,020 load this from the API then I'll log the 17350 12:27:54,000 --> 12:27:58,080 sucker 17351 12:27:55,020 --> 12:28:00,416 the card slice and in there we'll have 17352 12:27:58,080 --> 12:28:01,800 the reducer and of course I'll talk 17353 12:28:00,416 --> 12:28:04,616 about this one a little bit later once 17354 12:28:01,800 --> 12:28:07,980 we get there and eventually what we want 17355 12:28:04,616 --> 12:28:10,616 to do is in the store we want to import 17356 12:28:07,980 --> 12:28:14,400 cart reducer so that's going to be the 17357 12:28:10,616 --> 12:28:17,220 function that controls this state in our 17358 12:28:14,400 --> 12:28:19,500 slides so think of our application we'll 17359 12:28:17,220 --> 12:28:22,800 just split up the functionality where 17360 12:28:19,500 --> 12:28:26,160 this slice is just responsible for the 17361 12:28:22,800 --> 12:28:27,416 cart and this reducer specifically is 17362 12:28:26,160 --> 12:28:30,116 going to modify it whatever 17363 12:28:27,416 --> 12:28:31,860 functionality we set up and then in that 17364 12:28:30,116 --> 12:28:33,540 reducer that we set up in the last video 17365 12:28:31,860 --> 12:28:36,416 notice there's a complaint that there's 17366 12:28:33,540 --> 12:28:38,640 no valve reducer now we want to set up a 17367 12:28:36,416 --> 12:28:41,096 key now key name is really up to you 17368 12:28:38,640 --> 12:28:42,360 just like everything else and I know 17369 12:28:41,096 --> 12:28:43,740 that you're probably sick of hearing 17370 12:28:42,360 --> 12:28:46,320 this but I just want to make this clear 17371 12:28:43,740 --> 12:28:48,720 where this is totally up to you you can 17372 12:28:46,320 --> 12:28:51,720 call this banana pudding if you want 17373 12:28:48,720 --> 12:28:53,580 just remember that later once we start 17374 12:28:51,720 --> 12:28:55,980 setting up our functionality yes of 17375 12:28:53,580 --> 12:28:57,660 course we'll have to access this name so 17376 12:28:55,980 --> 12:29:00,960 therefore I'm going to go with cart and 17377 12:28:57,660 --> 12:29:03,720 I want to set it equal to reducer so the 17378 12:29:00,960 --> 12:29:06,240 function that we'll have access to 17379 12:29:03,720 --> 12:29:08,400 and a function that will allow us to 17380 12:29:06,240 --> 12:29:11,220 control this 17381 12:29:08,400 --> 12:29:14,160 piece of State for this specific 17382 12:29:11,220 --> 12:29:16,916 functionality hopefully this is clear so 17383 12:29:14,160 --> 12:29:19,140 now let's go to Source we want to create 17384 12:29:16,916 --> 12:29:22,256 new folder like I said common convention 17385 12:29:19,140 --> 12:29:24,660 we'll call this features so features our 17386 12:29:22,256 --> 12:29:27,776 application in our case cart and model 17387 12:29:24,660 --> 12:29:29,756 then let's create another folder and 17388 12:29:27,776 --> 12:29:31,560 we're gonna go here with cart then 17389 12:29:29,756 --> 12:29:34,860 inside of the cart let's go with new 17390 12:29:31,560 --> 12:29:38,220 file and we're gonna go with cart slice 17391 12:29:34,860 --> 12:29:41,276 Js so once we're here we want to import 17392 12:29:38,220 --> 12:29:44,460 we want to import and we'll say create 17393 12:29:41,276 --> 12:29:47,160 and slice now for some reason it does 17394 12:29:44,460 --> 12:29:49,980 not oh yep it does so I have the 17395 12:29:47,160 --> 12:29:53,220 suggestion and let's right away invoke 17396 12:29:49,980 --> 12:29:56,220 it I'm going to call this const card 17397 12:29:53,220 --> 12:29:57,660 and slice and that is equal to create 17398 12:29:56,220 --> 12:29:59,460 slice 17399 12:29:57,660 --> 12:30:01,560 it's a function we pass in the object 17400 12:29:59,460 --> 12:30:02,520 and here we can set up a bunch of 17401 12:30:01,560 --> 12:30:05,096 properties 17402 12:30:02,520 --> 12:30:08,160 so first we want to set up the name 17403 12:30:05,096 --> 12:30:10,320 so I'm gonna go here with card then we 17404 12:30:08,160 --> 12:30:12,300 want to go with that initial State this 17405 12:30:10,320 --> 12:30:14,400 is the case since I have more properties 17406 12:30:12,300 --> 12:30:16,560 might as well set it up as a separate 17407 12:30:14,400 --> 12:30:17,820 object so let's set up here initial 17408 12:30:16,560 --> 12:30:20,040 state 17409 12:30:17,820 --> 12:30:22,140 and then we'll add card items which 17410 12:30:20,040 --> 12:30:24,300 initially is going to be empty array but 17411 12:30:22,140 --> 12:30:26,820 then we'll take a look at multiple 17412 12:30:24,300 --> 12:30:29,040 approaches to local data and then the 17413 12:30:26,820 --> 12:30:31,916 data coming from the API I also want to 17414 12:30:29,040 --> 12:30:34,500 go with amount so how many items I have 17415 12:30:31,916 --> 12:30:36,480 in the cart and Please be aware that I'm 17416 12:30:34,500 --> 12:30:39,840 not just talking about the products I 17417 12:30:36,480 --> 12:30:42,596 have in my card I'm actually 17418 12:30:39,840 --> 12:30:46,140 looking for the item count 17419 12:30:42,596 --> 12:30:48,416 so how many items of that specific 17420 12:30:46,140 --> 12:30:50,460 product I have and this is going to be 17421 12:30:48,416 --> 12:30:52,320 that amount hopefully that is clear then 17422 12:30:50,460 --> 12:30:54,660 we want to go with total so Total Money 17423 12:30:52,320 --> 12:30:57,480 both of them zero initially and that is 17424 12:30:54,660 --> 12:31:00,060 loading because eventually we'll pull 17425 12:30:57,480 --> 12:31:02,220 this from the API so that's going to be 17426 12:31:00,060 --> 12:31:03,360 our initial State once we save notice 17427 12:31:02,220 --> 12:31:06,720 right away 17428 12:31:03,360 --> 12:31:08,820 initial studio in here references the 17429 12:31:06,720 --> 12:31:11,276 initial property then we want to log 17430 12:31:08,820 --> 12:31:14,460 this sucker and we want to quickly 17431 12:31:11,276 --> 12:31:16,800 import install.js now in a second we'll 17432 12:31:14,460 --> 12:31:19,256 do more proper setup I just want to 17433 12:31:16,800 --> 12:31:22,020 showcase what we have in the card slice 17434 12:31:19,256 --> 12:31:23,936 so let me save it then I'll navigate 17435 12:31:22,020 --> 12:31:26,700 back to the store and for time being 17436 12:31:23,936 --> 12:31:29,096 let's just import the entire file like I 17437 12:31:26,700 --> 12:31:32,460 said more proper setup is coming let's 17438 12:31:29,096 --> 12:31:35,700 just go here with the features 17439 12:31:32,460 --> 12:31:39,000 red card and then card slice and once we 17440 12:31:35,700 --> 12:31:42,000 import of course we right away invoke 17441 12:31:39,000 --> 12:31:45,480 the file and here we'll see that console 17442 12:31:42,000 --> 12:31:47,580 log so we'll have some actions here 17443 12:31:45,480 --> 12:31:50,220 something that we'll discuss a little 17444 12:31:47,580 --> 12:31:53,580 bit later once we set up some reducers 17445 12:31:50,220 --> 12:31:55,500 we have case reducers again something we 17446 12:31:53,580 --> 12:31:58,140 don't need to worry about right now get 17447 12:31:55,500 --> 12:32:00,776 initial State again a function that gets 17448 12:31:58,140 --> 12:32:02,700 the state name and then notice all the 17449 12:32:00,776 --> 12:32:05,096 way in the bottom we have that reducer 17450 12:32:02,700 --> 12:32:09,596 so this reducer 17451 12:32:05,096 --> 12:32:12,840 is the one that is going to control that 17452 12:32:09,596 --> 12:32:14,640 state in this slice that's why we want 17453 12:32:12,840 --> 12:32:17,276 to export that we want to export this 17454 12:32:14,640 --> 12:32:20,520 and in the store we want to come up with 17455 12:32:17,276 --> 12:32:22,080 a key and set it equal to that reducer 17456 12:32:20,520 --> 12:32:24,960 so let's try this out I'm going to go 17457 12:32:22,080 --> 12:32:27,540 with cart then instead of just getting 17458 12:32:24,960 --> 12:32:30,300 the file I'll actually look for that 17459 12:32:27,540 --> 12:32:33,300 reducer but I'll set it up as a default 17460 12:32:30,300 --> 12:32:34,916 one so for time being I'll comment this 17461 12:32:33,300 --> 12:32:36,900 one out we'll come back to this one 17462 12:32:34,916 --> 12:32:40,020 because again we'll talk about these 17463 12:32:36,900 --> 12:32:42,596 actions eventually and now I just want 17464 12:32:40,020 --> 12:32:45,360 to export that reducer now there's 17465 12:32:42,596 --> 12:32:47,340 Million Ways How we can export Us in 17466 12:32:45,360 --> 12:32:51,000 this case I'm going to go export default 17467 12:32:47,340 --> 12:32:53,460 and then cart slice dot reducer again 17468 12:32:51,000 --> 12:32:56,640 this is an object and this is a property 17469 12:32:53,460 --> 12:32:59,880 so let's export this event in the store 17470 12:32:56,640 --> 12:33:04,140 we wanna import so I'm gonna go with 17471 12:32:59,880 --> 12:33:05,820 cart not slice but reducer again if you 17472 12:33:04,140 --> 12:33:08,276 want to name this differently of course 17473 12:33:05,820 --> 12:33:11,820 you can and then we're going with from 17474 12:33:08,276 --> 12:33:15,596 and now go with cart equals to card 17475 12:33:11,820 --> 12:33:17,640 reducer and we shouldn't see any error 17476 12:33:15,596 --> 12:33:20,936 messages anymore because now we have the 17477 12:33:17,640 --> 12:33:24,300 proper set up so now we can take a look 17478 12:33:20,936 --> 12:33:26,220 how we can access the data first from 17479 12:33:24,300 --> 12:33:28,916 that initial State and then of course 17480 12:33:26,220 --> 12:33:31,256 eventually also take a look at how we 17481 12:33:28,916 --> 12:33:33,480 can set up functionality to control this 17482 12:33:31,256 --> 12:33:36,300 but our first step is going to be 17483 12:33:33,480 --> 12:33:38,880 accessing this data okay and before we 17484 12:33:36,300 --> 12:33:41,880 go any further let's also quickly 17485 12:33:38,880 --> 12:33:44,820 install Redux devtools and the good news 17486 12:33:41,880 --> 12:33:48,240 is that as far as the code we don't need 17487 12:33:44,820 --> 12:33:51,596 to add any configuration basically the 17488 12:33:48,240 --> 12:33:53,700 moment we install Redux toolkit we're 17489 12:33:51,596 --> 12:33:56,040 good to go however when it comes to 17490 12:33:53,700 --> 12:33:58,200 browser we do need to install extra 17491 12:33:56,040 --> 12:34:00,776 extension and the extension you're 17492 12:33:58,200 --> 12:34:03,320 looking for is this one Redux Dev tools 17493 12:34:00,776 --> 12:34:06,480 and in the Chrome if you want to install 17494 12:34:03,320 --> 12:34:09,360 extension just go to your extensions 17495 12:34:06,480 --> 12:34:13,560 and then more specifically look for open 17496 12:34:09,360 --> 12:34:16,616 Chrome web store and then in here look 17497 12:34:13,560 --> 12:34:19,616 for the Redux one not the Red X 4K 17498 12:34:16,616 --> 12:34:22,080 apologies Redux so this is the extension 17499 12:34:19,616 --> 12:34:24,960 you're looking for and once you do that 17500 12:34:22,080 --> 12:34:27,416 let me open this up in the big browser 17501 12:34:24,960 --> 12:34:31,020 so that's my localhost 3000 17502 12:34:27,416 --> 12:34:33,240 you'll notice a tab it Redux Tab and 17503 12:34:31,020 --> 12:34:36,060 here we'll be able to find bunch of 17504 12:34:33,240 --> 12:34:38,220 useful info now at the moment 17505 12:34:36,060 --> 12:34:40,020 I mean we have a clean application we 17506 12:34:38,220 --> 12:34:42,540 only have the state but yes eventually 17507 12:34:40,020 --> 12:34:45,660 once we set up the reducers the actions 17508 12:34:42,540 --> 12:34:49,320 and all that yes this is going to be a 17509 12:34:45,660 --> 12:34:50,820 very useful tool now at the moment the 17510 12:34:49,320 --> 12:34:53,820 only thing that I want to showcase that 17511 12:34:50,820 --> 12:34:56,756 this is our state notice that's my card 17512 12:34:53,820 --> 12:34:58,860 card items amount total and is loading 17513 12:34:56,756 --> 12:35:01,200 and yes of course 17514 12:34:58,860 --> 12:35:05,040 in our store 17515 12:35:01,200 --> 12:35:07,560 once we add more reducers 17516 12:35:05,040 --> 12:35:09,000 all of them will meet over here and 17517 12:35:07,560 --> 12:35:11,520 that's why this tool is very useful 17518 12:35:09,000 --> 12:35:15,360 because as will dispatch our actions 17519 12:35:11,520 --> 12:35:18,540 we'll clearly see how our state changes 17520 12:35:15,360 --> 12:35:21,900 beautiful and once we have set up 17521 12:35:18,540 --> 12:35:25,320 the Redux details up next let's see how 17522 12:35:21,900 --> 12:35:27,480 we can access that initial state in any 17523 12:35:25,320 --> 12:35:30,540 of the components 17524 12:35:27,480 --> 12:35:31,436 so we have the initial state in the 17525 12:35:30,540 --> 12:35:33,960 slice 17526 12:35:31,436 --> 12:35:36,500 and let's say that I want to set up a 17527 12:35:33,960 --> 12:35:38,520 navbar component where I want to access 17528 12:35:36,500 --> 12:35:41,096 the amount 17529 12:35:38,520 --> 12:35:43,380 and the setup is following where we do 17530 12:35:41,096 --> 12:35:46,680 want to create the components folder 17531 12:35:43,380 --> 12:35:49,800 then navbar.js and in there we'll import 17532 12:35:46,680 --> 12:35:52,560 cart icon so that's the component that's 17533 12:35:49,800 --> 12:35:55,436 coming from the icons folder and I'll 17534 12:35:52,560 --> 12:35:57,116 discuss how we can set up the component 17535 12:35:55,436 --> 12:35:59,640 e in the following video because at the 17536 12:35:57,116 --> 12:36:02,096 moment I do want to focus on the state 17537 12:35:59,640 --> 12:36:05,756 value access and then we want to import 17538 12:36:02,096 --> 12:36:08,756 use selector so that's a hook coming 17539 12:36:05,756 --> 12:36:10,860 from react reduction 17540 12:36:08,756 --> 12:36:13,560 and then in the navbar component before 17541 12:36:10,860 --> 12:36:16,740 the return we want to invoke use 17542 12:36:13,560 --> 12:36:19,400 selector and the selector is looking for 17543 12:36:16,740 --> 12:36:23,880 one thing it's looking for the function 17544 12:36:19,400 --> 12:36:27,116 and as a parameter we get access to the 17545 12:36:23,880 --> 12:36:28,740 entire state so we're talking about the 17546 12:36:27,116 --> 12:36:31,916 entire store 17547 12:36:28,740 --> 12:36:35,756 again in our case we just have the card 17548 12:36:31,916 --> 12:36:39,180 but eventually we'll add more reducers 17549 12:36:35,756 --> 12:36:40,560 over here so since this is a parameter 17550 12:36:39,180 --> 12:36:43,560 of course we can call it whatever we 17551 12:36:40,560 --> 12:36:45,840 want so you'll see probably state but 17552 12:36:43,560 --> 12:36:48,060 actually my preference is to call this a 17553 12:36:45,840 --> 12:36:50,936 store because that signals to me that 17554 12:36:48,060 --> 12:36:52,916 that is entire store so the entire state 17555 12:36:50,936 --> 12:36:54,900 of my application and then more 17556 12:36:52,916 --> 12:36:56,276 specifically I'm looking for DOT card 17557 12:36:54,900 --> 12:36:58,140 wire 17558 12:36:56,276 --> 12:37:00,720 because that's the name of the property 17559 12:36:58,140 --> 12:37:02,936 over here and then initial state if you 17560 12:37:00,720 --> 12:37:04,916 remember we do have the amount property 17561 12:37:02,936 --> 12:37:06,960 and of course there's Million Ways How 17562 12:37:04,916 --> 12:37:08,756 we can set this up for example we can 17563 12:37:06,960 --> 12:37:11,096 just return from this function this 17564 12:37:08,756 --> 12:37:14,276 amount or since in this case we're 17565 12:37:11,096 --> 12:37:17,400 returning the entire object we can to 17566 12:37:14,276 --> 12:37:20,700 structure it so let's try to set this up 17567 12:37:17,400 --> 12:37:23,116 where in the source I want to create a 17568 12:37:20,700 --> 12:37:26,640 new folder and I'm going to call this 17569 12:37:23,116 --> 12:37:28,256 components over here then let's set up 17570 12:37:26,640 --> 12:37:32,096 that nav bar 17571 12:37:28,256 --> 12:37:35,096 nav bar JS in here 17572 12:37:32,096 --> 12:37:38,520 like I said first let's import card icon 17573 12:37:35,096 --> 12:37:40,616 something I'm going to discuss in the 17574 12:37:38,520 --> 12:37:42,300 following video then we also want to get 17575 12:37:40,616 --> 12:37:44,820 that U selector 17576 12:37:42,300 --> 12:37:47,400 so let's say use 17577 12:37:44,820 --> 12:37:50,400 selector hook that is coming from react 17578 12:37:47,400 --> 12:37:52,800 Redux and now let's set up that navbar 17579 12:37:50,400 --> 12:37:55,256 component so I'm going to use my 17580 12:37:52,800 --> 12:37:58,080 extension I'll set up the number and 17581 12:37:55,256 --> 12:38:00,540 first let's set up the return and then 17582 12:37:58,080 --> 12:38:04,080 we'll worry about the actual state so 17583 12:38:00,540 --> 12:38:07,500 let's say here nav I do want to add here 17584 12:38:04,080 --> 12:38:09,060 a div so inside of the div let's go or 17585 12:38:07,500 --> 12:38:11,900 I'm sorry inside of the nav let's go 17586 12:38:09,060 --> 12:38:14,936 with there let's add a class name of nav 17587 12:38:11,900 --> 12:38:18,500 Center and here we want to go with 17588 12:38:14,936 --> 12:38:22,320 heading 3 Redux toolkit 17589 12:38:18,500 --> 12:38:23,820 and after that let's save it and we 17590 12:38:22,320 --> 12:38:25,980 don't see anything so let me refresh 17591 12:38:23,820 --> 12:38:27,660 don't see anything in the browser and of 17592 12:38:25,980 --> 12:38:29,756 course the reason for that is because I 17593 12:38:27,660 --> 12:38:30,840 didn't import in the object so let me go 17594 12:38:29,756 --> 12:38:33,776 back 17595 12:38:30,840 --> 12:38:34,860 and in the app.js let's import the nav 17596 12:38:33,776 --> 12:38:37,380 bar 17597 12:38:34,860 --> 12:38:40,436 and then let's set it up over here 17598 12:38:37,380 --> 12:38:42,000 so what I want to do as far as the 17599 12:38:40,436 --> 12:38:45,240 return in app.js 17600 12:38:42,000 --> 12:38:48,360 I just want to go with Main and then the 17601 12:38:45,240 --> 12:38:51,416 number so instead of the heading to 17602 12:38:48,360 --> 12:38:53,756 index toolkit we'll go with main tags 17603 12:38:51,416 --> 12:38:57,360 and then first we'll set up the nav bar 17604 12:38:53,756 --> 12:39:00,300 and then we'll set up rest of the card 17605 12:38:57,360 --> 12:39:02,096 items as well so that should be our 17606 12:39:00,300 --> 12:39:05,520 number okay that's awesome 17607 12:39:02,096 --> 12:39:08,220 and as far as the other stuff 17608 12:39:05,520 --> 12:39:11,400 I think I'm gonna go here 17609 12:39:08,220 --> 12:39:14,820 with a card icon but that is going to be 17610 12:39:11,400 --> 12:39:17,520 placed in the nav container so right 17611 12:39:14,820 --> 12:39:22,980 after this heading three I'm gonna go 17612 12:39:17,520 --> 12:39:26,580 with div the class of nav container and 17613 12:39:22,980 --> 12:39:29,340 here let's set up that cart icon yep 17614 12:39:26,580 --> 12:39:31,436 that is how it's going to look like and 17615 12:39:29,340 --> 12:39:33,080 then let's go with div the class of 17616 12:39:31,436 --> 12:39:37,680 amount 17617 12:39:33,080 --> 12:39:41,096 container and then paragraph with a 17618 12:39:37,680 --> 12:39:43,380 class of total total hyphen 17619 12:39:41,096 --> 12:39:45,900 and amount and at the moment let's just 17620 12:39:43,380 --> 12:39:49,020 place a zero here let's save this and 17621 12:39:45,900 --> 12:39:52,860 now let's see how we can access the 17622 12:39:49,020 --> 12:39:55,616 entire state of our application so first 17623 12:39:52,860 --> 12:39:57,416 let's just log the sucker let's say use 17624 12:39:55,616 --> 12:40:00,116 selector 17625 12:39:57,416 --> 12:40:01,980 so that's the hook and like I said it's 17626 12:40:00,116 --> 12:40:04,800 looking for one parameter which is going 17627 12:40:01,980 --> 12:40:07,080 to be our function and then inside of 17628 12:40:04,800 --> 12:40:10,320 this function as a parameter we get that 17629 12:40:07,080 --> 12:40:13,740 entire store so for time being I'm gonna 17630 12:40:10,320 --> 12:40:15,416 call this store and I'll log it I'll say 17631 12:40:13,740 --> 12:40:18,300 but the only thing that I want to do in 17632 12:40:15,416 --> 12:40:20,276 this function is log the store and what 17633 12:40:18,300 --> 12:40:24,000 you'll notice in the console 17634 12:40:20,276 --> 12:40:27,000 is our entire state which is just 17635 12:40:24,000 --> 12:40:30,240 awesome if you ask me so take a look 17636 12:40:27,000 --> 12:40:33,540 over here we have card items we have a 17637 12:40:30,240 --> 12:40:35,160 mount and we have the total so again I 17638 12:40:33,540 --> 12:40:38,160 know I'm repeating myself 17639 12:40:35,160 --> 12:40:41,340 but essentially the idea is that 17640 12:40:38,160 --> 12:40:43,320 using this use selector notice how we 17641 12:40:41,340 --> 12:40:45,776 don't need to pass anything coming from 17642 12:40:43,320 --> 12:40:49,380 the specific slice or nothing like that 17643 12:40:45,776 --> 12:40:51,596 we can right away in this function axis 17644 12:40:49,380 --> 12:40:54,480 our entire store 17645 12:40:51,596 --> 12:40:56,756 and essentially what we want to do we 17646 12:40:54,480 --> 12:40:59,340 want to return something 17647 12:40:56,756 --> 12:41:03,116 whether that is specific 17648 12:40:59,340 --> 12:41:05,756 property for example amount or of course 17649 12:41:03,116 --> 12:41:08,160 you can return the entire card and then 17650 12:41:05,756 --> 12:41:10,140 you can the structure so first let's 17651 12:41:08,160 --> 12:41:13,560 just set it up where 17652 12:41:10,140 --> 12:41:15,180 we return the amount so notice over here 17653 12:41:13,560 --> 12:41:16,916 we have undefined in line five because 17654 12:41:15,180 --> 12:41:18,180 we're not returning anything from this 17655 12:41:16,916 --> 12:41:20,580 function yet 17656 12:41:18,180 --> 12:41:22,616 and let's set up over here in the 17657 12:41:20,580 --> 12:41:26,096 paragraph and then we'll take a look at 17658 12:41:22,616 --> 12:41:27,240 the the structuring option as well I 17659 12:41:26,096 --> 12:41:29,160 don't think I'm going to leave this for 17660 12:41:27,240 --> 12:41:31,200 your reference because again 17661 12:41:29,160 --> 12:41:33,480 we'll set up multiple ways anyway so 17662 12:41:31,200 --> 12:41:36,360 first let's go with a mount and that one 17663 12:41:33,480 --> 12:41:39,660 is equal to Let's again invoke use 17664 12:41:36,360 --> 12:41:41,400 selector let's pass in the function and 17665 12:41:39,660 --> 12:41:43,616 I'm going to be setting up the arrow 17666 12:41:41,400 --> 12:41:46,256 function and I'll go right away with 17667 12:41:43,616 --> 12:41:48,596 implicit return so again in here we are 17668 12:41:46,256 --> 12:41:51,480 accessing the entire state of our 17669 12:41:48,596 --> 12:41:54,060 application and we go with store cart 17670 12:41:51,480 --> 12:41:58,200 because that's the property value 17671 12:41:54,060 --> 12:42:01,200 and yes once we add more reducers 17672 12:41:58,200 --> 12:42:03,360 in our store then of course we'll be 17673 12:42:01,200 --> 12:42:07,020 able to access it so I'm gonna go here 17674 12:42:03,360 --> 12:42:08,880 let's say with modal and I'm not going 17675 12:42:07,020 --> 12:42:11,096 to pass anything in because I do need to 17676 12:42:08,880 --> 12:42:13,080 set up the reducer then of course we'll 17677 12:42:11,096 --> 12:42:16,616 be able to access with 17678 12:42:13,080 --> 12:42:18,360 store dot model hopefully that is clear 17679 12:42:16,616 --> 12:42:20,756 and this is very very useful because 17680 12:42:18,360 --> 12:42:23,460 again we don't need to import anything 17681 12:42:20,756 --> 12:42:26,400 specific from that slice we have access 17682 12:42:23,460 --> 12:42:28,320 to entire store and then let's go with 17683 12:42:26,400 --> 12:42:29,640 the mount so this is what I'm returning 17684 12:42:28,320 --> 12:42:31,320 from the function 17685 12:42:29,640 --> 12:42:34,616 and probably it's not going to be 17686 12:42:31,320 --> 12:42:37,320 surprising if I pass here the value 17687 12:42:34,616 --> 12:42:40,200 still going to be zero now let's test 17688 12:42:37,320 --> 12:42:42,480 this out though let's go to our features 17689 12:42:40,200 --> 12:42:44,340 slice and let's change this around let's 17690 12:42:42,480 --> 12:42:47,756 say there's going to be five items 17691 12:42:44,340 --> 12:42:49,740 number one I save notice now of course I 17692 12:42:47,756 --> 12:42:52,756 have this value of five so we know that 17693 12:42:49,740 --> 12:42:55,320 our functionality works and like I said 17694 12:42:52,756 --> 12:42:58,256 there's million different ways how we 17695 12:42:55,320 --> 12:43:00,840 can set this up and one of them is 17696 12:42:58,256 --> 12:43:04,380 actually the structuring so I know that 17697 12:43:00,840 --> 12:43:08,096 I can return store Dot card which is 17698 12:43:04,380 --> 12:43:10,560 essentially a object correct and then 17699 12:43:08,096 --> 12:43:13,916 inside of it I have the amount property 17700 12:43:10,560 --> 12:43:16,680 so this is also valid honest value did 17701 12:43:13,916 --> 12:43:20,160 not change and we didn't get any bugs so 17702 12:43:16,680 --> 12:43:23,160 that's how we can access data from our 17703 12:43:20,160 --> 12:43:25,860 slice we need to pick the component we 17704 12:43:23,160 --> 12:43:28,020 need to use a use selector and here we 17705 12:43:25,860 --> 12:43:31,500 pass in the function 17706 12:43:28,020 --> 12:43:34,200 and as a parameter of this function gets 17707 12:43:31,500 --> 12:43:36,596 the entire store and then we just need 17708 12:43:34,200 --> 12:43:38,640 to pick what we want to return in my 17709 12:43:36,596 --> 12:43:40,916 case I want to return cart from this 17710 12:43:38,640 --> 12:43:44,580 function it is an object and it 17711 12:43:40,916 --> 12:43:46,980 represents this initial state 17712 12:43:44,580 --> 12:43:49,740 and more specifically I'm looking for 17713 12:43:46,980 --> 12:43:52,740 the amount that's why I destructured it 17714 12:43:49,740 --> 12:43:55,680 and then I display it here in the return 17715 12:43:52,740 --> 12:43:58,860 as far as the icons in this project I 17716 12:43:55,680 --> 12:44:04,020 decided to go different route where I 17717 12:43:58,860 --> 12:44:06,660 use the hero icons site so once you 17718 12:44:04,020 --> 12:44:10,020 navigate there you'll notice a bunch of 17719 12:44:06,660 --> 12:44:12,720 nice icons and essentially I just set 17720 12:44:10,020 --> 12:44:15,000 them up as components so let me show you 17721 12:44:12,720 --> 12:44:17,340 with one and then you understand the 17722 12:44:15,000 --> 12:44:20,400 general concept just pick the icon you 17723 12:44:17,340 --> 12:44:23,460 want in this case I think I'm gonna go 17724 12:44:20,400 --> 12:44:24,720 with this bad one and as far as I 17725 12:44:23,460 --> 12:44:27,840 noticed there's really no difference 17726 12:44:24,720 --> 12:44:30,360 whether you go with jsx or SVG 17727 12:44:27,840 --> 12:44:33,080 so let me just copy this one let's 17728 12:44:30,360 --> 12:44:36,180 navigate back and then in the icons 17729 12:44:33,080 --> 12:44:39,180 you'll notice full-blown components that 17730 12:44:36,180 --> 12:44:40,800 I will right away export so let me keep 17731 12:44:39,180 --> 12:44:43,200 scrolling and I'm going to call this 17732 12:44:40,800 --> 12:44:46,916 testing so I'm going to say export const 17733 12:44:43,200 --> 12:44:49,680 that's going to be my testing component 17734 12:44:46,916 --> 12:44:51,300 and in here you just want to set up the 17735 12:44:49,680 --> 12:44:55,320 value you want to return 17736 12:44:51,300 --> 12:44:59,160 so this essentially will be the icon now 17737 12:44:55,320 --> 12:45:01,380 I am styling it though in CSS and in a 17738 12:44:59,160 --> 12:45:03,360 second you'll see what I mean so I have 17739 12:45:01,380 --> 12:45:06,000 this testing icon right now so that's 17740 12:45:03,360 --> 12:45:07,916 the return that I just copied so now 17741 12:45:06,000 --> 12:45:09,360 let's navigate to the navbar since I 17742 12:45:07,916 --> 12:45:10,500 already have the import for the card 17743 12:45:09,360 --> 12:45:13,140 icon 17744 12:45:10,500 --> 12:45:14,756 and let's set up over here the testing 17745 12:45:13,140 --> 12:45:17,640 one as well 17746 12:45:14,756 --> 12:45:19,740 and we can place it anywhere we want so 17747 12:45:17,640 --> 12:45:22,140 let me put it side by side and you'll 17748 12:45:19,740 --> 12:45:24,660 notice that this is a giant ore I'm 17749 12:45:22,140 --> 12:45:28,080 sorry well I already applied The Styling 17750 12:45:24,660 --> 12:45:32,040 so maybe this is not gonna work over 17751 12:45:28,080 --> 12:45:34,380 here maybe let me set up here the 17752 12:45:32,040 --> 12:45:36,416 fragment and then you'll see what I mean 17753 12:45:34,380 --> 12:45:38,040 so let me move the sucker down again 17754 12:45:36,416 --> 12:45:39,060 this is just temporary you don't have to 17755 12:45:38,040 --> 12:45:41,340 follow along 17756 12:45:39,060 --> 12:45:45,000 but you'll notice that if we don't apply 17757 12:45:41,340 --> 12:45:47,160 a styling we'll get this massive icon 17758 12:45:45,000 --> 12:45:48,116 basically that's the default that they 17759 12:45:47,160 --> 12:45:53,276 provide 17760 12:45:48,116 --> 12:45:56,640 so in the Styles in the index CSS 17761 12:45:53,276 --> 12:45:59,340 I added some styling for specific icons 17762 12:45:56,640 --> 12:46:03,660 so now I'm talking about the cart icon 17763 12:45:59,340 --> 12:46:07,616 and also same applies for these ones 17764 12:46:03,660 --> 12:46:09,116 over here as well so not only you'll 17765 12:46:07,616 --> 12:46:12,416 need to get the icon but notice over 17766 12:46:09,116 --> 12:46:14,820 here I applied some width as well as the 17767 12:46:12,416 --> 12:46:17,160 color and of course you can add more 17768 12:46:14,820 --> 12:46:18,300 Styles so essentially that's how I set 17769 12:46:17,160 --> 12:46:22,800 up 17770 12:46:18,300 --> 12:46:25,980 my icons where I created icons Js 17771 12:46:22,800 --> 12:46:28,436 and in here you'll find three icons so 17772 12:46:25,980 --> 12:46:32,700 one for the cart and then the other ones 17773 12:46:28,436 --> 12:46:35,340 for the actual card items and I imported 17774 12:46:32,700 --> 12:46:38,580 the icon from the hero icon so I set it 17775 12:46:35,340 --> 12:46:41,160 up as a component I export and then I 17776 12:46:38,580 --> 12:46:43,320 import in any of the components that I 17777 12:46:41,160 --> 12:46:45,180 want and then I just need to apply a 17778 12:46:43,320 --> 12:46:48,300 little bit of styling and you want to 17779 12:46:45,180 --> 12:46:49,436 Target the SVG so now let me remove the 17780 12:46:48,300 --> 12:46:52,740 testing one 17781 12:46:49,436 --> 12:46:55,500 and also let me remove it from the icons 17782 12:46:52,740 --> 12:46:58,380 since we won't use it and now let's 17783 12:46:55,500 --> 12:47:00,900 proceed to The Next Step all right and 17784 12:46:58,380 --> 12:47:04,256 up next let's work on our car items so 17785 12:47:00,900 --> 12:47:05,756 at the moment it is empty array or I did 17786 12:47:04,256 --> 12:47:09,060 prepare 17787 12:47:05,756 --> 12:47:11,460 an array of products just so we can set 17788 12:47:09,060 --> 12:47:14,756 up the initial functionality yes 17789 12:47:11,460 --> 12:47:17,936 eventually we'll fetch this data from my 17790 12:47:14,756 --> 12:47:19,980 API and in the process we'll practice of 17791 12:47:17,936 --> 12:47:23,460 how we can set up asynchronous 17792 12:47:19,980 --> 12:47:26,276 operations in Redux toolkit but for time 17793 12:47:23,460 --> 12:47:28,980 being this will be hard-coded data and 17794 12:47:26,276 --> 12:47:31,740 it is located in the car atoms and as 17795 12:47:28,980 --> 12:47:34,680 you can see it is an array where each 17796 12:47:31,740 --> 12:47:38,756 item is an object with some properties 17797 12:47:34,680 --> 12:47:42,300 so what we're going to do we're going to 17798 12:47:38,756 --> 12:47:44,220 set up the initial State equal to that 17799 12:47:42,300 --> 12:47:46,980 array so of course we just need to 17800 12:47:44,220 --> 12:47:51,480 import and then we'll repeat the same 17801 12:47:46,980 --> 12:47:54,240 steps like we did with an amount in 17802 12:47:51,480 --> 12:47:56,936 number however since we have unraveled 17803 12:47:54,240 --> 12:48:00,300 after iterate over and all of that cool 17804 12:47:56,936 --> 12:48:03,360 stuff so first what I want to do is go 17805 12:48:00,300 --> 12:48:06,720 to the card slice and we want to import 17806 12:48:03,360 --> 12:48:09,240 we want to import those card items so 17807 12:48:06,720 --> 12:48:10,616 let's go here with car items it is a 17808 12:48:09,240 --> 12:48:12,540 default export so it doesn't really 17809 12:48:10,616 --> 12:48:15,000 matter how we call it here 17810 12:48:12,540 --> 12:48:18,960 and then we need to go two levels up 17811 12:48:15,000 --> 12:48:19,680 we're looking for the card items and in 17812 12:48:18,960 --> 12:48:21,960 here 17813 12:48:19,680 --> 12:48:24,300 what do we have the initial State let's 17814 12:48:21,960 --> 12:48:27,116 set up current items equal to current 17815 12:48:24,300 --> 12:48:29,520 items now if we want to see that we can 17816 12:48:27,116 --> 12:48:32,276 either go to the nav bar since in there 17817 12:48:29,520 --> 12:48:33,900 we have the use selector and we can of 17818 12:48:32,276 --> 12:48:36,720 course log it or 17819 12:48:33,900 --> 12:48:39,416 we can take an advantage of the Redux 17820 12:48:36,720 --> 12:48:42,180 devtool so let's navigate here 17821 12:48:39,416 --> 12:48:45,116 let's refresh just so we're on safe side 17822 12:48:42,180 --> 12:48:48,596 if we navigate to the Redux depth tools 17823 12:48:45,116 --> 12:48:50,160 now what you'll notice that card items 17824 12:48:48,596 --> 12:48:52,916 in the card 17825 12:48:50,160 --> 12:48:56,340 is an action array like I said each 17826 12:48:52,916 --> 12:48:58,980 object represents that item with a bunch 17827 12:48:56,340 --> 12:49:01,800 of useful properties so our next step is 17828 12:48:58,980 --> 12:49:04,320 going to be setting this up in the 17829 12:49:01,800 --> 12:49:07,140 app.js where we'll grab them we'll 17830 12:49:04,320 --> 12:49:10,860 iterate over and then for each item 17831 12:49:07,140 --> 12:49:14,096 we'll return a component with the image 17832 12:49:10,860 --> 12:49:17,700 the title and rest of the stuff not bad 17833 12:49:14,096 --> 12:49:21,060 not bad so we set up the card items 17834 12:49:17,700 --> 12:49:24,300 equal to an actual array instead of 17835 12:49:21,060 --> 12:49:28,200 empty one now let's set up two more 17836 12:49:24,300 --> 12:49:30,480 components the cart container just and 17837 12:49:28,200 --> 12:49:32,820 cart item.js 17838 12:49:30,480 --> 12:49:36,000 and just the Showcase so this is going 17839 12:49:32,820 --> 12:49:38,756 to be the container where we'll have all 17840 12:49:36,000 --> 12:49:42,000 of the items as well as the total 17841 12:49:38,756 --> 12:49:44,096 and the clear card functionality now 17842 12:49:42,000 --> 12:49:46,980 eventually we'll add the model but the 17843 12:49:44,096 --> 12:49:50,040 idea won't change if we click on clear 17844 12:49:46,980 --> 12:49:52,380 card then of course we'll have empty 17845 12:49:50,040 --> 12:49:55,340 card and then we also want to set up 17846 12:49:52,380 --> 12:49:59,520 that cart item where again we'll display 17847 12:49:55,340 --> 12:50:03,060 all of this info and yes we'll practice 17848 12:49:59,520 --> 12:50:05,820 on use selector again where in the card 17849 12:50:03,060 --> 12:50:08,160 container we not only want to get the 17850 12:50:05,820 --> 12:50:11,220 car items we also want to get the total 17851 12:50:08,160 --> 12:50:12,540 as well as the amount because we'll use 17852 12:50:11,220 --> 12:50:15,480 that amount 17853 12:50:12,540 --> 12:50:17,580 to display some things conditionally in 17854 12:50:15,480 --> 12:50:20,756 this case if the amount is less than one 17855 12:50:17,580 --> 12:50:22,256 then I'll display this and then if there 17856 12:50:20,756 --> 12:50:24,840 are some items in a card then of course 17857 12:50:22,256 --> 12:50:27,540 there's going to be a different logic 17858 12:50:24,840 --> 12:50:30,900 and in the card item we'll just pass in 17859 12:50:27,540 --> 12:50:33,360 the ready as a key as well as the 17860 12:50:30,900 --> 12:50:35,820 properties of the atom and then we'll 17861 12:50:33,360 --> 12:50:37,740 destructure it now when it comes to card 17862 12:50:35,820 --> 12:50:41,640 item for the most part we'll get there 17863 12:50:37,740 --> 12:50:44,580 our action creators which of course will 17864 12:50:41,640 --> 12:50:47,460 set up a little bit later so for now we 17865 12:50:44,580 --> 12:50:50,160 just want to worry about rendering the 17866 12:50:47,460 --> 12:50:52,800 cart items on the screen 17867 12:50:50,160 --> 12:50:55,020 so let's get cracking where I want to go 17868 12:50:52,800 --> 12:50:56,096 two components and let's go here with 17869 12:50:55,020 --> 12:50:58,436 cart 17870 12:50:56,096 --> 12:51:01,640 container.js 17871 12:50:58,436 --> 12:51:04,740 and I also want to set up that card 17872 12:51:01,640 --> 12:51:07,380 item.js now when it comes to car animal 17873 12:51:04,740 --> 12:51:09,776 work on that one in next video 17874 12:51:07,380 --> 12:51:13,320 for a moment and as you know it should 17875 12:51:09,776 --> 12:51:15,720 be card items should be call Item one 17876 12:51:13,320 --> 12:51:19,916 and now let me fix this one as well 17877 12:51:15,720 --> 12:51:21,660 where I'm gonna go with cart item now we 17878 12:51:19,916 --> 12:51:24,416 want to import that in the cart 17879 12:51:21,660 --> 12:51:25,500 container so let's set up this component 17880 12:51:24,416 --> 12:51:30,360 as well 17881 12:51:25,500 --> 12:51:34,080 so r a f c e and that's right away 17882 12:51:30,360 --> 12:51:37,020 import that card item so let's go here 17883 12:51:34,080 --> 12:51:40,740 with cart item then we want to navigate 17884 12:51:37,020 --> 12:51:44,040 to the app.js and we also want to get 17885 12:51:40,740 --> 12:51:47,040 that card container so let's get that 17886 12:51:44,040 --> 12:51:49,860 one and I'll place it right after the 17887 12:51:47,040 --> 12:51:51,840 container meaning the number sorry and 17888 12:51:49,860 --> 12:51:53,580 we should see on the screen card 17889 12:51:51,840 --> 12:51:55,980 container awesome 17890 12:51:53,580 --> 12:51:58,380 now what's next well let's take a look 17891 12:51:55,980 --> 12:52:00,360 at the readme where we do have the car 17892 12:51:58,380 --> 12:52:03,180 item we also want to get the use 17893 12:52:00,360 --> 12:52:06,480 selector and now from my application 17894 12:52:03,180 --> 12:52:09,416 State I want to get the entire card and 17895 12:52:06,480 --> 12:52:12,300 I'll adjust the structure columns total 17896 12:52:09,416 --> 12:52:14,116 as well as the amount like I said the 17897 12:52:12,300 --> 12:52:17,640 amount we'll use to display 17898 12:52:14,116 --> 12:52:19,500 conditionally empty card 17899 12:52:17,640 --> 12:52:21,720 um again I already showcased that but 17900 12:52:19,500 --> 12:52:24,240 let me show you one more time this is 17901 12:52:21,720 --> 12:52:26,160 what we want to set up if the card is 17902 12:52:24,240 --> 12:52:28,380 empty and we'll control that with the 17903 12:52:26,160 --> 12:52:30,300 mod now if there are some values in the 17904 12:52:28,380 --> 12:52:32,936 card then this is what we want to return 17905 12:52:30,300 --> 12:52:35,276 we want to go with Section header 17906 12:52:32,936 --> 12:52:37,740 iterate over card items 17907 12:52:35,276 --> 12:52:41,340 and then also in the footer we want to 17908 12:52:37,740 --> 12:52:45,116 display the total with the clear card 17909 12:52:41,340 --> 12:52:47,700 button so let's set this one up where 17910 12:52:45,116 --> 12:52:51,180 I'm gonna go with import then use 17911 12:52:47,700 --> 12:52:53,276 selector from yeah Redux let's right 17912 12:52:51,180 --> 12:52:54,776 away access all of the items so let's 17913 12:52:53,276 --> 12:52:57,540 say here const 17914 12:52:54,776 --> 12:53:00,000 I'm looking for cart 17915 12:52:57,540 --> 12:53:03,660 and items 17916 12:53:00,000 --> 12:53:06,596 total also we want to get the amount and 17917 12:53:03,660 --> 12:53:09,116 all of that is equal to use 17918 12:53:06,596 --> 12:53:10,980 selector let's pass in the function and 17919 12:53:09,116 --> 12:53:14,040 what we want to return we want to go 17920 12:53:10,980 --> 12:53:15,116 here with store or state however you 17921 12:53:14,040 --> 12:53:18,416 want to call it 17922 12:53:15,116 --> 12:53:20,400 store and then cart so we get all those 17923 12:53:18,416 --> 12:53:23,400 values right away let's set up that 17924 12:53:20,400 --> 12:53:25,436 condition where I'll say amount is less 17925 12:53:23,400 --> 12:53:28,020 than one if that is the case what we 17926 12:53:25,436 --> 12:53:30,360 want to return well we want to go here 17927 12:53:28,020 --> 12:53:33,360 with section 17928 12:53:30,360 --> 12:53:35,400 then let's add a class name of card 17929 12:53:33,360 --> 12:53:38,460 and let's set up some logic over here 17930 12:53:35,400 --> 12:53:41,160 let's say here header 17931 12:53:38,460 --> 12:53:43,436 and side of the header we'll have a 17932 12:53:41,160 --> 12:53:45,840 heading to your 17933 12:53:43,436 --> 12:53:48,480 bag and then right after that let's go 17934 12:53:45,840 --> 12:53:51,416 with heading four and we'll say empty 17935 12:53:48,480 --> 12:53:53,820 card now we do need to add here empty 17936 12:53:51,416 --> 12:53:56,400 cart class 17937 12:53:53,820 --> 12:53:59,160 and let's add a text your card is 17938 12:53:56,400 --> 12:54:01,916 currently empty or your bag 17939 12:53:59,160 --> 12:54:04,680 it's empty let's save it now since the 17940 12:54:01,916 --> 12:54:07,436 amount is zero this is exactly what we 17941 12:54:04,680 --> 12:54:09,360 display so in order to see the atoms of 17942 12:54:07,436 --> 12:54:11,640 course we'll have to navigate back to 17943 12:54:09,360 --> 12:54:13,616 the card slice and again for time being 17944 12:54:11,640 --> 12:54:16,560 we'll just hard code this don't worry 17945 12:54:13,616 --> 12:54:18,000 eventually we'll set up some action 17946 12:54:16,560 --> 12:54:20,096 creators 17947 12:54:18,000 --> 12:54:22,916 that control this logic essentially will 17948 12:54:20,096 --> 12:54:25,560 set up some reducers and by default 17949 12:54:22,916 --> 12:54:29,820 Redux toolkit is going to give us those 17950 12:54:25,560 --> 12:54:31,560 action creators so let's go back to the 17951 12:54:29,820 --> 12:54:34,256 cart container 17952 12:54:31,560 --> 12:54:37,980 and right after this one you want to set 17953 12:54:34,256 --> 12:54:41,460 up another return so as you saw this one 17954 12:54:37,980 --> 12:54:43,916 was if we have less than one and now 17955 12:54:41,460 --> 12:54:46,020 since we hard-coded we have four 17956 12:54:43,916 --> 12:54:47,160 so in here we want to go with another 17957 12:54:46,020 --> 12:54:48,720 turn 17958 12:54:47,160 --> 12:54:52,020 another section 17959 12:54:48,720 --> 12:54:52,800 and we do one on a class so class will 17960 12:54:52,020 --> 12:54:55,256 be 17961 12:54:52,800 --> 12:54:58,200 equal to cart 17962 12:54:55,256 --> 12:55:00,116 and then the same deal we want to set up 17963 12:54:58,200 --> 12:55:02,340 that header 17964 12:55:00,116 --> 12:55:04,740 so header over here 17965 12:55:02,340 --> 12:55:07,680 and inside of it we're just gonna go 17966 12:55:04,740 --> 12:55:12,116 with heading to your bag 17967 12:55:07,680 --> 12:55:14,096 your bag let's save that one and yeah as 17968 12:55:12,116 --> 12:55:15,540 I know this is wrong so it should be 17969 12:55:14,096 --> 12:55:18,840 class name 17970 12:55:15,540 --> 12:55:21,240 then we want to iterate over the card 17971 12:55:18,840 --> 12:55:23,880 items so for time being we just want to 17972 12:55:21,240 --> 12:55:26,700 set up a div then we want to grab those 17973 12:55:23,880 --> 12:55:29,040 card items we want to run the map and 17974 12:55:26,700 --> 12:55:31,020 this is the actual array correct and 17975 12:55:29,040 --> 12:55:35,096 then for every item we want to return 17976 12:55:31,020 --> 12:55:38,040 that card item that we just set up 17977 12:55:35,096 --> 12:55:39,360 and we want to pass in the data now on 17978 12:55:38,040 --> 12:55:41,756 the screen we'll just see that text 17979 12:55:39,360 --> 12:55:44,460 whatever we have over there so here we 17980 12:55:41,756 --> 12:55:46,860 have the Karam but eventually yes we'll 17981 12:55:44,460 --> 12:55:49,320 access it and we'll correctly display it 17982 12:55:46,860 --> 12:55:52,080 so we're mapping over I'll call this 17983 12:55:49,320 --> 12:55:54,480 item so now I'm talking about each and 17984 12:55:52,080 --> 12:55:58,256 every object over there let's go with a 17985 12:55:54,480 --> 12:56:00,540 return then let's set up a cart item we 17986 12:55:58,256 --> 12:56:03,776 do need to pass in the key since this is 17987 12:56:00,540 --> 12:56:05,880 react and I already know that there's a 17988 12:56:03,776 --> 12:56:06,900 an ID property now if you don't believe 17989 12:56:05,880 --> 12:56:09,480 me 17990 12:56:06,900 --> 12:56:11,520 you can navigate over here and you'll 17991 12:56:09,480 --> 12:56:14,520 clearly see that so each and every item 17992 12:56:11,520 --> 12:56:17,640 has that ID property so let's say here 17993 12:56:14,520 --> 12:56:21,060 dot ID and then I want to use the spread 17994 12:56:17,640 --> 12:56:24,116 operator Dot and then pass in rest of 17995 12:56:21,060 --> 12:56:27,240 the properties let's save it so since we 17996 12:56:24,116 --> 12:56:30,000 have four items we have four card items 17997 12:56:27,240 --> 12:56:32,936 on the screen and then right after this 17998 12:56:30,000 --> 12:56:36,000 one right after this div let's set up a 17999 12:56:32,936 --> 12:56:39,116 footer this is where we'll display the 18000 12:56:36,000 --> 12:56:41,160 card total as well as the clear button 18001 12:56:39,116 --> 12:56:45,416 cut which eventually will revoke the 18002 12:56:41,160 --> 12:56:48,416 model and clear the card so I'm gonna go 18003 12:56:45,416 --> 12:56:52,460 with a horizontal line here then let's 18004 12:56:48,416 --> 12:56:56,096 add heading 2. with some data so total 18005 12:56:52,460 --> 12:56:59,160 span and set of this one I want to set 18006 12:56:56,096 --> 12:57:01,616 up a dollar sign I'll say total yes the 18007 12:56:59,160 --> 12:57:04,200 moment it is going to be zero 18008 12:57:01,616 --> 12:57:05,640 don't worry about it and as I not 18009 12:57:04,200 --> 12:57:07,436 actually messed it up over here it 18010 12:57:05,640 --> 12:57:10,740 should be there with a class of card 18011 12:57:07,436 --> 12:57:11,936 total my apologies that's why the CSS is 18012 12:57:10,740 --> 12:57:14,756 not correct 18013 12:57:11,936 --> 12:57:17,700 and after that we want to go with that 18014 12:57:14,756 --> 12:57:19,200 clear card button so still within the 18015 12:57:17,700 --> 12:57:22,020 footer 18016 12:57:19,200 --> 12:57:24,000 we want to go with a button let's add a 18017 12:57:22,020 --> 12:57:25,616 class right away it should be the end 18018 12:57:24,000 --> 12:57:29,096 clear begin 18019 12:57:25,616 --> 12:57:31,916 and let's say clear card 18020 12:57:29,096 --> 12:57:34,200 let's save this let's refresh so we 18021 12:57:31,916 --> 12:57:36,060 don't have any errors and this is what 18022 12:57:34,200 --> 12:57:39,776 we should see 18023 12:57:36,060 --> 12:57:41,096 on the screen where again we used use 18024 12:57:39,776 --> 12:57:44,880 selector 18025 12:57:41,096 --> 12:57:47,276 to access our entire store more 18026 12:57:44,880 --> 12:57:49,860 specifically we looked for cart which 18027 12:57:47,276 --> 12:57:52,256 represents that initial state in the 18028 12:57:49,860 --> 12:57:55,320 initial State we have all these values 18029 12:57:52,256 --> 12:57:57,000 so we're able to structure and then we 18030 12:57:55,320 --> 12:57:59,340 check for the amount 18031 12:57:57,000 --> 12:58:01,916 if it's less than one we display the 18032 12:57:59,340 --> 12:58:05,096 message if we have some items in there 18033 12:58:01,916 --> 12:58:07,916 then we just iterate over them and for 18034 12:58:05,096 --> 12:58:10,800 every item we return that card item and 18035 12:58:07,916 --> 12:58:12,720 then below that we have total which at 18036 12:58:10,800 --> 12:58:15,840 the moment is zero eventually this will 18037 12:58:12,720 --> 12:58:18,900 be dynamic as well as the clear card 18038 12:58:15,840 --> 12:58:22,680 button which eventually will remove all 18039 12:58:18,900 --> 12:58:26,340 the items from the cart awesome and up 18040 12:58:22,680 --> 12:58:29,820 next I want to set up the card item 18041 12:58:26,340 --> 12:58:32,880 where we're passing in all of the data 18042 12:58:29,820 --> 12:58:36,480 from the cart items so we go here with 18043 12:58:32,880 --> 12:58:38,880 dot dot an item basically we spread out 18044 12:58:36,480 --> 12:58:42,360 all the properties so we can access them 18045 12:58:38,880 --> 12:58:44,580 of course here when we destructure the 18046 12:58:42,360 --> 12:58:47,936 props and then we just want to set up 18047 12:58:44,580 --> 12:58:50,520 some return with the image title and 18048 12:58:47,936 --> 12:58:52,680 rest of the stuff yes of course none of 18049 12:58:50,520 --> 12:58:54,240 the buttons at the moment will work but 18050 12:58:52,680 --> 12:58:56,936 at least we'll have a better looking 18051 12:58:54,240 --> 12:58:59,700 card item and also we want to get two 18052 12:58:56,936 --> 12:59:03,000 icons from the icons we want to get rest 18053 12:58:59,700 --> 12:59:06,116 of them the Chevron down and Chevron up 18054 12:59:03,000 --> 12:59:09,540 so I'm gonna navigate to the card item 18055 12:59:06,116 --> 12:59:11,820 first let me grab both of them let me 18056 12:59:09,540 --> 12:59:15,480 get the import then we're looking for 18057 12:59:11,820 --> 12:59:17,460 Chevron down and Chevron 18058 12:59:15,480 --> 12:59:19,916 Chevron up 18059 12:59:17,460 --> 12:59:22,380 after that we want to go in the 18060 12:59:19,916 --> 12:59:23,520 structure all of the props so we're 18061 12:59:22,380 --> 12:59:28,200 looking for ID 18062 12:59:23,520 --> 12:59:30,720 image title price as well as the amount 18063 12:59:28,200 --> 12:59:33,300 and as far as the return let's start 18064 12:59:30,720 --> 12:59:36,360 over here with simple article 18065 12:59:33,300 --> 12:59:39,060 so we want to go here with article and 18066 12:59:36,360 --> 12:59:42,840 let's add a class of cart 18067 12:59:39,060 --> 12:59:45,540 item then we want to go with image and 18068 12:59:42,840 --> 12:59:48,416 Source will be the image 18069 12:59:45,540 --> 12:59:50,700 so let me say here dynamically image and 18070 12:59:48,416 --> 12:59:53,040 as far as the alternative well we have 18071 12:59:50,700 --> 12:59:55,140 the title for that so let's save it and 18072 12:59:53,040 --> 12:59:58,080 we should see the images and if we do 18073 12:59:55,140 --> 12:59:59,220 and of course we can proceed to the next 18074 12:59:58,080 --> 13:00:01,740 step 18075 12:59:59,220 --> 13:00:04,860 after that we want to set up a div 18076 13:00:01,740 --> 13:00:08,040 ing four is where we'll display the 18077 13:00:04,860 --> 13:00:10,680 title so let's save that then we also 18078 13:00:08,040 --> 13:00:13,436 want to set up the price so let's say 18079 13:00:10,680 --> 13:00:15,660 here heading 4 with the class of item 18080 13:00:13,436 --> 13:00:17,820 price which comes with a little bit of 18081 13:00:15,660 --> 13:00:20,640 styling and then I'll add the dollar 18082 13:00:17,820 --> 13:00:23,936 sign and I'll say a price let's save 18083 13:00:20,640 --> 13:00:27,360 that then we want to set up the button 18084 13:00:23,936 --> 13:00:29,340 so right below we'll go with button then 18085 13:00:27,360 --> 13:00:32,520 class and remove 18086 13:00:29,340 --> 13:00:35,340 iPhone B10 and we'll say remove would 18087 13:00:32,520 --> 13:00:39,360 save that and then we just want to set 18088 13:00:35,340 --> 13:00:41,640 up those buttons to increase or decrease 18089 13:00:39,360 --> 13:00:43,020 the amount so we want to do that right 18090 13:00:41,640 --> 13:00:45,900 after this div 18091 13:00:43,020 --> 13:00:48,060 so this is where we set up the heading 18092 13:00:45,900 --> 13:00:49,256 force and the button but after that we 18093 13:00:48,060 --> 13:00:51,000 want to go with another div there's 18094 13:00:49,256 --> 13:00:53,880 going to be no class 18095 13:00:51,000 --> 13:00:55,560 well we want to set up a button let's 18096 13:00:53,880 --> 13:01:00,000 call this amount 18097 13:00:55,560 --> 13:01:02,096 hyphen BTN and then let's add that icon 18098 13:01:00,000 --> 13:01:05,220 over here so in this case I want to go 18099 13:01:02,096 --> 13:01:08,160 with Chevron up let's close it okay good 18100 13:01:05,220 --> 13:01:10,400 then we want to copy and paste and set 18101 13:01:08,160 --> 13:01:13,200 up the rundown 18102 13:01:10,400 --> 13:01:15,480 so the class is going to be the same 18103 13:01:13,200 --> 13:01:17,880 mount button and here we just want to 18104 13:01:15,480 --> 13:01:20,936 say Chevron down and in between them 18105 13:01:17,880 --> 13:01:23,220 this is where we'll place the amount so 18106 13:01:20,936 --> 13:01:26,580 let's say here paragraph class name 18107 13:01:23,220 --> 13:01:29,820 amount and we want to render the amount 18108 13:01:26,580 --> 13:01:32,400 so say amount over here let's save it 18109 13:01:29,820 --> 13:01:34,256 and for all of them we have one 18110 13:01:32,400 --> 13:01:37,740 of course in the following videos we'll 18111 13:01:34,256 --> 13:01:40,320 set up the functionality that allows us 18112 13:01:37,740 --> 13:01:42,840 to change that and with this in place 18113 13:01:40,320 --> 13:01:47,160 now we can start slowly 18114 13:01:42,840 --> 13:01:49,680 adding our first reducers okay so how we 18115 13:01:47,160 --> 13:01:52,256 can set up the functionality with Redux 18116 13:01:49,680 --> 13:01:54,116 toolkit so if you remember previously we 18117 13:01:52,256 --> 13:01:56,936 needed to set up the action we needed 18118 13:01:54,116 --> 13:02:01,500 dispatch and then we always always need 18119 13:01:56,936 --> 13:02:03,960 it to return a new state and then just 18120 13:02:01,500 --> 13:02:07,200 of course copy the values and all that 18121 13:02:03,960 --> 13:02:11,160 now this is not the case with Redux 18122 13:02:07,200 --> 13:02:15,116 token in fact it's much much much easier 18123 13:02:11,160 --> 13:02:18,596 and the way we can do that we simply go 18124 13:02:15,116 --> 13:02:20,936 with a reducer's property in the slice 18125 13:02:18,596 --> 13:02:22,560 and again key value pair 18126 13:02:20,936 --> 13:02:25,320 so in my case I'm going to call this 18127 13:02:22,560 --> 13:02:28,320 clear card that's going to be the name 18128 13:02:25,320 --> 13:02:31,380 of my reducer over here now as a 18129 13:02:28,320 --> 13:02:33,360 parameter this function gets a state and 18130 13:02:31,380 --> 13:02:36,060 notice how we don't have to return 18131 13:02:33,360 --> 13:02:40,140 anything I don't have to return the new 18132 13:02:36,060 --> 13:02:42,416 state and always avoid the mutation 18133 13:02:40,140 --> 13:02:45,060 basically remember with user reducer we 18134 13:02:42,416 --> 13:02:46,800 always always needed to return a new 18135 13:02:45,060 --> 13:02:49,740 state we don't have to do that right now 18136 13:02:46,800 --> 13:02:51,720 why because when we installed Redux 18137 13:02:49,740 --> 13:02:54,660 token we also installed email Library 18138 13:02:51,720 --> 13:02:57,916 which behind the scenes does all the 18139 13:02:54,660 --> 13:03:00,840 heavy lifting so in our case we can 18140 13:02:57,916 --> 13:03:02,756 modify the state or mutate the state 18141 13:03:00,840 --> 13:03:05,096 directly and yes you're not 18142 13:03:02,756 --> 13:03:07,436 hallucinating this is clearly the code 18143 13:03:05,096 --> 13:03:10,080 that mutates the state 18144 13:03:07,436 --> 13:03:13,080 the reason we can write such code is 18145 13:03:10,080 --> 13:03:17,460 because Redux toolkit comes with Emer 18146 13:03:13,080 --> 13:03:19,436 library now it's also really cool that 18147 13:03:17,460 --> 13:03:21,240 if we take a look at card slice 18148 13:03:19,436 --> 13:03:25,616 Something That We're logging right now 18149 13:03:21,240 --> 13:03:28,380 in the card slash.js we have actions and 18150 13:03:25,616 --> 13:03:31,800 the moment we create that reducer 18151 13:03:28,380 --> 13:03:34,860 will be able to see the action created 18152 13:03:31,800 --> 13:03:36,240 by the name of yes exactly the same 18153 13:03:34,860 --> 13:03:38,460 Clear card 18154 13:03:36,240 --> 13:03:40,560 and what that means is that we don't 18155 13:03:38,460 --> 13:03:43,200 need this code again remember with use 18156 13:03:40,560 --> 13:03:46,560 reducer we set up the action we set it 18157 13:03:43,200 --> 13:03:48,720 equal to string and we didn't set up the 18158 13:03:46,560 --> 13:03:49,800 action Creator per se when we used user 18159 13:03:48,720 --> 13:03:52,140 reducer 18160 13:03:49,800 --> 13:03:55,040 but the idea would be that we actually 18161 13:03:52,140 --> 13:03:57,900 set up a function that returns that type 18162 13:03:55,040 --> 13:03:59,520 so we don't need to run around with this 18163 13:03:57,900 --> 13:04:00,776 variable basically have right away 18164 13:03:59,520 --> 13:04:02,936 function and 18165 13:04:00,776 --> 13:04:05,640 optionally we can also pass in the 18166 13:04:02,936 --> 13:04:08,820 payload now why am I saying this because 18167 13:04:05,640 --> 13:04:11,096 now we can directly use this clear card 18168 13:04:08,820 --> 13:04:12,320 we don't need to set up the variable we 18169 13:04:11,096 --> 13:04:15,360 don't need to create the function 18170 13:04:12,320 --> 13:04:18,720 essentially it's already provided for us 18171 13:04:15,360 --> 13:04:20,936 and then in order to invoke it 18172 13:04:18,720 --> 13:04:24,720 we need to get another Hook from the 18173 13:04:20,936 --> 13:04:26,520 react Redux and this is use dispatch 18174 13:04:24,720 --> 13:04:29,220 so remember 18175 13:04:26,520 --> 13:04:31,616 with userducer we use dispatch in this 18176 13:04:29,220 --> 13:04:34,800 case we right away get it if we invoke 18177 13:04:31,616 --> 13:04:36,240 use dispatch and then we invoke this 18178 13:04:34,800 --> 13:04:39,240 patch so whatever we're getting back 18179 13:04:36,240 --> 13:04:42,240 from here and we just pass in action 18180 13:04:39,240 --> 13:04:45,116 Creator so yes we do need to import 18181 13:04:42,240 --> 13:04:47,520 clear card that's why we're exporting 18182 13:04:45,116 --> 13:04:49,916 over here on this export console and 18183 13:04:47,520 --> 13:04:51,116 then name import or I'm sorry name 18184 13:04:49,916 --> 13:04:53,820 export 18185 13:04:51,116 --> 13:04:55,680 and then we just need to use it and 18186 13:04:53,820 --> 13:04:57,240 don't worry if some of this seems 18187 13:04:55,680 --> 13:04:59,820 confusing at this point 18188 13:04:57,240 --> 13:05:02,520 it will make total sense once we 18189 13:04:59,820 --> 13:05:06,060 complete the entire setup so first I 18190 13:05:02,520 --> 13:05:09,180 want to navigate to cart slice like I 18191 13:05:06,060 --> 13:05:12,596 said we want to start over here by 18192 13:05:09,180 --> 13:05:15,300 setting up that reducer the one that is 18193 13:05:12,596 --> 13:05:17,276 going to clear the cut so let's go here 18194 13:05:15,300 --> 13:05:19,740 with a reducers 18195 13:05:17,276 --> 13:05:21,720 and it's going to be an object inside of 18196 13:05:19,740 --> 13:05:24,180 it we just need to come up with name for 18197 13:05:21,720 --> 13:05:26,416 our reducer okay so it's going to be 18198 13:05:24,180 --> 13:05:30,200 clear card it's going to be a function 18199 13:05:26,416 --> 13:05:33,480 that gets the access to the state now 18200 13:05:30,200 --> 13:05:34,916 also we'll have access to the payload 18201 13:05:33,480 --> 13:05:36,720 basically 18202 13:05:34,916 --> 13:05:38,756 let's say we want to pass in the ID 18203 13:05:36,720 --> 13:05:40,680 something we'll do a little bit later 18204 13:05:38,756 --> 13:05:42,540 which is going to be a second parameter 18205 13:05:40,680 --> 13:05:45,000 for now we don't need to worry about it 18206 13:05:42,540 --> 13:05:46,200 I'll set it up as a arrow function and 18207 13:05:45,000 --> 13:05:48,776 like I said 18208 13:05:46,200 --> 13:05:52,256 I know I keep repeating this but we can 18209 13:05:48,776 --> 13:05:55,140 mutate the state directly because behind 18210 13:05:52,256 --> 13:05:57,480 the scenes immer takes care of that so 18211 13:05:55,140 --> 13:05:59,756 we can go here with State then what is 18212 13:05:57,480 --> 13:06:01,740 the value here it is card items whatever 18213 13:05:59,756 --> 13:06:04,200 I want to do I want to set it equal to 18214 13:06:01,740 --> 13:06:06,776 an empty array that's it that's all I 18215 13:06:04,200 --> 13:06:10,680 want to do and now let's go to the cart 18216 13:06:06,776 --> 13:06:12,900 slice let's uncomment the log 18217 13:06:10,680 --> 13:06:15,480 and we'll see in the console that yes we 18218 13:06:12,900 --> 13:06:18,900 have name and all that but now in the 18219 13:06:15,480 --> 13:06:21,776 actions we have a function we have 18220 13:06:18,900 --> 13:06:24,720 action Creator by the name of clearcard 18221 13:06:21,776 --> 13:06:27,180 and again the beauty indices 18222 13:06:24,720 --> 13:06:28,916 the fact that we don't need to set up 18223 13:06:27,180 --> 13:06:31,256 anything manually as far as the action 18224 13:06:28,916 --> 13:06:33,360 as far as the action Creator we right 18225 13:06:31,256 --> 13:06:36,416 away get in by default 18226 13:06:33,360 --> 13:06:37,320 and the only thing we want to do is pass 18227 13:06:36,416 --> 13:06:39,900 in 18228 13:06:37,320 --> 13:06:42,360 the clear card function 18229 13:06:39,900 --> 13:06:44,580 into whatever we're getting back from 18230 13:06:42,360 --> 13:06:48,360 this bachelor use dispatch and then pass 18231 13:06:44,580 --> 13:06:50,640 it in so let's go back to the slice and 18232 13:06:48,360 --> 13:06:52,436 instead of logging which as I said I'll 18233 13:06:50,640 --> 13:06:55,500 comment this out we want to go with 18234 13:06:52,436 --> 13:06:58,140 export const so all of these will be 18235 13:06:55,500 --> 13:06:59,700 named and yes we'll add more functions 18236 13:06:58,140 --> 13:07:02,040 of course over here 18237 13:06:59,700 --> 13:07:04,320 and let's go with clear card and now 18238 13:07:02,040 --> 13:07:07,436 where are we looking we're looking in 18239 13:07:04,320 --> 13:07:11,340 the cart slice dot actions so let's set 18240 13:07:07,436 --> 13:07:14,460 it equal to cart slash Dot and actions 18241 13:07:11,340 --> 13:07:16,916 so from that object we export this and 18242 13:07:14,460 --> 13:07:19,740 now we just need to decide well in which 18243 13:07:16,916 --> 13:07:21,720 component we're going to use it of 18244 13:07:19,740 --> 13:07:24,060 course in our case that will be cart 18245 13:07:21,720 --> 13:07:24,900 container yes eventually we'll set up 18246 13:07:24,060 --> 13:07:27,660 the model 18247 13:07:24,900 --> 13:07:30,480 so we will move that functionality from 18248 13:07:27,660 --> 13:07:31,200 the card container to the model but for 18249 13:07:30,480 --> 13:07:33,840 now 18250 13:07:31,200 --> 13:07:35,880 let's just see how we can invoke it in 18251 13:07:33,840 --> 13:07:38,700 here so let's say use 18252 13:07:35,880 --> 13:07:42,116 this patch that's the first one then 18253 13:07:38,700 --> 13:07:44,416 comma and Below or above it doesn't 18254 13:07:42,116 --> 13:07:47,040 really matter we want to go with that 18255 13:07:44,416 --> 13:07:50,460 huge dispatch 18256 13:07:47,040 --> 13:07:53,096 so const is going to be equal to this 18257 13:07:50,460 --> 13:07:54,960 patch whatever we get back use dispatch 18258 13:07:53,096 --> 13:07:57,416 invoke we don't need to pass anything in 18259 13:07:54,960 --> 13:07:59,460 then let's keep on scrolling keep on 18260 13:07:57,416 --> 13:08:01,860 scrolling this is where we have our 18261 13:07:59,460 --> 13:08:03,360 button and now we do want to pass in the 18262 13:08:01,860 --> 13:08:05,700 arrow function because of course I don't 18263 13:08:03,360 --> 13:08:08,040 want to invoke this immediately I only 18264 13:08:05,700 --> 13:08:12,000 want to invoke it when we click on a 18265 13:08:08,040 --> 13:08:15,116 button so let's say here on click 18266 13:08:12,000 --> 13:08:17,276 then let's pass in the arrow function 18267 13:08:15,116 --> 13:08:20,220 and we'll say that every time we'll 18268 13:08:17,276 --> 13:08:23,520 click on a button will dispatch 18269 13:08:20,220 --> 13:08:26,640 an action and in here let's pass in that 18270 13:08:23,520 --> 13:08:29,220 clear card again 18271 13:08:26,640 --> 13:08:32,880 the beauty here is that we don't need to 18272 13:08:29,220 --> 13:08:35,460 do all of that manual labor we just pass 18273 13:08:32,880 --> 13:08:37,616 in the function that the side note I 18274 13:08:35,460 --> 13:08:40,020 didn't import so let me go back we want 18275 13:08:37,616 --> 13:08:43,616 to go with clear card which is coming 18276 13:08:40,020 --> 13:08:45,660 from the card slice now let me save it 18277 13:08:43,616 --> 13:08:48,840 and in here it says unknown event 18278 13:08:45,660 --> 13:08:52,500 handler because I probably did some oh 18279 13:08:48,840 --> 13:08:55,080 yeah this is not right and once we click 18280 13:08:52,500 --> 13:08:58,140 this is what we'll see now we're not 18281 13:08:55,080 --> 13:09:00,116 displaying this one the condition simply 18282 13:08:58,140 --> 13:09:03,540 because we're not controlling the amount 18283 13:09:00,116 --> 13:09:05,460 yet don't worry all of that is coming up 18284 13:09:03,540 --> 13:09:08,936 for time being we are just displaying 18285 13:09:05,460 --> 13:09:11,756 The Heading 2 with your back here and 18286 13:09:08,936 --> 13:09:14,580 the total basically since we removed all 18287 13:09:11,756 --> 13:09:17,220 of the items since it's set equal to an 18288 13:09:14,580 --> 13:09:21,000 empty array we just have the your back 18289 13:09:17,220 --> 13:09:24,540 and the total and with this in place now 18290 13:09:21,000 --> 13:09:26,700 we can set up rest of the reducers and 18291 13:09:24,540 --> 13:09:30,240 while we're still on the topic of State 18292 13:09:26,700 --> 13:09:32,340 Management in the Redux toolkit reducers 18293 13:09:30,240 --> 13:09:34,500 let's also cover 18294 13:09:32,340 --> 13:09:36,360 an alternative option 18295 13:09:34,500 --> 13:09:40,256 and that is simply the fact that we can 18296 13:09:36,360 --> 13:09:42,240 return a new state from the reducer so 18297 13:09:40,256 --> 13:09:44,820 let me comment this one out let me type 18298 13:09:42,240 --> 13:09:47,936 here return but we need to be aware of 18299 13:09:44,820 --> 13:09:50,220 the major gacha and that is following 18300 13:09:47,936 --> 13:09:53,756 whatever we will return 18301 13:09:50,220 --> 13:09:56,820 from reducer as our new state will 18302 13:09:53,756 --> 13:10:00,180 become the new state so let's say if I'm 18303 13:09:56,820 --> 13:10:02,820 going to return an empty object yes our 18304 13:10:00,180 --> 13:10:05,160 state will immediately turn into an 18305 13:10:02,820 --> 13:10:06,960 empty object without any of these 18306 13:10:05,160 --> 13:10:10,800 properties same works if I'm gonna 18307 13:10:06,960 --> 13:10:13,680 return here empty array so let's say if 18308 13:10:10,800 --> 13:10:17,936 I return an object and if I just say 18309 13:10:13,680 --> 13:10:21,240 card items equals to an empty array yes 18310 13:10:17,936 --> 13:10:23,880 I will update this property 18311 13:10:21,240 --> 13:10:26,756 but automatically I'll remove these ones 18312 13:10:23,880 --> 13:10:28,800 just to Showcase that let me save it let 18313 13:10:26,756 --> 13:10:32,040 me click on clear cut and you'll notice 18314 13:10:28,800 --> 13:10:34,916 that now I'm nothing here as far as the 18315 13:10:32,040 --> 13:10:37,436 amount as well as the total and I can 18316 13:10:34,916 --> 13:10:41,276 clearly see that if I navigate two 18317 13:10:37,436 --> 13:10:43,140 adapto's so let me open up the Redux 18318 13:10:41,276 --> 13:10:46,916 death tools on a small browser window 18319 13:10:43,140 --> 13:10:50,160 and then when it comes to our state 18320 13:10:46,916 --> 13:10:52,980 notice all of the properties are missing 18321 13:10:50,160 --> 13:10:55,140 now only the card items is there 18322 13:10:52,980 --> 13:10:57,240 so let me comment this one out and let's 18323 13:10:55,140 --> 13:11:01,256 take a look at the use case where 18324 13:10:57,240 --> 13:11:04,380 this is going to come in handy so in one 18325 13:11:01,256 --> 13:11:07,140 of our upcoming projects we'll have some 18326 13:11:04,380 --> 13:11:10,140 pages and in there we'll have a bunch of 18327 13:11:07,140 --> 13:11:11,460 inputs that are going to be our state 18328 13:11:10,140 --> 13:11:13,500 values 18329 13:11:11,460 --> 13:11:15,596 and essentially there's also going to be 18330 13:11:13,500 --> 13:11:18,060 a clear button 18331 13:11:15,596 --> 13:11:19,980 and the idea is like this once the user 18332 13:11:18,060 --> 13:11:22,380 starts typing something let's say 18333 13:11:19,980 --> 13:11:24,480 changes some values around here 18334 13:11:22,380 --> 13:11:27,596 at some point maybe he or she wants to 18335 13:11:24,480 --> 13:11:30,840 clear out all the input values so in 18336 13:11:27,596 --> 13:11:32,040 that case we're gonna take that initial 18337 13:11:30,840 --> 13:11:34,616 state 18338 13:11:32,040 --> 13:11:36,960 and we'll just return that from our 18339 13:11:34,616 --> 13:11:39,900 reducer so let's say you set up your 18340 13:11:36,960 --> 13:11:41,400 state with whatever default values 18341 13:11:39,900 --> 13:11:44,460 and then 18342 13:11:41,400 --> 13:11:46,616 when you have the reducer that just sets 18343 13:11:44,460 --> 13:11:49,616 it back to default you just pass that 18344 13:11:46,616 --> 13:11:51,840 initial State as a return in your 18345 13:11:49,616 --> 13:11:54,840 Edition so that's one of the use cases 18346 13:11:51,840 --> 13:11:57,300 where this comes in handy but we do need 18347 13:11:54,840 --> 13:12:00,500 to be careful again whatever we're going 18348 13:11:57,300 --> 13:12:06,060 to return from the reducer will become 18349 13:12:00,500 --> 13:12:08,520 that new state value so if we omit some 18350 13:12:06,060 --> 13:12:12,000 of the properties yes they will be 18351 13:12:08,520 --> 13:12:13,080 excluded okay and once we can clear the 18352 13:12:12,000 --> 13:12:15,840 cart 18353 13:12:13,080 --> 13:12:18,300 it's a smooth sailing from here 18354 13:12:15,840 --> 13:12:21,840 we just want to set up those functions 18355 13:12:18,300 --> 13:12:25,140 we want to add the functionality and yes 18356 13:12:21,840 --> 13:12:26,756 we will take a look at the action how it 18357 13:12:25,140 --> 13:12:29,700 looks like and why we're looking for the 18358 13:12:26,756 --> 13:12:32,040 payload and I know I said this already a 18359 13:12:29,700 --> 13:12:36,000 million different times but 18360 13:12:32,040 --> 13:12:37,500 since right now we can modify the state 18361 13:12:36,000 --> 13:12:39,660 directly 18362 13:12:37,500 --> 13:12:44,220 there's tons of ways how you can set 18363 13:12:39,660 --> 13:12:47,520 this up now I purposely kind of tried to 18364 13:12:44,220 --> 13:12:48,900 showcase the mutation approach where yes 18365 13:12:47,520 --> 13:12:50,820 of course 18366 13:12:48,900 --> 13:12:55,380 in these examples for increase and 18367 13:12:50,820 --> 13:12:58,436 decrease we can return a new array 18368 13:12:55,380 --> 13:13:01,916 but I purposely just found the exact 18369 13:12:58,436 --> 13:13:04,680 item and then updated the amount plus or 18370 13:13:01,916 --> 13:13:07,436 minus and also of course you can combine 18371 13:13:04,680 --> 13:13:11,160 increase and decrease into one function 18372 13:13:07,436 --> 13:13:13,256 let's say you can call this toggle and 18373 13:13:11,160 --> 13:13:15,116 of course you'll just have to provide 18374 13:13:13,256 --> 13:13:16,860 here more values whether you're 18375 13:13:15,116 --> 13:13:19,200 increasing the equation that could be 18376 13:13:16,860 --> 13:13:21,000 your challenge for now we just want to 18377 13:13:19,200 --> 13:13:23,096 focus on remove 18378 13:13:21,000 --> 13:13:25,616 item and the first thing that I want to 18379 13:13:23,096 --> 13:13:28,436 showcase is the fact that we can access 18380 13:13:25,616 --> 13:13:30,360 the payload so whatever we pass in 18381 13:13:28,436 --> 13:13:33,116 because in order 18382 13:13:30,360 --> 13:13:35,580 to get the specific item in order to 18383 13:13:33,116 --> 13:13:38,700 remove it I do need to get the Eddie 18384 13:13:35,580 --> 13:13:40,380 correct if you're paying attention in 18385 13:13:38,700 --> 13:13:42,180 the card item we did this structure 18386 13:13:40,380 --> 13:13:44,820 already however 18387 13:13:42,180 --> 13:13:47,580 we haven't used it here so this is 18388 13:13:44,820 --> 13:13:48,660 something that we will pass into the 18389 13:13:47,580 --> 13:13:50,700 function 18390 13:13:48,660 --> 13:13:53,276 and essentially as far as the remove 18391 13:13:50,700 --> 13:13:55,256 item I'll get it from action.payload 18392 13:13:53,276 --> 13:13:57,540 because that is a structure I'll name 18393 13:13:55,256 --> 13:14:00,840 this item ID 18394 13:13:57,540 --> 13:14:03,480 and then I just want to set equal State 18395 13:14:00,840 --> 13:14:05,520 card items to the new array now I wasn't 18396 13:14:03,480 --> 13:14:06,960 doing this purposely again there's tons 18397 13:14:05,520 --> 13:14:08,640 of ways how you can set it up but in 18398 13:14:06,960 --> 13:14:11,040 like I said just thought that filter is 18399 13:14:08,640 --> 13:14:13,980 the fastest one so let me go to state 18400 13:14:11,040 --> 13:14:15,616 DOT card items we filter it we look for 18401 13:14:13,980 --> 13:14:19,256 specific item 18402 13:14:15,616 --> 13:14:22,140 that does not match the Eddie and we 18403 13:14:19,256 --> 13:14:24,240 return that into this car items so if 18404 13:14:22,140 --> 13:14:26,580 the ID matches 18405 13:14:24,240 --> 13:14:30,300 so whatever we pass in Manchester to the 18406 13:14:26,580 --> 13:14:32,820 80 then that item won't get returned and 18407 13:14:30,300 --> 13:14:36,720 in the process we'll move it from this 18408 13:14:32,820 --> 13:14:38,700 array so let's try this one out where in 18409 13:14:36,720 --> 13:14:41,640 the cart slash 18410 13:14:38,700 --> 13:14:44,756 let's set up that remove item so I'm 18411 13:14:41,640 --> 13:14:47,340 gonna go here with a move item and first 18412 13:14:44,756 --> 13:14:50,756 I just want to showcase 18413 13:14:47,340 --> 13:14:52,916 how we can access the payload so first 18414 13:14:50,756 --> 13:14:56,040 one is the state so we access the state 18415 13:14:52,916 --> 13:14:57,960 here then I'm gonna go with action and 18416 13:14:56,040 --> 13:15:00,360 later I'll showcase how we can structure 18417 13:14:57,960 --> 13:15:02,460 that and for time being let's just look 18418 13:15:00,360 --> 13:15:05,820 for the action and now let's think about 18419 13:15:02,460 --> 13:15:08,096 it where do we want to access all these 18420 13:15:05,820 --> 13:15:12,116 functions they remove items increase 18421 13:15:08,096 --> 13:15:14,640 decrease and not calculate totals so 18422 13:15:12,116 --> 13:15:18,000 only these three well we want to get 18423 13:15:14,640 --> 13:15:20,400 that in the car item so first in the 18424 13:15:18,000 --> 13:15:23,160 card slice we want to export that again 18425 13:15:20,400 --> 13:15:24,840 what's really cool is the fact that the 18426 13:15:23,160 --> 13:15:27,776 name is exactly the same we don't really 18427 13:15:24,840 --> 13:15:30,900 need to think about it and as I said it 18428 13:15:27,776 --> 13:15:32,460 shouldn't be remove items it should be a 18429 13:15:30,900 --> 13:15:36,116 remove item I apologize for some reason 18430 13:15:32,460 --> 13:15:39,416 I just keep adding those s's there then 18431 13:15:36,116 --> 13:15:43,380 let's navigate to the card item first we 18432 13:15:39,416 --> 13:15:45,776 want to import the remove item so we're 18433 13:15:43,380 --> 13:15:47,880 looking for a remove item 18434 13:15:45,776 --> 13:15:50,880 and notice how right away we get that 18435 13:15:47,880 --> 13:15:53,936 named import and also we want to get 18436 13:15:50,880 --> 13:15:56,936 that dispatch so let's just set it up 18437 13:15:53,936 --> 13:16:00,596 over here where we'll say import then 18438 13:15:56,936 --> 13:16:03,240 use dispatch that is coming from react 18439 13:16:00,596 --> 13:16:05,936 Redux then 18440 13:16:03,240 --> 13:16:08,040 the very top we're gonna go here with 18441 13:16:05,936 --> 13:16:10,800 const dispatch 18442 13:16:08,040 --> 13:16:13,080 and that will be equal to use dispatch 18443 13:16:10,800 --> 13:16:15,660 let's invoke it and then let's look for 18444 13:16:13,080 --> 13:16:18,776 that remove button 18445 13:16:15,660 --> 13:16:19,860 so now basically every time you click on 18446 13:16:18,776 --> 13:16:23,040 a button 18447 13:16:19,860 --> 13:16:25,256 we will remove that specific item so 18448 13:16:23,040 --> 13:16:27,300 let's say here on click 18449 13:16:25,256 --> 13:16:29,756 again we do want to pass in the arrow 18450 13:16:27,300 --> 13:16:33,240 function first and then let's say 18451 13:16:29,756 --> 13:16:35,880 dispatch and we'll go with a remove item 18452 13:16:33,240 --> 13:16:38,640 and now let's pass in the ID 18453 13:16:35,880 --> 13:16:41,400 so first thing what you'll see once you 18454 13:16:38,640 --> 13:16:43,380 click is the log in the console so let's 18455 13:16:41,400 --> 13:16:45,480 click and check it out 18456 13:16:43,380 --> 13:16:47,340 in here we see two things 18457 13:16:45,480 --> 13:16:49,916 first we see type 18458 13:16:47,340 --> 13:16:52,140 so notice how that is right away already 18459 13:16:49,916 --> 13:16:54,480 set up for us like I said we don't need 18460 13:16:52,140 --> 13:16:57,416 to do anything right away it has cart 18461 13:16:54,480 --> 13:16:59,756 and then remove item so this is the 18462 13:16:57,416 --> 13:17:02,640 action that we're dispatching and we 18463 13:16:59,756 --> 13:17:05,096 also get the payload now can you pass in 18464 13:17:02,640 --> 13:17:08,160 the payload as an object absolutely but 18465 13:17:05,096 --> 13:17:10,860 if we pass in as a simple property 18466 13:17:08,160 --> 13:17:12,300 then we have payload is equal to 18467 13:17:10,860 --> 13:17:14,936 whatever 18468 13:17:12,300 --> 13:17:17,400 so knowing this we can set up rest of 18469 13:17:14,936 --> 13:17:18,660 the functionality where in the card 18470 13:17:17,400 --> 13:17:22,916 slice 18471 13:17:18,660 --> 13:17:26,880 I'll assign action.payload equal to an 18472 13:17:22,916 --> 13:17:30,416 ID const and I think I'm gonna call this 18473 13:17:26,880 --> 13:17:32,340 product ID or sorry now I'll call this 18474 13:17:30,416 --> 13:17:37,560 item ID so const 18475 13:17:32,340 --> 13:17:39,180 Item ID is equal to action payload 18476 13:17:37,560 --> 13:17:43,256 and then 18477 13:17:39,180 --> 13:17:46,380 let's set up the new value for the state 18478 13:17:43,256 --> 13:17:48,300 card items State again yes we're 18479 13:17:46,380 --> 13:17:51,360 modifying this we're going to go with 18480 13:17:48,300 --> 13:17:54,060 cart items is equal to and now let's 18481 13:17:51,360 --> 13:17:56,700 access the old value basically before 18482 13:17:54,060 --> 13:17:59,580 the update and we can do that with State 18483 13:17:56,700 --> 13:18:03,960 card items then let's run filter and 18484 13:17:59,580 --> 13:18:06,720 like I said if the ID matches the item 18485 13:18:03,960 --> 13:18:09,240 won't be returned so essentially we'll 18486 13:18:06,720 --> 13:18:11,580 remove it from our cart items array 18487 13:18:09,240 --> 13:18:13,320 we're going to call this items and I'm 18488 13:18:11,580 --> 13:18:16,500 gonna go with implicit return where I'm 18489 13:18:13,320 --> 13:18:20,040 going to say if item id does not match 18490 13:18:16,500 --> 13:18:21,116 the item id only then return it from the 18491 13:18:20,040 --> 13:18:24,800 array 18492 13:18:21,116 --> 13:18:28,500 and as a result check it out and click 18493 13:18:24,800 --> 13:18:31,616 and we just keep removing the items from 18494 13:18:28,500 --> 13:18:34,560 our bag again yes we're not affecting 18495 13:18:31,616 --> 13:18:37,080 the amount as well as the total all of 18496 13:18:34,560 --> 13:18:41,096 that is coming up but we should be able 18497 13:18:37,080 --> 13:18:45,596 to remove all of the items from our cart 18498 13:18:41,096 --> 13:18:48,596 be it with clear card or individually by 18499 13:18:45,596 --> 13:18:51,000 clicking on the remove button all right 18500 13:18:48,596 --> 13:18:53,400 and now once we're familiar now we can 18501 13:18:51,000 --> 13:18:56,936 remove item let's also do the same thing 18502 13:18:53,400 --> 13:18:58,980 with increase and decrease now in order 18503 13:18:56,936 --> 13:19:01,080 to make things interesting I already 18504 13:18:58,980 --> 13:19:02,820 destructured the payload so remember 18505 13:19:01,080 --> 13:19:04,380 we're getting the action object and 18506 13:19:02,820 --> 13:19:07,200 we're looking for the payload so in this 18507 13:19:04,380 --> 13:19:09,776 case I just destructured that in the 18508 13:19:07,200 --> 13:19:12,416 case of increase I'm going to State card 18509 13:19:09,776 --> 13:19:16,436 items find so we get that specific item 18510 13:19:12,416 --> 13:19:19,020 in this case where the ID matches unlike 18511 13:19:16,436 --> 13:19:21,180 the filter approach and then I just want 18512 13:19:19,020 --> 13:19:23,640 to update the amount so every time I'll 18513 13:19:21,180 --> 13:19:28,140 click on the increase button will 18514 13:19:23,640 --> 13:19:31,080 increase the amount of specific items we 18515 13:19:28,140 --> 13:19:32,040 have in the cart for this phone or that 18516 13:19:31,080 --> 13:19:33,720 phone 18517 13:19:32,040 --> 13:19:36,540 and hopefully you see where I'm going 18518 13:19:33,720 --> 13:19:39,596 with this and eventually of course this 18519 13:19:36,540 --> 13:19:42,116 will also affect the amount as well as 18520 13:19:39,596 --> 13:19:45,300 the total and yes I'll set them up 18521 13:19:42,116 --> 13:19:47,220 pretty much right away since the 18522 13:19:45,300 --> 13:19:48,596 functionality is exactly the same apart 18523 13:19:47,220 --> 13:19:50,820 from the fact that in this case we 18524 13:19:48,596 --> 13:19:52,500 increase and in this case we decreasing 18525 13:19:50,820 --> 13:19:55,460 like I said if you want to challenge 18526 13:19:52,500 --> 13:19:58,436 yourself set up a toggle functionality 18527 13:19:55,460 --> 13:20:01,020 where you look for one more value 18528 13:19:58,436 --> 13:20:03,180 increase or decrease which of course is 18529 13:20:01,020 --> 13:20:04,916 going to be in the payload and then 18530 13:20:03,180 --> 13:20:07,256 depending on that set up the 18531 13:20:04,916 --> 13:20:10,380 functionality but for now let's just 18532 13:20:07,256 --> 13:20:14,040 worry about these two so we're gonna 18533 13:20:10,380 --> 13:20:18,240 navigate to cart slash and then right 18534 13:20:14,040 --> 13:20:20,520 after move item let's say here increase 18535 13:20:18,240 --> 13:20:23,276 let's set up our function 18536 13:20:20,520 --> 13:20:25,680 let's say State like I said we'll right 18537 13:20:23,276 --> 13:20:27,840 away look for the payload 18538 13:20:25,680 --> 13:20:30,660 I'll set it equal to 18539 13:20:27,840 --> 13:20:32,756 and when it comes to functionality we 18540 13:20:30,660 --> 13:20:36,240 want to go with const first we want to 18541 13:20:32,756 --> 13:20:38,160 get that card item each specific one 18542 13:20:36,240 --> 13:20:39,840 where the ID matches and we can do that 18543 13:20:38,160 --> 13:20:43,800 with state 18544 13:20:39,840 --> 13:20:46,860 card items dot find then let's pass in 18545 13:20:43,800 --> 13:20:52,320 the Callback function let's say item and 18546 13:20:46,860 --> 13:20:55,020 then Item ID is equal to payload and 18547 13:20:52,320 --> 13:20:58,256 we'll pass this in as an object so it's 18548 13:20:55,020 --> 13:20:59,936 going to be payload 18549 13:20:58,256 --> 13:21:01,500 dot ID 18550 13:20:59,936 --> 13:21:04,500 and then 18551 13:21:01,500 --> 13:21:07,320 let's increase that amount so now I'm 18552 13:21:04,500 --> 13:21:09,300 accessing that one specific card item I 18553 13:21:07,320 --> 13:21:12,360 have the amount property and please 18554 13:21:09,300 --> 13:21:14,640 don't confuse this with the total amount 18555 13:21:12,360 --> 13:21:16,616 so that's the total with all of the 18556 13:21:14,640 --> 13:21:19,020 items that we have in a cart 18557 13:21:16,616 --> 13:21:21,116 when we're talking about this cart item 18558 13:21:19,020 --> 13:21:24,596 on property we're talking about this one 18559 13:21:21,116 --> 13:21:27,660 over here for that specific card item 18560 13:21:24,596 --> 13:21:28,980 and we'll just say card item 18561 13:21:27,660 --> 13:21:32,640 mount 18562 13:21:28,980 --> 13:21:36,540 so whatever it is right now plus or yeah 18563 13:21:32,640 --> 13:21:40,500 in this case plus one and then 18564 13:21:36,540 --> 13:21:42,900 in order to set up decrease you want to 18565 13:21:40,500 --> 13:21:45,360 copy and paste you want to go with 18566 13:21:42,900 --> 13:21:47,220 decrease still going to be a payload 18567 13:21:45,360 --> 13:21:49,380 okay all of that is awesome we'll still 18568 13:21:47,220 --> 13:21:53,820 look for the card item the difference 18569 13:21:49,380 --> 13:21:57,116 here is minus one and then we will add 18570 13:21:53,820 --> 13:21:59,276 them of course to our exports so let's 18571 13:21:57,116 --> 13:22:02,820 say increase 18572 13:21:59,276 --> 13:22:04,980 decrease and we want to navigate to the 18573 13:22:02,820 --> 13:22:08,276 card item 18574 13:22:04,980 --> 13:22:11,580 we want to import the other two as well 18575 13:22:08,276 --> 13:22:13,500 so we're going to go here with increase 18576 13:22:11,580 --> 13:22:16,680 then decrease 18577 13:22:13,500 --> 13:22:18,480 both of them and first let's set up the 18578 13:22:16,680 --> 13:22:20,220 increase because for a decrease we'll 18579 13:22:18,480 --> 13:22:21,480 have to add a little bit more 18580 13:22:20,220 --> 13:22:24,900 functionality 18581 13:22:21,480 --> 13:22:28,916 so let's go with mount button let's say 18582 13:22:24,900 --> 13:22:31,436 on click we already have dispatch so we 18583 13:22:28,916 --> 13:22:34,616 don't need to worry about that one and 18584 13:22:31,436 --> 13:22:38,160 we just want to pass in the increase 18585 13:22:34,616 --> 13:22:40,560 so we're gonna go here with this patch 18586 13:22:38,160 --> 13:22:43,020 and then inside of this patch let's pass 18587 13:22:40,560 --> 13:22:44,756 in the increase and like I said in order 18588 13:22:43,020 --> 13:22:48,300 to make this interesting I'm gonna go 18589 13:22:44,756 --> 13:22:49,800 with an ID now normally if you're 18590 13:22:48,300 --> 13:22:52,616 setting this up as an object of course 18591 13:22:49,800 --> 13:22:55,740 you'll pass in more properties 18592 13:22:52,616 --> 13:22:58,800 but in this case I won't do that and for 18593 13:22:55,740 --> 13:23:00,596 some reason oh yeah my bad so I set up 18594 13:22:58,800 --> 13:23:03,116 the import in the wrong place 18595 13:23:00,596 --> 13:23:04,740 me out of comma here 18596 13:23:03,116 --> 13:23:08,400 and hopefully everything is going to 18597 13:23:04,740 --> 13:23:11,096 work Yep looks about right and if we try 18598 13:23:08,400 --> 13:23:13,800 this out notice how again we're changing 18599 13:23:11,096 --> 13:23:16,200 this value over here for each item and 18600 13:23:13,800 --> 13:23:19,256 again the functionality here is 18601 13:23:16,200 --> 13:23:21,180 following where we look for the payload 18602 13:23:19,256 --> 13:23:24,360 which in this case is an object that's 18603 13:23:21,180 --> 13:23:26,040 why we go here with payload dot ID just 18604 13:23:24,360 --> 13:23:28,140 to Showcase that we can pass in more 18605 13:23:26,040 --> 13:23:30,660 data and then we just increase the 18606 13:23:28,140 --> 13:23:32,400 amount and the same thing we can do with 18607 13:23:30,660 --> 13:23:33,720 decrease so let's go back to the card 18608 13:23:32,400 --> 13:23:36,960 item 18609 13:23:33,720 --> 13:23:41,880 I'll take this entire thing 40 on click 18610 13:23:36,960 --> 13:23:44,820 or copy and paste and now let's go with 18611 13:23:41,880 --> 13:23:47,096 decrease instead so say decrease again 18612 13:23:44,820 --> 13:23:49,200 pass in the area that doesn't change 18613 13:23:47,096 --> 13:23:51,840 it's refreshed so we start from scratch 18614 13:23:49,200 --> 13:23:54,180 and now notice how we can decrease the 18615 13:23:51,840 --> 13:23:55,860 amount now the problem is that we're 18616 13:23:54,180 --> 13:23:59,040 going negative 18617 13:23:55,860 --> 13:24:03,060 so I think it's going to make a bit more 18618 13:23:59,040 --> 13:24:06,240 sense if we'll check for the amount and 18619 13:24:03,060 --> 13:24:09,540 again we're talking about the amount of 18620 13:24:06,240 --> 13:24:11,640 specific item not overall amount and if 18621 13:24:09,540 --> 13:24:13,436 it is equal to one 18622 13:24:11,640 --> 13:24:16,380 we want to remove it since that way 18623 13:24:13,436 --> 13:24:18,480 we'll have zero items in the cart 18624 13:24:16,380 --> 13:24:20,180 and if it's bigger than one then of 18625 13:24:18,480 --> 13:24:23,820 course we just want to run the decrease 18626 13:24:20,180 --> 13:24:27,060 so I'm going to go back to this on click 18627 13:24:23,820 --> 13:24:29,340 and we'll set up if amount is equal to 18628 13:24:27,060 --> 13:24:31,320 one that means that we're pressing the 18629 13:24:29,340 --> 13:24:33,240 button amount is already equal to one 18630 13:24:31,320 --> 13:24:35,220 and if that is the case well we might as 18631 13:24:33,240 --> 13:24:38,096 well remove the item from the card 18632 13:24:35,220 --> 13:24:40,020 correct we'll say remove item and we'll 18633 13:24:38,096 --> 13:24:42,960 just pass in the edit now we do want to 18634 13:24:40,020 --> 13:24:45,840 pass the return right after because you 18635 13:24:42,960 --> 13:24:47,880 don't want to continue reading the code 18636 13:24:45,840 --> 13:24:50,820 and if the amount is bigger than one 18637 13:24:47,880 --> 13:24:52,740 then of course we just decrease the 18638 13:24:50,820 --> 13:24:55,800 amount so let me go here let me refresh 18639 13:24:52,740 --> 13:24:58,436 and notice I can increase I can actually 18640 13:24:55,800 --> 13:25:00,300 decrease so all of that is working but 18641 13:24:58,436 --> 13:25:03,900 the moment I get 18642 13:25:00,300 --> 13:25:06,540 the amount of one if I will click the 18643 13:25:03,900 --> 13:25:09,960 decrease I'll automatically remove the 18644 13:25:06,540 --> 13:25:12,140 item from the cart all right and up next 18645 13:25:09,960 --> 13:25:14,276 let's take a look at how we can 18646 13:25:12,140 --> 13:25:15,596 calculate the totals because at the 18647 13:25:14,276 --> 13:25:20,700 moment yes 18648 13:25:15,596 --> 13:25:23,276 we can nicely control our cart but that 18649 13:25:20,700 --> 13:25:25,436 is not affecting the amount then we have 18650 13:25:23,276 --> 13:25:28,616 all the way at the top and also it's not 18651 13:25:25,436 --> 13:25:30,660 affecting the total and in order to fix 18652 13:25:28,616 --> 13:25:33,960 that we'll come up with new reducer 18653 13:25:30,660 --> 13:25:37,200 we'll call this calculate totals and I 18654 13:25:33,960 --> 13:25:39,660 went with this functionality so I 18655 13:25:37,200 --> 13:25:43,680 created new variables amount and total 18656 13:25:39,660 --> 13:25:47,040 then I iterate over the current cart 18657 13:25:43,680 --> 13:25:47,936 items and each of them has the amount of 18658 13:25:47,040 --> 13:25:52,140 property 18659 13:25:47,936 --> 13:25:54,480 so I just add to the overall amount one 18660 13:25:52,140 --> 13:25:57,840 so the one that represents the entire 18661 13:25:54,480 --> 13:26:00,840 card and the same goes for a total only 18662 13:25:57,840 --> 13:26:03,720 in this case I multiply 18663 13:26:00,840 --> 13:26:06,596 the amount of items I have with the 18664 13:26:03,720 --> 13:26:09,960 price and that is equal to the total one 18665 13:26:06,596 --> 13:26:11,820 so I set up the loop I add 18666 13:26:09,960 --> 13:26:13,800 both of these values and at the very end 18667 13:26:11,820 --> 13:26:15,960 and just go with State amount is equal 18668 13:26:13,800 --> 13:26:18,960 to amount so whatever we created over 18669 13:26:15,960 --> 13:26:21,000 here the same goes for total now where 18670 13:26:18,960 --> 13:26:22,740 do we want to invoke that one that's a 18671 13:26:21,000 --> 13:26:25,916 good question let's keep on scrolling in 18672 13:26:22,740 --> 13:26:27,540 the readme and I actually set this one 18673 13:26:25,916 --> 13:26:29,400 up in the app 18674 13:26:27,540 --> 13:26:32,040 so in the app 18675 13:26:29,400 --> 13:26:34,916 I will set up a use effect 18676 13:26:32,040 --> 13:26:38,160 on that user effect will depend on the 18677 13:26:34,916 --> 13:26:40,680 car items so use use selector grab the 18678 13:26:38,160 --> 13:26:43,200 card items from the cart and then every 18679 13:26:40,680 --> 13:26:45,360 time there's going to be some change in 18680 13:26:43,200 --> 13:26:48,000 the car atoms will dispatch the 18681 13:26:45,360 --> 13:26:51,020 calculate totals and as a result 18682 13:26:48,000 --> 13:26:55,680 both of those values will change in here 18683 13:26:51,020 --> 13:26:57,596 as well as in the total so let's try to 18684 13:26:55,680 --> 13:26:59,540 set this one up and let's start by 18685 13:26:57,596 --> 13:27:03,116 creating the function 18686 13:26:59,540 --> 13:27:05,160 calculate and then totals 18687 13:27:03,116 --> 13:27:07,860 again it's going to be our function 18688 13:27:05,160 --> 13:27:11,340 we'll access the state as far as the 18689 13:27:07,860 --> 13:27:15,000 logic let's go with let amount is equal 18690 13:27:11,340 --> 13:27:17,160 to zero and let's say not I have a tiny 18691 13:27:15,000 --> 13:27:20,460 bug over here then I want to copy and 18692 13:27:17,160 --> 13:27:22,080 paste and let's just say total so by 18693 13:27:20,460 --> 13:27:23,096 default it's always going to be zero and 18694 13:27:22,080 --> 13:27:26,520 zero 18695 13:27:23,096 --> 13:27:30,300 then we want to go with State then cart 18696 13:27:26,520 --> 13:27:33,000 items let's iterate over so call this 18697 13:27:30,300 --> 13:27:35,460 for each and then we'll access each and 18698 13:27:33,000 --> 13:27:37,980 every item and each and every item has 18699 13:27:35,460 --> 13:27:39,900 the amount and Total Property and we 18700 13:27:37,980 --> 13:27:41,580 just want to add this to the total 18701 13:27:39,900 --> 13:27:46,320 amount therefore we'll go with plus 18702 13:27:41,580 --> 13:27:49,380 equals and then item amount now when it 18703 13:27:46,320 --> 13:27:52,800 comes to the amount of money we want to 18704 13:27:49,380 --> 13:27:55,560 multiply the price that each item has 18705 13:27:52,800 --> 13:27:58,256 with the amount of items we have in a 18706 13:27:55,560 --> 13:28:01,880 cart so total is equal to and now in 18707 13:27:58,256 --> 13:28:06,960 this case we'll go with plus equals item 18708 13:28:01,880 --> 13:28:09,060 amount multiplied by item dot price and 18709 13:28:06,960 --> 13:28:13,220 once we have this one in place 18710 13:28:09,060 --> 13:28:16,560 up next we want to set State amount 18711 13:28:13,220 --> 13:28:19,560 equals to amount that we just set up and 18712 13:28:16,560 --> 13:28:24,116 the same goes with total so let's go 18713 13:28:19,560 --> 13:28:27,776 here let's say total is equal to total 18714 13:28:24,116 --> 13:28:30,480 and after that we want to export that so 18715 13:28:27,776 --> 13:28:34,800 let's say calculate totals and we just 18716 13:28:30,480 --> 13:28:36,960 need to set it up in the app.js so let's 18717 13:28:34,800 --> 13:28:40,680 navigate to app.js 18718 13:28:36,960 --> 13:28:43,200 and in here let's grab first the U 18719 13:28:40,680 --> 13:28:44,640 selector and use dispatch we'll need 18720 13:28:43,200 --> 13:28:48,360 both of them 18721 13:28:44,640 --> 13:28:51,720 so let's say import use dispatch use 18722 13:28:48,360 --> 13:28:55,200 selector and we'll set it equal to from 18723 13:28:51,720 --> 13:28:58,020 react reduct and also I want to get that 18724 13:28:55,200 --> 13:28:59,880 calculate totals so let's say calculate 18725 13:28:58,020 --> 13:29:03,180 totals that is coming from the features 18726 13:28:59,880 --> 13:29:06,596 okay beautiful and as far as the logic 18727 13:29:03,180 --> 13:29:09,020 well first let's grab the card items and 18728 13:29:06,596 --> 13:29:12,116 we'll use you selector for that so cart 18729 13:29:09,020 --> 13:29:14,700 items now that is equal to use selector 18730 13:29:12,116 --> 13:29:18,480 let's pass in the function and let's say 18731 13:29:14,700 --> 13:29:20,400 here store is equal to store cart and 18732 13:29:18,480 --> 13:29:22,860 I'm not going to repeat myself how we 18733 13:29:20,400 --> 13:29:25,916 can access that and also we want to set 18734 13:29:22,860 --> 13:29:28,500 up that dispatch so const dispatch 18735 13:29:25,916 --> 13:29:29,700 not is equal to use dispatch let's 18736 13:29:28,500 --> 13:29:32,640 involve that 18737 13:29:29,700 --> 13:29:35,040 and let's finally set up that use effect 18738 13:29:32,640 --> 13:29:37,500 so let's say here use effect 18739 13:29:35,040 --> 13:29:40,560 and then as far as the Callback function 18740 13:29:37,500 --> 13:29:43,500 we will invoke this every time there's a 18741 13:29:40,560 --> 13:29:45,060 change to the cart items so every time 18742 13:29:43,500 --> 13:29:47,756 we'll update something 18743 13:29:45,060 --> 13:29:49,980 related to the card items we will invoke 18744 13:29:47,756 --> 13:29:52,096 this use effect specifically we want to 18745 13:29:49,980 --> 13:29:56,580 go with dispatch and we want to pass in 18746 13:29:52,096 --> 13:29:58,436 calculate totals so check it out 18747 13:29:56,580 --> 13:30:00,416 now we still have four here but I can 18748 13:29:58,436 --> 13:30:02,340 clearly see my total 18749 13:30:00,416 --> 13:30:04,916 and what's going to happen every time 18750 13:30:02,340 --> 13:30:08,460 we'll click on any of the buttons and 18751 13:30:04,916 --> 13:30:10,616 all that nicely display whatever items 18752 13:30:08,460 --> 13:30:11,880 we have in the card now there's tiny bug 18753 13:30:10,616 --> 13:30:14,460 over here 18754 13:30:11,880 --> 13:30:17,820 basically we need to set this one to 18755 13:30:14,460 --> 13:30:19,680 fixed essentially how many uh numbers 18756 13:30:17,820 --> 13:30:22,256 after that and don't worry we'll do that 18757 13:30:19,680 --> 13:30:26,460 in a second but the goal here 18758 13:30:22,256 --> 13:30:29,640 is that now every time we remove item 18759 13:30:26,460 --> 13:30:31,020 from the card or we clear the card 18760 13:30:29,640 --> 13:30:33,840 altogether 18761 13:30:31,020 --> 13:30:37,200 it will affect the amount of items we 18762 13:30:33,840 --> 13:30:41,520 have which we display over here and also 18763 13:30:37,200 --> 13:30:43,740 the total now in order to fix the bug 18764 13:30:41,520 --> 13:30:47,096 there with too many numbers we just need 18765 13:30:43,740 --> 13:30:49,500 to go to card container 18766 13:30:47,096 --> 13:30:53,220 and where we have the total let's go 18767 13:30:49,500 --> 13:30:55,140 with two fixed so that's again the 18768 13:30:53,220 --> 13:30:58,500 JavaScript method that we have access to 18769 13:30:55,140 --> 13:31:00,776 and we'll say that we only want two 18770 13:30:58,500 --> 13:31:02,276 numbers after the dot so now everything 18771 13:31:00,776 --> 13:31:05,220 is going to work 18772 13:31:02,276 --> 13:31:08,276 and just like that we have calculate 18773 13:31:05,220 --> 13:31:10,800 totals in place and every time we'll 18774 13:31:08,276 --> 13:31:15,596 make some changes to our cart 18775 13:31:10,800 --> 13:31:18,720 they will affect our initial state in 18776 13:31:15,596 --> 13:31:21,720 the card slice all right and up next I 18777 13:31:18,720 --> 13:31:24,900 want to set up a modal and in the 18778 13:31:21,720 --> 13:31:28,436 process we'll create another slice and 18779 13:31:24,900 --> 13:31:32,820 we'll see how we can access data when we 18780 13:31:28,436 --> 13:31:35,580 have multiple reducers in our store and 18781 13:31:32,820 --> 13:31:40,140 the first step is going to be creating a 18782 13:31:35,580 --> 13:31:42,000 model JS with following code and then we 18783 13:31:40,140 --> 13:31:46,320 want to import that in the app.js and 18784 13:31:42,000 --> 13:31:48,416 place it above the number so we'll start 18785 13:31:46,320 --> 13:31:51,180 working on that where first I'm going to 18786 13:31:48,416 --> 13:31:53,276 create model Js 18787 13:31:51,180 --> 13:31:54,416 I'm going to set up any kind of imports 18788 13:31:53,276 --> 13:31:56,936 in this case 18789 13:31:54,416 --> 13:31:59,580 we haven't set up the slice yet so let's 18790 13:31:56,936 --> 13:32:02,640 just create that component 18791 13:31:59,580 --> 13:32:05,340 and as far as the return we want to go 18792 13:32:02,640 --> 13:32:08,720 here with a side 18793 13:32:05,340 --> 13:32:11,040 and we want to add a class name of modal 18794 13:32:08,720 --> 13:32:12,776 container and as I said if you're 18795 13:32:11,040 --> 13:32:16,256 interested in the CSS just please look 18796 13:32:12,776 --> 13:32:18,240 for those classes in the index CSS then 18797 13:32:16,256 --> 13:32:21,116 we want to create a div of the class of 18798 13:32:18,240 --> 13:32:24,116 model inside of it let's say heading 4 18799 13:32:21,116 --> 13:32:26,340 with the text of remove all items from 18800 13:32:24,116 --> 13:32:28,140 your shopping cart and after that we 18801 13:32:26,340 --> 13:32:30,180 want to go with button container where 18802 13:32:28,140 --> 13:32:32,340 basically we'll place two buttons just 18803 13:32:30,180 --> 13:32:34,740 to Showcase how it's going to look like 18804 13:32:32,340 --> 13:32:37,680 let me go to complete one 18805 13:32:34,740 --> 13:32:40,020 and again we'll display that once we try 18806 13:32:37,680 --> 13:32:42,660 to clear the cloud so this is going to 18807 13:32:40,020 --> 13:32:45,900 be the result so right off 30 heading 18808 13:32:42,660 --> 13:32:47,580 we're gonna go with div with a class of 18809 13:32:45,900 --> 13:32:50,456 BTN 18810 13:32:47,580 --> 13:32:53,756 container inside of it let's place two 18811 13:32:50,456 --> 13:32:55,616 buttons first one will be confirm one 18812 13:32:53,756 --> 13:32:58,080 and the second one will be the clear one 18813 13:32:55,616 --> 13:33:00,840 type for both of them will be button 18814 13:32:58,080 --> 13:33:04,436 then class name BTN and I'll call this 18815 13:33:00,840 --> 13:33:07,380 confirm vtn and as far as the text I'm 18816 13:33:04,436 --> 13:33:10,320 gonna go with confirm as well and the 18817 13:33:07,380 --> 13:33:11,700 same deal with the clear one we just 18818 13:33:10,320 --> 13:33:13,680 want to change some values around where 18819 13:33:11,700 --> 13:33:16,200 it's not going to be 18820 13:33:13,680 --> 13:33:19,500 confirm it's going to be cancel instead 18821 13:33:16,200 --> 13:33:22,860 and instead of confirm hyphen vtn we're 18822 13:33:19,500 --> 13:33:24,720 going to go with clear BTN now we want 18823 13:33:22,860 --> 13:33:27,660 to go to app.js 18824 13:33:24,720 --> 13:33:29,276 we want to get this particular component 18825 13:33:27,660 --> 13:33:31,916 so 18826 13:33:29,276 --> 13:33:35,580 I mean let me try here with the Auto 18827 13:33:31,916 --> 13:33:39,180 Import Let's see we have model yep 18828 13:33:35,580 --> 13:33:42,180 that worked and once we save check it 18829 13:33:39,180 --> 13:33:44,160 out this is going to be our model so we 18830 13:33:42,180 --> 13:33:47,456 have the component now we just need to 18831 13:33:44,160 --> 13:33:49,980 set up the logic all right up next let's 18832 13:33:47,456 --> 13:33:53,756 set up the slice for the model 18833 13:33:49,980 --> 13:33:56,580 so remember we have features folder 18834 13:33:53,756 --> 13:33:58,380 for time being we only have one feature 18835 13:33:56,580 --> 13:34:01,500 we have the car 18836 13:33:58,380 --> 13:34:03,596 but now I want to add the Moto feature 18837 13:34:01,500 --> 13:34:05,880 which is going to be located in the 18838 13:34:03,596 --> 13:34:07,560 model slice and then we want to set up 18839 13:34:05,880 --> 13:34:10,320 the functionality where again we're 18840 13:34:07,560 --> 13:34:12,300 going to go with create slice 18841 13:34:10,320 --> 13:34:15,776 we want to set up some initial state in 18842 13:34:12,300 --> 13:34:19,020 this case it's going to be is open 18843 13:34:15,776 --> 13:34:21,480 and I'll set it equal to false then we 18844 13:34:19,020 --> 13:34:24,300 want to go with model slice come up with 18845 13:34:21,480 --> 13:34:26,456 name initial State and right away let's 18846 13:34:24,300 --> 13:34:29,756 set up those two reducers we're gonna go 18847 13:34:26,456 --> 13:34:31,800 with open model and close model we want 18848 13:34:29,756 --> 13:34:36,240 to export that and we also want to 18849 13:34:31,800 --> 13:34:39,360 export model slash reducer and in the 18850 13:34:36,240 --> 13:34:40,860 store we want to set up another key only 18851 13:34:39,360 --> 13:34:44,936 in this case it's going to be equal to 18852 13:34:40,860 --> 13:34:48,116 this model slice reducer and 18853 13:34:44,936 --> 13:34:51,360 as a result we'll be able to access the 18854 13:34:48,116 --> 13:34:53,880 state as well as the reducers all over 18855 13:34:51,360 --> 13:34:55,080 our application and the first thing that 18856 13:34:53,880 --> 13:34:58,860 we'll do 18857 13:34:55,080 --> 13:35:01,980 is in the app.js we'll grab is open 18858 13:34:58,860 --> 13:35:05,820 and we'll display this one conditionally 18859 13:35:01,980 --> 13:35:08,160 so I'm gonna go to the features we want 18860 13:35:05,820 --> 13:35:11,640 to create a new folder let's call this 18861 13:35:08,160 --> 13:35:14,160 modal then we want to go to new file and 18862 13:35:11,640 --> 13:35:19,140 we'll call this model 18863 13:35:14,160 --> 13:35:22,140 slice JS and then inside of it we first 18864 13:35:19,140 --> 13:35:24,480 want to grab the create slice so let's 18865 13:35:22,140 --> 13:35:27,776 go here with create and let's see 18866 13:35:24,480 --> 13:35:28,560 whether yep gives me the import then we 18867 13:35:27,776 --> 13:35:31,320 wanna 18868 13:35:28,560 --> 13:35:33,116 set up the initial state in my case just 18869 13:35:31,320 --> 13:35:34,680 going to be one property again this is 18870 13:35:33,116 --> 13:35:36,900 just a practice 18871 13:35:34,680 --> 13:35:38,520 working with more realistic application 18872 13:35:36,900 --> 13:35:41,340 where of course there's going to be more 18873 13:35:38,520 --> 13:35:44,040 features so we're going to go here is 18874 13:35:41,340 --> 13:35:45,596 open and by default let's set it equal 18875 13:35:44,040 --> 13:35:47,880 to false 18876 13:35:45,596 --> 13:35:52,020 false and then let's set up that slice 18877 13:35:47,880 --> 13:35:54,660 so const modal slice is equal to create 18878 13:35:52,020 --> 13:35:56,400 slice let's pass in the object name 18879 13:35:54,660 --> 13:35:58,916 model 18880 13:35:56,400 --> 13:36:00,776 then initial state is equal to initial 18881 13:35:58,916 --> 13:36:03,776 State let's right away set up those 18882 13:36:00,776 --> 13:36:07,436 reducers now what reducers we're gonna 18883 13:36:03,776 --> 13:36:10,200 have we're gonna have open motor reducer 18884 13:36:07,436 --> 13:36:13,080 which is going to be equal to state 18885 13:36:10,200 --> 13:36:14,640 and action again we're just practicing 18886 13:36:13,080 --> 13:36:18,660 as far as the action we're not going to 18887 13:36:14,640 --> 13:36:21,956 use it and in here let's just say state 18888 13:36:18,660 --> 13:36:24,480 is open so if I want to open a model 18889 13:36:21,956 --> 13:36:26,520 what is going to be value well you might 18890 13:36:24,480 --> 13:36:29,220 as well set it equal to True kind of 18891 13:36:26,520 --> 13:36:30,900 makes sense don't you think then let's 18892 13:36:29,220 --> 13:36:32,640 copy in page then now we want to close 18893 13:36:30,900 --> 13:36:35,096 the model now what is going to be the 18894 13:36:32,640 --> 13:36:38,700 value for is open well let's set it back 18895 13:36:35,096 --> 13:36:40,860 to false then we want to export three 18896 13:36:38,700 --> 13:36:42,540 things the main reducer and I'm not 18897 13:36:40,860 --> 13:36:46,020 going to log it we already have done 18898 13:36:42,540 --> 13:36:48,840 that so export default now of course we 18899 13:36:46,020 --> 13:36:50,936 have different object but the property 18900 13:36:48,840 --> 13:36:53,160 we want to export meaning the 18901 13:36:50,936 --> 13:36:55,320 method is still the same we want to 18902 13:36:53,160 --> 13:36:57,180 export the reducer that's number one and 18903 13:36:55,320 --> 13:36:59,400 then remember those action creators so 18904 13:36:57,180 --> 13:37:02,040 we're gonna go here with export const 18905 13:36:59,400 --> 13:37:04,916 and now we're looking for open model 18906 13:37:02,040 --> 13:37:10,080 close model and both of them are coming 18907 13:37:04,916 --> 13:37:12,956 from modal slice and the actions now 18908 13:37:10,080 --> 13:37:14,880 in order for everything to work we 18909 13:37:12,956 --> 13:37:17,400 definitely definitely definitely need to 18910 13:37:14,880 --> 13:37:20,160 go to store 18911 13:37:17,400 --> 13:37:23,820 so let's find where is that sucker over 18912 13:37:20,160 --> 13:37:25,740 here and then in the store now I want to 18913 13:37:23,820 --> 13:37:27,300 get the modal slice 18914 13:37:25,740 --> 13:37:28,860 so I have to change some things around 18915 13:37:27,300 --> 13:37:29,936 this is going to be coming from the 18916 13:37:28,860 --> 13:37:32,700 model 18917 13:37:29,936 --> 13:37:36,660 name will also be modal this is going to 18918 13:37:32,700 --> 13:37:38,456 be a modal reducer and yes of course the 18919 13:37:36,660 --> 13:37:41,276 property I'm going to go with model as 18920 13:37:38,456 --> 13:37:43,140 well so model will be equal to modal 18921 13:37:41,276 --> 13:37:46,800 reducer 18922 13:37:43,140 --> 13:37:49,380 so that's good now let's try it out in 18923 13:37:46,800 --> 13:37:50,456 our application on a big screen 18924 13:37:49,380 --> 13:37:52,380 and 18925 13:37:50,456 --> 13:37:55,320 at the moment we're calculating the 18926 13:37:52,380 --> 13:37:57,900 totals notice we have our E2 actions not 18927 13:37:55,320 --> 13:38:00,300 only the init and we also run calculate 18928 13:37:57,900 --> 13:38:01,200 totals what I want to showcase though is 18929 13:38:00,300 --> 13:38:04,380 that 18930 13:38:01,200 --> 13:38:05,276 set up just cart now we also have the 18931 13:38:04,380 --> 13:38:08,400 model 18932 13:38:05,276 --> 13:38:10,436 and now let's utilize that we're in the 18933 13:38:08,400 --> 13:38:13,980 app Js 18934 13:38:10,436 --> 13:38:16,980 not only I want to get the model I also 18935 13:38:13,980 --> 13:38:18,480 want to access what I want to access is 18936 13:38:16,980 --> 13:38:20,276 open 18937 13:38:18,480 --> 13:38:22,680 and I want to set this one up 18938 13:38:20,276 --> 13:38:25,080 conditionally since in a second we'll 18939 13:38:22,680 --> 13:38:27,900 add all the functionality so in here 18940 13:38:25,080 --> 13:38:30,540 remember we were accessing the cart 18941 13:38:27,900 --> 13:38:33,300 so the only difference is that now I 18942 13:38:30,540 --> 13:38:34,500 want to access the modal and I'll say is 18943 13:38:33,300 --> 13:38:35,580 open 18944 13:38:34,500 --> 13:38:38,756 is 18945 13:38:35,580 --> 13:38:40,140 open and that will be equal not to the 18946 13:38:38,756 --> 13:38:43,500 store cart 18947 13:38:40,140 --> 13:38:45,540 I'll say store model 18948 13:38:43,500 --> 13:38:48,360 and where I have the model 18949 13:38:45,540 --> 13:38:51,956 will have the conditional rendering 18950 13:38:48,360 --> 13:38:54,956 where I'll say is open if it's true only 18951 13:38:51,956 --> 13:38:58,140 then display the model so let's move it 18952 13:38:54,956 --> 13:39:00,776 in and since the value is false we don't 18953 13:38:58,140 --> 13:39:03,900 display the model now if I'll manually 18954 13:39:00,776 --> 13:39:06,060 go back to model slice and I'll say that 18955 13:39:03,900 --> 13:39:07,436 the value is true 18956 13:39:06,060 --> 13:39:08,756 I mean 18957 13:39:07,436 --> 13:39:11,220 you're not going to be surprised 18958 13:39:08,756 --> 13:39:13,320 probably by the fact that we display the 18959 13:39:11,220 --> 13:39:15,116 model okay awesome 18960 13:39:13,320 --> 13:39:17,520 so now let's add that functionality 18961 13:39:15,116 --> 13:39:20,456 where instead of just clearing the cart 18962 13:39:17,520 --> 13:39:23,096 why don't we open up the model when we 18963 13:39:20,456 --> 13:39:26,340 click on this button and then once we 18964 13:39:23,096 --> 13:39:28,916 get to the modal container then we'll 18965 13:39:26,340 --> 13:39:31,560 decide do we really want to remove all 18966 13:39:28,916 --> 13:39:34,560 the contents and then close the model or 18967 13:39:31,560 --> 13:39:37,560 we simply want to close the model so 18968 13:39:34,560 --> 13:39:40,560 first let's start in the card container 18969 13:39:37,560 --> 13:39:43,256 and I'll send out yes you'll find all of 18970 13:39:40,560 --> 13:39:45,180 the logic here in the readmewhere first 18971 13:39:43,256 --> 13:39:48,480 in the car container we want to get the 18972 13:39:45,180 --> 13:39:50,160 open model from the model slice and 18973 13:39:48,480 --> 13:39:53,580 instead of clearing the card directly 18974 13:39:50,160 --> 13:39:56,276 we'll go dispatch and open model and 18975 13:39:53,580 --> 13:39:58,436 from there we'll finish everything in 18976 13:39:56,276 --> 13:39:59,756 the model just so let's start in the 18977 13:39:58,436 --> 13:40:01,500 cart container 18978 13:39:59,756 --> 13:40:02,756 so we have all of these Imports okay 18979 13:40:01,500 --> 13:40:05,936 that's awesome 18980 13:40:02,756 --> 13:40:09,060 but we want to copy and paste and we 18981 13:40:05,936 --> 13:40:13,500 want to get open modal from the modal 18982 13:40:09,060 --> 13:40:16,200 slice open model and instead of card 18983 13:40:13,500 --> 13:40:20,096 slash we're gonna go here with model and 18984 13:40:16,200 --> 13:40:22,320 then modal slice then let's keep on 18985 13:40:20,096 --> 13:40:23,700 moving I don't think we actually need 18986 13:40:22,320 --> 13:40:26,580 this import anymore 18987 13:40:23,700 --> 13:40:28,616 so we can remove it and then where we 18988 13:40:26,580 --> 13:40:31,740 have the clear card 18989 13:40:28,616 --> 13:40:33,596 now let's pass in the open modal 18990 13:40:31,740 --> 13:40:35,520 same deal we invoke it so that doesn't 18991 13:40:33,596 --> 13:40:37,140 change now the difference of course is 18992 13:40:35,520 --> 13:40:40,320 going to be that now once we click on 18993 13:40:37,140 --> 13:40:42,300 clearing cart we display the model so 18994 13:40:40,320 --> 13:40:43,380 now let's navigate there and let's 18995 13:40:42,300 --> 13:40:46,740 handle that 18996 13:40:43,380 --> 13:40:49,200 so what first do we need over there well 18997 13:40:46,740 --> 13:40:50,936 we'll need quite a few things first we 18998 13:40:49,200 --> 13:40:53,096 want to get the closed model because 18999 13:40:50,936 --> 13:40:55,080 regardless on which button we click I 19000 13:40:53,096 --> 13:40:58,436 want to clear it I want to get the use 19001 13:40:55,080 --> 13:41:01,080 dispatch I'll need it since I wanna 19002 13:40:58,436 --> 13:41:03,180 invoke some reducers and I also want to 19003 13:41:01,080 --> 13:41:06,416 get the clear card so notice how again 19004 13:41:03,180 --> 13:41:10,380 we're getting this data from multiple 19005 13:41:06,416 --> 13:41:11,820 slices model slice and card slice and 19006 13:41:10,380 --> 13:41:13,436 then of course we'll just set up the 19007 13:41:11,820 --> 13:41:16,380 functionality so 19008 13:41:13,436 --> 13:41:19,500 let's go back over here let's set up all 19009 13:41:16,380 --> 13:41:21,300 those three Imports we're gonna get the 19010 13:41:19,500 --> 13:41:25,200 closed model 19011 13:41:21,300 --> 13:41:29,820 closed model and I also want to get the 19012 13:41:25,200 --> 13:41:31,800 clear card for your cart and also we 19013 13:41:29,820 --> 13:41:33,660 want to get the use dispatch so let's 19014 13:41:31,800 --> 13:41:36,300 see where that is going to work so use 19015 13:41:33,660 --> 13:41:39,060 this patch yep I have all of them so now 19016 13:41:36,300 --> 13:41:40,320 I can remove these suckers then we want 19017 13:41:39,060 --> 13:41:43,320 to quickly 19018 13:41:40,320 --> 13:41:45,240 set up the dispatch where effectively we 19019 13:41:43,320 --> 13:41:47,580 just go with use dispatch and we invoke 19020 13:41:45,240 --> 13:41:51,416 it and now it's set up the functionality 19021 13:41:47,580 --> 13:41:52,680 where if we click on confirm I want to 19022 13:41:51,416 --> 13:41:54,956 do two things 19023 13:41:52,680 --> 13:41:57,116 not only I want to clear the card but I 19024 13:41:54,956 --> 13:42:00,300 also want to close the model now if we 19025 13:41:57,116 --> 13:42:03,000 go to cancel then we'll just close the 19026 13:42:00,300 --> 13:42:04,740 model so let's go here with button we're 19027 13:42:03,000 --> 13:42:08,580 gonna go with on 19028 13:42:04,740 --> 13:42:09,840 click and then in here let's pass in the 19029 13:42:08,580 --> 13:42:11,340 function 19030 13:42:09,840 --> 13:42:13,200 and then 19031 13:42:11,340 --> 13:42:15,900 We're not gonna pass in any kind of 19032 13:42:13,200 --> 13:42:17,880 argument we'll just go with dispatch 19033 13:42:15,900 --> 13:42:19,200 then clear 19034 13:42:17,880 --> 13:42:21,300 card 19035 13:42:19,200 --> 13:42:23,880 that's the first thing we want to invoke 19036 13:42:21,300 --> 13:42:26,220 and then the second one it looks like I 19037 13:42:23,880 --> 13:42:30,360 have some weird bug oh yeah oh here and 19038 13:42:26,220 --> 13:42:32,936 the second thing is the close model 19039 13:42:30,360 --> 13:42:35,160 so let's pass this one in and I know I 19040 13:42:32,936 --> 13:42:37,916 keep repeating this but I really like 19041 13:42:35,160 --> 13:42:41,276 the fact that we don't need to do any of 19042 13:42:37,916 --> 13:42:43,616 that manual labor where we need to set 19043 13:42:41,276 --> 13:42:46,200 up the actions and all that in this case 19044 13:42:43,616 --> 13:42:49,020 we simply right away get those functions 19045 13:42:46,200 --> 13:42:51,776 and we pass in dispatch which obviously 19046 13:42:49,020 --> 13:42:54,956 is way less time consuming and also less 19047 13:42:51,776 --> 13:42:57,180 chance for errors so let's do the same 19048 13:42:54,956 --> 13:42:59,160 thing with this button and the only 19049 13:42:57,180 --> 13:43:00,956 thing we want to change here is clear 19050 13:42:59,160 --> 13:43:04,860 card where we basically want to remove 19051 13:43:00,956 --> 13:43:08,400 it so if we confirm then our card is 19052 13:43:04,860 --> 13:43:11,456 empty if our we just click on cancel we 19053 13:43:08,400 --> 13:43:14,756 close out the model so that's how we can 19054 13:43:11,456 --> 13:43:17,220 add another feature to our application 19055 13:43:14,756 --> 13:43:20,340 all right and now let's see how we can 19056 13:43:17,220 --> 13:43:25,200 set up a synchronous functionality with 19057 13:43:20,340 --> 13:43:27,416 a Redux toolkit so here's the plan so I 19058 13:43:25,200 --> 13:43:31,436 have quarters API 19059 13:43:27,416 --> 13:43:34,080 basically an API that serves some Json 19060 13:43:31,436 --> 13:43:38,520 data just so we can practice on fetching 19061 13:43:34,080 --> 13:43:41,756 data and in the use reducer project we 19062 13:43:38,520 --> 13:43:44,820 already work with this URL so it's 19063 13:43:41,756 --> 13:43:47,520 course API and then react use reducer 19064 13:43:44,820 --> 13:43:50,756 card project which essentially Returns 19065 13:43:47,520 --> 13:43:54,360 the same data that we currently have in 19066 13:43:50,756 --> 13:43:56,880 the card items so use this URL to fetch 19067 13:43:54,360 --> 13:43:58,500 data when initially our application 19068 13:43:56,880 --> 13:44:01,080 loads 19069 13:43:58,500 --> 13:44:04,020 and here's the thing we cannot just 19070 13:44:01,080 --> 13:44:06,776 simply set this up in our current 19071 13:44:04,020 --> 13:44:10,020 reducers it's not going to work that's 19072 13:44:06,776 --> 13:44:14,820 why with redex toolkit we install 19073 13:44:10,020 --> 13:44:18,480 another Library the thunk one and from 19074 13:44:14,820 --> 13:44:19,860 the Redux tokit we get this create hsync 19075 13:44:18,480 --> 13:44:23,820 funk 19076 13:44:19,860 --> 13:44:26,096 and we right away wanna invoke it and 19077 13:44:23,820 --> 13:44:28,256 the result we want to export 19078 13:44:26,096 --> 13:44:30,776 so this is something that will require 19079 13:44:28,256 --> 13:44:34,916 directly in our components so it's a 19080 13:44:30,776 --> 13:44:37,320 very similar to how we exported the 19081 13:44:34,916 --> 13:44:39,300 action creators from the reducers 19082 13:44:37,320 --> 13:44:41,096 however in this case we export this 19083 13:44:39,300 --> 13:44:44,776 directly in my case I'm going to call 19084 13:44:41,096 --> 13:44:48,060 this get cart items so we invoke create 19085 13:44:44,776 --> 13:44:50,456 async Funk and it's looking for two 19086 13:44:48,060 --> 13:44:52,800 things for the action type so 19087 13:44:50,456 --> 13:44:53,700 essentially in here we just come up with 19088 13:44:52,800 --> 13:44:57,300 a name 19089 13:44:53,700 --> 13:44:59,340 of our action and second is going to be 19090 13:44:57,300 --> 13:45:02,276 that callback function 19091 13:44:59,340 --> 13:45:05,520 now in the following video I'll show you 19092 13:45:02,276 --> 13:45:07,380 more options but at the very least we 19093 13:45:05,520 --> 13:45:11,040 pass here this function and this 19094 13:45:07,380 --> 13:45:13,680 function needs to return a promise 19095 13:45:11,040 --> 13:45:15,180 um I'm just using fetch to fetch data 19096 13:45:13,680 --> 13:45:18,956 and remember 19097 13:45:15,180 --> 13:45:22,256 when we go with fetch dot then we return 19098 13:45:18,956 --> 13:45:23,936 basically a success response and with 19099 13:45:22,256 --> 13:45:26,596 catch of course it's going to be the 19100 13:45:23,936 --> 13:45:29,456 error but regardless from this function 19101 13:45:26,596 --> 13:45:31,500 we are automatically returning the 19102 13:45:29,456 --> 13:45:34,200 promise this is what the Callback 19103 13:45:31,500 --> 13:45:36,720 function is looking for it's looking for 19104 13:45:34,200 --> 13:45:39,660 that promise and of course you're not 19105 13:45:36,720 --> 13:45:41,456 limited to the just fetch you can set up 19106 13:45:39,660 --> 13:45:43,080 all kinds of things in here but just 19107 13:45:41,456 --> 13:45:45,240 remember that this function needs to 19108 13:45:43,080 --> 13:45:47,820 return a promise so of course if you 19109 13:45:45,240 --> 13:45:49,980 just stick async in front of it 19110 13:45:47,820 --> 13:45:52,320 then by default it's going to return 19111 13:45:49,980 --> 13:45:54,776 promise which is something that we'll 19112 13:45:52,320 --> 13:45:56,700 take a look at in the next video and 19113 13:45:54,776 --> 13:45:57,840 then in the fetch I want to pass in the 19114 13:45:56,700 --> 13:46:02,040 URL 19115 13:45:57,840 --> 13:46:04,800 so the one that gets me the product and 19116 13:46:02,040 --> 13:46:09,000 when it comes to this callback function 19117 13:46:04,800 --> 13:46:10,560 it returns a life cycle actions so if 19118 13:46:09,000 --> 13:46:12,540 you remember when we work with promises 19119 13:46:10,560 --> 13:46:15,660 we add few options 19120 13:46:12,540 --> 13:46:17,700 promise could be pending it can be 19121 13:46:15,660 --> 13:46:21,180 fulfilled and rejected 19122 13:46:17,700 --> 13:46:24,000 so where we have the create slice what 19123 13:46:21,180 --> 13:46:26,520 we want to do is set up extra reducers 19124 13:46:24,000 --> 13:46:29,160 property it's going to be an object and 19125 13:46:26,520 --> 13:46:31,916 then in order to access those life cycle 19126 13:46:29,160 --> 13:46:34,916 actions the syntax is like this where we 19127 13:46:31,916 --> 13:46:37,200 go with name what we're getting back and 19128 13:46:34,916 --> 13:46:38,340 then that pending fulfilled and rejected 19129 13:46:37,200 --> 13:46:40,800 and yes 19130 13:46:38,340 --> 13:46:42,240 these need to match to the T 19131 13:46:40,800 --> 13:46:45,300 because 19132 13:46:42,240 --> 13:46:49,380 they're provided by the Redux token and 19133 13:46:45,300 --> 13:46:51,000 then we wanna and close this with the 19134 13:46:49,380 --> 13:46:53,456 square brackets 19135 13:46:51,000 --> 13:46:55,740 and then we'll set this equal to a 19136 13:46:53,456 --> 13:46:57,596 function so what do we want to do when 19137 13:46:55,740 --> 13:46:59,456 we're pending basically when we're 19138 13:46:57,596 --> 13:47:01,616 loading that's why we have the state 19139 13:46:59,456 --> 13:47:04,080 value is loading and we'll set it equal 19140 13:47:01,616 --> 13:47:06,116 to true then what do we want to do if 19141 13:47:04,080 --> 13:47:10,560 we're successful and this is the case 19142 13:47:06,116 --> 13:47:12,776 where the data that we're returning in 19143 13:47:10,560 --> 13:47:15,060 the case of success in our case response 19144 13:47:12,776 --> 13:47:16,980 that Json is going to be located in the 19145 13:47:15,060 --> 13:47:18,660 action payload and of course the lug it 19146 13:47:16,980 --> 13:47:21,240 just so you can see so once we have 19147 13:47:18,660 --> 13:47:24,840 successfully data we want to set is 19148 13:47:21,240 --> 13:47:28,256 loading to false and card items to the 19149 13:47:24,840 --> 13:47:31,680 actual payload and if there's some kind 19150 13:47:28,256 --> 13:47:34,320 of error we have rejected option and in 19151 13:47:31,680 --> 13:47:37,680 that case for this application we just 19152 13:47:34,320 --> 13:47:39,596 want to set is loading to false 19153 13:47:37,680 --> 13:47:43,740 and also keep in mind that when it comes 19154 13:47:39,596 --> 13:47:46,256 to fetch 404 is not an R so we'll only 19155 13:47:43,740 --> 13:47:49,740 have this one if there's some kind of 19156 13:47:46,256 --> 13:47:53,640 network error and at the very last we 19157 13:47:49,740 --> 13:47:56,340 want to grab get card items in the app 19158 13:47:53,640 --> 13:47:58,500 app component and then when our 19159 13:47:56,340 --> 13:48:01,320 application loads we want to do two 19160 13:47:58,500 --> 13:48:04,140 things we want dispatch get card items 19161 13:48:01,320 --> 13:48:07,200 so the asynchronous function 19162 13:48:04,140 --> 13:48:09,360 and then we also want to grab is loading 19163 13:48:07,200 --> 13:48:10,680 from the state and while we're loading 19164 13:48:09,360 --> 13:48:14,340 we'll display this 19165 13:48:10,680 --> 13:48:18,300 and once we're successful we'll set the 19166 13:48:14,340 --> 13:48:19,740 state card items equal to the data that 19167 13:48:18,300 --> 13:48:22,020 we're getting back 19168 13:48:19,740 --> 13:48:24,776 so let's try this one out where I'm 19169 13:48:22,020 --> 13:48:28,680 going to go to card slice 19170 13:48:24,776 --> 13:48:31,256 and first I want to grab that create a 19171 13:48:28,680 --> 13:48:34,436 sink thunk so that one is coming from 19172 13:48:31,256 --> 13:48:37,560 Redux toolkit then I want to set up the 19173 13:48:34,436 --> 13:48:40,616 URL so right after the import let's go 19174 13:48:37,560 --> 13:48:41,880 here with const URL and you have two 19175 13:48:40,616 --> 13:48:44,700 options you can either get it from 19176 13:48:41,880 --> 13:48:46,800 readme or you can just navigate here in 19177 13:48:44,700 --> 13:48:48,416 the browser so since I already have it 19178 13:48:46,800 --> 13:48:51,116 open in the browser 19179 13:48:48,416 --> 13:48:53,700 I'll use this approach so again this is 19180 13:48:51,116 --> 13:48:55,616 the URL where we can fetch those 19181 13:48:53,700 --> 13:48:58,256 products just like we have in the card 19182 13:48:55,616 --> 13:49:00,480 items and then let's keep on scrolling 19183 13:48:58,256 --> 13:49:03,060 and we'll go with 19184 13:49:00,480 --> 13:49:05,456 create a sync thunk and we'll set it 19185 13:49:03,060 --> 13:49:07,916 equal to our function but we want to 19186 13:49:05,456 --> 13:49:10,200 right away export it so let's say const 19187 13:49:07,916 --> 13:49:13,500 get cart 19188 13:49:10,200 --> 13:49:15,116 items and that is equal to creative sync 19189 13:49:13,500 --> 13:49:16,200 Duncan like I said it's looking for two 19190 13:49:15,116 --> 13:49:18,540 things 19191 13:49:16,200 --> 13:49:20,220 the type so in here we just need to come 19192 13:49:18,540 --> 13:49:23,160 up with a name in my case I'm going to 19193 13:49:20,220 --> 13:49:25,380 go with cart forward slash get 19194 13:49:23,160 --> 13:49:28,436 card and I'm so that's going to be my 19195 13:49:25,380 --> 13:49:29,640 action and second will be that callback 19196 13:49:28,436 --> 13:49:32,540 function 19197 13:49:29,640 --> 13:49:36,596 and here let's right away go with return 19198 13:49:32,540 --> 13:49:39,480 fetch let's pass in the URL then that 19199 13:49:36,596 --> 13:49:42,180 then so if you're successful I want to 19200 13:49:39,480 --> 13:49:45,360 turn this into a Json so say response 19201 13:49:42,180 --> 13:49:47,640 response Json let's invoke that and then 19202 13:49:45,360 --> 13:49:49,680 let's also go with catch and don't worry 19203 13:49:47,640 --> 13:49:51,360 next video to take a look at more 19204 13:49:49,680 --> 13:49:53,456 reasonable approach how to handle this 19205 13:49:51,360 --> 13:49:56,040 but for time being I just want to get 19206 13:49:53,456 --> 13:50:00,900 that data from the URL so let's go here 19207 13:49:56,040 --> 13:50:03,180 with error and then log that sucker 19208 13:50:00,900 --> 13:50:06,956 and once we are done 19209 13:50:03,180 --> 13:50:10,380 we want to keep on scrolling and in the 19210 13:50:06,956 --> 13:50:13,380 create slides right after reducers the 19211 13:50:10,380 --> 13:50:15,360 property name is extra reducers notice 19212 13:50:13,380 --> 13:50:18,000 we right away get that suggestion and 19213 13:50:15,360 --> 13:50:19,616 like I said then we get those life cycle 19214 13:50:18,000 --> 13:50:21,900 actions 19215 13:50:19,616 --> 13:50:22,680 so for every function that we will 19216 13:50:21,900 --> 13:50:25,560 create 19217 13:50:22,680 --> 13:50:28,320 in this case it is get card items we'll 19218 13:50:25,560 --> 13:50:31,616 get those three life cycle actions 19219 13:50:28,320 --> 13:50:33,480 pending fulfilled and rejected and the 19220 13:50:31,616 --> 13:50:35,720 way we can access that we go with the 19221 13:50:33,480 --> 13:50:38,936 same name so get card items in this case 19222 13:50:35,720 --> 13:50:42,240 and then notice we right away have those 19223 13:50:38,936 --> 13:50:43,980 options fulfilled pending and rejected 19224 13:50:42,240 --> 13:50:46,980 so in my case I'm going to start with 19225 13:50:43,980 --> 13:50:49,740 pending that is why we're still loading 19226 13:50:46,980 --> 13:50:52,436 and in this function same way we can 19227 13:50:49,740 --> 13:50:54,360 access the state and also we can get the 19228 13:50:52,436 --> 13:50:57,000 action so action is going to be useful 19229 13:50:54,360 --> 13:50:59,220 once we get the data for now while we're 19230 13:50:57,000 --> 13:51:02,580 pending we just want to grab the state 19231 13:50:59,220 --> 13:51:05,220 and I want to set the state is loading 19232 13:51:02,580 --> 13:51:08,520 value equal to what equal to True 19233 13:51:05,220 --> 13:51:10,500 correct so while we're loading is 19234 13:51:08,520 --> 13:51:13,680 loading will be equal to 19235 13:51:10,500 --> 13:51:15,956 true then we want to copy and paste like 19236 13:51:13,680 --> 13:51:18,240 so we just want to add the commas so 19237 13:51:15,956 --> 13:51:20,340 there's no bugs in here and we want to 19238 13:51:18,240 --> 13:51:23,520 change these ones around where instead 19239 13:51:20,340 --> 13:51:25,560 of bending now I'm looking for fulfilled 19240 13:51:23,520 --> 13:51:27,300 and in this case I do want to add 19241 13:51:25,560 --> 13:51:30,300 another parameter which is going to be 19242 13:51:27,300 --> 13:51:32,640 action and again this action will 19243 13:51:30,300 --> 13:51:35,160 contain the result 19244 13:51:32,640 --> 13:51:38,220 if we're successful so in this case if 19245 13:51:35,160 --> 13:51:42,000 we're successful we return the Json 19246 13:51:38,220 --> 13:51:45,240 response and what I'm going to do is set 19247 13:51:42,000 --> 13:51:49,740 my card item so state 19248 13:51:45,240 --> 13:51:52,916 and then cart items equal to what action 19249 13:51:49,740 --> 13:51:53,936 and payload and yes let's also log it 19250 13:51:52,916 --> 13:51:55,436 just so you can see where it's coming 19251 13:51:53,936 --> 13:51:57,840 from but I can tell right away that 19252 13:51:55,436 --> 13:52:01,616 there's a payload property and that's 19253 13:51:57,840 --> 13:52:04,080 the one that holds those card items so 19254 13:52:01,616 --> 13:52:07,080 let's log the action and lastly 19255 13:52:04,080 --> 13:52:09,300 we also have rejected 19256 13:52:07,080 --> 13:52:12,116 and like I said since we're using fetch 19257 13:52:09,300 --> 13:52:14,220 it's only for network errors and for 19258 13:52:12,116 --> 13:52:15,300 time being also the sequel to false and 19259 13:52:14,220 --> 13:52:18,300 also 19260 13:52:15,300 --> 13:52:20,700 there's a tiny issue here once we have 19261 13:52:18,300 --> 13:52:22,020 the data of course the loading will be 19262 13:52:20,700 --> 13:52:25,436 false 19263 13:52:22,020 --> 13:52:27,900 so we don't need to export this we're 19264 13:52:25,436 --> 13:52:29,160 actually in good shape we already export 19265 13:52:27,900 --> 13:52:32,220 that function 19266 13:52:29,160 --> 13:52:35,700 here with export now we just want to go 19267 13:52:32,220 --> 13:52:39,900 to app.js we want to grab the function 19268 13:52:35,700 --> 13:52:42,540 get cart items we want to invoke it 19269 13:52:39,900 --> 13:52:46,140 once our application loads and we also 19270 13:52:42,540 --> 13:52:48,416 want to check for is loading so let's 19271 13:52:46,140 --> 13:52:50,756 navigate to app.js 19272 13:52:48,416 --> 13:52:53,520 and since we already import 19273 13:52:50,756 --> 13:52:56,040 the calculator from the card slice 19274 13:52:53,520 --> 13:52:58,980 simply want to add a comma and we'll go 19275 13:52:56,040 --> 13:53:01,020 with get card items and then let's keep 19276 13:52:58,980 --> 13:53:03,596 on moving not only I want to get the 19277 13:53:01,020 --> 13:53:06,840 card items I also want to check for is 19278 13:53:03,596 --> 13:53:09,596 loading sorry for loading then we'll 19279 13:53:06,840 --> 13:53:12,180 display different return and then let's 19280 13:53:09,596 --> 13:53:13,980 set up a different use effect 19281 13:53:12,180 --> 13:53:16,200 where I'm going to go with use effect 19282 13:53:13,980 --> 13:53:19,320 and I'll invoke it 19283 13:53:16,200 --> 13:53:21,360 only when my application loads so once 19284 13:53:19,320 --> 13:53:24,360 my application loads what do I want to 19285 13:53:21,360 --> 13:53:28,256 do I want to dispatch and we'll go with 19286 13:53:24,360 --> 13:53:30,660 get card items and we'll invoke it it's 19287 13:53:28,256 --> 13:53:34,500 also set up that return 19288 13:53:30,660 --> 13:53:36,060 where basically if we're loading this is 19289 13:53:34,500 --> 13:53:37,680 what we want to return we want to go 19290 13:53:36,060 --> 13:53:40,080 with Dave with glass of loading and then 19291 13:53:37,680 --> 13:53:44,640 heading one loading so let's set it up 19292 13:53:40,080 --> 13:53:46,616 here if is loading it's true then we 19293 13:53:44,640 --> 13:53:49,740 want to go with return 19294 13:53:46,616 --> 13:53:52,436 then let's set up the div here let's add 19295 13:53:49,740 --> 13:53:54,560 a class of loading and on the side of it 19296 13:53:52,436 --> 13:53:57,660 we're gonna go with heading one and 19297 13:53:54,560 --> 13:53:59,820 loading dot dot and once we save for 19298 13:53:57,660 --> 13:54:01,436 second we'll notice that loading and 19299 13:53:59,820 --> 13:54:02,880 then we get the product and if you don't 19300 13:54:01,436 --> 13:54:05,096 believe me 19301 13:54:02,880 --> 13:54:09,240 let's navigate to the big screen over 19302 13:54:05,096 --> 13:54:12,360 here let's refresh and then let's check 19303 13:54:09,240 --> 13:54:14,456 it out in here we have the pending which 19304 13:54:12,360 --> 13:54:16,680 is going to be for that loading one and 19305 13:54:14,456 --> 13:54:19,436 we can clearly see our state 19306 13:54:16,680 --> 13:54:22,020 so notice how awesome it is in the feed 19307 13:54:19,436 --> 13:54:24,660 explos where you can clearly see your 19308 13:54:22,020 --> 13:54:27,840 actions so what actions were dispatching 19309 13:54:24,660 --> 13:54:30,000 and what is the state at that time and 19310 13:54:27,840 --> 13:54:31,680 we can even see the difference so 19311 13:54:30,000 --> 13:54:34,020 basically if I'm gonna go here with 19312 13:54:31,680 --> 13:54:36,180 fulfilled notice this is going to be the 19313 13:54:34,020 --> 13:54:39,360 difference so in the cart 19314 13:54:36,180 --> 13:54:42,240 we basically get this is loading and we 19315 13:54:39,360 --> 13:54:44,160 set it equal to false and also in the 19316 13:54:42,240 --> 13:54:47,340 console you can see what we're getting 19317 13:54:44,160 --> 13:54:50,520 back remember I said that once we log 19318 13:54:47,340 --> 13:54:55,140 over here in the card slice 19319 13:54:50,520 --> 13:54:58,740 we are logging the action so what is in 19320 13:54:55,140 --> 13:55:00,956 this action it is a payload and that's 19321 13:54:58,740 --> 13:55:04,740 the data that we're getting back so we 19322 13:55:00,956 --> 13:55:09,416 have this array of four items we set 19323 13:55:04,740 --> 13:55:11,756 here State card items equal to the 19324 13:55:09,416 --> 13:55:14,880 payload so again something we can 19325 13:55:11,756 --> 13:55:18,180 definitely double check over here so if 19326 13:55:14,880 --> 13:55:20,340 I go here with State notice with 19327 13:55:18,180 --> 13:55:23,220 fulfilled for example this is going to 19328 13:55:20,340 --> 13:55:25,380 get me those car items those four card 19329 13:55:23,220 --> 13:55:28,436 items and one more thing that we can 19330 13:55:25,380 --> 13:55:31,916 actually do is go back to the cart slice 19331 13:55:28,436 --> 13:55:34,020 and set this equal to an empty array so 19332 13:55:31,916 --> 13:55:37,436 the car items and that way you'll 19333 13:55:34,020 --> 13:55:41,700 clearly see how we can change this once 19334 13:55:37,436 --> 13:55:43,500 the Theta gets here asynchronously so 19335 13:55:41,700 --> 13:55:45,776 let's navigate back again let's refresh 19336 13:55:43,500 --> 13:55:48,840 just so we are on the safe side and if 19337 13:55:45,776 --> 13:55:50,880 we go right now with action fulfill them 19338 13:55:48,840 --> 13:55:52,800 if we take a look at the difference now 19339 13:55:50,880 --> 13:55:56,456 I'll notice two things first of all is 19340 13:55:52,800 --> 13:55:59,580 loading changed from True to false and 19341 13:55:56,456 --> 13:56:02,756 also notice how we switch the card items 19342 13:55:59,580 --> 13:56:05,340 so initially it was empty array and now 19343 13:56:02,756 --> 13:56:07,800 essentially we get those four items and 19344 13:56:05,340 --> 13:56:10,616 again all of our functionality still 19345 13:56:07,800 --> 13:56:12,116 works we can clearly see that in our 19346 13:56:10,616 --> 13:56:14,660 actions over here 19347 13:56:12,116 --> 13:56:17,456 but now we're getting this data 19348 13:56:14,660 --> 13:56:20,756 asynchronously once our application 19349 13:56:17,456 --> 13:56:24,720 loads all right and lastly I just want 19350 13:56:20,756 --> 13:56:27,060 to showcase what options we have with 19351 13:56:24,720 --> 13:56:29,160 the Callback function because in our 19352 13:56:27,060 --> 13:56:31,200 previous example we just looked at the 19353 13:56:29,160 --> 13:56:34,320 most basic setup 19354 13:56:31,200 --> 13:56:36,900 so now let's go over the more complex 19355 13:56:34,320 --> 13:56:39,720 one and don't worry in this video you 19356 13:56:36,900 --> 13:56:42,956 don't have to type along and in fact we 19357 13:56:39,720 --> 13:56:45,300 won't use any of this extra data in our 19358 13:56:42,956 --> 13:56:46,860 current application the goal is to 19359 13:56:45,300 --> 13:56:49,680 Simply get the gears in your head 19360 13:56:46,860 --> 13:56:52,800 turning and showcase large amount of 19361 13:56:49,680 --> 13:56:55,320 options we have and first what I want to 19362 13:56:52,800 --> 13:56:58,140 do is install another Library the HTTP 19363 13:56:55,320 --> 13:56:59,820 Library by the name of axios because I 19364 13:56:58,140 --> 13:57:03,840 do want to showcase 19365 13:56:59,820 --> 13:57:06,240 how essentially we can return a value 19366 13:57:03,840 --> 13:57:09,060 for the error and since fetch is not 19367 13:57:06,240 --> 13:57:11,756 responding to 404 basically for fetch 19368 13:57:09,060 --> 13:57:14,160 the 404 errors are not errors I actually 19369 13:57:11,756 --> 13:57:15,360 want to use the axios instead again if 19370 13:57:14,160 --> 13:57:17,880 you don't want to do that you can just 19371 13:57:15,360 --> 13:57:22,680 sit back and relax where I'm going to go 19372 13:57:17,880 --> 13:57:24,180 with npm install axios first then we 19373 13:57:22,680 --> 13:57:27,180 definitely want to spin up the dev 19374 13:57:24,180 --> 13:57:29,220 server of course so npm start let me 19375 13:57:27,180 --> 13:57:32,936 close this one then we want to navigate 19376 13:57:29,220 --> 13:57:35,820 to cart slice I think I can remove the 19377 13:57:32,936 --> 13:57:40,020 items we won't need it anymore so axios 19378 13:57:35,820 --> 13:57:41,936 from axios let's keep on moving and 19379 13:57:40,020 --> 13:57:44,820 before we do anything 19380 13:57:41,936 --> 13:57:48,300 like I said we can create this function 19381 13:57:44,820 --> 13:57:49,980 right away as a sync which essentially 19382 13:57:48,300 --> 13:57:52,320 just means that we'll right away return 19383 13:57:49,980 --> 13:57:54,240 the promise correct so go here with a 19384 13:57:52,320 --> 13:57:57,596 sync and that also means that we can 19385 13:57:54,240 --> 13:58:01,500 await so we can set up more logic so let 19386 13:57:57,596 --> 13:58:03,000 me remove all of this code here and 19387 13:58:01,500 --> 13:58:06,540 let's just go with try and catch right 19388 13:58:03,000 --> 13:58:08,640 away since we can also do that and then 19389 13:58:06,540 --> 13:58:10,916 let's wait for that response so I'm 19390 13:58:08,640 --> 13:58:13,800 going to say here const and then 19391 13:58:10,916 --> 13:58:15,956 response is equal to oh wait now I'm 19392 13:58:13,800 --> 13:58:19,436 going to go with axios and then I'll 19393 13:58:15,956 --> 13:58:21,240 pass in the URL and if we're successful 19394 13:58:19,436 --> 13:58:24,240 what do we want to do we want to return 19395 13:58:21,240 --> 13:58:26,640 that response again keep in mind we are 19396 13:58:24,240 --> 13:58:30,596 returning a promise and yes we're 19397 13:58:26,640 --> 13:58:33,416 handling that in the X reducers so none 19398 13:58:30,596 --> 13:58:37,080 of this functionality changes right now 19399 13:58:33,416 --> 13:58:40,320 we're just switching right now our 19400 13:58:37,080 --> 13:58:43,616 create a sync done callback function to 19401 13:58:40,320 --> 13:58:46,380 a sync where we're using axios now once 19402 13:58:43,616 --> 13:58:48,900 we save we will get some errors in the 19403 13:58:46,380 --> 13:58:51,416 console don't worry about it the reason 19404 13:58:48,900 --> 13:58:55,380 for that is because when it comes to 19405 13:58:51,416 --> 13:58:57,180 axios the data is located in the data 19406 13:58:55,380 --> 13:59:01,200 property and just to Showcase that let 19407 13:58:57,180 --> 13:59:03,596 me go here with log and then response 19408 13:59:01,200 --> 13:59:06,360 and you'll see in a console 19409 13:59:03,596 --> 13:59:08,936 if you scroll up you'll notice over here 19410 13:59:06,360 --> 13:59:10,860 this is what we're getting back and 19411 13:59:08,936 --> 13:59:14,096 essentially what we want to do we want 19412 13:59:10,860 --> 13:59:18,240 to pass in this data property 19413 13:59:14,096 --> 13:59:20,880 down to these lifecycle actions so we're 19414 13:59:18,240 --> 13:59:24,060 talking about this one over here so 19415 13:59:20,880 --> 13:59:28,140 let's scroll up and we'll go here with 19416 13:59:24,060 --> 13:59:29,340 return and let's go with response and 19417 13:59:28,140 --> 13:59:31,916 then there again 19418 13:59:29,340 --> 13:59:34,200 this is just because that's how the 19419 13:59:31,916 --> 13:59:36,660 response is structured so once we save 19420 13:59:34,200 --> 13:59:38,520 notice how everything worked correctly 19421 13:59:36,660 --> 13:59:41,640 where again we're getting those items 19422 13:59:38,520 --> 13:59:43,560 and if we take a look at the action it's 19423 13:59:41,640 --> 13:59:47,580 still the same thing we're getting this 19424 13:59:43,560 --> 13:59:49,200 payload and the payload is that array 19425 13:59:47,580 --> 13:59:52,980 so that's the first thing that I want to 19426 13:59:49,200 --> 13:59:55,200 mention also well we can pass here the 19427 13:59:52,980 --> 13:59:57,000 arguments now what does that mean well 19428 13:59:55,200 --> 13:59:59,096 in our case we're not going to use that 19429 13:59:57,000 --> 14:00:02,040 but we need to imagine that let's say 19430 13:59:59,096 --> 14:00:04,916 we're going to be setting up some kind 19431 14:00:02,040 --> 14:00:08,640 of functionality and we do want to pass 19432 14:00:04,916 --> 14:00:11,520 here the value let's say whether that is 19433 14:00:08,640 --> 14:00:13,740 a user or some kind of parameter or 19434 14:00:11,520 --> 14:00:15,776 something so in this case I'm just going 19435 14:00:13,740 --> 14:00:17,936 to say random 19436 14:00:15,776 --> 14:00:18,900 and if we want to access that again in 19437 14:00:17,936 --> 14:00:20,756 our case it's not going to change 19438 14:00:18,900 --> 14:00:22,800 anything but if we want to access that 19439 14:00:20,756 --> 14:00:24,900 this is going to be the first parameter 19440 14:00:22,800 --> 14:00:28,020 so in my case I'm going to go with name 19441 14:00:24,900 --> 14:00:29,340 and let's simply log that and we'll 19442 14:00:28,020 --> 14:00:32,040 clearly see 19443 14:00:29,340 --> 14:00:33,956 that we can access that value again not 19444 14:00:32,040 --> 14:00:35,880 something we'll use in our application 19445 14:00:33,956 --> 14:00:38,096 but it's very very important because 19446 14:00:35,880 --> 14:00:39,660 there's going to be applications where 19447 14:00:38,096 --> 14:00:42,900 of course this is going to be important 19448 14:00:39,660 --> 14:00:46,220 the fact that we can pass something here 19449 14:00:42,900 --> 14:00:50,220 from our component and we can access it 19450 14:00:46,220 --> 14:00:52,080 in our create async thumb that's the 19451 14:00:50,220 --> 14:00:56,400 first thing that I want to mention now 19452 14:00:52,080 --> 14:00:59,040 second we also have access to thunk API 19453 14:00:56,400 --> 14:01:00,900 again you can name it differently but I 19454 14:00:59,040 --> 14:01:04,140 mean common convention is calling this 19455 14:01:00,900 --> 14:01:06,956 Bank API which gives us even more 19456 14:01:04,140 --> 14:01:08,700 options now what am I talking about well 19457 14:01:06,956 --> 14:01:12,840 first of all let's log something let's 19458 14:01:08,700 --> 14:01:14,820 go with log then let's access bank API 19459 14:01:12,840 --> 14:01:16,140 and you know what let's just log the 19460 14:01:14,820 --> 14:01:19,140 entire thing 19461 14:01:16,140 --> 14:01:21,596 so you know what there's quite a few 19462 14:01:19,140 --> 14:01:23,096 things here in the console so let me 19463 14:01:21,596 --> 14:01:24,980 remove this action basically I'll 19464 14:01:23,096 --> 14:01:27,840 comment this one out for your reference 19465 14:01:24,980 --> 14:01:30,720 and also hopefully it's clear how we can 19466 14:01:27,840 --> 14:01:32,160 pass the parameters from the components 19467 14:01:30,720 --> 14:01:34,500 so that's clear 19468 14:01:32,160 --> 14:01:37,560 but then check it out we have this 19469 14:01:34,500 --> 14:01:39,116 somewhat giant object with bunch of 19470 14:01:37,560 --> 14:01:42,776 useful things 19471 14:01:39,116 --> 14:01:45,300 so here's the thing we could get the 19472 14:01:42,776 --> 14:01:47,040 state now what's interesting we're 19473 14:01:45,300 --> 14:01:48,300 getting the state of the entire 19474 14:01:47,040 --> 14:01:51,416 application 19475 14:01:48,300 --> 14:01:54,116 and this should get the wheels turning 19476 14:01:51,416 --> 14:01:57,240 in your head because that means that we 19477 14:01:54,116 --> 14:01:59,400 can get any values from the rest of the 19478 14:01:57,240 --> 14:02:01,256 features so let me quick quickly 19479 14:01:59,400 --> 14:02:05,456 showcase that so I'm gonna go with log 19480 14:02:01,256 --> 14:02:08,580 and then thunk API and then let's go 19481 14:02:05,456 --> 14:02:11,700 with get State and let's invoke it and 19482 14:02:08,580 --> 14:02:14,340 in the console I have both I have the 19483 14:02:11,700 --> 14:02:17,276 values for the card as well as the model 19484 14:02:14,340 --> 14:02:19,980 so imagine if you have for example a 19485 14:02:17,276 --> 14:02:23,456 user feature where you're setting up the 19486 14:02:19,980 --> 14:02:25,500 user yes when you're setting up your 19487 14:02:23,456 --> 14:02:28,380 async action 19488 14:02:25,500 --> 14:02:32,040 you can actually access it so I can set 19489 14:02:28,380 --> 14:02:33,956 up the user in a different feature 19490 14:02:32,040 --> 14:02:36,660 and I can still access it with the help 19491 14:02:33,956 --> 14:02:39,776 of this tank API which is very very very 19492 14:02:36,660 --> 14:02:41,880 powerful and also what's interesting 19493 14:02:39,776 --> 14:02:44,160 well we can dispatch 19494 14:02:41,880 --> 14:02:47,820 if we take a look at the object over 19495 14:02:44,160 --> 14:02:50,276 here notice we have this dispatch option 19496 14:02:47,820 --> 14:02:52,436 so what we can dispatch well for example 19497 14:02:50,276 --> 14:02:55,256 let's open up the model while we're 19498 14:02:52,436 --> 14:02:57,776 fetching so let's try this one out we're 19499 14:02:55,256 --> 14:03:01,740 right after the get State and all that 19500 14:02:57,776 --> 14:03:03,416 we go here and we're gonna go with funk 19501 14:03:01,740 --> 14:03:06,000 API 19502 14:03:03,416 --> 14:03:10,200 we're gonna go with this patch and now 19503 14:03:06,000 --> 14:03:14,096 we want to pass in open and modal again 19504 14:03:10,200 --> 14:03:17,160 something that is not even in our slice 19505 14:03:14,096 --> 14:03:18,596 and check it out and as I said I forgot 19506 14:03:17,160 --> 14:03:22,680 to add here 19507 14:03:18,596 --> 14:03:26,340 the parentheses notice even though this 19508 14:03:22,680 --> 14:03:28,916 reducer is not even in this feature we 19509 14:03:26,340 --> 14:03:31,616 can still access it with tank API and I 19510 14:03:28,916 --> 14:03:34,500 know you're sick of hearing this but 19511 14:03:31,616 --> 14:03:38,220 this is extremely powerful because we 19512 14:03:34,500 --> 14:03:41,096 can do all kinds of functionality in the 19513 14:03:38,220 --> 14:03:44,820 create async thumb and lastly what I 19514 14:03:41,096 --> 14:03:47,220 want to showcase is how we can return a 19515 14:03:44,820 --> 14:03:49,980 specific response because at the moment 19516 14:03:47,220 --> 14:03:52,680 yes we have catch and we are basically 19517 14:03:49,980 --> 14:03:53,880 getting the error but normally from the 19518 14:03:52,680 --> 14:03:56,936 API 19519 14:03:53,880 --> 14:03:59,936 you'll get some kind of specific message 19520 14:03:56,936 --> 14:04:02,640 so first of all how we can pass this 19521 14:03:59,936 --> 14:04:04,980 through again we're gonna look for thunk 19522 14:04:02,640 --> 14:04:07,560 API and we want to go here with the 19523 14:04:04,980 --> 14:04:09,660 return first of all and then the 19524 14:04:07,560 --> 14:04:13,160 property or the method you're looking 19525 14:04:09,660 --> 14:04:17,276 for so let's go here with thunk API is 19526 14:04:13,160 --> 14:04:19,020 reject with value so in this case I will 19527 14:04:17,276 --> 14:04:21,000 hard code this but keep in mind that 19528 14:04:19,020 --> 14:04:24,720 normally with axios it's going to be 19529 14:04:21,000 --> 14:04:27,116 located in error dot response so in here 19530 14:04:24,720 --> 14:04:28,800 let's say something 19531 14:04:27,116 --> 14:04:32,640 went 19532 14:04:28,800 --> 14:04:35,456 wrong like so and then let's keep on 19533 14:04:32,640 --> 14:04:38,340 moving let's keep on moving and where we 19534 14:04:35,456 --> 14:04:41,520 have the rejected now we can look for 19535 14:04:38,340 --> 14:04:43,680 that action in our case again we don't 19536 14:04:41,520 --> 14:04:45,720 have any error values in the state or 19537 14:04:43,680 --> 14:04:47,700 nothing like that but let's just log 19538 14:04:45,720 --> 14:04:49,800 whether we can see that action whether 19539 14:04:47,700 --> 14:04:51,956 we can see that everything works and 19540 14:04:49,800 --> 14:04:55,380 since we are using axial so now I can 19541 14:04:51,956 --> 14:04:58,200 mess up the URL and for axios it will be 19542 14:04:55,380 --> 14:05:01,020 an error what that means well we will 19543 14:04:58,200 --> 14:05:02,340 trigger this and we'll basically pass 19544 14:05:01,020 --> 14:05:05,520 this value 19545 14:05:02,340 --> 14:05:08,340 down to our rejected so let's save it 19546 14:05:05,520 --> 14:05:10,916 and then notice in the console we right 19547 14:05:08,340 --> 14:05:13,200 away see this log and we have this 19548 14:05:10,916 --> 14:05:16,800 payload where we have something went 19549 14:05:13,200 --> 14:05:19,436 wrong so for example if we're getting 19550 14:05:16,800 --> 14:05:21,900 some kind of error from the API it's 19551 14:05:19,436 --> 14:05:25,020 going to be located and error response 19552 14:05:21,900 --> 14:05:29,096 and we can actually pass this down using 19553 14:05:25,020 --> 14:05:32,520 thunk API and then reject with the value 19554 14:05:29,096 --> 14:05:36,776 so that's how we can set up asynchronous 19555 14:05:32,520 --> 14:05:39,596 functions with Redux toolkit and this 19556 14:05:36,776 --> 14:05:42,180 concludes our tutorial hopefully 19557 14:05:39,596 --> 14:05:44,756 everyone enjoyed it and I'll see you 19558 14:05:42,180 --> 14:05:47,276 next videos if you have been following 19559 14:05:44,756 --> 14:05:50,640 along with the videos and if your app 19560 14:05:47,276 --> 14:05:51,616 uses latest Redux toolkit version most 19561 14:05:50,640 --> 14:05:54,480 likely 19562 14:05:51,616 --> 14:05:57,480 you see the following warning message in 19563 14:05:54,480 --> 14:06:00,560 the browser the object notation for 19564 14:05:57,480 --> 14:06:04,916 create slice extra reducers is 19565 14:06:00,560 --> 14:06:06,980 deprecated and will be removed in rtk so 19566 14:06:04,916 --> 14:06:09,900 Redux took it 19567 14:06:06,980 --> 14:06:11,880 2.0 please use the Builder callback 19568 14:06:09,900 --> 14:06:14,936 notation instead 19569 14:06:11,880 --> 14:06:16,916 and before we navigate to the docs 19570 14:06:14,936 --> 14:06:19,916 effectively 19571 14:06:16,916 --> 14:06:24,480 it means that we need to refactor our 19572 14:06:19,916 --> 14:06:26,756 extra reducer setup in the card slice 19573 14:06:24,480 --> 14:06:29,520 now the good news is that we won't have 19574 14:06:26,756 --> 14:06:32,456 to change our logic too much only the 19575 14:06:29,520 --> 14:06:34,916 way we pass our callback function and 19576 14:06:32,456 --> 14:06:37,380 life cycle actions 19577 14:06:34,916 --> 14:06:40,200 so let me click on this link we'll 19578 14:06:37,380 --> 14:06:42,840 navigate to the docs create slice yada 19579 14:06:40,200 --> 14:06:45,116 yada and we're looking for that I mean I 19580 14:06:42,840 --> 14:06:48,180 keep forgetting his name the Builder 19581 14:06:45,116 --> 14:06:51,900 callback notation that's the one that we 19582 14:06:48,180 --> 14:06:54,480 want to use effectively like I said we 19583 14:06:51,900 --> 14:06:57,480 won't have to change too much 19584 14:06:54,480 --> 14:06:59,520 so we'll go with X reducers and I will 19585 14:06:57,480 --> 14:07:02,276 have a function so pass in the function 19586 14:06:59,520 --> 14:07:04,860 and the first parameter they call 19587 14:07:02,276 --> 14:07:08,096 Builder so we will also call it Builder 19588 14:07:04,860 --> 14:07:09,480 and then we go with Builder the dot add 19589 14:07:08,096 --> 14:07:12,180 cache 19590 14:07:09,480 --> 14:07:14,340 and then lifecycle actions stay the same 19591 14:07:12,180 --> 14:07:17,456 so we're still looking for pending 19592 14:07:14,340 --> 14:07:19,980 fulfilled as well as rejected and the 19593 14:07:17,456 --> 14:07:21,540 same is with the Callback function it's 19594 14:07:19,980 --> 14:07:25,860 still going to have the same parameters 19595 14:07:21,540 --> 14:07:28,740 we'll just pass it here as a argument 19596 14:07:25,860 --> 14:07:30,540 so life cycle actions stay the same as 19597 14:07:28,740 --> 14:07:32,340 well as the Callback function the only 19598 14:07:30,540 --> 14:07:35,936 difference is that now we need to pass 19599 14:07:32,340 --> 14:07:38,520 them as arguments in this ad cache and 19600 14:07:35,936 --> 14:07:40,860 yes for every life cycle action 19601 14:07:38,520 --> 14:07:43,860 just like we did with object notation 19602 14:07:40,860 --> 14:07:45,660 we'll just add this case 19603 14:07:43,860 --> 14:07:49,020 and I think 19604 14:07:45,660 --> 14:07:51,180 it's going to make more sense if we just 19605 14:07:49,020 --> 14:07:55,200 start working on that so let me navigate 19606 14:07:51,180 --> 14:07:57,720 back again I'm located in the cart slash 19607 14:07:55,200 --> 14:08:00,720 I have my extra reducers as you can see 19608 14:07:57,720 --> 14:08:04,020 we have these lifecycle actions for get 19609 14:08:00,720 --> 14:08:07,320 card items and now we just want to 19610 14:08:04,020 --> 14:08:08,936 refactor this to Builder I mean again 19611 14:08:07,320 --> 14:08:10,560 let me double check 19612 14:08:08,936 --> 14:08:12,540 for some reason I just can't remember 19613 14:08:10,560 --> 14:08:15,116 the name Builder callback notation 19614 14:08:12,540 --> 14:08:17,040 that's the one that we want to use 19615 14:08:15,116 --> 14:08:19,380 um in order to 19616 14:08:17,040 --> 14:08:22,500 make it easier I'm just going to type 19617 14:08:19,380 --> 14:08:24,240 here extra reducers on top and I'll 19618 14:08:22,500 --> 14:08:27,300 reuse the code 19619 14:08:24,240 --> 14:08:29,936 so that way you can clearly see that the 19620 14:08:27,300 --> 14:08:33,060 setup is pretty much the same so here we 19621 14:08:29,936 --> 14:08:36,060 want to passively function like so 19622 14:08:33,060 --> 14:08:37,500 that's the first step then we want to 19623 14:08:36,060 --> 14:08:39,900 set up the parameter 19624 14:08:37,500 --> 14:08:43,256 and we'll call this Builder and then we 19625 14:08:39,900 --> 14:08:45,720 go with Builder and then add cache so 19626 14:08:43,256 --> 14:08:48,116 this is a method so we need to pass in 19627 14:08:45,720 --> 14:08:51,540 some arguments the first one is going to 19628 14:08:48,116 --> 14:08:53,520 be that a life cycle action what do we 19629 14:08:51,540 --> 14:08:55,080 have over here we have get card item 19630 14:08:53,520 --> 14:08:57,840 spending so that's the one that we want 19631 14:08:55,080 --> 14:09:01,140 to use so let's cut this one out pass it 19632 14:08:57,840 --> 14:09:03,000 in as you can see syntax stays exactly 19633 14:09:01,140 --> 14:09:05,700 the same the only difference is that now 19634 14:09:03,000 --> 14:09:07,616 we don't pass this into the square 19635 14:09:05,700 --> 14:09:09,720 brackets that's it that's the first 19636 14:09:07,616 --> 14:09:12,480 parameter second one 19637 14:09:09,720 --> 14:09:14,220 or argument more correctly the second 19638 14:09:12,480 --> 14:09:16,200 thing that we want to pass is that 19639 14:09:14,220 --> 14:09:18,180 callback function and notice how the 19640 14:09:16,200 --> 14:09:19,740 syntax pretty much stays exactly the 19641 14:09:18,180 --> 14:09:22,916 same and yes 19642 14:09:19,740 --> 14:09:25,320 the same parameters State and action and 19643 14:09:22,916 --> 14:09:27,416 yes we still update the state within the 19644 14:09:25,320 --> 14:09:29,096 function body again I know I'm being 19645 14:09:27,416 --> 14:09:31,040 annoying right now but I just want to 19646 14:09:29,096 --> 14:09:34,080 make it extremely clear 19647 14:09:31,040 --> 14:09:36,840 that all the functionality we have 19648 14:09:34,080 --> 14:09:39,720 learned so far stays the same the only 19649 14:09:36,840 --> 14:09:41,880 difference is how we invoke it here 19650 14:09:39,720 --> 14:09:43,200 that's it that's the only difference so 19651 14:09:41,880 --> 14:09:45,480 let's cut this one out again the 19652 14:09:43,200 --> 14:09:48,900 function make sure that you don't have 19653 14:09:45,480 --> 14:09:52,080 any syntax bugs and we're done that's 19654 14:09:48,900 --> 14:09:54,360 our first case for depending one and of 19655 14:09:52,080 --> 14:09:56,820 course what do we need to do we need to 19656 14:09:54,360 --> 14:09:59,936 set up the same ones for fulfilled 19657 14:09:56,820 --> 14:10:01,980 rejected then we can remove the sucker 19658 14:09:59,936 --> 14:10:05,880 altogether and we're going to be good to 19659 14:10:01,980 --> 14:10:08,220 go we'll have no warning messages in the 19660 14:10:05,880 --> 14:10:11,180 console and as you saw in the message 19661 14:10:08,220 --> 14:10:15,480 where if you're using the 19662 14:10:11,180 --> 14:10:17,160 Redux toolkit 2.0 or creator of course 19663 14:10:15,480 --> 14:10:18,480 then 19664 14:10:17,160 --> 14:10:20,340 essentially it's not going to be 19665 14:10:18,480 --> 14:10:22,800 supported so let's just go with that 19666 14:10:20,340 --> 14:10:25,320 case yes effectively we just want to 19667 14:10:22,800 --> 14:10:27,540 change here add one and then I'll add 19668 14:10:25,320 --> 14:10:28,800 one more case right away like so and now 19669 14:10:27,540 --> 14:10:31,256 let's set it up 19670 14:10:28,800 --> 14:10:33,720 this one is going to be straightforward 19671 14:10:31,256 --> 14:10:36,000 we just want to copy and paste 19672 14:10:33,720 --> 14:10:38,700 there's not much logic in here so let's 19673 14:10:36,000 --> 14:10:40,256 cut it out let's pass in the comma the 19674 14:10:38,700 --> 14:10:43,256 only thing you need to look out for is 19675 14:10:40,256 --> 14:10:45,660 some uh sewer syntax bug 19676 14:10:43,256 --> 14:10:48,596 so let me take it here which by the way 19677 14:10:45,660 --> 14:10:51,900 I'm an expert on so I'll take my sweet 19678 14:10:48,596 --> 14:10:54,956 time while I'm setting this one up comma 19679 14:10:51,900 --> 14:10:57,116 here copy and paste yep everything is 19680 14:10:54,956 --> 14:10:59,936 good to go we just remove the extra 19681 14:10:57,116 --> 14:11:03,000 reducers we save once I navigate to the 19682 14:10:59,936 --> 14:11:07,860 browser as you can see I have no error 19683 14:11:03,000 --> 14:11:11,820 so now our extra reducers is using the 19684 14:11:07,860 --> 14:11:13,740 Builder callback notation all right that 19685 14:11:11,820 --> 14:11:15,776 concludes the video thank you for 19686 14:11:13,740 --> 14:11:18,000 watching I hope you enjoyed this video 19687 14:11:15,776 --> 14:11:20,160 and learn something new 19688 14:11:18,000 --> 14:11:22,380 if you want to continue learning react 19689 14:11:20,160 --> 14:11:25,680 and take your skills to the next level 19690 14:11:22,380 --> 14:11:28,140 be sure to check out my reactors in 19691 14:11:25,680 --> 14:11:31,080 there you'll find bunch of additional 19692 14:11:28,140 --> 14:11:33,180 in-depth tutorials challenges and 19693 14:11:31,080 --> 14:11:36,300 projects designed to help you master 19694 14:11:33,180 --> 14:11:39,300 this powerful library to get started 19695 14:11:36,300 --> 14:11:41,340 just head over to johnsmilk.com thanks 19696 14:11:39,300 --> 14:11:43,880 again for watching and I'll see you in 19697 14:11:41,340 --> 14:11:43,880 the next one1473746

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