Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,150 --> 00:00:07,830
So we want to fetch all ingredients whenever there's ingredients, components gets rendered.
2
00:00:08,450 --> 00:00:12,290
Now, as I mentioned at the end of the last lecture, normally we would have used components that mount
3
00:00:12,290 --> 00:00:15,100
for that to fetch something when this component mounts.
4
00:00:15,590 --> 00:00:19,960
But this is now not an option anymore unless we transformed this to a Class-Based component.
5
00:00:20,180 --> 00:00:22,520
But there is another hook that helps us with that.
6
00:00:22,820 --> 00:00:24,770
It's to use the fact hook.
7
00:00:26,240 --> 00:00:27,130
How does it work?
8
00:00:28,130 --> 00:00:33,880
Just like you state, the same rules apply, you can only use it in functional components or Ibrox and
9
00:00:33,890 --> 00:00:37,310
you have to use it on the root level, let's maybe use it here.
10
00:00:37,580 --> 00:00:38,330
Use effect.
11
00:00:38,990 --> 00:00:43,940
Use effect has the name because it is there for you to manage side effects.
12
00:00:44,120 --> 00:00:47,600
And HTP requests are a typical side effect.
13
00:00:48,490 --> 00:00:53,760
Side effect basically means that you have some logic that runs that does affect your application.
14
00:00:53,770 --> 00:01:00,730
For example, we are fetching some data here, but it's not getting finished in this current renderers
15
00:01:00,730 --> 00:01:01,330
cycle.
16
00:01:01,750 --> 00:01:06,920
Or maybe it affects something which is outside of the scope of your job.
17
00:01:06,920 --> 00:01:07,960
Is Xcode down there?
18
00:01:07,960 --> 00:01:11,040
Maybe you're setting to document title, anything like that.
19
00:01:11,110 --> 00:01:15,820
So anything you cannot manage with your normal disk component is getting rendered flow.
20
00:01:16,790 --> 00:01:26,090
Use a fact, and that's important by default, gets executed right after important after every component
21
00:01:26,090 --> 00:01:27,110
render a cycle.
22
00:01:27,140 --> 00:01:29,420
So often this component has been rendered the first time.
23
00:01:29,990 --> 00:01:35,220
The function you pass to use effect because you have to pass a function there will get executed.
24
00:01:35,660 --> 00:01:39,290
So this runs whenever this component got rendered.
25
00:01:39,290 --> 00:01:43,370
And that's really important to keep in mind after and for every render cycle.
26
00:01:43,380 --> 00:01:49,040
These are the two important pieces, not the four, not simultaneously, but after and for every render
27
00:01:49,040 --> 00:01:49,550
cycle.
28
00:01:50,270 --> 00:01:52,310
Now, why do we need use effect, though?
29
00:01:52,670 --> 00:01:59,210
We could just call fetch to get our data like this outside of use effect.
30
00:01:59,210 --> 00:01:59,430
Right.
31
00:01:59,750 --> 00:02:01,340
We could call fetch point to.
32
00:02:01,700 --> 00:02:04,220
Well, it will automatically send a get request.
33
00:02:04,220 --> 00:02:13,070
And indeed Stapleford we will get our, our data back and then we can add a then block will get a response
34
00:02:13,370 --> 00:02:17,590
and we can return response Jason to get access to the response body.
35
00:02:18,860 --> 00:02:23,990
And we can add another then blog, which is our response data, and yes, we also should handle errors.
36
00:02:24,020 --> 00:02:24,800
We'll do that later.
37
00:02:25,430 --> 00:02:29,630
So here we got our response data diskettes executed, Windass component renderers.
38
00:02:29,630 --> 00:02:31,610
And that is what we want, right?
39
00:02:31,610 --> 00:02:34,010
When diskettes rendered for the first time, we want to factis.
40
00:02:34,370 --> 00:02:37,850
So here we got our response data and response data.
41
00:02:37,850 --> 00:02:41,380
And Firebase Case will actually be an object, not an array.
42
00:02:41,390 --> 00:02:42,980
So we'll have transformed us.
43
00:02:43,890 --> 00:02:50,460
Our loaded ingredients, let's say, is an empty tray that's a helper constant here I'm creating then
44
00:02:50,460 --> 00:02:56,340
I use a foreign lube to go for all my keys in that response, STADA, because, again, response data
45
00:02:56,340 --> 00:02:57,210
will be an object.
46
00:02:57,690 --> 00:03:01,200
Every key will be and you can basically see that object here.
47
00:03:01,200 --> 00:03:02,970
Every key will be such a unique idea.
48
00:03:02,970 --> 00:03:06,230
And the value for that key will be the actual data for our ingredient.
49
00:03:06,600 --> 00:03:12,090
So we'll have that overall object with these ID keys here and then these nested objects.
50
00:03:12,570 --> 00:03:14,520
So your a key will be this unique ID.
51
00:03:15,720 --> 00:03:20,440
And disvalue for response data key will then be our concrete ingredient data.
52
00:03:20,820 --> 00:03:26,790
So therefore, here I can use loaded ingredients that array I'm creating here and push a new ingredient
53
00:03:26,790 --> 00:03:27,290
onto it.
54
00:03:27,570 --> 00:03:32,160
It's constant, but with Porche, we're not editing the value stored and loaded ingredients.
55
00:03:32,160 --> 00:03:35,220
We're editing the array stored in memory.
56
00:03:35,220 --> 00:03:41,070
And if that's not clear to you, attached to find a resource on JavaScript reference, reference and
57
00:03:41,070 --> 00:03:43,630
primitive types which have something to do with that.
58
00:03:44,220 --> 00:03:47,680
So here I push a new object onto this loaded ingredient array.
59
00:03:48,060 --> 00:03:58,500
The idea is that key and the title is response data for that key with this dynamic syntax here Daubs
60
00:03:58,500 --> 00:04:05,610
title and the amount is response data for that key dot amount.
61
00:04:06,480 --> 00:04:10,170
So this is how I load my ingredients, how I transformed this object to an array.
62
00:04:10,170 --> 00:04:17,250
And once this transformation is done, so after this for Loop here, we can of course call set user
63
00:04:17,250 --> 00:04:20,610
ingredients and set is equal to the loaded ingredients.
64
00:04:20,610 --> 00:04:25,500
So to Desir� we generated this approach has one huge issue.
65
00:04:26,340 --> 00:04:30,930
If you save this, you actually end up with an infinite loop.
66
00:04:30,930 --> 00:04:31,950
Here can see this.
67
00:04:31,950 --> 00:04:35,040
If you access to network tab, you're sending tons of requests.
68
00:04:35,490 --> 00:04:41,640
Therefore, I'll quickly comment out the set user ingredients line here so that this reloads and we
69
00:04:41,640 --> 00:04:42,570
stop sending this.
70
00:04:43,290 --> 00:04:46,800
So why are we entering an infinite loop here if we're fetching data like this?
71
00:04:47,820 --> 00:04:54,180
If you fetch data right here in your render function, then whenever ingredients renders you're sending
72
00:04:54,180 --> 00:05:02,130
HGP request and when you then as a result often is HDP request, update your state.
73
00:05:02,130 --> 00:05:03,870
What happens if you update your state?
74
00:05:04,420 --> 00:05:05,870
The component renders again.
75
00:05:05,880 --> 00:05:07,320
So ingredients renders again.
76
00:05:07,320 --> 00:05:08,280
And what does this mean?
77
00:05:08,520 --> 00:05:10,200
We send an outrage to dippie request.
78
00:05:10,200 --> 00:05:11,010
What does this mean?
79
00:05:11,310 --> 00:05:13,140
We update the state at some point.
80
00:05:13,170 --> 00:05:13,980
What does this mean?
81
00:05:14,280 --> 00:05:19,650
We render we send in a request, we update the state, we render and on.
82
00:05:19,650 --> 00:05:20,670
So that's the infinite loop.
83
00:05:20,790 --> 00:05:23,160
That's why sending this here is not an option.
84
00:05:23,760 --> 00:05:28,170
So let's cut this year and let's instead move it into disuse effect function here.
85
00:05:29,130 --> 00:05:34,890
Now the comment is back and now we're executing this after every Renner's cycle, but still by default
86
00:05:34,950 --> 00:05:39,180
for every renderers cycle and hence, unsurprisingly, for safeties.
87
00:05:39,180 --> 00:05:40,590
And we go to the network tab.
88
00:05:41,630 --> 00:05:47,780
We still have an infinite loop, so again, let's comment out this year and safeties to update our page
89
00:05:47,780 --> 00:05:48,620
and stop that loop.
90
00:05:49,400 --> 00:05:52,730
So how can we fix that was a fact.
91
00:05:52,970 --> 00:05:56,060
Actually, all that takes a second argument.
92
00:05:56,060 --> 00:05:59,980
The first argument is this function which executes after every render cycle.
93
00:06:00,410 --> 00:06:06,110
The second argument is an array with the dependencies of your function.
94
00:06:06,230 --> 00:06:11,720
And only when such a dependency changed, only then the function will rerun.
95
00:06:11,930 --> 00:06:17,680
So this allows you to control how often this function runs by default for every render cycle.
96
00:06:17,690 --> 00:06:19,810
But you can change this now.
97
00:06:19,820 --> 00:06:26,540
Here we actually have no external dependencies and with external dependencies, I mean variables or
98
00:06:26,540 --> 00:06:32,160
data you're using, which you define in your component outside of the use effect function.
99
00:06:32,780 --> 00:06:34,310
We're not using anything of that.
100
00:06:34,820 --> 00:06:40,940
Does you is hard coded response is a variable created here on the fly response data loaded ingredients.
101
00:06:41,060 --> 00:06:44,240
It's all created on the fly set are ingredients.
102
00:06:44,250 --> 00:06:48,890
That's actually a function stored in a variable coming from outside.
103
00:06:49,280 --> 00:06:55,190
But that's a special function since it's generated by use state react automatically creates that such
104
00:06:55,190 --> 00:07:00,590
that dysfunction never really changes and doesn't have to be considered as a dependency.
105
00:07:00,740 --> 00:07:07,580
So that's an exception because it is a function generated by use state and therefore dis use affect
106
00:07:07,580 --> 00:07:08,000
function.
107
00:07:08,000 --> 00:07:13,430
You're actually has no external dependencies and so we can add an empty array, but we have to do that
108
00:07:13,530 --> 00:07:14,000
permitting.
109
00:07:14,000 --> 00:07:17,060
It is not an option then this runs for every renderer cycle.
110
00:07:17,570 --> 00:07:24,080
If you do add an empty array, though, what happens is that this now runs when this component gets
111
00:07:24,080 --> 00:07:27,290
first rendered, so it runs this one time.
112
00:07:27,800 --> 00:07:33,160
It's not omitted entirely, but it runs one time, but it never runs thereafter.
113
00:07:33,500 --> 00:07:40,370
So with an empty array, it's like component did mount can kind of memorize that with an empty array.
114
00:07:40,370 --> 00:07:41,450
As a second argument.
115
00:07:41,600 --> 00:07:45,080
The function you past use a fact is like component did mount.
116
00:07:46,440 --> 00:07:47,610
So if we save this.
117
00:07:49,320 --> 00:07:51,060
Now, if we go to the network tap.
118
00:07:52,320 --> 00:07:58,020
We see no ongoing requests down there and we see our data was loaded, that we're safe again, safety's
119
00:07:58,020 --> 00:07:59,840
you see our data gets loaded.
120
00:08:00,660 --> 00:08:03,340
So this is now working with the help of U.S.
121
00:08:03,360 --> 00:08:03,810
Effect.
11942
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.