Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,290 --> 00:00:05,510
So here's a more realistic project.
2
00:00:05,510 --> 00:00:10,510
Attached you find this project where after you downloaded it
3
00:00:11,300 --> 00:00:14,920
you need to add in your Firebase links
4
00:00:14,920 --> 00:00:18,140
and therefore to understand what's going on here,
5
00:00:18,140 --> 00:00:20,880
you should make sure that you did not skip
6
00:00:20,880 --> 00:00:24,170
the Http course section.
7
00:00:24,170 --> 00:00:27,810
The section where I talk about connecting React apps
8
00:00:27,810 --> 00:00:29,420
to a backend.
9
00:00:29,420 --> 00:00:33,960
There I show how to set up Firebase as a dummy backend
10
00:00:33,960 --> 00:00:38,350
and I show how sending Http requests works.
11
00:00:38,350 --> 00:00:40,740
Which is of course, something you need a lot
12
00:00:40,740 --> 00:00:44,470
in any kind of application you're building basically.
13
00:00:44,470 --> 00:00:47,940
So, in this application here we are sending requests
14
00:00:47,940 --> 00:00:51,420
and if you wanna follow along on your system
15
00:00:51,420 --> 00:00:52,940
you should therefor make sure
16
00:00:52,940 --> 00:00:55,130
that you have a Firebase account,
17
00:00:55,130 --> 00:00:57,120
you can get started for free
18
00:00:57,120 --> 00:01:00,490
as shown in this Http course section
19
00:01:00,490 --> 00:01:03,810
and that there you are using the real time database
20
00:01:03,810 --> 00:01:07,630
and you got your rules configured like this.
21
00:01:07,630 --> 00:01:10,100
Set both read and write to true
22
00:01:10,100 --> 00:01:14,020
or use the default test mode configuration,
23
00:01:14,020 --> 00:01:18,660
and then use this URL which you find under data
24
00:01:18,660 --> 00:01:23,520
and use that URL here, instead of my URL.
25
00:01:23,520 --> 00:01:28,520
Keep the /tasks.json part though, you will need that.
26
00:01:28,690 --> 00:01:30,840
So that's what you should do to follow along.
27
00:01:30,840 --> 00:01:33,140
But what's this application doing?
28
00:01:33,140 --> 00:01:34,700
Once you updated the code
29
00:01:34,700 --> 00:01:37,850
and you installed all dependencies with NPM install
30
00:01:37,850 --> 00:01:41,110
and you started the project with NPM start,
31
00:01:41,110 --> 00:01:43,820
this is what you'll see on the screen.
32
00:01:43,820 --> 00:01:47,890
It's a simple tasks application, a very simple one actually
33
00:01:47,890 --> 00:01:50,780
where initially the tasks are loaded
34
00:01:50,780 --> 00:01:52,490
and if we have no tasks
35
00:01:52,490 --> 00:01:55,140
we of course have to add some tasks first,
36
00:01:55,140 --> 00:01:58,810
for example, learn all about Hooks
37
00:01:58,810 --> 00:02:00,470
and you simply click add task
38
00:02:00,470 --> 00:02:02,920
and now this is sent to Firebase
39
00:02:02,920 --> 00:02:04,660
it's all to display down there
40
00:02:04,660 --> 00:02:07,200
and if you reload the application it's loaded
41
00:02:07,200 --> 00:02:11,293
and displayed and it's stored here on Firebase.
42
00:02:13,360 --> 00:02:15,230
Now I am only using what you learned
43
00:02:15,230 --> 00:02:17,530
in the Http course section
44
00:02:17,530 --> 00:02:21,170
and this all works by sending an Http request
45
00:02:21,170 --> 00:02:24,700
with the fetch API, by handling errors
46
00:02:24,700 --> 00:02:26,700
and a loading state
47
00:02:26,700 --> 00:02:29,390
by transforming the data when it arrives
48
00:02:29,390 --> 00:02:31,810
and by then displaying the data.
49
00:02:31,810 --> 00:02:34,440
And we've got various involved components
50
00:02:34,440 --> 00:02:37,790
but most importantly we got the app JS component
51
00:02:37,790 --> 00:02:40,190
where we are fetching tasks
52
00:02:40,190 --> 00:02:43,000
with this fetch task function,
53
00:02:43,000 --> 00:02:45,060
which is either executed
54
00:02:45,060 --> 00:02:48,720
because the component loaded due to use effect,
55
00:02:48,720 --> 00:02:52,580
or because inside of this tasks component,
56
00:02:52,580 --> 00:02:54,370
a button was clicked,
57
00:02:54,370 --> 00:02:57,130
a button which has shown if we had an error
58
00:02:57,130 --> 00:02:59,950
which allows us to try again.
59
00:02:59,950 --> 00:03:03,260
So that's when this fetch task function is triggered
60
00:03:03,260 --> 00:03:04,790
in the app component
61
00:03:04,790 --> 00:03:08,070
and besides that in the new task component
62
00:03:08,070 --> 00:03:11,210
we got the enter task handler function
63
00:03:11,210 --> 00:03:13,800
where we also send a request
64
00:03:13,800 --> 00:03:16,780
but here we are sending a post request
65
00:03:16,780 --> 00:03:21,780
with some data attached to store task data on Firebase.
66
00:03:22,210 --> 00:03:24,460
And this function is triggered
67
00:03:24,460 --> 00:03:28,420
whenever this task form is submitted in the end,
68
00:03:28,420 --> 00:03:30,450
which happens when this button is clicked
69
00:03:30,450 --> 00:03:33,283
and we verified the entered value.
70
00:03:34,360 --> 00:03:36,330
Now feel free to pause the video
71
00:03:36,330 --> 00:03:38,600
and dig a bit deeper into the code,
72
00:03:38,600 --> 00:03:40,930
but that is what's going on here.
73
00:03:40,930 --> 00:03:44,780
And if you followed along in the Http course section,
74
00:03:44,780 --> 00:03:45,960
which you should have,
75
00:03:45,960 --> 00:03:50,560
then this is all basically just something you already saw.
76
00:03:50,560 --> 00:03:55,410
Now I did create this basic yet more realistic example
77
00:03:55,410 --> 00:03:58,430
because here we also got another chance
78
00:03:58,430 --> 00:04:01,100
of adding at least one custom Hook.
79
00:04:01,100 --> 00:04:06,100
And do you see where we could utilize the custom Hook?
80
00:04:08,030 --> 00:04:12,760
Well, we got two similar kind of operations
81
00:04:12,760 --> 00:04:15,920
in two different parts of this application.
82
00:04:15,920 --> 00:04:18,959
And that would be here in the new task component
83
00:04:18,959 --> 00:04:22,270
when we send the request to store data
84
00:04:22,270 --> 00:04:23,950
and in the app component
85
00:04:23,950 --> 00:04:26,543
when we send the request to get data.
86
00:04:27,550 --> 00:04:31,260
Now, regarding the details, the code is different.
87
00:04:31,260 --> 00:04:35,370
When we get the tasks, we configured a request like this
88
00:04:35,370 --> 00:04:39,920
we send a get request without a body and without headers
89
00:04:39,920 --> 00:04:42,180
and we have some transformation logic
90
00:04:42,180 --> 00:04:44,103
which we apply to the response.
91
00:04:45,000 --> 00:04:48,520
Whereas in the new task case we send a post request
92
00:04:48,520 --> 00:04:50,350
and we do add data
93
00:04:50,350 --> 00:04:52,770
and we have a different transformation logic
94
00:04:52,770 --> 00:04:54,700
for the response.
95
00:04:54,700 --> 00:04:57,510
But even though these details are different,
96
00:04:57,510 --> 00:04:59,390
there are a lot of parts
97
00:04:59,390 --> 00:05:02,810
where the code is pretty much the same.
98
00:05:02,810 --> 00:05:06,280
For example, in both components, we are managing a loading
99
00:05:06,280 --> 00:05:08,520
and an error State
100
00:05:08,520 --> 00:05:11,630
and we are setting the loading and error States
101
00:05:11,630 --> 00:05:13,580
in the same way.
102
00:05:13,580 --> 00:05:16,940
We are also having the same error handling logic here
103
00:05:16,940 --> 00:05:17,990
and so on.
104
00:05:17,990 --> 00:05:21,600
So there are parts which are quite similar
105
00:05:22,650 --> 00:05:25,320
and because that's the case
106
00:05:25,320 --> 00:05:28,660
because we have some code duplication
107
00:05:28,660 --> 00:05:31,740
some code re-usage here
108
00:05:31,740 --> 00:05:32,800
because of that
109
00:05:32,800 --> 00:05:35,760
we might wanna consider outsourcing this logic
110
00:05:35,760 --> 00:05:37,820
into a separate function.
111
00:05:37,820 --> 00:05:39,610
But since the logic
112
00:05:39,610 --> 00:05:41,860
or at least parts of the logic,
113
00:05:41,860 --> 00:05:45,110
which is being reused contains the usage
114
00:05:45,110 --> 00:05:46,610
of other React Hooks
115
00:05:46,610 --> 00:05:49,150
and the usage of State.
116
00:05:49,150 --> 00:05:50,880
Since that's the case,
117
00:05:50,880 --> 00:05:54,350
our regular function won't do the trick
118
00:05:54,350 --> 00:05:58,180
because there we're not allowed to use React Hooks.
119
00:05:58,180 --> 00:06:01,570
That's why here we have a great opportunity
120
00:06:01,570 --> 00:06:03,660
for building a custom Hook
121
00:06:03,660 --> 00:06:07,290
where we outsource the Http logic,
122
00:06:07,290 --> 00:06:12,050
and then still in the end can reuse that stateful logic
123
00:06:12,050 --> 00:06:14,410
for the loading and error State.
124
00:06:14,410 --> 00:06:16,630
And that's therefor what we're going to do now
125
00:06:16,630 --> 00:06:20,410
we're now going to apply our custom Hook knowledge
126
00:06:20,410 --> 00:06:22,710
to build another accustomed Hook,
127
00:06:22,710 --> 00:06:24,780
but this time our custom Hook,
128
00:06:24,780 --> 00:06:28,023
which in the end encapsulates this Http logic.
129
00:06:28,990 --> 00:06:32,140
Definitely feel free to try this on your own first.
130
00:06:32,140 --> 00:06:33,880
It'll definitely be challenging
131
00:06:33,880 --> 00:06:35,440
and they're also will not
132
00:06:35,440 --> 00:06:38,300
just be one single way of doing this.
133
00:06:38,300 --> 00:06:40,530
Nonetheless, try it on your own first
134
00:06:40,530 --> 00:06:43,430
in the next lecture, we're going to implement it together.
10329
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.