All language subtitles for 025 Sharing Data Between Custom Hooks & 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 Download
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,190 --> 00:00:07,860 So how can we update our ingredients here in the ingredients component when our delete request is done? 2 00:00:08,550 --> 00:00:10,440 Well, again, we can use a hook. 3 00:00:10,440 --> 00:00:12,480 We already learned about use effect. 4 00:00:12,720 --> 00:00:15,810 Remember that use effect runs after every render cycle. 5 00:00:16,620 --> 00:00:19,940 So use HTP sends a request. 6 00:00:19,980 --> 00:00:25,290 And in that request, in the end, we dispatch response. 7 00:00:25,290 --> 00:00:25,890 If we succeed. 8 00:00:26,020 --> 00:00:33,170 This updates the state and therefore it will trigger the component that uses our hook to rebuild itself. 9 00:00:33,630 --> 00:00:40,470 So our component ingredients component will rebuild itself when we send request is done thanks to the 10 00:00:40,470 --> 00:00:47,640 logic we have in our hook, because this patch HTP, which is coming from use reducer updates to state 11 00:00:47,970 --> 00:00:50,580 this rebuilds the component in which it is used. 12 00:00:50,940 --> 00:00:54,000 It's of course using our hook, but that hook is used in this component. 13 00:00:54,000 --> 00:00:55,440 They for this component rebuilds. 14 00:00:55,830 --> 00:01:00,210 By the way, if we would use the same hook in a different component that our component would not rebuild, 15 00:01:00,330 --> 00:01:04,010 because as I said, the logic is copied for every component. 16 00:01:04,019 --> 00:01:05,810 They're not sharing the same data. 17 00:01:06,030 --> 00:01:10,920 So if you're sending a request in Component A, that does not mean that Component B also sends Bernet 18 00:01:11,040 --> 00:01:11,850 or anything like that. 19 00:01:12,120 --> 00:01:18,530 You're just sharing in the sense of copying the same logic across components here. 20 00:01:18,540 --> 00:01:22,920 However, we know that this component will rebuild whenever the request response there. 21 00:01:24,530 --> 00:01:30,470 Hence, we can actually turn this, use a fat call here into a more useful one, we can listen, for 22 00:01:30,470 --> 00:01:37,490 example, to the data because we know that on our accustomed request here, when a response is there, 23 00:01:37,490 --> 00:01:38,900 we opted response data. 24 00:01:38,900 --> 00:01:42,650 And that means that we set data here on our states. 25 00:01:42,650 --> 00:01:50,000 Our data changes when our response is there now for a delete request, we get no data, but still will 26 00:01:50,210 --> 00:01:50,810 change. 27 00:01:50,810 --> 00:01:55,850 Something here will set a new value in response data and that alone should suffice here. 28 00:01:56,750 --> 00:02:01,580 So in ingredients charts, we're listening to data because it this a fact call here. 29 00:02:01,790 --> 00:02:10,009 When we have new data, then I want to call dispatch, which is now coming from my ingredient user here, 30 00:02:10,490 --> 00:02:15,710 dispatch type delete because we deleted a component in the end here. 31 00:02:17,850 --> 00:02:24,900 And pass the ID problem is, where do I get that ID from, I need the ID here on the actually I'm creating. 32 00:02:25,080 --> 00:02:28,110 But this U.S. Faycal knows nothing about the ID. 33 00:02:29,620 --> 00:02:37,000 Well, solution, again, could be that we add a feature to our hook here to our custom hook Indy custom 34 00:02:37,000 --> 00:02:43,040 hook, we can add another field like a request extra, which initially allows is null. 35 00:02:43,870 --> 00:02:50,590 Now, that extra can be sent set when the request is sent there, we can set extra to action extra, 36 00:02:50,590 --> 00:02:52,180 let's say so. 37 00:02:52,180 --> 00:02:57,130 Expect to have an extra field on the incoming action we're dispatching to send request here and there. 38 00:02:57,130 --> 00:03:02,070 We can send an extra a Saturday Extra and I expect to get this from outside. 39 00:03:02,680 --> 00:03:10,060 So as an extra in to send request function, I get my request extra and I'm storing this in my state 40 00:03:10,060 --> 00:03:10,360 here. 41 00:03:12,350 --> 00:03:14,120 So back to our ingredients component. 42 00:03:15,150 --> 00:03:16,800 When we call send request here. 43 00:03:18,930 --> 00:03:26,190 I sent nobody saw a personal here, but for request extra, I forward the ingredient ID, so now it 44 00:03:26,190 --> 00:03:31,310 is ingredient ID gets added to send request in to hook where we define this function. 45 00:03:31,320 --> 00:03:34,530 We then store it in the state, which is the state for this component. 46 00:03:34,530 --> 00:03:34,800 Right. 47 00:03:35,310 --> 00:03:36,240 And therefore. 48 00:03:37,380 --> 00:03:41,310 We can now all the return that request extra here. 49 00:03:42,530 --> 00:03:48,290 In the data by getting it from our HTP state extra, and that means that in the ingredients component. 50 00:03:50,330 --> 00:03:58,640 There we can also extract request extra from the result of use HTP and now use that here in use effect. 51 00:03:59,520 --> 00:04:00,330 So that the. 52 00:04:01,340 --> 00:04:04,240 I.D., I need to provide for the delete action. 53 00:04:05,670 --> 00:04:12,000 Here actually is my request, because we know that we'll store our ID and request extra when we send 54 00:04:12,000 --> 00:04:17,370 that delete request once the delete request is done, once the response is their data will be emitted. 55 00:04:17,589 --> 00:04:24,620 Hence this effect will run and then we can dispatch, just delete action, which adjusts our ingredients. 56 00:04:25,470 --> 00:04:26,100 So here. 57 00:04:28,220 --> 00:04:30,950 Let's add request extra and there should work. 58 00:04:31,750 --> 00:04:37,630 Now, let me also step in here for a second and make it really clear that it can be strange to think 59 00:04:37,630 --> 00:04:45,340 about HTTP requests like this at first, that we're sending them somewhere and we're handling the response 60 00:04:45,340 --> 00:04:47,390 somewhere else in use of fact here. 61 00:04:48,040 --> 00:04:51,990 Nonetheless, this really embraces the concept of hoax, after all. 62 00:04:52,000 --> 00:04:56,380 And what's it can be strange to look at a HTP request like this initially? 63 00:04:56,800 --> 00:05:02,800 It actually makes handling it much more elegant in the long run, I believe, because you can clearly 64 00:05:02,800 --> 00:05:09,790 see in your code where your handling result of your request, where you're changing your data and where 65 00:05:09,790 --> 00:05:17,180 you're just dispatching that request, where you're sending it off so to say with send request nonetheless. 66 00:05:17,200 --> 00:05:19,360 And I want to emphasize this here. 67 00:05:19,480 --> 00:05:26,650 An alternative would have been that the send request function were returning in our use of the hook, 68 00:05:27,070 --> 00:05:33,880 that this send request function, for example, returns the normal promise, the fetch method, the 69 00:05:33,880 --> 00:05:35,590 fetch API always gives us. 70 00:05:35,800 --> 00:05:42,100 So did it in the places where we called send request, we can add a then and a catch block and then 71 00:05:42,100 --> 00:05:49,960 simply still do our logic in these then and cache blocks instead of using use affect this approach here 72 00:05:50,200 --> 00:05:57,070 of splitting the sending of the request and the handling of the response can be more confusing initially 73 00:05:57,310 --> 00:06:00,670 because now you have to care about getting things like that. 74 00:06:00,670 --> 00:06:07,540 Request extra from the I send my request place to the I handle the response place, which is our effect 75 00:06:07,540 --> 00:06:07,810 here. 76 00:06:08,350 --> 00:06:14,170 Nonetheless, in the long run, as I said, I think at least you to write cleaner code where you split 77 00:06:14,170 --> 00:06:19,900 your responsibilities and where you just have to switch to where you think about it, and then you'll 78 00:06:19,900 --> 00:06:22,010 actually have no problems with that approach. 79 00:06:22,120 --> 00:06:27,790 Just be aware that if you're not liking this approach at all, you could return a promise in your HTP 80 00:06:27,790 --> 00:06:35,650 hook instead of send request there and then simply change all your response, handling logic after send 81 00:06:35,650 --> 00:06:37,950 request in this component in this ingredients. 82 00:06:38,200 --> 00:06:38,980 And you could do that. 83 00:06:39,340 --> 00:06:44,140 But using this approach here with you as a fact, handling the response simply is the approach where 84 00:06:44,140 --> 00:06:47,500 you really embrace Hooke's and use them to their fullest potential. 85 00:06:48,010 --> 00:06:53,530 Now, I ran out of ingredients, so in order to test this, let's first fix the add ingredient handler 86 00:06:53,530 --> 00:06:53,800 now. 87 00:06:54,940 --> 00:07:00,400 An add ingredient handler thus far, we're also dispatching a send request and so on, instead, now 88 00:07:00,400 --> 00:07:05,320 we call send request, which again is dysfunction coming from our custom hook here. 89 00:07:06,870 --> 00:07:12,390 So we call send request, do you are l is of course, the same you are we used to down there so I'll 90 00:07:12,390 --> 00:07:13,440 just copy that. 91 00:07:14,100 --> 00:07:20,820 The method here, since we add ingredients will be post the body will be that body we had before needs 92 00:07:20,820 --> 00:07:21,570 to be JSON. 93 00:07:21,570 --> 00:07:28,050 So let's use JSON string of ify and here I need no special extra so I won't pass that. 94 00:07:29,080 --> 00:07:35,020 So this again called send request and justice before this goes to a rook, executes this function amidst 95 00:07:35,020 --> 00:07:39,820 this event and once the response is there amidst is responsive and it stores the response data. 96 00:07:40,420 --> 00:07:48,160 So now, again, we can use our effect to react to that data in the ingredients, Jaspal, and update 97 00:07:48,160 --> 00:07:52,210 our local ingredients based on that data we got back. 98 00:07:53,190 --> 00:07:58,080 So here we have use a fact watch reruns where the data changes were when the request extra changes and 99 00:07:58,200 --> 00:08:00,970 oh, we then always dispatch a delete request. 100 00:08:01,170 --> 00:08:03,330 Now, that's probably not what we want here. 101 00:08:04,520 --> 00:08:06,890 Well, in the end, we can find out what should be dispatched. 102 00:08:07,370 --> 00:08:13,820 We know that if we have a request extra in our application here, we want to dispatch a delete request 103 00:08:13,850 --> 00:08:17,360 because we only use that extra thing for delete requests. 104 00:08:17,360 --> 00:08:21,890 So if we have extra if that's not null, then it's a delete request. 105 00:08:22,610 --> 00:08:27,620 And to make sure that this gets cleared for every new request, I'll go to my custom hook and actually 106 00:08:27,620 --> 00:08:34,520 set extra to null here when we send the request, but instead store it on the response. 107 00:08:34,730 --> 00:08:37,480 They're all set extra to action extra. 108 00:08:38,059 --> 00:08:44,240 And that means that further down in our custom hook, we no longer append the extra when we sent the 109 00:08:44,240 --> 00:08:44,680 request. 110 00:08:44,930 --> 00:08:46,660 So I'll get rid of this year. 111 00:08:46,880 --> 00:08:49,850 Instead, we spent the extra ones, the responses there. 112 00:08:52,620 --> 00:08:58,470 So a tiny tweak here so that in ingredients chest, we know if we have an extra, then we're deleting 113 00:08:58,850 --> 00:08:59,790 or else. 114 00:09:01,250 --> 00:09:07,370 In our case thus far, we're adding so here I can dispatch, well, basically what we did before. 115 00:09:07,460 --> 00:09:12,020 Let me scroll down, can dispatch this here, so let's copy that. 116 00:09:13,930 --> 00:09:19,480 And use it here, remove all the comments now, of course, here I'm relying on some. 117 00:09:20,700 --> 00:09:26,790 Data from the response, the good thing is we're getting that data, so that should be data name because 118 00:09:26,790 --> 00:09:30,240 we're storing whatever response data we're getting in our accustomed hook. 119 00:09:31,340 --> 00:09:37,160 Restoring that, we're sending this here along with our action, and that gets stored in our state there, 120 00:09:37,730 --> 00:09:39,850 so we store response data and data. 121 00:09:40,490 --> 00:09:42,260 So a good thing is we have access to this. 122 00:09:42,920 --> 00:09:46,490 But for the ingredient which we all want, we're missing that. 123 00:09:47,490 --> 00:09:50,370 Now, he could sentence along as I request extra, of course. 124 00:09:51,390 --> 00:09:52,500 But then this check here, your. 125 00:09:53,880 --> 00:09:54,360 Still. 126 00:09:55,500 --> 00:10:01,800 Let's use requests extra here and then worry about this in a second step, so request extra should be 127 00:10:01,800 --> 00:10:03,360 the ingredient which gets added. 128 00:10:03,360 --> 00:10:09,120 So an add ingredient handler where we get that ingredient, which just misstated, I now will forward 129 00:10:09,120 --> 00:10:10,640 that ingredient as an extra. 130 00:10:10,950 --> 00:10:15,600 But now, since we have extras for both deleting and for adding, we need a different way of differentiating 131 00:10:15,600 --> 00:10:18,390 these two facts for handling the two results. 132 00:10:18,390 --> 00:10:19,470 We're getting back from our hook. 133 00:10:19,850 --> 00:10:27,150 Therefore, in the hook I'll add a new field, the last one here to my state, to the initial state. 134 00:10:27,540 --> 00:10:28,820 That's the identifier. 135 00:10:28,860 --> 00:10:30,510 Initially, it's null. 136 00:10:33,140 --> 00:10:36,320 But we sent this where we dispatch to send request, let's say. 137 00:10:37,230 --> 00:10:45,300 Then we set identifier equal to and I expect to get an identifier as an argument, I request identifier 138 00:10:45,300 --> 00:10:47,640 maybe so I'll set this here. 139 00:10:47,640 --> 00:10:56,020 I passed the action and then the reducer when we sent this all set identifier here equal to action dot 140 00:10:56,070 --> 00:10:56,860 identifier. 141 00:10:57,360 --> 00:11:01,590 So now we can append an identifier to every HDP request. 142 00:11:01,750 --> 00:11:04,560 And that's just what I'll do in ingredients. 143 00:11:04,560 --> 00:11:10,440 Jass when we dispatch excuse me, when we sent the request here for adding an ingredient all named as 144 00:11:10,440 --> 00:11:13,920 add ingredient, that identifier here is totally up to you. 145 00:11:13,920 --> 00:11:17,310 You can use any identifier you want, can be a string, can be a number. 146 00:11:17,340 --> 00:11:21,390 I'll use add ingredient as a string for removing an ingredient. 147 00:11:21,390 --> 00:11:25,080 I'll use an identifier of remove ingredient. 148 00:11:26,020 --> 00:11:33,040 And now we can use these identifiers in our effect to clearly identify our logic, so let's. 149 00:11:34,230 --> 00:11:40,170 Gets the identifier from use HTP, let's make sure we return it there so in our custom hook at the bottom 150 00:11:40,740 --> 00:11:42,630 or return everything there. 151 00:11:42,660 --> 00:11:47,850 We should also return to request identifier from HTP State Identifier. 152 00:11:48,390 --> 00:11:53,720 Now, since we're returning it here in a custom hook, we can pull it out of the return value here in 153 00:11:53,720 --> 00:11:56,880 ingredients Jass request identifier like this. 154 00:11:57,390 --> 00:12:01,650 And now with it pulled out, we can add it as a dependency to use effect. 155 00:12:01,770 --> 00:12:04,740 And then here check if request identifier is equal to. 156 00:12:05,700 --> 00:12:11,130 What did I use, remove ingredient that's my identifier here for deleting. 157 00:12:12,130 --> 00:12:20,680 And for adding, I use add ingredient, so here, I'll check else if request identifier is equal to 158 00:12:20,680 --> 00:12:22,780 add ingredient, otherwise will not do anything. 159 00:12:24,380 --> 00:12:31,460 So this should now be all and I know it was a lot that we need to also add ingredients. 160 00:12:32,690 --> 00:12:33,950 So let's log in here. 161 00:12:36,840 --> 00:12:39,390 And let's add a couple of apples here. 162 00:12:40,510 --> 00:12:46,120 And I got an error Keniry property, name of NUL in ingredients, Shabaz, line three. 163 00:12:47,130 --> 00:12:49,170 Do you have any idea what could be causing that? 164 00:12:50,510 --> 00:12:56,030 Well, it happened pretty fast, if you saw that, so the problem is that he was, in fact, runs after 165 00:12:56,030 --> 00:12:57,100 every Renderos cycle. 166 00:12:57,110 --> 00:13:00,230 And when does this component render balladry renders? 167 00:13:00,640 --> 00:13:07,040 For example, when our custom hook here does something, when it dispatches to send action, which actually 168 00:13:07,040 --> 00:13:13,130 sets loading to true, that causes ingredients to render because we're using loading in there. 169 00:13:13,130 --> 00:13:16,730 Makes sense right now since that is case. 170 00:13:18,010 --> 00:13:19,870 We want to make sure that we don't. 171 00:13:20,860 --> 00:13:26,590 Just check the identifier here before we dispatch actions, but we all to check whether we're done with 172 00:13:26,590 --> 00:13:32,240 loading because of reloading, then we might have data, but we're not done loading it. 173 00:13:32,260 --> 00:13:37,510 Yes, data will only be set if a response is there in our custom hook here. 174 00:13:38,900 --> 00:13:40,940 But when we dispatched the first action. 175 00:13:42,180 --> 00:13:49,290 We also see data for the first time, so just watching for data changes alone won't do the trick, especially 176 00:13:49,290 --> 00:13:53,910 since request extra and requests identify all of them might have changed and hence the effect will rerun. 177 00:13:54,330 --> 00:13:56,190 So here we need a more granular check. 178 00:13:56,610 --> 00:13:59,870 We can check if we're not loading anymore. 179 00:14:00,060 --> 00:14:04,500 And of course, that means that we now need to add is loading as a dependency or to use effect. 180 00:14:05,250 --> 00:14:07,170 And we have that identifier here. 181 00:14:09,240 --> 00:14:16,120 And down here for adding it's the same, we're checking for not loading and the identifier as add ingredient. 182 00:14:16,140 --> 00:14:22,500 In addition, I also want to sure as you are here that we got data so that this is not null or that 183 00:14:22,500 --> 00:14:23,540 we have no error. 184 00:14:23,580 --> 00:14:25,080 That's the better check, probably. 185 00:14:25,140 --> 00:14:27,150 So we should all get error here. 186 00:14:29,120 --> 00:14:33,320 All the check that makes sense for deleting, I want to ensure that we have no error here. 187 00:14:35,330 --> 00:14:37,880 So now let's save this log in. 188 00:14:39,230 --> 00:14:41,860 And let's add some bananas here. 189 00:14:42,960 --> 00:14:45,360 And this looks good, this looks way better. 190 00:14:45,390 --> 00:14:50,010 They're getting added, you can see them here, too, and this works in half a delete. 191 00:14:50,010 --> 00:14:53,640 Let's say the apples dissolves is looking pretty decent to me. 192 00:14:53,640 --> 00:14:55,710 It now works with de help for a custom hook. 20288

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