All language subtitles for 032 Using Context In Components_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:02,050 --> 00:00:03,850 They should instead be called 2 00:00:03,850 --> 00:00:08,380 from inside some of our other components and pages. 3 00:00:08,380 --> 00:00:10,820 So actually what we wanna do here is 4 00:00:10,820 --> 00:00:13,720 we don't just wanna manage values 5 00:00:13,720 --> 00:00:16,750 which are then accessible by different components, 6 00:00:16,750 --> 00:00:20,500 but also functions that will change those values 7 00:00:20,500 --> 00:00:23,980 that are accessible from different components. 8 00:00:23,980 --> 00:00:28,310 And hence we can expose pointers at these functions 9 00:00:28,310 --> 00:00:31,250 to our different components as well. 10 00:00:31,250 --> 00:00:35,040 So in our context here, we don't just have the values 11 00:00:35,040 --> 00:00:39,490 for our favorites, but we also can add function steer. 12 00:00:39,490 --> 00:00:43,050 We can add add favorite key, for example 13 00:00:43,050 --> 00:00:45,570 which stores are pointer at the add 14 00:00:45,570 --> 00:00:48,430 favorite handler as a value. 15 00:00:48,430 --> 00:00:50,240 So we don't execute the function here 16 00:00:50,240 --> 00:00:53,480 we point at it, and that exposes 17 00:00:53,480 --> 00:00:56,870 this function defined in this component 18 00:00:56,870 --> 00:01:01,100 to all components in this application that are interested. 19 00:01:01,100 --> 00:01:03,790 And you will see how to use this context 20 00:01:03,790 --> 00:01:06,223 in our components in just a second. 21 00:01:07,130 --> 00:01:08,380 And we can therefore also add, 22 00:01:08,380 --> 00:01:10,660 remove favorite here and point at 23 00:01:10,660 --> 00:01:12,570 the remove favorite handler 24 00:01:12,570 --> 00:01:15,420 and add the item is favorite key 25 00:01:15,420 --> 00:01:19,740 and point at the item is favorite handler function. 26 00:01:19,740 --> 00:01:22,100 And now we expose these functions 27 00:01:22,100 --> 00:01:24,853 to all interested components as well. 28 00:01:25,990 --> 00:01:29,330 Now that that's the case, I will also go 29 00:01:29,330 --> 00:01:33,060 to my initial context and add those keys here as well 30 00:01:33,060 --> 00:01:36,240 and set that to empty functions 31 00:01:36,240 --> 00:01:40,650 that just gets the correct parameters 32 00:01:40,650 --> 00:01:43,210 and I'm only doing this because that 33 00:01:43,210 --> 00:01:47,680 will actually give us better auto completion later. 34 00:01:47,680 --> 00:01:49,540 It doesn't really do anything here 35 00:01:49,540 --> 00:01:52,420 since these are functions that don't do anything 36 00:01:52,420 --> 00:01:53,950 but it will help us with 37 00:01:53,950 --> 00:01:56,357 auto-completion in the IDE later. 38 00:01:56,357 --> 00:01:59,050 And that's why we'll just add them here 39 00:01:59,050 --> 00:02:01,053 to the initial context as well. 40 00:02:03,030 --> 00:02:05,460 Okay, so now with that, we spent a lot 41 00:02:05,460 --> 00:02:08,080 of time in that file and I could imagine 42 00:02:08,080 --> 00:02:11,890 that it's not all 100% clear yet 43 00:02:11,890 --> 00:02:15,600 but we're now done with working in this file. 44 00:02:15,600 --> 00:02:20,200 Now we need to wrap our context provider around 45 00:02:20,200 --> 00:02:24,170 all the components that want to interact with this context. 46 00:02:24,170 --> 00:02:26,780 And then in the different components 47 00:02:26,780 --> 00:02:31,000 we need to pull out the values we're interested in 48 00:02:31,000 --> 00:02:36,000 or call the methods, the functions here we defined. 49 00:02:37,140 --> 00:02:39,730 And for that we need to export 50 00:02:39,730 --> 00:02:42,580 this favorites context for wider component here 51 00:02:42,580 --> 00:02:45,880 by adding the export keyword in front of this function 52 00:02:46,730 --> 00:02:50,930 and also maybe at the bottom of the file export 53 00:02:50,930 --> 00:02:55,110 the context as itself, the favorites context itself 54 00:02:56,150 --> 00:02:57,830 as a default. 55 00:02:57,830 --> 00:03:00,740 So we have two exports this function here, 56 00:03:00,740 --> 00:03:03,430 which we export by its name, and then 57 00:03:03,430 --> 00:03:05,270 this default export, which is 58 00:03:05,270 --> 00:03:07,360 our favorites context itself. 59 00:03:07,360 --> 00:03:09,900 These two things are exported here 60 00:03:09,900 --> 00:03:13,610 because we need to interact with both objects, 61 00:03:13,610 --> 00:03:17,113 with both things from outside this file. 62 00:03:18,000 --> 00:03:19,470 Now let's start with interacting 63 00:03:19,470 --> 00:03:23,400 with this favorites context provider component. 64 00:03:23,400 --> 00:03:24,860 I mentioned multiple times that 65 00:03:24,860 --> 00:03:27,550 we wanna wrap this component around 66 00:03:27,550 --> 00:03:30,310 all the components that are interested 67 00:03:30,310 --> 00:03:32,650 in our context data. 68 00:03:32,650 --> 00:03:35,700 And in this case, the my favorites page 69 00:03:35,700 --> 00:03:39,280 will be interested, the navigation bar will be interested 70 00:03:39,280 --> 00:03:41,460 since I wanna show a batch here 71 00:03:41,460 --> 00:03:44,960 and the all meetups page will also be interested 72 00:03:44,960 --> 00:03:47,470 or to be precise, the meetup item components 73 00:03:47,470 --> 00:03:49,930 will be interested because there I have 74 00:03:49,930 --> 00:03:52,950 this two favorites button which for example, 75 00:03:52,950 --> 00:03:56,470 should trigger the add favorite handler. 76 00:03:56,470 --> 00:03:58,930 So I have various components from all 77 00:03:58,930 --> 00:04:01,480 over the app that are interested 78 00:04:01,480 --> 00:04:03,880 and hence the easiest way of providing 79 00:04:03,880 --> 00:04:08,350 the context is probably to wrap everything with it. 80 00:04:08,350 --> 00:04:12,750 So here in index JS I will import the favorite 81 00:04:12,750 --> 00:04:14,800 favorites context provider wider from 82 00:04:15,940 --> 00:04:19,370 the store folder and there to favorites context file 83 00:04:19,370 --> 00:04:21,910 and actually I'll wrap it in curly braces because 84 00:04:21,910 --> 00:04:26,120 I don't want the default export the context object here 85 00:04:26,120 --> 00:04:29,340 but I want my component functioning stat. 86 00:04:29,340 --> 00:04:31,850 These favorites context provider function 87 00:04:31,850 --> 00:04:35,320 which I export like this under its name 88 00:04:35,320 --> 00:04:39,340 and we import named exports between curly braces. 89 00:04:39,340 --> 00:04:41,883 That is standard Java script. 90 00:04:43,640 --> 00:04:48,513 Now we can wrap this provider component 91 00:04:50,680 --> 00:04:53,940 around the browser router and the app 92 00:04:54,820 --> 00:04:58,170 and with that everything, all components 93 00:04:58,170 --> 00:05:00,750 in this application are able 94 00:05:00,750 --> 00:05:03,823 to interact with this context. 95 00:05:04,780 --> 00:05:07,270 Now, how do we interact though? 96 00:05:07,270 --> 00:05:10,810 Let's maybe start with the meetup item component. 97 00:05:10,810 --> 00:05:13,620 Here we have this two favorites button. 98 00:05:13,620 --> 00:05:16,140 I wanna make sure that when this button is clicked 99 00:05:16,140 --> 00:05:19,990 we add this item to our context. 100 00:05:19,990 --> 00:05:22,710 And I also wanna update the text on the button 101 00:05:22,710 --> 00:05:24,730 depending on whether this item is 102 00:05:24,730 --> 00:05:27,543 already part of my context or not. 103 00:05:28,540 --> 00:05:31,250 And actually, if it is part of the context already 104 00:05:31,250 --> 00:05:34,350 if we already did favorite and meetup item 105 00:05:34,350 --> 00:05:38,320 this button should no longer add this item to the context 106 00:05:38,320 --> 00:05:42,640 but remove it from my favorites array in the context. 107 00:05:42,640 --> 00:05:45,740 Hence first step is to add a function 108 00:05:45,740 --> 00:05:48,240 in this meetup item component 109 00:05:48,240 --> 00:05:53,240 which we could name toggle favorite status handler. 110 00:05:53,480 --> 00:05:55,600 The name is up to you though, and I'll bind 111 00:05:55,600 --> 00:05:59,470 this function to the on click prop of this button. 112 00:05:59,470 --> 00:06:01,290 so that when we click this button, 113 00:06:01,290 --> 00:06:04,220 this function will be executed. 114 00:06:04,220 --> 00:06:05,860 And now in this function, 115 00:06:05,860 --> 00:06:08,430 we will need data from the context. 116 00:06:08,430 --> 00:06:10,980 We need to find out whether this meetup item 117 00:06:10,980 --> 00:06:13,250 is already part of the context or not 118 00:06:13,250 --> 00:06:16,603 and then do different things depending on that information. 119 00:06:17,510 --> 00:06:19,800 Now for that, we now need to tap 120 00:06:19,800 --> 00:06:22,550 into the context and get values from there. 121 00:06:22,550 --> 00:06:27,550 And we do this with help of another hook provided by react. 122 00:06:28,070 --> 00:06:32,270 And that would be the use context hook. 123 00:06:32,270 --> 00:06:36,740 This hook allows us to establish a connection 124 00:06:36,740 --> 00:06:41,200 between this component and the context 125 00:06:41,200 --> 00:06:43,660 simply by calling use context here, 126 00:06:43,660 --> 00:06:47,210 and then by passing the context object 127 00:06:47,210 --> 00:06:50,770 to which we wanna connect as our argument. 128 00:06:50,770 --> 00:06:52,670 And here, I'm not talking about 129 00:06:52,670 --> 00:06:55,720 our favorites context provider component 130 00:06:55,720 --> 00:06:58,580 but about the context object, which we created 131 00:06:58,580 --> 00:07:02,070 with create context, which is why I'm exporting 132 00:07:02,070 --> 00:07:04,263 that as a default as well. 133 00:07:06,110 --> 00:07:09,540 With this in meetup item we can import 134 00:07:09,540 --> 00:07:13,430 the favorites context, whoops, like this 135 00:07:13,430 --> 00:07:17,130 so that default export from going up 136 00:07:17,130 --> 00:07:20,253 going up store favorites context, 137 00:07:21,260 --> 00:07:23,140 and instead the favorites context, 138 00:07:23,140 --> 00:07:25,473 which we pass to use context. 139 00:07:26,480 --> 00:07:28,040 And that now gives us access to 140 00:07:28,040 --> 00:07:31,150 this favorites context object 141 00:07:31,150 --> 00:07:36,150 so to this object here, which we're distributing 142 00:07:36,580 --> 00:07:39,590 through the value prop to all the components. 143 00:07:39,590 --> 00:07:42,040 So to this object to this object 144 00:07:42,040 --> 00:07:45,510 will have access now instead of meetup item. 145 00:07:45,510 --> 00:07:47,720 That's the object which is stored 146 00:07:47,720 --> 00:07:50,723 in this favorites, CTX constant now. 147 00:07:51,580 --> 00:07:54,000 And now we can, for example, find out 148 00:07:54,000 --> 00:07:56,800 if this item this meetup item here 149 00:07:56,800 --> 00:08:00,450 is a favorite by using this favorites 150 00:08:00,450 --> 00:08:04,900 CTX constant and calling item it's favorite. 151 00:08:04,900 --> 00:08:07,530 So this method, which we defined 152 00:08:07,530 --> 00:08:10,780 on our context there as well, this method 153 00:08:10,780 --> 00:08:15,340 which points at the item, it's favorite handler function. 154 00:08:15,340 --> 00:08:17,420 We execute this function now 155 00:08:18,510 --> 00:08:22,340 by executing it here in the component like this. 156 00:08:22,340 --> 00:08:24,700 And we need to pass in the idea of the meetup 157 00:08:24,700 --> 00:08:27,890 for what you wanna check, whether it's favorite or not. 158 00:08:27,890 --> 00:08:31,470 And we get the idea of this meetup through props. 159 00:08:31,470 --> 00:08:34,090 There is this ID prop, which we pass 160 00:08:34,090 --> 00:08:37,750 into our meetup item here when we render it. 161 00:08:37,750 --> 00:08:40,400 So we can use props that ID to pass the ID 162 00:08:40,400 --> 00:08:44,030 off this specific meetup to item as favorite 163 00:08:44,030 --> 00:08:46,560 and add will return true or false 164 00:08:46,560 --> 00:08:49,363 so here we then have that response. 165 00:08:50,350 --> 00:08:53,730 Now here in the toggle favorite status handler 166 00:08:53,730 --> 00:08:57,290 we can check if the item is a favorite 167 00:08:57,290 --> 00:09:00,850 so if this constant has a true value, 168 00:09:00,850 --> 00:09:02,560 and if that's the case we know that 169 00:09:02,560 --> 00:09:06,360 we wanna remove that item from the context now 170 00:09:06,360 --> 00:09:07,710 when that button was clicked. 171 00:09:07,710 --> 00:09:09,610 Because it is a favorite already 172 00:09:09,610 --> 00:09:12,060 which means now we wanna get rid of it. 173 00:09:12,060 --> 00:09:16,260 So then we use our favorites, CTX, constant 174 00:09:16,260 --> 00:09:20,300 this context object, and call remove favorite 175 00:09:20,300 --> 00:09:23,100 and remove favorite alls that wants to meet up ID 176 00:09:23,100 --> 00:09:25,163 So we pass props.id here. 177 00:09:26,640 --> 00:09:29,480 Else if the item is not a favorite yet 178 00:09:29,480 --> 00:09:33,683 we call favorites, CTX.add favorite. 179 00:09:34,660 --> 00:09:37,970 And here we need to pass in a new meetup item now 180 00:09:37,970 --> 00:09:41,250 so I will construct our object on the fly 181 00:09:41,250 --> 00:09:45,170 and simply set my ID to props.id 182 00:09:45,170 --> 00:09:48,070 the title to props.title 183 00:09:48,070 --> 00:09:52,080 the description to props.description 184 00:09:52,080 --> 00:09:54,970 the image to props.image 185 00:09:54,970 --> 00:09:58,593 and the address to props.address. 186 00:09:59,920 --> 00:10:03,250 And that now recreates this meetup item 187 00:10:03,250 --> 00:10:07,400 and passes it as an argument to the add favorite function 188 00:10:07,400 --> 00:10:12,210 which in the end triggers this add favorite handler function 189 00:10:12,210 --> 00:10:15,343 in this favorites context provider component. 190 00:10:17,040 --> 00:10:20,160 And add will then update the state there 191 00:10:20,160 --> 00:10:23,230 add this item to the user favorites array. 192 00:10:23,230 --> 00:10:25,290 And because the state was updated 193 00:10:25,290 --> 00:10:28,110 it will update all child components 194 00:10:28,110 --> 00:10:30,620 because this component here as a whole 195 00:10:30,620 --> 00:10:33,180 will be updated and hence the JSX code 196 00:10:33,180 --> 00:10:36,230 with all the child components will be updated again. 197 00:10:36,230 --> 00:10:38,860 And hence all components will have access 198 00:10:38,860 --> 00:10:40,960 to that latest state now. 199 00:10:40,960 --> 00:10:42,730 So that's how we can now trigger 200 00:10:42,730 --> 00:10:45,970 a state update to this global state, 201 00:10:45,970 --> 00:10:48,150 this favorite state from insight to 202 00:10:48,150 --> 00:10:49,780 meet up item component. 203 00:10:49,780 --> 00:10:52,440 We changed some state, which is not managed 204 00:10:52,440 --> 00:10:55,723 in this component, but on a global level. 205 00:10:57,000 --> 00:10:58,820 And now going down to this button, 206 00:10:58,820 --> 00:11:01,680 we probably wanna change the caption 207 00:11:01,680 --> 00:11:04,660 depending on whether the item is a favorite or not. 208 00:11:04,660 --> 00:11:07,930 So I'll put something dynamic here as a caption 209 00:11:07,930 --> 00:11:10,510 and check if item is favorite. 210 00:11:10,510 --> 00:11:13,130 So I'm using this constant here again, 211 00:11:13,130 --> 00:11:16,040 which has true or false as a value. 212 00:11:16,040 --> 00:11:19,190 And if it's true, then the button should say, 213 00:11:19,190 --> 00:11:22,620 remove from favorites. 214 00:11:22,620 --> 00:11:26,470 If it's false so if the item is not a favorite yet 215 00:11:26,470 --> 00:11:30,810 the button should say to favorites 216 00:11:30,810 --> 00:11:32,370 because then clicking the button 217 00:11:32,370 --> 00:11:35,073 will add the item to the favorites. 218 00:11:36,710 --> 00:11:39,380 And now that was a lot of work. 219 00:11:39,380 --> 00:11:42,380 But if you did all of that, if you save this, 220 00:11:42,380 --> 00:11:46,500 now you should be able to reload and click to favorites 221 00:11:46,500 --> 00:11:49,730 and it should change to remove from favorites 222 00:11:49,730 --> 00:11:51,920 and back and forth. 223 00:11:51,920 --> 00:11:55,300 We don't see the favorites on the my favorites page yet 224 00:11:55,300 --> 00:11:59,530 but adding favorites does work as we can tell 225 00:11:59,530 --> 00:12:01,080 by this button. 226 00:12:01,080 --> 00:12:03,680 Now let's make sure we also render 227 00:12:03,680 --> 00:12:06,290 the favorites on this my favorites page, 228 00:12:06,290 --> 00:12:08,363 and that we have a little batch here. 17998

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