All language subtitles for 003 Alternative_ Using the Context API_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,170 --> 00:00:07,420 So let's start with approach number one, an approach that is sometimes the right choice, but not always, 2 00:00:07,689 --> 00:00:11,380 and this approach will use the context API for this. 3 00:00:11,380 --> 00:00:14,470 I'll add a new folder to this project and I'll name it context. 4 00:00:14,470 --> 00:00:16,570 But you can, of course, name just whatever you want. 5 00:00:16,570 --> 00:00:18,700 You can manage just wherever you want. 6 00:00:19,360 --> 00:00:25,590 In this context folder, I'll create a new fall and I'll name it products dash context jazz. 7 00:00:25,600 --> 00:00:29,740 Again, this name is also up to you and I'll import react from react. 8 00:00:29,890 --> 00:00:35,320 And then in here we can create our, let's say products context or whatever you want to name it. 9 00:00:36,340 --> 00:00:42,880 And I'll actually give us a capital P because we'll use that as a component, and then here we use react, 10 00:00:42,880 --> 00:00:49,810 create context and we'll create our context, which should actually, let's say, manage the object 11 00:00:49,810 --> 00:00:53,800 with a product's property in there, which holds an array of products. 12 00:00:54,100 --> 00:00:58,230 And you could also argue that your entire context value is just an array. 13 00:00:58,240 --> 00:01:01,750 But in case you had other fields, too, you would probably have an object here. 14 00:01:02,440 --> 00:01:04,510 So we have our products here. 15 00:01:06,640 --> 00:01:10,010 And my initial products here already, initial products. 16 00:01:10,030 --> 00:01:12,610 I also have in their reducers file here. 17 00:01:12,610 --> 00:01:14,560 So it's this array of products. 18 00:01:14,560 --> 00:01:21,310 I'll just copy that, move that in their products context file and then simply, well, replace this 19 00:01:21,310 --> 00:01:22,660 array with this copied array. 20 00:01:23,870 --> 00:01:31,730 And with that, we're creating react context object here, there's one square brackets too much. 21 00:01:33,540 --> 00:01:39,780 Now, this is the context object now here in this file, I want to export. 22 00:01:40,630 --> 00:01:49,360 This context, but I also want to export a component as a default, which gets some props, of course, 23 00:01:49,360 --> 00:01:51,940 and has to return some jazz. 24 00:01:52,150 --> 00:01:54,890 So I'm just exporting a normal functional component here. 25 00:01:55,270 --> 00:01:56,760 This is a functional component. 26 00:01:57,220 --> 00:02:01,540 And there I use my products, context, thought provider component. 27 00:02:01,540 --> 00:02:07,990 And again, check out my context, my react context lectures in this course, in case this doesn't tell 28 00:02:07,990 --> 00:02:14,320 you anything, I cover it in the components deep dive section and I also have a look at it again in 29 00:02:14,320 --> 00:02:17,270 the React Hooks section when we cover the U.S. context. 30 00:02:17,950 --> 00:02:24,430 So here I provide my context and I want to provide it to everything that's passed between the opening 31 00:02:24,430 --> 00:02:26,380 and closing tax of my export. 32 00:02:26,380 --> 00:02:28,480 That component here with props children. 33 00:02:29,260 --> 00:02:35,960 Now, important, though, the value of this context should be a value which I manage in this component. 34 00:02:36,400 --> 00:02:43,120 So since that should be able to change, I'll use the use state Hoak, which is baked into react to 35 00:02:43,120 --> 00:02:48,220 work with functional components only, though theoretically that could also be a Class-Based component 36 00:02:48,220 --> 00:02:48,460 here. 37 00:02:48,460 --> 00:02:49,440 That would be fine too. 38 00:02:50,170 --> 00:02:54,610 And then here I want to manage my state here now. 39 00:02:54,670 --> 00:02:58,360 My state I manage here in the end will be that array of products. 40 00:02:58,540 --> 00:02:59,290 So actually. 41 00:03:00,280 --> 00:03:06,190 I'll grab that array, remove it from the air, and here simply initialize an empty array to get better 42 00:03:06,190 --> 00:03:11,530 order completion and I.T. support, but the value here doesn't matter because I will now manage to products 43 00:03:11,530 --> 00:03:12,580 here in this state. 44 00:03:13,000 --> 00:03:15,170 So that will now just be an array, not an object. 45 00:03:15,190 --> 00:03:16,690 Just be an array of products. 46 00:03:17,560 --> 00:03:19,870 Make sure the square brackets are correct. 47 00:03:20,230 --> 00:03:26,610 And here we'll have our products and a set products function which we extract from you state. 48 00:03:26,980 --> 00:03:34,810 And now here, the value I pass to my provider, to my product context provider is an object with the 49 00:03:34,810 --> 00:03:39,550 products key because I want to have that in my context and that's just my personal preference here. 50 00:03:40,210 --> 00:03:47,300 And the value for that product key will be my products list here and they'll actually name this products 51 00:03:47,320 --> 00:03:53,410 list and name it here products list when we extract it so that we have no confusion regarding what's 52 00:03:53,410 --> 00:03:53,710 what. 53 00:03:54,040 --> 00:04:01,060 So here I manage an array of products with states named Products List and I assign this to my products 54 00:04:01,060 --> 00:04:05,620 key here in that object I pass as a value to my products context provider. 55 00:04:06,490 --> 00:04:11,590 So now whenever this value here changes, whenever we update the state here and there for this component 56 00:04:11,590 --> 00:04:17,980 rebuilds, whenever that happens, our provider will get a new value and every child that listens to 57 00:04:17,980 --> 00:04:21,459 our provider will be able to get that new value. 58 00:04:24,060 --> 00:04:30,510 Now, with that prepared, we can use our provider here, an index chart where I previously provided 59 00:04:30,510 --> 00:04:35,700 react redux, I'll not do that anymore and hence I can get rid of these two imports. 60 00:04:36,450 --> 00:04:46,590 And instead here I want to import my products provider from context, context, products, context. 61 00:04:46,920 --> 00:04:48,420 That's our default export there. 62 00:04:48,420 --> 00:04:49,830 So we imported like this. 63 00:04:50,650 --> 00:04:51,930 Get rid of all of that here. 64 00:04:51,930 --> 00:04:52,800 We don't need that. 65 00:04:53,800 --> 00:04:58,720 And then here have our products provider opening and closing, we don't need to pass anything, could 66 00:04:58,720 --> 00:04:59,970 we just use it like this? 67 00:05:00,130 --> 00:05:02,290 We can also get rid of the products, reduce our import. 68 00:05:02,320 --> 00:05:05,590 You could also delete the entire store folder now if you wanted to. 69 00:05:06,220 --> 00:05:07,630 I'll leave it here for reference. 70 00:05:07,780 --> 00:05:11,500 So now I'm providing my contacts here and anywhere in this componentry. 71 00:05:11,500 --> 00:05:17,800 So any child component here or in a child component of that component, I can kind of listen to that 72 00:05:17,800 --> 00:05:18,490 context. 73 00:05:20,180 --> 00:05:24,500 Now, one place where I'm interested in products is the products chestful fall in the containers folder, 74 00:05:24,500 --> 00:05:25,220 for example. 75 00:05:26,660 --> 00:05:32,480 There we can add an import statement to import something from our context folder, and they're from 76 00:05:32,480 --> 00:05:38,090 products context and does something here is now not the provider component, but the products context 77 00:05:38,090 --> 00:05:43,610 object itself, because we can use that together with use context and therefore we can also get rid 78 00:05:43,610 --> 00:05:45,170 of that react redux import here. 79 00:05:45,800 --> 00:05:48,220 We can use that here to get our products. 80 00:05:48,740 --> 00:05:55,370 So here we can add the use, context, hook and point at products context to get access to that context. 81 00:05:55,970 --> 00:06:06,650 And then yea I get my products context or I simply get my product list here, if you will, by accessing 82 00:06:06,650 --> 00:06:08,810 this context here and there. 83 00:06:09,410 --> 00:06:15,650 My products desk exists on my context after all, and this will be my list of products which is recreated 84 00:06:15,650 --> 00:06:17,500 whenever we change something in that list. 85 00:06:18,380 --> 00:06:24,140 So now we have that product list here and we can use that product list here to render the products will 86 00:06:24,140 --> 00:06:26,900 not be able to mark them as favorites at the moment. 87 00:06:27,170 --> 00:06:29,780 But this should work at least now to test this. 88 00:06:29,780 --> 00:06:36,440 Let's go to components products, product item where I use dispatch and remove that import and remove 89 00:06:36,440 --> 00:06:42,320 the usage of dispatch here so that we don't get an error about using some react redux feature without 90 00:06:42,320 --> 00:06:43,220 providing a store. 91 00:06:43,730 --> 00:06:46,700 If you do that, we save all the faults. 92 00:06:47,030 --> 00:06:50,540 This page should be able to reload and render all our products. 93 00:06:50,570 --> 00:06:56,180 Now again, favorite thing won't work for now, but we can use our context to distribute data to a different 94 00:06:56,180 --> 00:07:02,270 component because we're now managing our products here in the products context, Jaspal, and we're 95 00:07:02,270 --> 00:07:06,790 accessing our products in the container folder, in the products JS file. 96 00:07:07,580 --> 00:07:11,750 Now let's also add a functionality to market product as a favorite. 10156

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