Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,240 --> 00:00:09,500
Now, if you had a look at so many different hooks, let's take a step back or actually stay in the
2
00:00:09,500 --> 00:00:17,810
Hooke's world, but see how we can optimize our project, how we can avoid unnecessary rewriter cycles,
3
00:00:17,810 --> 00:00:21,800
because that's, of course, something we might all be interested in doing to ensure that we have an
4
00:00:21,800 --> 00:00:23,510
app that performs well.
5
00:00:24,330 --> 00:00:26,120
For that, let's have a look at the ingredients.
6
00:00:26,120 --> 00:00:27,230
Jaspal, there.
7
00:00:27,230 --> 00:00:28,790
We're managing a bunch of logic.
8
00:00:28,790 --> 00:00:30,320
We have to reducers in there.
9
00:00:30,590 --> 00:00:35,300
We're changing the loading state, for example, and we update the UI in this case.
10
00:00:35,780 --> 00:00:41,360
And one thing I can already see there is that, for example, I'm passing the add ingredient handler
11
00:00:41,360 --> 00:00:47,130
to the ingredient form and add ingredient handler in the end is just function.
12
00:00:47,420 --> 00:00:52,970
This means that since it's a function defined inside of this functional component, whenever a dysfunctional
13
00:00:52,970 --> 00:00:59,460
component rebuilds and effort is whole function gets executed again, dysfunction will be recreated.
14
00:00:59,810 --> 00:01:04,680
As a side note here, by the way, use producer and so on will not be recreated.
15
00:01:04,700 --> 00:01:06,620
Of course, this is called again, but react.
16
00:01:06,620 --> 00:01:12,630
The text at this reducer is already initialized for this component and uses that already initialized
17
00:01:12,660 --> 00:01:13,000
value.
18
00:01:13,010 --> 00:01:14,120
So that's no problem.
19
00:01:14,660 --> 00:01:18,560
But here for add ingredient Hanlon's on, we're creating a brand new function.
20
00:01:18,560 --> 00:01:23,660
This brand new function is still for passed down to the ingredient form and hence in the ingredient
21
00:01:23,660 --> 00:01:24,110
form.
22
00:01:24,110 --> 00:01:32,030
Even though I'm using Memmo dismal, rebuilt and I can prove this to you can throw in a console rendering
23
00:01:32,660 --> 00:01:34,670
ingredient form.
24
00:01:35,420 --> 00:01:40,460
And theoretically that should only be rendered whenever we type in there because these are the only
25
00:01:40,460 --> 00:01:41,720
things that change the form.
26
00:01:41,720 --> 00:01:43,310
And of course, when we initially loaded.
27
00:01:44,150 --> 00:01:50,060
But what you will see is that, for example, if I if I add something, test five, let's clear the
28
00:01:50,060 --> 00:01:52,100
lock it add ingredient.
29
00:01:52,490 --> 00:01:57,560
You see it's rearranged a bunch and that makes sense because it's re rendered when we change the loading
30
00:01:57,560 --> 00:01:57,950
state.
31
00:01:58,250 --> 00:02:01,670
Well actually that makes sense because we're outputting the loading indicator here.
32
00:02:02,210 --> 00:02:08,060
But it's also rendered once the data is the area where our putting the data down there and this is not
33
00:02:08,060 --> 00:02:14,120
what we want, there is no reason to render this forum if we're just outputting some data down there.
34
00:02:14,690 --> 00:02:16,400
So that should be one renderer cycle.
35
00:02:16,400 --> 00:02:16,880
Too much.
36
00:02:16,880 --> 00:02:21,920
We have here to make sense because we switch loading from faults to true and then back from true to
37
00:02:21,920 --> 00:02:22,430
false ones.
38
00:02:22,430 --> 00:02:22,820
We're done.
39
00:02:22,970 --> 00:02:26,420
So we definitely will render twice because the loading spinner is in the form.
40
00:02:26,420 --> 00:02:28,790
But the third output here makes no sense.
41
00:02:29,480 --> 00:02:32,600
We can get rid of debt by wrapping.
42
00:02:33,690 --> 00:02:39,870
Adding Raegan handler with use callback, which we already saw before, and for that you need to import
43
00:02:39,870 --> 00:02:41,610
use callback from react of course.
44
00:02:43,000 --> 00:02:45,670
And then you wrap your entire handler function with that.
45
00:02:46,740 --> 00:02:53,010
And then you add square brackets where you defined the dependencies of this function, the dependency
46
00:02:53,280 --> 00:02:59,250
dispatch HTP, which you don't need to specify because just like the updated function from use state
47
00:02:59,520 --> 00:03:03,860
does is guaranteed by react to not change between render cycles.
48
00:03:03,870 --> 00:03:08,930
So you could added, but you don't have to ingredient is a local argument.
49
00:03:08,940 --> 00:03:11,180
We're getting into function, not an external one.
50
00:03:11,190 --> 00:03:12,600
So nothing we need to do there.
51
00:03:13,280 --> 00:03:18,750
Therefore, overall, there's nothing in here on which this would depend.
52
00:03:18,780 --> 00:03:21,180
So this function actually should never be rebuilt.
53
00:03:21,180 --> 00:03:22,610
It's always the same function.
54
00:03:22,710 --> 00:03:29,070
It has no external dependencies other than dispatch HTP, which, as I said, is managed by react such
55
00:03:29,070 --> 00:03:30,110
that it won't change.
56
00:03:30,120 --> 00:03:31,740
So it's not really a dependency.
57
00:03:32,930 --> 00:03:33,890
If we do that.
58
00:03:35,540 --> 00:03:37,280
Then if we log in here.
59
00:03:38,310 --> 00:03:41,010
And I add another ingredient here.
60
00:03:43,180 --> 00:03:50,890
We only see two render cycles, the third one is gone because dysfunction add ingredient handler is
61
00:03:50,890 --> 00:03:52,000
now not rebuilt.
62
00:03:52,480 --> 00:03:55,270
We're passing this function into ingredient form.
63
00:03:55,270 --> 00:04:01,420
But since it doesn't change between render cycles react, Memmo detects that the new function are getting
64
00:04:01,990 --> 00:04:07,080
when the parent component rebuilds is the old function and therefore doesn't rebuild this component.
65
00:04:07,930 --> 00:04:08,890
That's how it works.
66
00:04:09,250 --> 00:04:13,990
Loading when it changes, of course, breaks through that as it should, and triggers a rebuild on the
67
00:04:13,990 --> 00:04:18,880
ingredient form if it changes, if loading would not change, if we had faults before and faults for
68
00:04:18,880 --> 00:04:22,000
the next render cycle as well, this would not be rebuilt.
69
00:04:22,250 --> 00:04:25,150
So now we're taking advantage of react Memmo here.
70
00:04:25,990 --> 00:04:27,550
The same for the ingredient list.
71
00:04:28,000 --> 00:04:33,610
When we change the ingredients, this should rebuilt, but it will now all the rebuild all the time
72
00:04:33,610 --> 00:04:39,490
because we pass in the remove ingredient handler and that's all not wrapped by use callback.
73
00:04:39,940 --> 00:04:45,690
The result of that is that the ingredient list will always rebuild too often if we add a Corn Solok
74
00:04:45,730 --> 00:04:46,600
statement in there.
75
00:04:48,740 --> 00:04:53,120
Rendering ingredient list, something like this.
76
00:04:54,090 --> 00:04:58,610
Then you'll see, of course, loads initially a couple of times because of the loading stage change
77
00:04:58,770 --> 00:05:04,440
it loads initially and then loads once the data's there, but you will all to see that all those in
78
00:05:04,440 --> 00:05:05,500
cases where it shouldn't.
79
00:05:05,790 --> 00:05:09,500
For example, if you click on Test here, it renders way too often.
80
00:05:09,510 --> 00:05:14,250
Of course, it should render once the data is gone, but it should not render just because we're showing
81
00:05:14,250 --> 00:05:15,390
a loading spinner up there.
82
00:05:15,390 --> 00:05:17,040
And that is the first thing we're doing.
83
00:05:17,910 --> 00:05:20,310
So same logic as before.
84
00:05:20,580 --> 00:05:24,710
The dependencies of ingredient list are on remove item.
85
00:05:25,290 --> 00:05:27,300
Please note, I'm not using Rick Memmo here.
86
00:05:27,570 --> 00:05:30,630
So the following change will not alone have a result.
87
00:05:31,290 --> 00:05:32,550
But still it's a first step.
88
00:05:32,730 --> 00:05:33,960
Remove ingredient handler.
89
00:05:34,350 --> 00:05:41,880
We use use callback here and wrap it, wrap this entire function with it and then specify the dependencies.
90
00:05:42,330 --> 00:05:47,400
Dependencies are dispatch HTP, which as mentioned, doesn't have to be specified.
91
00:05:47,640 --> 00:05:51,570
Ingredient ID is an internal value, not an external one.
92
00:05:52,410 --> 00:05:54,090
Nothing else in there which matters.
93
00:05:54,090 --> 00:05:55,090
So it's an empty array.
94
00:05:55,410 --> 00:05:57,690
Now, again, as mentioned, this alone won't make a difference.
95
00:05:58,110 --> 00:06:04,080
If I delete test here, we still see a bunch of ingredient lists re renderings.
96
00:06:05,010 --> 00:06:11,300
Now, it makes sense because an ingredient list I'm not using REAC memo, so of course, we can do this.
97
00:06:11,310 --> 00:06:15,780
We can use react memo here, wrap this entire component with it like this.
98
00:06:16,320 --> 00:06:20,670
And if we do so, we log in, cleared this and I click bananas.
99
00:06:21,330 --> 00:06:22,710
We only render it once.
100
00:06:22,920 --> 00:06:25,260
So we're having the effect we want.
101
00:06:25,260 --> 00:06:29,760
We're only rendering this when this changes, not when something totally different changes.
102
00:06:30,540 --> 00:06:31,980
And there's nothing wrong with doing this.
103
00:06:31,980 --> 00:06:34,040
And actually does this new approach you'll typically use.
104
00:06:34,290 --> 00:06:39,180
Still, I want to show you an alternative, because there always is a never hook you can use.
105
00:06:39,510 --> 00:06:41,970
And that's the use memo.
106
00:06:41,980 --> 00:06:49,200
Hook use callback was a hook to save a function that doesn't change so that no new function is generated.
107
00:06:49,650 --> 00:06:55,620
Use, Memmo, is a hook where you can save a value which has saved so that the value isn't recreated.
108
00:06:56,280 --> 00:07:01,040
And that is a no way of memorizing component.
109
00:07:01,560 --> 00:07:04,410
So here that would be our ingredient list.
110
00:07:04,860 --> 00:07:07,530
Let me cut it from there and add it here.
111
00:07:08,470 --> 00:07:10,660
Ingredient list, maybe like this.
112
00:07:11,800 --> 00:07:18,610
And now I use you as Memmo here, pass a function to it, this is not a function that's now memorized,
113
00:07:18,610 --> 00:07:18,900
though.
114
00:07:18,910 --> 00:07:24,040
Instead it's a function that react will execute for you and that function should return to value, which
115
00:07:24,040 --> 00:07:24,940
you want to memorize.
116
00:07:25,060 --> 00:07:26,090
Ingredient list here.
117
00:07:26,980 --> 00:07:32,320
Now use Memmo also needs a second argument, which is a list of dependencies you have dishtowels react
118
00:07:32,320 --> 00:07:36,730
when it should rerun this function to create a new object that it should memorize.
119
00:07:37,130 --> 00:07:41,040
And we know it should rerun this function whenever the user ingredients change.
120
00:07:41,410 --> 00:07:46,420
So let's add this here or whenever removing Creedon handler changes.
121
00:07:46,420 --> 00:07:49,870
And that shouldn't change because we wrapped that with use callback.
122
00:07:50,290 --> 00:07:56,260
So only changes to user ingredients are really rerun dysfunction and rebuild a new ingredient list.
123
00:07:56,680 --> 00:07:59,290
And now ingredient list is, of course, something we can output.
124
00:08:00,330 --> 00:08:05,550
Down there, ingredient lists like this, we save this and we log in.
125
00:08:06,480 --> 00:08:10,530
Ingredients are getting rendered now let's add bananas here.
126
00:08:11,750 --> 00:08:12,260
Ten.
127
00:08:13,390 --> 00:08:20,560
ATIS, we updated the ingredient list once, if I delete them, we updated it once, it's an alternative
128
00:08:20,560 --> 00:08:21,550
to react memo.
129
00:08:21,670 --> 00:08:27,640
Now if we're talking about storing components, you typically want to use react memo instead of use
130
00:08:27,640 --> 00:08:28,020
memo.
131
00:08:28,300 --> 00:08:30,010
But keep in mind that we use memo.
132
00:08:30,130 --> 00:08:36,110
You can store any data which you don't want to recreate on every Virender cycle of this component.
133
00:08:36,400 --> 00:08:41,890
So if you have some operation that calculates a more complex value, calculates a value which maybe
134
00:08:41,890 --> 00:08:47,980
takes a bit longer to calculate than you want to consider wrapping it with use memo so that it's not
135
00:08:47,980 --> 00:08:54,540
recalculated whenever the component renders, but really only recalculated when you need to recalculated.
136
00:08:54,730 --> 00:08:55,820
That's the idea here.
137
00:08:56,530 --> 00:09:00,160
Now, last optimization I want to apply is here on the error model.
138
00:09:00,190 --> 00:09:03,120
On close we call clear error dysfunction.
139
00:09:03,370 --> 00:09:09,210
And there again we can use use callback to make sure that the error model isn't be rendered unnecessarily.
140
00:09:09,580 --> 00:09:12,630
We have no external dependencies here, so it's as simple as that.
141
00:09:12,820 --> 00:09:18,400
And now we also make sure that the error model which already uses react memo isn't re rendered in cases
142
00:09:18,400 --> 00:09:19,030
where it shouldn't.
143
00:09:19,480 --> 00:09:24,100
So these are some optimizations you can apply and you should think about now.
144
00:09:24,100 --> 00:09:29,680
It's not the end of the world if you forget one, because re rendering sounds like a super intensive
145
00:09:29,680 --> 00:09:29,950
work.
146
00:09:29,950 --> 00:09:33,370
If it's a small component, the easy update might be super fast.
147
00:09:33,370 --> 00:09:37,540
To render and render always means re rendered in the virtual dom.
148
00:09:37,540 --> 00:09:37,870
Right.
149
00:09:37,880 --> 00:09:42,640
You learned that earlier and of course does not mean that whatever change is automatically written to
150
00:09:42,640 --> 00:09:43,420
the real dom.
151
00:09:43,540 --> 00:09:48,940
But even that virtual dom re rendering can be avoided with use called they use Memmo and react Memmo.
152
00:09:49,300 --> 00:09:54,610
But if you have very trivial components, it might even be worth not adding Riak memo.
153
00:09:54,610 --> 00:10:00,700
So you shouldn't wrap every component with Riak memo with use memo because reaction always needs to
154
00:10:00,700 --> 00:10:02,230
check whatever props changed.
155
00:10:02,230 --> 00:10:07,830
And if it's a super small component re rendering, it might just be faster than performing that check.
156
00:10:08,080 --> 00:10:11,020
So it's always something you have to evaluate and think about.
157
00:10:11,140 --> 00:10:17,320
For example, for the error model we could probably get away with always re rendering that and not using
158
00:10:17,320 --> 00:10:17,880
react memo.
159
00:10:18,070 --> 00:10:23,290
So these are examples and these are things you just have to play around with test and also check how
160
00:10:23,290 --> 00:10:28,120
fast your application is to get a feeling for this where it makes sense and where it doesn't make sense.
16130
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.