All language subtitles for 010 Using The Custom Hook In More 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,719 --> 00:00:04,100 [Maximilian Schwarzm�ller] So here in new task, 2 00:00:04,100 --> 00:00:07,000 I also want to use my custom hook. 3 00:00:07,000 --> 00:00:08,280 And therefore, first of all, 4 00:00:08,280 --> 00:00:12,510 we should import use-http from hooks, 5 00:00:12,510 --> 00:00:13,833 use-http. 6 00:00:14,950 --> 00:00:17,350 Then we can call useHttp 7 00:00:17,350 --> 00:00:20,890 and we no longer need to pass in any parameters 8 00:00:20,890 --> 00:00:24,453 because of the refactoring work we did in the last lecture. 9 00:00:25,800 --> 00:00:28,100 Now we still get back an object though, 10 00:00:28,100 --> 00:00:30,880 which we can de-structure where we get access 11 00:00:30,880 --> 00:00:34,363 to isLoading, to the error, and to send request. 12 00:00:35,850 --> 00:00:39,877 And here I will rename send request to sendTaskRequest, 13 00:00:41,820 --> 00:00:43,710 but that of course is up to you. 14 00:00:43,710 --> 00:00:46,630 We can get rid of the two states here now 15 00:00:46,630 --> 00:00:49,520 and of the useState import therefore, 16 00:00:49,520 --> 00:00:51,970 because we're using our custom hook for this now. 17 00:00:53,290 --> 00:00:55,800 Now in the enterTaskHandler, 18 00:00:55,800 --> 00:00:59,630 we, in the end one, wanna call sendTaskRequest now. 19 00:00:59,630 --> 00:01:02,320 We wanna call that here because we wanna send 20 00:01:02,320 --> 00:01:05,720 that request whenever enterTaskHandler is triggered 21 00:01:05,720 --> 00:01:09,003 which happens when that form was submitted in the end. 22 00:01:09,943 --> 00:01:12,530 Thereafter, we will soon delete all that code, 23 00:01:12,530 --> 00:01:14,584 but before we do that, we need to ensure 24 00:01:14,584 --> 00:01:18,130 that we pass the proper configuration 25 00:01:18,130 --> 00:01:21,713 and data handling function to send request. 26 00:01:23,200 --> 00:01:26,750 And for this, let's start with the conflict object. 27 00:01:26,750 --> 00:01:31,457 I pass in a new object where the URL is set to this URL. 28 00:01:32,750 --> 00:01:35,680 And here now, since I want to set a POST request, 29 00:01:35,680 --> 00:01:38,320 I'll also set the method to POST 30 00:01:38,320 --> 00:01:42,380 and I'll set the headers here to the headers object 31 00:01:42,380 --> 00:01:43,590 which I got down there 32 00:01:45,010 --> 00:01:46,593 like this. 33 00:01:47,470 --> 00:01:50,435 And I will, of course, also add a body. 34 00:01:50,435 --> 00:01:52,700 However, not as JSON 35 00:01:52,700 --> 00:01:55,650 because we're doing that JSON transformation 36 00:01:55,650 --> 00:01:57,763 inside of the custom hook here. 37 00:01:59,410 --> 00:02:03,320 But instead I will therefore just send in this object here 38 00:02:03,320 --> 00:02:05,950 which I previously changed to JSON 39 00:02:05,950 --> 00:02:07,673 in the new task component. 40 00:02:09,250 --> 00:02:13,020 So that's my request conflict object now. 41 00:02:13,020 --> 00:02:14,930 The second argument is a function 42 00:02:14,930 --> 00:02:18,940 which takes the response data and does something with it. 43 00:02:18,940 --> 00:02:20,920 And there's something I wanna do with it 44 00:02:20,920 --> 00:02:23,730 is in the end, this down here. 45 00:02:23,730 --> 00:02:26,410 I wanna extract the generated ID 46 00:02:26,410 --> 00:02:28,840 which Firebase gives me automatically. 47 00:02:28,840 --> 00:02:32,470 I wanna create a task object, which incorporates that ID. 48 00:02:32,470 --> 00:02:35,653 And I then when I call props on add task. 49 00:02:36,690 --> 00:02:41,500 So we could either create this function on the fly here 50 00:02:41,500 --> 00:02:43,090 or we create it in advance 51 00:02:44,800 --> 00:02:45,820 like this here. 52 00:02:45,820 --> 00:02:49,620 The createTask function, let's say, 53 00:02:49,620 --> 00:02:51,470 and here I get my 54 00:02:52,850 --> 00:02:53,683 data, 55 00:02:53,683 --> 00:02:57,650 my task data as an argument. 56 00:02:57,650 --> 00:03:00,740 And then we grab this logic from down there. 57 00:03:00,740 --> 00:03:01,723 This logic, 58 00:03:02,910 --> 00:03:07,540 copy that and add this here in createTask. 59 00:03:07,540 --> 00:03:11,090 And data is now renamed to top taskData like this. 60 00:03:12,520 --> 00:03:15,500 And we now use createTask to pass this 61 00:03:15,500 --> 00:03:18,300 as a second argument 62 00:03:19,510 --> 00:03:22,018 here to 63 00:03:22,018 --> 00:03:23,351 sendTaskRequest. 64 00:03:25,150 --> 00:03:27,480 We don't need to work with useCallback 65 00:03:27,480 --> 00:03:29,210 or anything like this here, 66 00:03:29,210 --> 00:03:32,235 because we are only calling sendTaskRequest 67 00:03:32,235 --> 00:03:34,290 in the enterTaskHandler, 68 00:03:34,290 --> 00:03:37,890 not inside of useEffect or anything like that. 69 00:03:37,890 --> 00:03:40,140 Therefore here, we won't have the problem 70 00:03:40,140 --> 00:03:41,440 of an infinite loop, 71 00:03:41,440 --> 00:03:43,740 because this request will not be sent whenever 72 00:03:43,740 --> 00:03:45,938 the component is re-evaluated, 73 00:03:45,938 --> 00:03:47,738 but only when this function runs 74 00:03:47,738 --> 00:03:50,690 which happens when the form is submitted. 75 00:03:50,690 --> 00:03:53,240 That was different in the app component. 76 00:03:53,240 --> 00:03:56,192 There we sent the request instead of useEffect. 77 00:03:56,192 --> 00:03:57,950 And therefore we had the danger 78 00:03:57,950 --> 00:03:59,993 of sending the request too often. 79 00:04:01,020 --> 00:04:03,490 In new task, we should be fine now. 80 00:04:03,490 --> 00:04:06,230 And therefore, we can grab this old code here 81 00:04:07,230 --> 00:04:08,910 and get rid of all of that. 82 00:04:08,910 --> 00:04:11,570 So that instead of the enterTaskHandler, 83 00:04:11,570 --> 00:04:13,950 I'm now only calling sendTaskRequest 84 00:04:15,190 --> 00:04:17,572 and we're configuring it like this. 85 00:04:19,480 --> 00:04:21,829 Now in my createTask function, 86 00:04:21,829 --> 00:04:25,070 I'm missing the task text though. 87 00:04:25,070 --> 00:04:26,400 That's a problem. 88 00:04:26,400 --> 00:04:28,440 I'm missing the text 89 00:04:28,440 --> 00:04:33,440 which was, well, passed from the form to that HTTP request. 90 00:04:34,660 --> 00:04:37,280 We do have that text available here 91 00:04:37,280 --> 00:04:40,110 inside of enterTaskHandler. 92 00:04:40,110 --> 00:04:43,290 To make it available inside of this createTask function, 93 00:04:43,290 --> 00:04:46,020 which we pass to sendTaskRequest, 94 00:04:46,020 --> 00:04:48,560 we got two main options. 95 00:04:48,560 --> 00:04:52,420 We could either grab this function and define it here, 96 00:04:52,420 --> 00:04:56,190 inside of enterTaskHandler to have a nested function 97 00:04:56,190 --> 00:04:57,520 inside of a nested function 98 00:04:57,520 --> 00:04:59,480 inside of the component function, 99 00:04:59,480 --> 00:05:03,590 and that would work due to how JavaScript scoping works. 100 00:05:03,590 --> 00:05:06,613 We now do have access to the enter task text. 101 00:05:07,830 --> 00:05:09,696 Or alternatively, 102 00:05:09,696 --> 00:05:13,730 we leave it like this to avoid this deep nesting. 103 00:05:13,730 --> 00:05:16,960 And we instead expect to get the task text 104 00:05:16,960 --> 00:05:20,413 as an argument here, as a parameter in createTask text. 105 00:05:21,350 --> 00:05:23,865 And therefore we want two parameters here 106 00:05:23,865 --> 00:05:27,320 when actually our custom hook, 107 00:05:27,320 --> 00:05:29,540 which is calling createTask 108 00:05:29,540 --> 00:05:33,750 in the end, only passes in one parameter. 109 00:05:33,750 --> 00:05:35,333 It only passes the data. 110 00:05:36,520 --> 00:05:38,600 To work around that, we could use 111 00:05:38,600 --> 00:05:41,380 the bind method in JavaScript. 112 00:05:41,380 --> 00:05:45,700 We could call bind here on createTask 113 00:05:45,700 --> 00:05:48,690 when we pass it to sendTaskRequest 114 00:05:48,690 --> 00:05:53,030 and the bind method allows us to pre-configure a function. 115 00:05:53,030 --> 00:05:55,500 It does not execute the function right away. 116 00:05:55,500 --> 00:05:58,120 Instead it's a default JavaScript method, 117 00:05:58,120 --> 00:06:00,680 which you can use on any function object 118 00:06:00,680 --> 00:06:02,763 to pre-configure that function. 119 00:06:03,760 --> 00:06:07,070 The first argument you pass to bind then allows you 120 00:06:07,070 --> 00:06:11,750 to set the this keyword in the to-be-executed function, 121 00:06:11,750 --> 00:06:13,260 which does not matter to us here. 122 00:06:13,260 --> 00:06:15,050 Hence, we can set this to null. 123 00:06:15,050 --> 00:06:17,254 But the second argument you pass to bind 124 00:06:17,254 --> 00:06:20,050 will then be the first argument received 125 00:06:20,050 --> 00:06:22,940 by that to-be-called function. 126 00:06:22,940 --> 00:06:26,260 So if you wanna pass in the task text here, 127 00:06:26,260 --> 00:06:28,970 we can set this to task text here 128 00:06:30,040 --> 00:06:32,270 because that will then refer to the task text. 129 00:06:32,270 --> 00:06:34,940 we're getting from the form submission. 130 00:06:34,940 --> 00:06:39,330 Now the other argument task data will still be received here 131 00:06:39,330 --> 00:06:42,080 because this is our pre-configuration. 132 00:06:42,080 --> 00:06:45,570 Any other arguments which might be passed by the place 133 00:06:45,570 --> 00:06:48,170 where the function is then actually being called 134 00:06:48,170 --> 00:06:50,520 which happens in useHttp, 135 00:06:50,520 --> 00:06:52,600 will then simply be appended to the end 136 00:06:52,600 --> 00:06:54,380 of the parameter list. 137 00:06:54,380 --> 00:06:56,420 So the data which we pass here 138 00:06:56,420 --> 00:06:59,620 as the only argument to apply data in the custom hook 139 00:06:59,620 --> 00:07:02,980 will be appended as a second argument 140 00:07:02,980 --> 00:07:06,640 on createTask because of us calling bind here. 141 00:07:06,640 --> 00:07:08,830 So when it's being called, this receives 142 00:07:08,830 --> 00:07:10,283 all the data which is needed. 143 00:07:11,270 --> 00:07:13,843 And this is all just regular JavaScript. 144 00:07:13,843 --> 00:07:16,310 This has nothing to do with React 145 00:07:16,310 --> 00:07:18,630 or with hooks or anything like that. 146 00:07:18,630 --> 00:07:21,200 That's just regular JavaScript. 147 00:07:21,200 --> 00:07:23,240 You can learn more about the bind method 148 00:07:23,240 --> 00:07:27,180 and about pre-configuring functions with a resource 149 00:07:27,180 --> 00:07:29,330 which you'll find attached to this lecture. 150 00:07:30,340 --> 00:07:32,840 And then for now, if I save this, 151 00:07:32,840 --> 00:07:36,090 we can reload and still load all tasks. 152 00:07:36,090 --> 00:07:38,600 But if I now send another task, 153 00:07:38,600 --> 00:07:41,000 if I click add task, we see sending here, 154 00:07:41,000 --> 00:07:43,090 and then we see Does this work? 155 00:07:43,090 --> 00:07:45,187 So if I reload, it's all still there 156 00:07:45,187 --> 00:07:48,483 and we can also see all that data on Firebase. 157 00:07:49,510 --> 00:07:53,961 Now, if I open the console, I also got no errors there. 158 00:07:53,961 --> 00:07:57,109 And if we go to the network tab, 159 00:07:57,109 --> 00:07:59,649 we also see that indeed, 160 00:07:59,649 --> 00:08:00,920 we are 161 00:08:01,760 --> 00:08:03,360 not sending an infinite amount 162 00:08:03,360 --> 00:08:05,690 of requests or anything like that. 163 00:08:05,690 --> 00:08:08,180 Instead, if I clear this and I test this, 164 00:08:08,180 --> 00:08:11,260 we are in the end, just sending one request. 165 00:08:11,260 --> 00:08:13,710 It looks like two, but this is just a side request 166 00:08:13,710 --> 00:08:15,250 which has sent along the way 167 00:08:15,250 --> 00:08:18,093 and therefore, this is all looking good. 168 00:08:19,000 --> 00:08:21,660 And that is how we can build a custom hook 169 00:08:21,660 --> 00:08:24,000 for sending HTTP requests. 170 00:08:24,000 --> 00:08:26,369 It definitely had its tricky parts, 171 00:08:26,369 --> 00:08:28,190 but with it, 172 00:08:28,190 --> 00:08:31,210 we now have this custom hook which outsources 173 00:08:31,210 --> 00:08:34,970 or which holds a lot of the duplicate logic, 174 00:08:34,970 --> 00:08:37,850 especially all that stateful logic. 175 00:08:37,850 --> 00:08:40,270 And we can use it in different components 176 00:08:40,270 --> 00:08:42,600 to send different kinds of requests, 177 00:08:42,600 --> 00:08:46,250 and do different kinds of things with the response data, 178 00:08:46,250 --> 00:08:49,090 and still utilize this shared loading 179 00:08:49,090 --> 00:08:51,833 and error state and this shared logic. 13737

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