All language subtitles for 017 Building & Using a Custom Context Provider Component_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
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 Download
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:02,200 --> 00:00:05,010 Now a little enhancement to our context. 2 00:00:05,010 --> 00:00:09,810 It is a good idea to also add your functions 3 00:00:09,810 --> 00:00:12,800 like onLogout in my case to the default context 4 00:00:12,800 --> 00:00:14,230 when you create a context. 5 00:00:14,230 --> 00:00:17,140 And there you can just store a dummy function 6 00:00:17,140 --> 00:00:18,470 which does nothing. 7 00:00:18,470 --> 00:00:21,750 I'm doing this for better IDE auto-completion. 8 00:00:21,750 --> 00:00:24,520 Because you will see that if I don't have this here 9 00:00:24,520 --> 00:00:26,040 in the Navigation component, 10 00:00:26,040 --> 00:00:29,620 here if I try to call onLogout, 11 00:00:29,620 --> 00:00:32,290 I'm not getting any suggestion by my IDE. 12 00:00:32,290 --> 00:00:35,200 It doesn't know that there is a onLogout prop. 13 00:00:35,200 --> 00:00:36,290 On the upper hand you will see 14 00:00:36,290 --> 00:00:39,390 that there is this isLoggedIn suggestion. 15 00:00:39,390 --> 00:00:42,270 Now that's simply happening because the way React works 16 00:00:42,270 --> 00:00:45,220 and the way Visual Studio Code at least works. 17 00:00:45,220 --> 00:00:48,480 It's looking at this default context object 18 00:00:48,480 --> 00:00:52,440 to find out what you are able to access on your context. 19 00:00:52,440 --> 00:00:55,210 So for better IDE auto-completion, 20 00:00:55,210 --> 00:00:57,650 it is a good idea to add onLogout here 21 00:00:57,650 --> 00:01:00,100 in your default context object as well 22 00:01:00,100 --> 00:01:01,600 just with a dummy function 23 00:01:01,600 --> 00:01:04,170 because we're not going to use this anyways. 24 00:01:04,170 --> 00:01:08,480 And then here you will see that now 25 00:01:08,480 --> 00:01:11,010 I get to this onLogout auto-completion. 26 00:01:11,010 --> 00:01:13,830 So that's just a tiny hint you might consider 27 00:01:13,830 --> 00:01:15,413 because it can be helpful. 28 00:01:16,530 --> 00:01:19,440 Another thing that you might consider 29 00:01:19,440 --> 00:01:22,070 is depending on your application structure 30 00:01:22,070 --> 00:01:24,710 and how you're managing your data, 31 00:01:24,710 --> 00:01:28,160 you also might want to pull more logic 32 00:01:28,160 --> 00:01:31,090 out of for example App component 33 00:01:31,090 --> 00:01:34,590 and create a separate Context Management component 34 00:01:34,590 --> 00:01:36,173 if you wanna call it like this. 35 00:01:37,530 --> 00:01:39,140 How could this look like? 36 00:01:39,140 --> 00:01:42,440 Well, maybe in our AuthContext store file, 37 00:01:42,440 --> 00:01:47,330 we could create a AuthContexProvider component now actually 38 00:01:49,800 --> 00:01:52,870 where we return AuthContext.Provider 39 00:01:55,610 --> 00:01:58,820 and we accept props and simply pass whatever we got 40 00:01:58,820 --> 00:02:02,230 on props.children between our provider. 41 00:02:02,230 --> 00:02:05,140 And then here I don't just export AuthContext 42 00:02:05,140 --> 00:02:09,840 as the default, but I also export my AuthContextProvider 43 00:02:09,840 --> 00:02:12,833 in addition to the default as a named export. 44 00:02:13,700 --> 00:02:15,480 Now, why am I doing that? 45 00:02:15,480 --> 00:02:20,137 Because now we can actually also import useState here 46 00:02:21,260 --> 00:02:24,160 inside of the auth-context.js file, 47 00:02:24,160 --> 00:02:29,160 and manage the isLoggedIn state and setIsLoggedIn here 48 00:02:32,520 --> 00:02:34,683 inside auth this AuthContextProvider, 49 00:02:35,790 --> 00:02:39,260 add the logoutHandler function here 50 00:02:39,260 --> 00:02:43,690 where setIsLoggedIn to false, 51 00:02:43,690 --> 00:02:47,840 and also add the loginHandler function here now 52 00:02:48,840 --> 00:02:51,230 so that we manage the entire authentication state 53 00:02:51,230 --> 00:02:54,200 in this separate provider component. 54 00:02:54,200 --> 00:02:57,930 And here I set IsLoggedIn to true of course. 55 00:02:57,930 --> 00:03:00,590 And then I set the value here of course 56 00:03:00,590 --> 00:03:02,900 to an object where I have isLoggedIn, 57 00:03:04,380 --> 00:03:07,500 which is equal to the isLoggedIn state 58 00:03:07,500 --> 00:03:09,070 where I have onLogout 59 00:03:09,070 --> 00:03:11,180 which points at the logoutHandler. 60 00:03:11,180 --> 00:03:14,250 And in addition where we now also have onLogin 61 00:03:14,250 --> 00:03:16,290 which points at the loginHandler 62 00:03:17,820 --> 00:03:20,570 and therefore, I will also add this 63 00:03:20,570 --> 00:03:25,570 to my default context here onLogin with a dummy function, 64 00:03:26,640 --> 00:03:27,970 just making it clear 65 00:03:27,970 --> 00:03:30,273 that we there want the email and a password. 66 00:03:32,080 --> 00:03:33,740 We're not doing anything with that data 67 00:03:33,740 --> 00:03:36,620 but technically we would need it in a real app. 68 00:03:36,620 --> 00:03:39,510 And now we have this standalone file 69 00:03:39,510 --> 00:03:43,280 which manages the entire login state 70 00:03:43,280 --> 00:03:45,540 in this AuthContextProvider component. 71 00:03:45,540 --> 00:03:48,530 And which also sets up all the context. 72 00:03:48,530 --> 00:03:52,170 And the advantage here could be, and I'm saying could 73 00:03:52,170 --> 00:03:54,410 because this is something you can consider. 74 00:03:54,410 --> 00:03:56,740 It always depends on your preferences, 75 00:03:56,740 --> 00:03:59,630 the exact useCase, and the size of your app. 76 00:03:59,630 --> 00:04:01,730 But this is something you could consider 77 00:04:01,730 --> 00:04:06,570 because now we can strip that all out of App component. 78 00:04:06,570 --> 00:04:10,130 We of course also though need to move over 79 00:04:10,130 --> 00:04:13,200 our local storage access here. 80 00:04:13,200 --> 00:04:17,293 So, clear data in the logoutHandler, 81 00:04:18,560 --> 00:04:21,757 set our data in the loginHandler, 82 00:04:24,380 --> 00:04:28,170 and of course also add our useEffect code here 83 00:04:29,130 --> 00:04:32,273 that all now needs to go into our AuthContextProvider, 84 00:04:33,967 --> 00:04:36,593 and therefore we also need to import useEffect here. 85 00:04:37,950 --> 00:04:42,950 But with that all we can now clear all that logic here 86 00:04:43,030 --> 00:04:48,030 from the App component, remove these hook imports here, 87 00:04:48,920 --> 00:04:53,920 remove our AuthContext import here, remove all of that. 88 00:04:55,540 --> 00:04:59,060 And therefore of course, bring back React Fragment 89 00:05:00,010 --> 00:05:02,060 like this, and I'm doing this 90 00:05:02,060 --> 00:05:05,850 because now we can wrap the App component as a whole. 91 00:05:05,850 --> 00:05:07,590 So therefore an index component 92 00:05:07,590 --> 00:05:10,055 which is where we rendered the app component 93 00:05:10,055 --> 00:05:12,933 with AuthContext provider. 94 00:05:14,020 --> 00:05:16,530 So with our AuthContextProvider component 95 00:05:16,530 --> 00:05:20,573 which we just created an exported in the auth-context file, 96 00:05:22,220 --> 00:05:25,290 and with that we have one central place 97 00:05:25,290 --> 00:05:26,680 for the state management 98 00:05:26,680 --> 00:05:28,980 for the off state management, I should say. 99 00:05:28,980 --> 00:05:32,280 And that central place is now not the App component 100 00:05:32,280 --> 00:05:34,870 but a dedicated context component 101 00:05:34,870 --> 00:05:37,170 and a dedicated context file. 102 00:05:37,170 --> 00:05:40,350 And this might be a structure you might prefer 103 00:05:40,350 --> 00:05:42,480 because it is more focused 104 00:05:42,480 --> 00:05:44,990 and gives you a leaner app component 105 00:05:44,990 --> 00:05:49,020 which now is not concerned with app wide state management 106 00:05:49,020 --> 00:05:52,100 but which now instead can just focus on 107 00:05:52,100 --> 00:05:54,233 well, bringing something onto the screen. 108 00:05:56,140 --> 00:05:58,700 Of course, since I'm using isLoggedIn here 109 00:05:58,700 --> 00:06:01,530 I need to be able to get that information. 110 00:06:01,530 --> 00:06:04,610 So now in the App component we wanna use useContext, 111 00:06:07,850 --> 00:06:11,050 and pass our AuthContext, not the provider 112 00:06:11,050 --> 00:06:13,120 but still the AuthContext object 113 00:06:13,120 --> 00:06:17,040 which is our default export in the AuthContext file. 114 00:06:17,040 --> 00:06:21,050 And therefore, we now got our context here as well 115 00:06:21,050 --> 00:06:23,050 and hence we can access it here. 116 00:06:23,050 --> 00:06:25,300 And now we could even remove the onLogin 117 00:06:25,300 --> 00:06:28,270 and onLog out props here 118 00:06:28,270 --> 00:06:31,060 because we're not managing the log out and log in handlers 119 00:06:31,060 --> 00:06:33,210 and App component anyways, 120 00:06:33,210 --> 00:06:37,270 and now tap into contexts in the Home and Login components. 121 00:06:37,270 --> 00:06:40,547 So in the Home component we can also import useContext 122 00:06:44,500 --> 00:06:47,250 and get access to our AuthContext 123 00:06:47,250 --> 00:06:50,970 maybe to mix up the naming and make it clear what's in there 124 00:06:50,970 --> 00:06:52,850 to which context we're listing here 125 00:06:52,850 --> 00:06:57,510 by importing AuthContext and passing it to useContext. 126 00:06:57,510 --> 00:07:01,050 Again, use AuthContext here not the provider. 127 00:07:01,050 --> 00:07:04,057 And then here we can use authCtx.onLogout 128 00:07:05,230 --> 00:07:08,410 and do something similar in the Login component. 129 00:07:08,410 --> 00:07:10,913 Import useContext here, 130 00:07:12,300 --> 00:07:14,490 and then in our component function 131 00:07:17,590 --> 00:07:22,300 maybe here we can set up our AuthContext 132 00:07:22,300 --> 00:07:26,450 by calling useContext and passing AuthContext to it. 133 00:07:26,450 --> 00:07:30,593 For that make sure you are importing AuthContext of course. 134 00:07:32,090 --> 00:07:37,060 And then we can use AuthContext when we need to log in. 135 00:07:37,060 --> 00:07:42,060 So here it's not props onLogin, but authCtx.onLogin 136 00:07:42,553 --> 00:07:45,410 to which we forward the email and password. 137 00:07:45,410 --> 00:07:48,020 Now again, this is totally optional. 138 00:07:48,020 --> 00:07:50,010 The advantage here could just be 139 00:07:50,010 --> 00:07:52,220 that you have a leaner app component 140 00:07:52,220 --> 00:07:55,600 which again is now just focused on returning JSX 141 00:07:55,600 --> 00:07:57,320 and bringing something onto the screen. 142 00:07:57,320 --> 00:08:00,360 And you have to actual off state management 143 00:08:00,360 --> 00:08:04,000 and auth-context management all in one file. 144 00:08:04,000 --> 00:08:06,770 And some developers including myself 145 00:08:06,770 --> 00:08:11,450 often prefer this more focused and separated approach 146 00:08:11,450 --> 00:08:13,000 where you have components, 147 00:08:13,000 --> 00:08:16,210 where every component has one job essentially 148 00:08:16,210 --> 00:08:18,350 and not a ton of jobs. 149 00:08:18,350 --> 00:08:20,690 And therefore I'm a fan of this split, 150 00:08:20,690 --> 00:08:23,530 but as often it's not the only way of doing this 151 00:08:23,530 --> 00:08:25,860 and it's also not the only correct way. 152 00:08:25,860 --> 00:08:28,650 It's definitely a practice that is worth looking into 153 00:08:28,650 --> 00:08:32,100 though, a practice you might see in projects out there, 154 00:08:32,100 --> 00:08:34,659 and therefore something that of course needs to go 155 00:08:34,659 --> 00:08:36,150 into a course like this, 156 00:08:36,150 --> 00:08:37,909 which has the job of turning you 157 00:08:37,909 --> 00:08:40,179 into a real React developer. 158 00:08:40,179 --> 00:08:42,640 And therefore you have to know patterns like this 159 00:08:42,640 --> 00:08:45,330 which is why we refactored this. 160 00:08:45,330 --> 00:08:46,880 With all of that out of the way 161 00:08:46,880 --> 00:08:50,323 let's test our application to ensure that this still works. 162 00:08:51,260 --> 00:08:54,860 And that all seems to work to me here. 163 00:08:54,860 --> 00:08:58,700 Let's also check the Logout button here. 164 00:08:58,700 --> 00:09:00,690 Yeah, that all works. 165 00:09:00,690 --> 00:09:03,000 Now with one central context 166 00:09:03,000 --> 00:09:05,103 with one central state management. 13300

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