All language subtitles for 030 Introducing React Context_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,100 --> 00:00:05,100 Now that we can add data to Firebase, 2 00:00:05,100 --> 00:00:07,550 so that we can store data on a server 3 00:00:07,550 --> 00:00:11,340 and that we can fetch data whenever we visit all meetups, 4 00:00:11,340 --> 00:00:14,580 now with that we got the core functionality built in. 5 00:00:14,580 --> 00:00:17,280 The last feature, which I wanna add here 6 00:00:17,280 --> 00:00:19,200 is this favorites feature. 7 00:00:19,200 --> 00:00:21,570 This button here should be clickable 8 00:00:21,570 --> 00:00:24,350 and when we click it, this meetup should 9 00:00:24,350 --> 00:00:27,230 be added to our favorites. 10 00:00:27,230 --> 00:00:29,220 Then on the my favorites page 11 00:00:29,220 --> 00:00:31,630 I wanna see a list of all my favorites 12 00:00:31,630 --> 00:00:34,670 and of course, as soon as item is a favorite 13 00:00:34,670 --> 00:00:37,740 I want to be able to unfavorite it. 14 00:00:37,740 --> 00:00:41,170 And then I also want to show a little badge here 15 00:00:41,170 --> 00:00:44,360 in the navigation next to my favorites 16 00:00:44,360 --> 00:00:48,110 where we see the current count of favorites. 17 00:00:48,110 --> 00:00:50,940 That's the last feature I wanna add. 18 00:00:50,940 --> 00:00:54,030 And for this we'll need to manage some state 19 00:00:54,030 --> 00:00:57,220 which affects more than one component. 20 00:00:57,220 --> 00:01:01,090 Because our list of favorites is some state 21 00:01:01,090 --> 00:01:04,790 that should cause the UI to update 22 00:01:04,790 --> 00:01:07,300 because we, for example, will have that badge here, 23 00:01:07,300 --> 00:01:10,430 which shows the current number of favorites. 24 00:01:10,430 --> 00:01:12,696 And of course this button should change once 25 00:01:12,696 --> 00:01:14,480 our item is a favorite. 26 00:01:14,480 --> 00:01:16,810 So we'll have different parts 27 00:01:16,810 --> 00:01:20,850 of this application that will be affected by our state. 28 00:01:20,850 --> 00:01:23,780 And because that's the case, because we'll have 29 00:01:23,780 --> 00:01:26,930 a state that affects more than one component 30 00:01:26,930 --> 00:01:31,700 we will need a mechanism of managing that state globally 31 00:01:31,700 --> 00:01:36,140 and distributing that state to different components. 32 00:01:36,140 --> 00:01:40,440 Because just using use state inside of a single component 33 00:01:40,440 --> 00:01:44,090 doesn't necessarily do the trick anymore, 34 00:01:44,090 --> 00:01:46,683 because that only affects one component. 35 00:01:47,520 --> 00:01:52,520 Now, one thing we could do is we could lift the state up. 36 00:01:53,170 --> 00:01:55,010 That means that we could, for example 37 00:01:55,010 --> 00:01:58,660 manage our favorites state in app JS, 38 00:01:58,660 --> 00:02:01,570 and then we pass it into all the components 39 00:02:01,570 --> 00:02:04,200 that are interested as props. 40 00:02:04,200 --> 00:02:06,450 So to the layout component which 41 00:02:06,450 --> 00:02:08,830 has access to the main navigation component, 42 00:02:08,830 --> 00:02:11,760 which is where I wanna show this badge in the end, 43 00:02:11,760 --> 00:02:14,480 to that component we could pass the number 44 00:02:14,480 --> 00:02:17,720 of favorites if we manage the favorites state 45 00:02:17,720 --> 00:02:20,060 in the app component, and then to 46 00:02:20,060 --> 00:02:22,340 the favorites page component we could 47 00:02:22,340 --> 00:02:25,590 pass the array of favorite meetups. 48 00:02:25,590 --> 00:02:28,640 So we could manage the state here in the app component 49 00:02:28,640 --> 00:02:31,743 and then just distribute it through props. 50 00:02:32,660 --> 00:02:37,170 That would work, but it has a couple of downsides. 51 00:02:37,170 --> 00:02:40,840 One problem would be that if we have a bigger application 52 00:02:40,840 --> 00:02:44,660 with different States that affect different components 53 00:02:44,660 --> 00:02:47,500 we have to manage more and more state in 54 00:02:47,500 --> 00:02:50,630 this app component, and hence this app component 55 00:02:50,630 --> 00:02:54,980 becomes bigger and bigger and that's maybe not ideal. 56 00:02:54,980 --> 00:02:59,650 Another problem is that if we pass states down through props 57 00:02:59,650 --> 00:03:03,960 that we can end up with very long prop chains. 58 00:03:03,960 --> 00:03:07,550 We pass the number of favorites to the layout component 59 00:03:07,550 --> 00:03:10,770 and from there we pass it into main navigation. 60 00:03:10,770 --> 00:03:13,720 So we already passed props to layout 61 00:03:13,720 --> 00:03:16,580 just so that this component can then 62 00:03:16,580 --> 00:03:20,580 pass that data on to another component. 63 00:03:20,580 --> 00:03:22,780 Whilst that is not horrible 64 00:03:22,780 --> 00:03:25,340 it's also not necessarily great. 65 00:03:25,340 --> 00:03:30,010 And it can also make our code a bit harder to maintain. 66 00:03:30,010 --> 00:03:32,410 And because we have issues like this 67 00:03:32,410 --> 00:03:34,660 there are state management solutions 68 00:03:34,660 --> 00:03:37,460 for managing application wide state 69 00:03:37,460 --> 00:03:39,600 in a more convenient way. 70 00:03:39,600 --> 00:03:44,560 One very popular state management package is Redux. 71 00:03:44,560 --> 00:03:47,850 I cover it in my course, but for many scenarios 72 00:03:47,850 --> 00:03:51,870 we don't even need Redux, because react also has a built 73 00:03:51,870 --> 00:03:56,190 in state management solution for application wide state. 74 00:03:56,190 --> 00:03:59,650 And that's a feature called context. 75 00:03:59,650 --> 00:04:02,960 Now for this to add this context feature, 76 00:04:02,960 --> 00:04:05,480 I'll add a new folder in the source folder 77 00:04:05,480 --> 00:04:07,140 and I'll name it store. 78 00:04:07,140 --> 00:04:08,770 The folder name is up to you 79 00:04:08,770 --> 00:04:11,070 but store is a common convention 80 00:04:11,070 --> 00:04:15,653 since we will set up the state store for this application. 81 00:04:16,810 --> 00:04:21,660 Instead of store, I'll add a favorite stash context JS file 82 00:04:21,660 --> 00:04:26,000 and in this file I'll now create such a context. 83 00:04:26,000 --> 00:04:28,530 For this in this file, we first of all, 84 00:04:28,530 --> 00:04:33,030 import create context from react 85 00:04:33,030 --> 00:04:36,410 so that's a function exposed by the react library 86 00:04:37,950 --> 00:04:41,220 and we can then call create context. 87 00:04:41,220 --> 00:04:45,810 And this that's what the name implies it creates a context. 88 00:04:45,810 --> 00:04:49,500 We don't know yet what exactly such a context is, 89 00:04:49,500 --> 00:04:53,300 but hey, we are creating one at least. 90 00:04:53,300 --> 00:04:56,810 Now context in the end is a JavaScript object. 91 00:04:56,810 --> 00:04:58,950 And we can store this in a constant 92 00:04:58,950 --> 00:05:01,820 and I'll name it favorites context. 93 00:05:01,820 --> 00:05:05,830 I start with a capital F because this object 94 00:05:05,830 --> 00:05:08,740 which is created by context actually 95 00:05:08,740 --> 00:05:12,520 will contain a react component. 96 00:05:12,520 --> 00:05:15,570 And when you build your own react components 97 00:05:15,570 --> 00:05:18,780 the convention which you should follow is that you start 98 00:05:18,780 --> 00:05:23,150 with a capital character for your component names. 99 00:05:23,150 --> 00:05:24,940 So that's why I store my context 100 00:05:24,940 --> 00:05:27,963 in a favorites context constant here. 101 00:05:29,180 --> 00:05:32,110 Now create context also takes an argument 102 00:05:32,110 --> 00:05:36,390 and then the argument is the initial value for that context. 103 00:05:36,390 --> 00:05:39,220 So the initial value for this application 104 00:05:39,220 --> 00:05:42,070 or component wide state. 105 00:05:42,070 --> 00:05:45,140 And that can be any value of your choice. 106 00:05:45,140 --> 00:05:47,253 It could, for example, be an object, 107 00:05:48,190 --> 00:05:51,290 let's say an object where we have a favorites key 108 00:05:51,290 --> 00:05:53,030 which is an empty array initially 109 00:05:53,030 --> 00:05:55,740 because we have no favorites at the beginning 110 00:05:55,740 --> 00:05:58,040 and maybe a total favorites key, 111 00:05:58,040 --> 00:05:59,620 which is zero initially 112 00:05:59,620 --> 00:06:02,463 which is the total number of favorites. 113 00:06:04,450 --> 00:06:06,200 That could be our context 114 00:06:06,200 --> 00:06:08,450 but like this it's not too useful 115 00:06:08,450 --> 00:06:11,210 we also need ways of changing 116 00:06:11,210 --> 00:06:15,230 these values of adding and removing favorites. 117 00:06:15,230 --> 00:06:19,150 And that's why I will also add a component in this file. 118 00:06:19,150 --> 00:06:21,420 So a component function 119 00:06:21,420 --> 00:06:24,533 which I'll name favorites context provider. 120 00:06:26,120 --> 00:06:29,860 This component will be a regular react component 121 00:06:29,860 --> 00:06:34,670 but it will have the job of providing this context 122 00:06:34,670 --> 00:06:37,290 to all the components that are interested 123 00:06:37,290 --> 00:06:39,320 in listening to the values, 124 00:06:39,320 --> 00:06:42,170 So all the components that need values 125 00:06:42,170 --> 00:06:45,870 from the context and this component here 126 00:06:45,870 --> 00:06:48,260 the favorites context provider component 127 00:06:48,260 --> 00:06:53,260 will also be responsible for updating the context values. 128 00:06:53,680 --> 00:06:57,410 Now for this what we'll do here is we'll return 129 00:06:57,410 --> 00:07:00,170 favorites context so this constant 130 00:07:00,170 --> 00:07:04,040 which we created here dot provider, 131 00:07:04,040 --> 00:07:08,153 that's a component this context object has built in. 132 00:07:09,120 --> 00:07:12,762 And this provider component needs to be wrapped 133 00:07:12,762 --> 00:07:16,133 around all the components that are interested 134 00:07:16,133 --> 00:07:18,883 in interacting with that context. 135 00:07:19,800 --> 00:07:22,832 Now, this favorites context provider component 136 00:07:22,832 --> 00:07:26,266 which we are building here itself should be wrapped 137 00:07:26,266 --> 00:07:27,980 around other components later. 138 00:07:27,980 --> 00:07:31,590 I plan on using it in index JS to wrap it 139 00:07:31,590 --> 00:07:34,920 around my entire app so that all the components 140 00:07:34,920 --> 00:07:38,090 in the app have access to the context. 141 00:07:38,090 --> 00:07:40,310 And therefore, what we can do here 142 00:07:40,310 --> 00:07:43,320 is we can accept props and simply wrap 143 00:07:43,320 --> 00:07:46,530 this provider component, which we get 144 00:07:46,530 --> 00:07:50,990 from this context object around props children. 145 00:07:50,990 --> 00:07:54,560 That means that we can now wrap our component here 146 00:07:54,560 --> 00:07:56,520 around any other components. 147 00:07:56,520 --> 00:07:58,500 And those components will be wrapped 148 00:07:58,500 --> 00:08:00,570 by context automatically. 149 00:08:00,570 --> 00:08:02,470 I hope this makes sense. 150 00:08:02,470 --> 00:08:04,910 Now why am I doing it like this? 151 00:08:04,910 --> 00:08:08,470 Because now in his favorites context provider component 152 00:08:08,470 --> 00:08:13,470 which we're building here, we can manage our context data. 153 00:08:13,730 --> 00:08:16,050 We can manage that with state, 154 00:08:16,050 --> 00:08:20,040 because this component which we are working on here 155 00:08:20,040 --> 00:08:23,590 is still a regular react component. 156 00:08:23,590 --> 00:08:25,820 So when we manage state in there, 157 00:08:25,820 --> 00:08:29,190 when we changed that state, that component here 158 00:08:29,190 --> 00:08:33,010 we'll still execute again and we'll be re-evaluated 159 00:08:33,010 --> 00:08:36,610 and that means that if we change our context value 160 00:08:36,610 --> 00:08:38,960 in this component, and we pass 161 00:08:38,960 --> 00:08:41,690 this context value to the provider 162 00:08:41,690 --> 00:08:44,840 all components that are listening 163 00:08:44,840 --> 00:08:48,000 to our context will be updated 164 00:08:48,000 --> 00:08:51,020 and will get that latest updated data. 165 00:08:51,020 --> 00:08:54,800 And in case it's not 100% clear yet 166 00:08:54,800 --> 00:08:56,300 we'll get there step-by-step 167 00:08:56,300 --> 00:08:58,963 because we're now going to add the logic for this. 168 00:09:00,120 --> 00:09:05,043 In favorites context provider I'll create a context object 169 00:09:06,150 --> 00:09:10,010 and I'll pass this context object as a value 170 00:09:10,010 --> 00:09:15,010 to favorites context dot provider, this component 171 00:09:15,250 --> 00:09:18,420 which is exposed by this favorites context object 172 00:09:18,420 --> 00:09:23,420 wants a value prop where we pass the current context value. 173 00:09:24,920 --> 00:09:29,360 We set the initial values here when we create the context 174 00:09:29,360 --> 00:09:31,940 but we can then update those values 175 00:09:31,940 --> 00:09:36,730 and pass the latest values with help of that value prop. 176 00:09:36,730 --> 00:09:38,900 And whenever that value changes 177 00:09:38,900 --> 00:09:41,420 all components that are listening 178 00:09:41,420 --> 00:09:44,740 to our context will be updated in the end. 179 00:09:44,740 --> 00:09:46,790 That's how this works. 180 00:09:46,790 --> 00:09:50,570 So now we just need to derive this context object 181 00:09:50,570 --> 00:09:52,740 and its values dynamically. 182 00:09:52,740 --> 00:09:56,350 And that's where use state comes into play now. 183 00:09:56,350 --> 00:09:58,410 We can manage some state here 184 00:09:58,410 --> 00:10:00,340 in this favorites context provider 185 00:10:00,340 --> 00:10:04,950 and we can manage an array of favorited meetups here. 186 00:10:04,950 --> 00:10:08,070 So here we have the user favorites 187 00:10:08,070 --> 00:10:11,663 let's say and a function for setting those user favorites. 188 00:10:12,720 --> 00:10:16,300 Now here this context object, which we construct 189 00:10:16,300 --> 00:10:19,700 which holds the latest values that should be exposed 190 00:10:19,700 --> 00:10:22,500 to our components, that object now 191 00:10:22,500 --> 00:10:26,000 has a favorites key because we defined 192 00:10:26,000 --> 00:10:28,920 it here in the initial context value as well. 193 00:10:28,920 --> 00:10:32,020 And we set our current user favorites array 194 00:10:32,020 --> 00:10:35,300 this state snapshot as a value. 195 00:10:35,300 --> 00:10:38,560 So when the state changes this value will change 196 00:10:38,560 --> 00:10:40,530 and will have a new context object 197 00:10:40,530 --> 00:10:44,470 and will pass this new updated context object 198 00:10:44,470 --> 00:10:47,360 to all the components that are wrapped 199 00:10:47,360 --> 00:10:48,833 by this provider in the end. 200 00:10:50,430 --> 00:10:52,550 And I'll also set total favorites here 201 00:10:52,550 --> 00:10:54,943 and that is simply user favorites dot length. 202 00:10:56,010 --> 00:10:58,310 Like this, now we just need a way 203 00:10:58,310 --> 00:11:00,733 of changing our state here. 16365

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