All language subtitles for 024 Getting Started with Custom Hooks_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,330 --> 00:00:10,120 So we're almost done with this module, but one very cool feature is missing, and that is custom hooks. 2 00:00:10,130 --> 00:00:11,570 You can build your own hooks. 3 00:00:11,600 --> 00:00:13,880 You're not restricted to the built in ones. 4 00:00:14,180 --> 00:00:15,050 So let's do that. 5 00:00:15,200 --> 00:00:20,510 I'll add a new hooks folder where I want to store my hooks, and there I'll add one custom hook and 6 00:00:20,510 --> 00:00:27,140 that's my HTP hook because I have that HTP functionality in ingredients. 7 00:00:27,140 --> 00:00:33,200 Shabaz, for example, where I send HTP requests, I do this multiple times and the pattern always is 8 00:00:33,200 --> 00:00:40,070 the same dispatch send at the beginning dispatch response once the response is their dispatch error. 9 00:00:40,070 --> 00:00:42,530 If we have an error, it's always the same pattern. 10 00:00:43,100 --> 00:00:46,250 So we have logic that we want to share across components. 11 00:00:46,250 --> 00:00:47,840 But and that's the important thing. 12 00:00:47,960 --> 00:00:54,500 Logic, which also influences the state of the components where we want to use that logic so we can't 13 00:00:54,500 --> 00:01:00,350 use a normal function because of course, we could build our normal send request function, which simply 14 00:01:00,350 --> 00:01:02,570 has all that send request logic in there. 15 00:01:03,080 --> 00:01:09,740 But from in a normal function, we couldn't dispatch events which then changed the state in a component 16 00:01:09,740 --> 00:01:13,610 that calls the function that something only Hooke's can do. 17 00:01:14,360 --> 00:01:16,260 And that's why we build our own hook now. 18 00:01:17,180 --> 00:01:21,380 So in the end, if you just fall into Hooke's folder, I want to build my own hook. 19 00:01:22,010 --> 00:01:26,780 And the first very important thing there is your own hooks should always be named. 20 00:01:26,780 --> 00:01:32,960 Use something in my case here, use HTP or use fetch or whatever you want to name it. 21 00:01:32,960 --> 00:01:38,210 But use something written like this and I'll go with use htp then. 22 00:01:38,210 --> 00:01:40,610 Your hook is just a function. 23 00:01:40,850 --> 00:01:42,530 It's a normal JavaScript function. 24 00:01:42,540 --> 00:01:45,230 Just treat it in a special way by react. 25 00:01:45,330 --> 00:01:46,310 That's the core thing. 26 00:01:47,310 --> 00:01:54,330 So now we have this and I will export use HTP here, so that's a very simple hook which doesn't do anything, 27 00:01:54,540 --> 00:01:55,590 but it's a start. 28 00:01:56,160 --> 00:01:59,760 The cool thing is in this hook, you can now use stateful features. 29 00:01:59,760 --> 00:02:04,110 You can use use, stage, use of fact, use reducer, anything. 30 00:02:04,110 --> 00:02:05,640 You can use any hook in there. 31 00:02:06,270 --> 00:02:11,790 And any component that uses your own hook will then run this hook as if you had to code written in here 32 00:02:11,940 --> 00:02:13,550 directly in the component. 33 00:02:13,950 --> 00:02:19,440 So it's not like you have one functio shared by multiple components and then they run the same code 34 00:02:19,440 --> 00:02:20,660 with the same data. 35 00:02:20,790 --> 00:02:26,340 No, each functional component gets its own snapshot of this hook, so to say. 36 00:02:26,610 --> 00:02:27,980 So that's a cool thing. 37 00:02:27,990 --> 00:02:33,780 You can have stateful logic in there, but the stateful logic will then be different for each component 38 00:02:33,870 --> 00:02:37,590 where you use that hook so that you can share the logic, not the data. 39 00:02:37,600 --> 00:02:39,030 That's the idea behind Hooke's. 40 00:02:40,030 --> 00:02:42,250 So what do we want to have in this custom hook? 41 00:02:42,880 --> 00:02:47,430 Well, I'd say it makes sense to move our HTP, reduce her into there. 42 00:02:48,220 --> 00:02:52,690 So let's go to ingredients, Jess and fetched this HTP. 43 00:02:53,660 --> 00:03:01,700 Where do you, sir, cut it from there and let me close the viewing folder, move it here into pieces 44 00:03:01,760 --> 00:03:07,610 and I'll define it outside of the hook because again, just need to rerun with every Rehren cycle and 45 00:03:07,610 --> 00:03:11,150 your hook will rerun with every Virender cycle. 46 00:03:11,160 --> 00:03:11,930 That's important. 47 00:03:12,320 --> 00:03:17,480 Your hook will get called whenever the component where you're using your hook gets executed. 48 00:03:17,780 --> 00:03:22,580 And then you can use techniques in your hook, of course, to make sure that nothing happens to the 49 00:03:22,580 --> 00:03:25,280 state or that something does happen depending on what you want. 50 00:03:25,370 --> 00:03:27,440 But in general, it will get re executed. 51 00:03:27,950 --> 00:03:32,390 I don't want to recreate the reduce all of the time, so I'll move that out of there just as we did 52 00:03:32,390 --> 00:03:33,560 it for the component. 53 00:03:34,740 --> 00:03:40,650 Now, of course, since the producer function was removed, all the remove the use, reduce recall from 54 00:03:40,650 --> 00:03:43,710 ingredients, jars and move it into the pictures. 55 00:03:43,860 --> 00:03:49,230 Now inside of my custom, look here now to use reuse producer, we have to import it. 56 00:03:49,710 --> 00:03:54,060 So let's import use reducer from react. 57 00:03:54,390 --> 00:03:58,260 We don't need to import react itself here because we're not building a react component here. 58 00:03:58,260 --> 00:04:02,240 We just want to use some features from the react package in this case, use producer. 59 00:04:03,120 --> 00:04:08,370 So here I don't have my state and dispatch htp which right now isn't that useful, but we'll get there. 60 00:04:09,560 --> 00:04:15,560 Now, the request itself, let's go to Ingredients Jass and let's grab one of these requests, let's 61 00:04:15,560 --> 00:04:21,230 grab the one where we handle the error here and remove ingredient, I'll take this entire block with 62 00:04:21,230 --> 00:04:26,920 all the then and catch blocks thereafter created and added to the Peach's. 63 00:04:29,220 --> 00:04:36,360 This would now execute this HTP request whenever the component that uses use HGP reruns. 64 00:04:37,070 --> 00:04:39,400 That's certainly not what we want here. 65 00:04:39,420 --> 00:04:46,110 We don't want to run this all the time instead of trade a new function here in my use. 66 00:04:46,110 --> 00:04:46,830 HTP Hook. 67 00:04:47,910 --> 00:04:50,240 Which will name send request name is up to you, though. 68 00:04:51,230 --> 00:04:57,310 And that is a function which should actually send the request so that it's not always getting sent when 69 00:04:57,320 --> 00:05:01,910 U.S. troops called, but only when send request is called, which right now never happens. 70 00:05:01,910 --> 00:05:03,230 But we'll do that soon. 71 00:05:03,590 --> 00:05:07,440 Now, send request, of course, needs to be a bit more flexible because we don't always want to send 72 00:05:07,440 --> 00:05:09,350 the request to this earlier. 73 00:05:10,310 --> 00:05:16,610 So there you are, l should probably something we get from outside the same for the method and the body 74 00:05:17,160 --> 00:05:18,130 of the request. 75 00:05:18,890 --> 00:05:23,180 So here I'll remove the URL and use the argument instead. 76 00:05:23,600 --> 00:05:28,850 And for method, I'll use method and we might have a body, which is the body we're getting. 77 00:05:30,310 --> 00:05:38,170 And because it doesn't hurt here, I'll always add the header content type application JSON for a get 78 00:05:38,170 --> 00:05:38,590 request. 79 00:05:38,590 --> 00:05:39,880 We don't need it but won't hurt. 80 00:05:39,880 --> 00:05:43,580 And for some requests like post requests, we need that so will always appended. 81 00:05:44,170 --> 00:05:51,160 Now we have a flexible if request a fetch call, which depends on data we get from outside, which will 82 00:05:51,160 --> 00:05:59,960 go ahead and then dispatch our response and at the beginning all the dispatch action of type send. 83 00:06:00,430 --> 00:06:02,710 So that's the state fulness I was talking about. 84 00:06:02,800 --> 00:06:07,720 We're interacting with our producer here because this custom hook will in the end be used in a component 85 00:06:07,900 --> 00:06:14,440 which then will automatically get all that state, so to say, or where this state will be connected 86 00:06:14,440 --> 00:06:16,360 to by react automatically. 87 00:06:17,230 --> 00:06:19,630 So here we send here we get the response. 88 00:06:20,440 --> 00:06:24,580 Now, of course, what we do with the response, however, in this case, that we delete the ingredient 89 00:06:24,580 --> 00:06:26,350 that doesn't belong into this hook. 90 00:06:26,380 --> 00:06:29,380 That's something the component that uses our hook needs to do. 91 00:06:29,380 --> 00:06:34,480 The hook should be just caring about the P request, not about what we do with the response. 92 00:06:35,840 --> 00:06:42,740 So we'll have to see how we manage debt for the error, I dispatch this error request, but how can 93 00:06:42,740 --> 00:06:46,460 we now get the response data back to. 94 00:06:46,670 --> 00:06:49,970 Well, the component that uses our own request. 95 00:06:51,430 --> 00:06:54,770 Well, we can add something new to our HTP state here. 96 00:06:55,180 --> 00:06:56,620 Let's start with the initial state. 97 00:06:57,070 --> 00:07:00,700 Besides loading an error, we can have some data in there which initially says no. 98 00:07:00,970 --> 00:07:09,250 But if we get a response besides setting loading to fall, as I said, data equal to action DOT response 99 00:07:09,250 --> 00:07:15,310 data, for example, a key I'm expecting on the action when we send the request all reset data to null 100 00:07:15,310 --> 00:07:18,630 here so that this is definitely null once we send the new request. 101 00:07:19,540 --> 00:07:22,060 So I get response data if we have a response. 102 00:07:22,720 --> 00:07:27,880 So here when we have a response, I'll actually cut this and return response. 103 00:07:27,880 --> 00:07:34,780 Jason, which extracts us to response body and then the next block, I got my response data extracted 104 00:07:35,230 --> 00:07:40,120 and in here I dispatch the response action, but now we added it. 105 00:07:40,330 --> 00:07:45,760 Does action or this case here to all extract response data from the action. 106 00:07:46,060 --> 00:07:52,600 So we'll set a response date, a key on the action we're dispatching and save my response data we're 107 00:07:52,600 --> 00:07:57,540 getting here thanks to response, Jason, in there so that it's now part of the action. 108 00:07:57,970 --> 00:08:03,310 So now the important thing is the data we're getting for the request we're sending is stored in our 109 00:08:03,310 --> 00:08:04,060 state here. 110 00:08:05,480 --> 00:08:10,520 That's all nice, but still it's in the hook, how do we now connect us to our component? 111 00:08:11,420 --> 00:08:16,730 Well, just like some of the built in Hooke's, our own hook can, of course, return something. 112 00:08:17,880 --> 00:08:23,460 So here at the end of the hook, I will return an array, but you don't have to return an array. 113 00:08:23,490 --> 00:08:28,120 You can always return an object here or a single number, whatever your hook needs. 114 00:08:28,500 --> 00:08:29,850 Here, I'll go with. 115 00:08:31,150 --> 00:08:37,070 An array or actually, I'll go with an object to show that you can return different things than just 116 00:08:37,090 --> 00:08:37,470 a race. 117 00:08:38,289 --> 00:08:43,390 And in that object I'll return my is loading key. 118 00:08:43,870 --> 00:08:49,540 And that is coming from HTP State, which is what used producer returns me here, which I've extracted 119 00:08:49,540 --> 00:08:53,410 here, which will have a loading property and Arab property in a data property. 120 00:08:53,650 --> 00:08:55,840 So here it's a state loading. 121 00:08:56,500 --> 00:09:04,000 Then we have data which is HTP state data and we have an error, which is HTP state error. 122 00:09:05,270 --> 00:09:09,470 So this is what my hope now returns and now we can use that hook in ingredients. 123 00:09:09,650 --> 00:09:13,410 Yes, there, let's import use HTP. 124 00:09:13,460 --> 00:09:18,920 And here you also have to you can name it whatever you want, but it has to start with us from. 125 00:09:20,830 --> 00:09:21,400 Hooke's. 126 00:09:22,820 --> 00:09:28,880 HTP like that, so now I want to use HTP, and just as all other hooks, you have to use it on the root 127 00:09:28,880 --> 00:09:32,760 level so we can't use it in here in the remove ingredient handler. 128 00:09:32,780 --> 00:09:33,500 That won't work. 129 00:09:34,280 --> 00:09:40,550 Instead, we have to call use HTP somewhere here like Det. 130 00:09:41,460 --> 00:09:47,400 But that's OK, because he was a HTP doesn't sent the request, actually, it just sets up the logic 131 00:09:47,400 --> 00:09:48,410 for sending a request. 132 00:09:48,420 --> 00:09:52,230 It sets up our state and it sets up this function, which sends a request. 133 00:09:52,680 --> 00:09:55,650 So we should probably return that function to. 134 00:09:56,070 --> 00:09:57,570 So in this object I'm returning. 135 00:09:59,350 --> 00:10:05,170 I'm all returning send request and that point at the send request function. 136 00:10:06,800 --> 00:10:09,520 So now we return a handle to dysfunction as well. 137 00:10:12,770 --> 00:10:19,700 Now, to avoid unnecessary referendums, already, all the import use callback here and wrap send request 138 00:10:20,150 --> 00:10:21,470 with use callback. 139 00:10:23,100 --> 00:10:29,220 Like this and add an empty array as a second argument, because this doesn't have any external dependencies, 140 00:10:29,220 --> 00:10:30,670 so it's good like that. 141 00:10:31,470 --> 00:10:36,720 So send request has returned and it will not render unnecessarily, which helps us optimize rebuilds. 142 00:10:37,620 --> 00:10:44,490 And now in ingredients, Jass use htp dhafer returns, our HTP object, whatever you want to call it. 143 00:10:44,850 --> 00:10:48,780 In the end, that's the object where we can use the structuring to pull out. 144 00:10:51,020 --> 00:10:57,470 Is loading a potential area, getting the data we're getting and send request, these are the four things, 145 00:10:57,470 --> 00:11:03,920 the four properties we're returning on our hook, these four properties and with object restructuring, 146 00:11:03,920 --> 00:11:09,170 which I'm using here and pulling them out and storing them in separate constants, now we can use these 147 00:11:09,170 --> 00:11:09,800 constants. 148 00:11:10,370 --> 00:11:11,390 So for the moment. 149 00:11:13,090 --> 00:11:16,090 Or comment, all of that logic here out. 150 00:11:17,140 --> 00:11:18,190 We'll fix that later. 151 00:11:19,060 --> 00:11:25,440 And here, where we use HSP said error in our JS code, I just use error because that's one of the things 152 00:11:25,450 --> 00:11:27,790 I pulled out of my result from use state. 153 00:11:28,690 --> 00:11:33,960 And for loading, well, this just is loading another thing and pulling out from there. 154 00:11:35,200 --> 00:11:42,310 Now, when we're deleting an ingredient here idea for India and just use send request to send the request 155 00:11:42,310 --> 00:11:42,880 to a U. 156 00:11:42,880 --> 00:11:43,360 RL. 157 00:11:44,870 --> 00:11:48,590 And that should be, of course, the other elephant ingredient. 158 00:11:49,700 --> 00:11:50,990 So it's this you are El. 159 00:11:52,880 --> 00:11:59,150 So let's use the tactics here, actually, so that we can inject values, then it's ingredients at the 160 00:11:59,150 --> 00:12:05,120 end, then just before the ingredient ID and then not chase, that's the usual. 161 00:12:06,030 --> 00:12:07,470 Then the method is the. 162 00:12:07,860 --> 00:12:13,500 That's the second argument, send request expects and we need no bodies so we can limit the third argument. 163 00:12:14,530 --> 00:12:22,870 Now, a send request is now a dependency of U.S callback, because send request is actually a thing 164 00:12:22,870 --> 00:12:28,690 we're returning here from use HTP and if we would not wrap send request with the U.S. callback inside 165 00:12:28,690 --> 00:12:35,050 of use HTP, this would actually change whenever the component where we use use https or whatever ingredients 166 00:12:35,050 --> 00:12:40,960 renders and therefore send request to change on every remender there and therefore remove ingredient 167 00:12:40,960 --> 00:12:44,380 handler would change and we would again lose all our optimizations. 168 00:12:44,680 --> 00:12:51,190 Since we use use callback in our custom hook, we ensure that send request never changes and we can 169 00:12:51,190 --> 00:12:54,400 safely added as a dependency here it will not change. 170 00:12:54,400 --> 00:13:00,860 And Dayle for this will not recreate dysfunction unnecessarily and hence our optimisations stay intact. 171 00:13:01,630 --> 00:13:07,240 So now we're sending this request here when we're clicking remove ingredient handler. 172 00:13:09,160 --> 00:13:10,190 Let's give it a try. 173 00:13:10,930 --> 00:13:12,400 Let's go here, Log-in. 174 00:13:13,530 --> 00:13:18,940 And click this, and you saw loading state, that's great, something changed. 175 00:13:18,960 --> 00:13:20,400 The ingredients are still there, though. 176 00:13:20,410 --> 00:13:24,980 But interestingly enough on Firebase, it's indeed gone now. 177 00:13:25,050 --> 00:13:28,410 That makes sense because we sent the request and it's good that it worked. 178 00:13:28,890 --> 00:13:31,470 But we're not updating our ingredients here. 179 00:13:31,920 --> 00:13:33,760 That's, of course, the next thing we'll have to do. 180 00:13:33,780 --> 00:13:35,120 We're not done entirely. 181 00:13:35,130 --> 00:13:41,790 We have our hook for managing HTP requests, but for updating our data Endace ingredients component. 182 00:13:41,940 --> 00:13:43,200 We have no solution yet. 19083

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