Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,080 --> 00:00:03,510
So now in this module
2
00:00:03,510 --> 00:00:06,140
we learn a lot of important new concepts
3
00:00:06,140 --> 00:00:09,020
and we learn about new react hooks.
4
00:00:09,020 --> 00:00:12,140
Specifically, we can see it here in the login component.
5
00:00:12,140 --> 00:00:15,570
We knew about use state before, but now we also learned
6
00:00:15,570 --> 00:00:20,570
about use effect, use reducer and also use context.
7
00:00:20,580 --> 00:00:22,680
So there are a bunch of react hooks.
8
00:00:22,680 --> 00:00:25,340
There are even more hooks than that, but we'll get there.
9
00:00:25,340 --> 00:00:28,690
Step-by-step now that we have all those hooks
10
00:00:28,690 --> 00:00:30,620
it is important that we also learn
11
00:00:30,620 --> 00:00:33,050
about some rules for using them.
12
00:00:33,050 --> 00:00:35,670
We always follow those rules thus far
13
00:00:35,670 --> 00:00:39,310
because of course I'm not teaching you incorrect patterns
14
00:00:39,310 --> 00:00:40,230
but I think it's now
15
00:00:40,230 --> 00:00:43,040
the time to give you a formal definition
16
00:00:43,040 --> 00:00:44,820
which you can print out, put
17
00:00:44,820 --> 00:00:47,660
on your wall and make sure you don't violate them.
18
00:00:47,660 --> 00:00:51,380
You find this slide attached there for side note
19
00:00:51,380 --> 00:00:53,080
all slides are always attached
20
00:00:53,080 --> 00:00:55,390
to the last lecture of each module.
21
00:00:55,390 --> 00:00:58,740
Anyways, there are two main rules you have to
22
00:00:58,740 --> 00:01:01,590
know when it comes to working with react hooks
23
00:01:01,590 --> 00:01:04,739
and react hooks are simply all those functions
24
00:01:04,739 --> 00:01:06,560
that start with use.
25
00:01:06,560 --> 00:01:08,150
So use effect use reducer.
26
00:01:08,150 --> 00:01:10,720
And so on the first rule,
27
00:01:10,720 --> 00:01:12,060
which is important is
28
00:01:12,060 --> 00:01:17,010
that you must only call react hooks in react functions.
29
00:01:17,010 --> 00:01:20,080
That means in react component functions
30
00:01:20,080 --> 00:01:21,720
as we did it thus far,
31
00:01:21,720 --> 00:01:25,250
or also allowed in custom hooks.
32
00:01:25,250 --> 00:01:27,540
That's not something we explored yet, but
33
00:01:27,540 --> 00:01:30,600
of course that will also be included in this course
34
00:01:30,600 --> 00:01:33,970
just as the name suggests, it's a bit more complex
35
00:01:33,970 --> 00:01:36,130
and therefore a bit more advanced and something we'll
36
00:01:36,130 --> 00:01:38,130
there for dive in later,
37
00:01:38,130 --> 00:01:40,520
these are the two scenarios
38
00:01:40,520 --> 00:01:43,750
where you are allowed to use react hooks.
39
00:01:43,750 --> 00:01:45,320
And what does this mean?
40
00:01:45,320 --> 00:01:47,960
Well, to give you a concrete example here
41
00:01:47,960 --> 00:01:49,890
in the log in component, we, for example
42
00:01:49,890 --> 00:01:52,280
have to email reducer function.
43
00:01:52,280 --> 00:01:55,688
This is a function next to the login function.
44
00:01:55,688 --> 00:01:57,720
The log in function here, however
45
00:01:57,720 --> 00:02:01,490
is my component function for the login component.
46
00:02:01,490 --> 00:02:04,250
And it identifies itself
47
00:02:04,250 --> 00:02:07,820
as such a function because it returns JSX at the end.
48
00:02:07,820 --> 00:02:10,720
That is what makes up a component function.
49
00:02:10,720 --> 00:02:13,030
Email reducer does not do that.
50
00:02:13,030 --> 00:02:14,110
It returns an object.
51
00:02:14,110 --> 00:02:15,600
It's not a react component.
52
00:02:15,600 --> 00:02:19,240
And therefore calling Us state in here is not allowed.
53
00:02:19,240 --> 00:02:23,520
And the good thing is a modern react set with a good IDE
54
00:02:23,520 --> 00:02:27,150
like Visual Studio code, weill even tell you about that.
55
00:02:27,150 --> 00:02:28,440
You see it down there
56
00:02:28,440 --> 00:02:32,020
react hook use state is called in function email reducer
57
00:02:32,020 --> 00:02:34,670
which is neither a react function component
58
00:02:34,670 --> 00:02:37,060
or accustomed react hook function.
59
00:02:37,060 --> 00:02:39,260
So exactly what I stated on the slide.
60
00:02:39,260 --> 00:02:41,030
You're only allowed to use hooks
61
00:02:41,030 --> 00:02:43,612
in react function components or in custom hooks.
62
00:02:43,612 --> 00:02:45,400
emaiL reducer
63
00:02:45,400 --> 00:02:46,600
is neither of that.
64
00:02:46,600 --> 00:02:48,610
and hence, we get an error,
65
00:02:48,610 --> 00:02:50,040
so you should get an error.
66
00:02:50,040 --> 00:02:51,250
If you try to do that
67
00:02:51,250 --> 00:02:54,740
should get a warning and here's all your technical rule.
68
00:02:54,740 --> 00:02:55,573
Don't do it
69
00:02:56,660 --> 00:02:58,760
The second important rule is
70
00:02:58,760 --> 00:03:01,200
that you must only call react hooks
71
00:03:01,200 --> 00:03:05,420
at the top level of your react component functions
72
00:03:05,420 --> 00:03:08,240
or your custom hook functions.
73
00:03:08,240 --> 00:03:10,820
Don't call hooks in nested functions.
74
00:03:10,820 --> 00:03:14,063
Don't call them in any block statements.
75
00:03:14,910 --> 00:03:18,380
So again, to give you an example here in the login
76
00:03:18,380 --> 00:03:22,440
component, so now inside of the functional component.
77
00:03:22,440 --> 00:03:24,190
We of course, for example
78
00:03:24,190 --> 00:03:27,070
have our use affect function here.
79
00:03:27,070 --> 00:03:30,330
So the function which we pass to use effect
80
00:03:30,330 --> 00:03:33,660
you're not allowed to call use context
81
00:03:33,660 --> 00:03:36,710
or use state or any other hook in there.
82
00:03:36,710 --> 00:03:39,960
Again, you will see, I get a warning here.
83
00:03:39,960 --> 00:03:42,450
React hook use context cannot be called inside.
84
00:03:42,450 --> 00:03:45,170
The callback react hooks must be called in a
85
00:03:45,170 --> 00:03:48,250
react function component or a custom react hook function
86
00:03:48,250 --> 00:03:52,020
and admits to be called directly on the top level there.
87
00:03:52,020 --> 00:03:54,440
So calling it here is okay
88
00:03:54,440 --> 00:03:57,960
because that's the top level of this login function
89
00:03:57,960 --> 00:04:00,790
but in a nested function is not allowed
90
00:04:00,790 --> 00:04:03,270
and that's not just true for nested functions.
91
00:04:03,270 --> 00:04:06,690
It would also not be allowed in if statement
92
00:04:06,690 --> 00:04:10,393
calling use state in there also not allowed.
93
00:04:12,080 --> 00:04:16,240
So again, a very simple rule, not too hard to follow
94
00:04:16,240 --> 00:04:19,329
only call react hooks at the top level.
95
00:04:19,329 --> 00:04:22,470
Now these are the two official rules of hooks.
96
00:04:22,470 --> 00:04:25,000
There is a third rule, which I want to add here
97
00:04:25,000 --> 00:04:28,780
not directly related to all hooks, but to one specific hook.
98
00:04:28,780 --> 00:04:30,930
And that's the use effect hook
99
00:04:30,930 --> 00:04:35,930
there make sure that you always add everything you refer
100
00:04:36,640 --> 00:04:39,950
to inside of use effect as a dependency
101
00:04:39,950 --> 00:04:42,220
unless there is a good reason not to do that.
102
00:04:42,220 --> 00:04:43,740
I should add.
103
00:04:43,740 --> 00:04:45,990
And to make it clear what I mean, let's again
104
00:04:45,990 --> 00:04:48,450
go to login component in here.
105
00:04:48,450 --> 00:04:51,120
We use use effect to execute this function.
106
00:04:51,120 --> 00:04:56,120
And in there we use nothing from the surrounding component.
107
00:04:56,560 --> 00:04:59,910
We use console log, but that's baked into the browser.
108
00:04:59,910 --> 00:05:02,860
I'm not talking about browser API things.
109
00:05:02,860 --> 00:05:06,280
I'm talking about data exposed by the surrounding function
110
00:05:06,280 --> 00:05:08,100
and here we're not using any data.
111
00:05:08,100 --> 00:05:10,290
Hence we have no dependencies.
112
00:05:10,290 --> 00:05:14,330
It's a different thing for this use effect here though,
113
00:05:14,330 --> 00:05:16,210
there, we have this function and
114
00:05:16,210 --> 00:05:19,670
in there we're using set timeout, which is a browser API.
115
00:05:19,670 --> 00:05:21,110
You don't need to add those
116
00:05:21,990 --> 00:05:25,870
but we are using email is valid and password is valid
117
00:05:25,870 --> 00:05:28,770
and that's data from the surrounding component.
118
00:05:28,770 --> 00:05:31,970
That's part of the surrounding components state.
119
00:05:31,970 --> 00:05:33,240
And if it would be part
120
00:05:33,240 --> 00:05:36,470
of the surrounding components props, the same would be true.
121
00:05:36,470 --> 00:05:37,740
So what I'm now saying would be true
122
00:05:37,740 --> 00:05:40,400
in that case as well, such dependencies
123
00:05:40,400 --> 00:05:43,343
or such values need to be added as dependencies.
124
00:05:44,190 --> 00:05:47,760
Technically we should there for also add set form is valid
125
00:05:47,760 --> 00:05:50,290
because that's not the browser API
126
00:05:50,290 --> 00:05:52,000
but this is one exception to the rule.
127
00:05:52,000 --> 00:05:56,640
Actually the state updating functions exposed by use reducer
128
00:05:56,640 --> 00:06:01,160
or by use state are guaranteed by react to never change.
129
00:06:01,160 --> 00:06:02,880
And therefore you don't need to add them
130
00:06:02,880 --> 00:06:06,520
as dependencies because if they never change well
131
00:06:06,520 --> 00:06:08,130
it's a redundant dependency.
132
00:06:08,130 --> 00:06:09,420
You could add it though.
133
00:06:09,420 --> 00:06:10,440
Wouldn't be wrong.
134
00:06:10,440 --> 00:06:11,370
Wouldn't be horrible.
135
00:06:11,370 --> 00:06:12,350
You could add it.
136
00:06:12,350 --> 00:06:13,940
And it would work in the same way.
137
00:06:13,940 --> 00:06:16,680
You are just allowed to omit it.
138
00:06:16,680 --> 00:06:19,220
And actually you don't need to memorize all of that
139
00:06:19,220 --> 00:06:21,310
if you think, well, that's too much.
140
00:06:21,310 --> 00:06:22,143
No worries.
141
00:06:22,143 --> 00:06:24,890
If I remove a dependency here in a modern setup
142
00:06:24,890 --> 00:06:27,500
you should get a warning here that react hook
143
00:06:27,500 --> 00:06:29,880
use effect has a missing dependency
144
00:06:29,880 --> 00:06:33,870
either included or remove the dependency array.
145
00:06:33,870 --> 00:06:36,030
So you are warned here as well.
146
00:06:36,030 --> 00:06:38,100
And I just want to explain this warning
147
00:06:38,100 --> 00:06:40,130
and why you are getting it.
148
00:06:40,130 --> 00:06:43,500
There are some exceptions like the state updating functions
149
00:06:43,500 --> 00:06:45,770
but all the average data, which is not coming
150
00:06:45,770 --> 00:06:49,150
from the browser or from outside your component function.
151
00:06:49,150 --> 00:06:50,560
So all the data that is
152
00:06:50,560 --> 00:06:52,910
from inside your a component function
153
00:06:52,910 --> 00:06:56,440
in which you are using use effect needs to go
154
00:06:56,440 --> 00:06:58,500
into the dependency array.
155
00:06:58,500 --> 00:07:00,850
That's my third rule here.
156
00:07:00,850 --> 00:07:04,590
And with that, you are well prepared to use hooks
157
00:07:04,590 --> 00:07:06,290
and to build amazing apps.
158
00:07:06,290 --> 00:07:07,943
Now let's continue on that road.
12405
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.