Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,100 --> 00:00:07,500
In the end, this infinite loop here is caused by this onload ingredients dependency.
2
00:00:08,690 --> 00:00:14,840
This effect, which fetches all our data, will run whatever entered filter or onload ingredients changed
3
00:00:15,030 --> 00:00:17,900
that will be the case for the first time search has rendered.
4
00:00:18,110 --> 00:00:21,560
So it will definitely fetch our data whenever a search is rendered.
5
00:00:22,830 --> 00:00:27,630
But thereafter, it will rerun whatever entered filter changes, which we never changed because that
6
00:00:27,630 --> 00:00:33,000
would only be the case if you type here or if all those ingredients changed, which were extracting
7
00:00:33,000 --> 00:00:33,810
from our props.
8
00:00:34,010 --> 00:00:35,100
That is the case.
9
00:00:35,340 --> 00:00:41,700
The parent component is rendered, the parent component is rendered because in the end, the first time
10
00:00:41,700 --> 00:00:46,770
we load a data we were calling OnLoad Ingredients, which calls this filtered ingredients handler function.
11
00:00:47,070 --> 00:00:51,630
And in there we're calling set user ingredients, which changes to state, which we is this component,
12
00:00:52,140 --> 00:00:59,460
which means that here, of course, we're also creating a new filter ingredients handler function because
13
00:00:59,460 --> 00:01:01,130
this entire component reruns.
14
00:01:01,140 --> 00:01:03,170
And in the end, this is just a JavaScript function.
15
00:01:03,180 --> 00:01:03,380
Right.
16
00:01:03,410 --> 00:01:07,830
So even these functions you have in there, they still rerun.
17
00:01:07,830 --> 00:01:09,450
They're still executed again.
18
00:01:09,930 --> 00:01:13,290
In the end, this is just a variable which now receives a new value.
19
00:01:13,470 --> 00:01:18,300
Of course, we know it has the same logic as before, but technically it's a new function object in
20
00:01:18,300 --> 00:01:18,990
JavaScript.
21
00:01:19,260 --> 00:01:21,540
So it's a new object we pass that to.
22
00:01:22,560 --> 00:01:27,510
All those ingredients, and since it's a new object inside of the search, shares fell.
23
00:01:28,430 --> 00:01:34,460
This is different to the last time do fact executed and therefore you will execute the effect because
24
00:01:34,460 --> 00:01:40,220
we rewriter the parent, all these functions are created new and well, we got a different value.
25
00:01:40,220 --> 00:01:42,290
We rerun the effect in the search component.
26
00:01:43,310 --> 00:01:50,280
To prevent this from happening, we can use another never hook provided by REAC, that's the use callback
27
00:01:51,200 --> 00:01:56,990
to use callback hook allows you to wrap one of your functions with it.
28
00:01:57,470 --> 00:02:03,110
Let's say dysfunctions on the right side of the equals sign you use use callback and simply wrap this.
29
00:02:04,040 --> 00:02:10,639
Around your function definition like this, and now you also pass a second argument to use callback,
30
00:02:10,880 --> 00:02:12,350
first argument is your function.
31
00:02:12,350 --> 00:02:15,920
Second argument, awls is an array with the dependencies of dysfunction.
32
00:02:16,070 --> 00:02:21,620
Dysfunction has no dependencies because set user ingredients est�s special dependency since it's the
33
00:02:21,620 --> 00:02:25,220
end return by use state astiz updating function by you state.
34
00:02:25,490 --> 00:02:30,920
So that special and Abbadon that we have no dependencies here so we can specify an empty array here.
35
00:02:31,070 --> 00:02:34,550
By the way, you could have all the past set or ingredients as a dependency here.
36
00:02:34,550 --> 00:02:37,100
It still wouldn't rerun because this never changes.
37
00:02:37,110 --> 00:02:38,690
That's guaranteed by react.
38
00:02:38,780 --> 00:02:40,190
That's why you can't commit it here.
39
00:02:40,220 --> 00:02:44,300
You can also specify it, but this will now never rerun.
40
00:02:44,300 --> 00:02:51,050
And therefore, what really does is it cashes your function for you so that it survives Birendra cycles
41
00:02:51,350 --> 00:02:57,020
and therefore Wendi's ingredients, componentry renders this specific function is not really created,
42
00:02:57,020 --> 00:02:58,450
so it will not change.
43
00:02:58,850 --> 00:03:05,090
So what we pass to the search component to onload ingredients will be the old function, the same function
44
00:03:05,090 --> 00:03:11,210
as on the previous render cycle and therefore in the search component on the use effect here on this
45
00:03:11,210 --> 00:03:18,120
check, all those ingredients will not have changed and therefore does effect will not rerun.
46
00:03:18,740 --> 00:03:20,930
So now let's comment on loading greens back in.
47
00:03:20,930 --> 00:03:26,540
And if we now Safet, as we see no infinite loop, still one extra renderers cycle to which I'll come
48
00:03:26,540 --> 00:03:27,410
back in a second.
49
00:03:27,830 --> 00:03:32,300
But if we have a look at the network tab, no extra requests and only if I type here.
50
00:03:32,330 --> 00:03:35,180
We're sending extra requests with every keystroke we see.
51
00:03:35,180 --> 00:03:37,910
Filtering is also working, for example, for apples.
52
00:03:37,910 --> 00:03:42,080
We only see that if I enter something which makes no sense, we see nothing for bananas.
53
00:03:42,080 --> 00:03:44,300
We see bananas that's working.
54
00:03:44,300 --> 00:03:47,480
We're sending the request with every keystroke, which I might not be ideal.
55
00:03:47,630 --> 00:03:52,460
And we have an extra renderer cycle initially, so we should probably fix that.
56
00:03:52,790 --> 00:03:55,070
But we certainly have no infinite loop anymore.
57
00:03:56,720 --> 00:04:02,410
Before we ensure that we don't send the request with every keystroke, let's ensure we don't have this
58
00:04:02,450 --> 00:04:08,180
extra cycle initially, and this is actually coming from a redundant HTP request we're sending.
59
00:04:08,430 --> 00:04:14,170
If I open by developer tools and they're the network tap for reload, get a bunch of requests.
60
00:04:14,180 --> 00:04:18,110
But in the end, you see there are two requests fetching our ingredients, these two requests.
61
00:04:19,100 --> 00:04:21,380
They're both sent to ingredients, Jason Firebrace.
62
00:04:22,250 --> 00:04:24,650
Now, why do we have two requests being sent there?
63
00:04:25,220 --> 00:04:30,710
Well, because we send one in surcharges here and use a fact when this component gets rendered, this
64
00:04:30,710 --> 00:04:31,670
effect executes.
65
00:04:31,700 --> 00:04:32,750
That's how effects work.
66
00:04:32,750 --> 00:04:37,280
They execute when this component gets rendered, because all these dependencies, of course, have changed
67
00:04:37,280 --> 00:04:41,990
compared to the previous render cycle because there hasn't been a previous render cycle.
68
00:04:42,350 --> 00:04:47,960
So this gets recalled and dear, of course, entered filter is empty initially, so we have no filter
69
00:04:47,960 --> 00:04:48,320
set.
70
00:04:48,470 --> 00:04:53,750
Hence we fetch all data and then we call onload ingredients to, well, use these ingredients.
71
00:04:54,050 --> 00:04:59,330
And in addition to that, for all the fetching data in the ingredients component there, we also have
72
00:04:59,330 --> 00:05:02,510
use effect, which we only execute once when the component loads.
73
00:05:02,510 --> 00:05:05,420
But we do that and there we all the fetch ingredients.
74
00:05:05,780 --> 00:05:11,960
Now, since we already are guaranteed to fetch ingredients because we're doing that in search, we of
75
00:05:11,960 --> 00:05:17,900
course don't need that extra use effect thing here in the ingredients component so we can get rid of
76
00:05:17,900 --> 00:05:21,350
debt because we are fetching ingredients with search component.
77
00:05:21,650 --> 00:05:23,570
So with that, we're still loading it.
78
00:05:23,570 --> 00:05:25,910
But now we got rid of the extra render cycle.
8445
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.