Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,140 --> 00:00:04,660
So, were are you successful?
2
00:00:04,660 --> 00:00:06,980
Let's try this together now.
3
00:00:06,980 --> 00:00:08,790
And for this in the source folder
4
00:00:08,790 --> 00:00:13,430
next to the components folder, I will add a hooks folder.
5
00:00:13,430 --> 00:00:16,219
This is just a convention, not something you have to do.
6
00:00:16,219 --> 00:00:18,080
You can name it differently,
7
00:00:18,080 --> 00:00:20,620
but I will follow this common convention
8
00:00:20,620 --> 00:00:22,390
and then add a new file in there
9
00:00:22,390 --> 00:00:25,200
which I will name use-http.js.
10
00:00:25,200 --> 00:00:27,730
Again, this name is all is up to you
11
00:00:27,730 --> 00:00:32,729
but since I will create a HTTP related custom hook in there,
12
00:00:32,759 --> 00:00:34,423
I think this is a fitting name.
13
00:00:35,350 --> 00:00:39,700
Now the hook function itself, I will call that useHttp
14
00:00:39,700 --> 00:00:43,090
following this must use pattern
15
00:00:43,090 --> 00:00:45,600
where your custom hook functions
16
00:00:45,600 --> 00:00:47,940
must start with the word use
17
00:00:48,970 --> 00:00:51,350
and I will export this custom hook here,
18
00:00:51,350 --> 00:00:53,570
because of course I want to be able to use it
19
00:00:53,570 --> 00:00:55,123
in other files.
20
00:00:56,210 --> 00:00:59,410
Now, regarding the content of this function,
21
00:00:59,410 --> 00:01:02,540
as I said, there will be more than one solution
22
00:01:02,540 --> 00:01:05,730
and definitely feel free to share your alternatives
23
00:01:05,730 --> 00:01:08,750
in the Q and A section if you want to.
24
00:01:08,750 --> 00:01:11,820
I'm just showing you one possible approach
25
00:01:11,820 --> 00:01:13,243
which I came up with.
26
00:01:14,240 --> 00:01:17,600
And for that, let's have a look at the general logic
27
00:01:17,600 --> 00:01:19,300
we wanna outsource.
28
00:01:19,300 --> 00:01:23,450
In the end, it is the loading and the error state,
29
00:01:23,450 --> 00:01:28,450
but then also the entire code where we send the request,
30
00:01:28,490 --> 00:01:31,470
where we evaluate the response.
31
00:01:31,470 --> 00:01:33,420
All of that should be outsourced.
32
00:01:33,420 --> 00:01:35,650
Also this error handling here
33
00:01:35,650 --> 00:01:38,650
where we then set the error state
34
00:01:38,650 --> 00:01:42,223
that all should be part of the custom hook.
35
00:01:43,160 --> 00:01:46,880
And therefore, I think it is reasonable
36
00:01:46,880 --> 00:01:48,670
to go to the app component
37
00:01:48,670 --> 00:01:52,530
and grab this entire fetch tasks function
38
00:01:52,530 --> 00:01:55,300
and grab these states here
39
00:01:55,300 --> 00:01:58,810
and in the end copy that into useHttp
40
00:01:58,810 --> 00:02:00,933
into this custom hook function.
41
00:02:02,490 --> 00:02:06,170
Now, here we can get rid of this tasks stayed here
42
00:02:06,170 --> 00:02:08,080
because that's a bit too specific.
43
00:02:08,080 --> 00:02:11,150
That's specific to the app component,
44
00:02:11,150 --> 00:02:13,230
that's not something I would wanna put
45
00:02:13,230 --> 00:02:16,830
in the useHttp custom hook itself
46
00:02:16,830 --> 00:02:20,290
because this hook should not just be usable with tasks
47
00:02:20,290 --> 00:02:23,080
but with any kind of data,
48
00:02:23,080 --> 00:02:24,967
but I will definitely keep deloading
49
00:02:24,967 --> 00:02:27,500
and error state in there.
50
00:02:27,500 --> 00:02:30,460
Now, of course, to use useState in this file
51
00:02:30,460 --> 00:02:34,560
as always we should import useState from react
52
00:02:34,560 --> 00:02:36,750
and I will rename this function here
53
00:02:36,750 --> 00:02:40,550
from fetchTasks to sendRequest
54
00:02:40,550 --> 00:02:43,180
so that this has a more generic name
55
00:02:43,180 --> 00:02:46,050
because we will also tweak the logic in there
56
00:02:46,050 --> 00:02:48,220
to be more generic.
57
00:02:48,220 --> 00:02:49,350
This custom hook
58
00:02:49,350 --> 00:02:53,220
should not just be dealing with fetching data,
59
00:02:53,220 --> 00:02:55,510
it should also not just be dealing
60
00:02:55,510 --> 00:02:58,990
with fetching tasks specifically
61
00:02:58,990 --> 00:03:02,850
because otherwise this hook would not be very reusable
62
00:03:02,850 --> 00:03:06,280
it would be basically limited to one operation,
63
00:03:06,280 --> 00:03:09,410
fetching tasks and that's not what I want here.
64
00:03:09,410 --> 00:03:12,040
Instead, this hook should be able
65
00:03:12,040 --> 00:03:15,880
to send any kind of requests to any kind of URL
66
00:03:15,880 --> 00:03:18,700
and to do any kind of data transformation
67
00:03:18,700 --> 00:03:22,740
but it should always manage the same state loading and error
68
00:03:22,740 --> 00:03:25,960
and execute the same steps in the same order.
69
00:03:25,960 --> 00:03:29,040
That's the actual reuse that we want here.
70
00:03:29,040 --> 00:03:32,120
And to make that hook that flexible,
71
00:03:32,120 --> 00:03:36,240
we, of course also will need some parameters here.
72
00:03:36,240 --> 00:03:39,160
We will need some parameters to configure this hook.
73
00:03:39,160 --> 00:03:41,883
And what should be configurable here?
74
00:03:42,810 --> 00:03:46,040
Well, I would say the request logic
75
00:03:46,040 --> 00:03:47,820
that needs to be configurable,
76
00:03:47,820 --> 00:03:52,170
the URL, but also the methods, the body, the headers,
77
00:03:52,170 --> 00:03:54,690
this all needs to be flexible.
78
00:03:54,690 --> 00:03:59,480
So I do expect a requestConfig parameter here
79
00:03:59,480 --> 00:04:02,780
and to disintern then should be an object let's say
80
00:04:02,780 --> 00:04:04,810
which contains the URL
81
00:04:04,810 --> 00:04:08,003
and any kind of other configuration that might be needed.
82
00:04:09,510 --> 00:04:12,820
Now, therefore here, I will get rid of that URL
83
00:04:12,820 --> 00:04:16,279
of the hard-coded string here in useHttp
84
00:04:16,279 --> 00:04:20,420
and instead refer to requestConfig.url.
85
00:04:20,420 --> 00:04:22,450
Since we will be calling that hook,
86
00:04:22,450 --> 00:04:25,590
we then have to make sure that we pass in an object
87
00:04:25,590 --> 00:04:29,273
which has a URL property, which holds the URL.
88
00:04:30,620 --> 00:04:33,940
This hook should also be usable for post requests.
89
00:04:33,940 --> 00:04:37,660
So therefore fetch should take a second argument here
90
00:04:37,660 --> 00:04:39,960
and then there, I pass my object
91
00:04:39,960 --> 00:04:43,810
where the method should be set to requestConfig.method,
92
00:04:43,810 --> 00:04:48,810
where the headers should be set to requestConfig.headers
93
00:04:48,945 --> 00:04:50,770
and where the body should be set
94
00:04:50,770 --> 00:04:55,770
to JSON.stringify requestConfig.body.
95
00:04:56,040 --> 00:04:58,920
So that we perform the JSON transformation
96
00:04:58,920 --> 00:05:00,240
inside of the hook,
97
00:05:00,240 --> 00:05:03,210
but the body itself has passed and from outside.
98
00:05:03,210 --> 00:05:06,240
And again, this is really just my approach here.
99
00:05:06,240 --> 00:05:08,670
You could make this more reusable,
100
00:05:08,670 --> 00:05:12,670
you could expect different parameters, that's all up to you.
101
00:05:12,670 --> 00:05:14,993
This is just one possible solution.
102
00:05:15,950 --> 00:05:19,730
Now this request and how this request is being sent
103
00:05:19,730 --> 00:05:22,460
is a bit more configurable.
104
00:05:22,460 --> 00:05:25,590
Of course, I wanna then check the response like this
105
00:05:25,590 --> 00:05:28,700
and I wanna transform the response like this,
106
00:05:28,700 --> 00:05:32,600
I am assuming here that we're always working with JSON data,
107
00:05:32,600 --> 00:05:35,270
so I will not make this hoax super generic
108
00:05:35,270 --> 00:05:37,260
to work with any kind of data.
109
00:05:37,260 --> 00:05:39,860
I will limit it to JSON data
110
00:05:40,860 --> 00:05:42,990
because everything else would be far
111
00:05:42,990 --> 00:05:44,710
beyond the scope of this course
112
00:05:44,710 --> 00:05:46,910
and therefore I will keep these steps here
113
00:05:46,910 --> 00:05:49,490
but the transformation and so on
114
00:05:49,490 --> 00:05:51,830
and what we do with the data in the end
115
00:05:51,830 --> 00:05:55,130
that of course is then again, highly specific
116
00:05:55,130 --> 00:05:59,130
and not something that should be of this hook.
117
00:05:59,130 --> 00:06:03,360
Instead, I would imagine that we get the data here
118
00:06:03,360 --> 00:06:06,460
and then maybe we execute a function
119
00:06:06,460 --> 00:06:09,780
which we get from the components that uses the hook
120
00:06:09,780 --> 00:06:12,970
and pass that data to that function
121
00:06:12,970 --> 00:06:15,540
so that the concrete transformation steps
122
00:06:15,540 --> 00:06:17,900
can be defined in the component
123
00:06:17,900 --> 00:06:19,763
where this hook is being used.
124
00:06:20,630 --> 00:06:24,210
So therefore here instead of executing this code,
125
00:06:24,210 --> 00:06:27,720
I do expect to get another parameter here
126
00:06:27,720 --> 00:06:32,680
and that's the, let's say applyData function.
127
00:06:32,680 --> 00:06:33,863
The name is up to you.
128
00:06:34,960 --> 00:06:37,730
And then here, once we got the data, we simply call
129
00:06:37,730 --> 00:06:40,663
applyData and pass the data to it.
130
00:06:41,910 --> 00:06:45,180
So that in the hook we just hand the data off
131
00:06:45,180 --> 00:06:47,220
to this applyData function
132
00:06:47,220 --> 00:06:49,880
and the function itself and what happens in there
133
00:06:49,880 --> 00:06:53,820
is provided by the component that uses the custom hook.
134
00:06:53,820 --> 00:06:56,310
So that we have that reusability
135
00:06:56,310 --> 00:06:59,780
and that reuse logic in the hook,
136
00:06:59,780 --> 00:07:03,080
but the specific steps that should be taken with the data
137
00:07:03,080 --> 00:07:05,500
in the components that uses the hook.
138
00:07:05,500 --> 00:07:08,073
This is a split which makes sense to me.
139
00:07:09,730 --> 00:07:13,054
Now, with that, we got to the loading and the errorState
140
00:07:13,054 --> 00:07:15,760
and to sendRequest function in the custom hook,
141
00:07:15,760 --> 00:07:18,100
but ultimately these are all things
142
00:07:18,100 --> 00:07:21,700
which we will need in the components that uses the hook.
143
00:07:21,700 --> 00:07:24,000
That component should to then have access
144
00:07:24,000 --> 00:07:25,950
to the loading and the errorState
145
00:07:25,950 --> 00:07:29,160
and it should have access to the send request function
146
00:07:29,160 --> 00:07:33,063
so that it's the component which can trigger a send request.
147
00:07:34,250 --> 00:07:36,540
Therefore at the end of the custom hook,
148
00:07:36,540 --> 00:07:38,870
not at the end of sendRequest,
149
00:07:38,870 --> 00:07:41,420
but at the end of the custom hook function,
150
00:07:41,420 --> 00:07:44,240
I wanna return something to the component
151
00:07:44,240 --> 00:07:46,720
where the custom hook is used.
152
00:07:46,720 --> 00:07:49,220
And I mentioned before in this module
153
00:07:49,220 --> 00:07:52,710
that your custom hooks of course can return anything,
154
00:07:52,710 --> 00:07:55,920
numbers, strings, arrays, objects,
155
00:07:55,920 --> 00:07:58,490
and here I will return an object.
156
00:07:58,490 --> 00:08:01,570
And there I will add is loading property
157
00:08:01,570 --> 00:08:05,390
which points at the isLoading state constant.
158
00:08:05,390 --> 00:08:08,070
So this is the property name on the left side,
159
00:08:08,070 --> 00:08:10,180
this is the value on the right side,
160
00:08:10,180 --> 00:08:12,370
and that is loading state
161
00:08:12,370 --> 00:08:16,263
which is managed for the component by this custom hook.
162
00:08:17,440 --> 00:08:22,440
And I will also return an object with an error property,
163
00:08:24,160 --> 00:08:26,060
which holds the errorState
164
00:08:26,060 --> 00:08:31,060
just as the loading state and then the send request property
165
00:08:31,070 --> 00:08:33,232
which points at sendRequest.
166
00:08:34,690 --> 00:08:36,443
So add this function here.
167
00:08:37,419 --> 00:08:40,669
And since I'm always using the same property names
168
00:08:40,669 --> 00:08:42,970
as variable names on the right side,
169
00:08:42,970 --> 00:08:46,240
we can also use a modern JavaScript shortcut here
170
00:08:46,240 --> 00:08:51,240
where we write this like that that's the same result
171
00:08:51,370 --> 00:08:53,010
or that will give us the same result
172
00:08:53,010 --> 00:08:55,540
as to code I had a couple of seconds ago,
173
00:08:55,540 --> 00:08:56,920
it's just a bit shorter
174
00:08:56,920 --> 00:09:00,000
and it will be transformed to that longer syntax
175
00:09:00,000 --> 00:09:01,610
under the hood by JavaScript.
176
00:09:01,610 --> 00:09:04,933
So that's a shorthand we can use in modern JavaScript.
177
00:09:06,000 --> 00:09:08,030
And with that, we got our custom hook
178
00:09:08,030 --> 00:09:10,830
at least the first version of it.
179
00:09:10,830 --> 00:09:13,933
Now let's see how we could use this custom hook.
14233
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.