Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,300 --> 00:00:05,960
We can make React Memo work for prop values
2
00:00:05,960 --> 00:00:08,350
that are objects as well.
3
00:00:08,350 --> 00:00:10,660
We just need to tweak the way we create
4
00:00:10,660 --> 00:00:12,750
and store those objects a little bit.
5
00:00:12,750 --> 00:00:15,800
There is an extra hook provided by React
6
00:00:15,800 --> 00:00:17,010
that helps us with that.
7
00:00:17,010 --> 00:00:21,090
And that is the Use Callback Hook.
8
00:00:21,090 --> 00:00:24,660
Use Callback is a hook that allows us
9
00:00:24,660 --> 00:00:29,660
to basically store a function across component executions.
10
00:00:31,070 --> 00:00:35,150
So it allows us to tell React that we wanna save a function
11
00:00:35,150 --> 00:00:37,680
and that this function should not be recreated
12
00:00:37,680 --> 00:00:39,450
with every execution.
13
00:00:39,450 --> 00:00:42,600
With that one in the same function object is stored
14
00:00:42,600 --> 00:00:44,660
so one in the same place in memory
15
00:00:44,660 --> 00:00:47,830
and therefore the comparison does work.
16
00:00:47,830 --> 00:00:50,480
Because just to make this clear again as well,
17
00:00:50,480 --> 00:00:52,373
if you have object one,
18
00:00:53,990 --> 00:00:57,710
like this and you have object two, like this,
19
00:01:00,150 --> 00:01:01,760
which looks similar you'll learn
20
00:01:01,760 --> 00:01:05,230
that they aren't similar in JavaScript.
21
00:01:05,230 --> 00:01:09,920
If you however, said object two equal to object one
22
00:01:09,920 --> 00:01:12,700
you point at the exact same place in memory
23
00:01:12,700 --> 00:01:16,610
and therefore now object one will be equal to object two.
24
00:01:16,610 --> 00:01:19,380
And that's in the end what Use Callback does for us,
25
00:01:19,380 --> 00:01:22,620
it will save a function of our choice
26
00:01:22,620 --> 00:01:26,950
basically somewhere in React's internal storage
27
00:01:26,950 --> 00:01:30,050
and we'll always reuse that same function object
28
00:01:30,050 --> 00:01:33,240
then when this component function executes.
29
00:01:33,240 --> 00:01:34,800
And using it is simple.
30
00:01:34,800 --> 00:01:38,180
We just wrapped the function we wanna save with it.
31
00:01:38,180 --> 00:01:41,680
So here we use the Use Callback Hook
32
00:01:41,680 --> 00:01:46,340
and pass our function as a first argument to use callback
33
00:01:46,340 --> 00:01:50,130
and Use Callback then returns that stored function
34
00:01:50,130 --> 00:01:53,990
and when the app function reruns Use Callback will look for
35
00:01:53,990 --> 00:01:56,980
that stored function which React stored for us
36
00:01:56,980 --> 00:01:59,343
and reuse that same function object.
37
00:02:00,490 --> 00:02:03,923
So if we know that this function should never change,
38
00:02:05,200 --> 00:02:07,573
we can use Use Callback to store it.
39
00:02:08,979 --> 00:02:11,940
Now you see I got some yellow squiggly lines here though
40
00:02:11,940 --> 00:02:16,250
which tells me that I did probably forget to pass
41
00:02:16,250 --> 00:02:18,490
an array of dependencies.
42
00:02:18,490 --> 00:02:21,250
Use Callback like Use Effect actually wants
43
00:02:21,250 --> 00:02:24,430
a second argument and it wants it even more so
44
00:02:24,430 --> 00:02:27,320
than Use Effect and just like for Use Effect
45
00:02:27,320 --> 00:02:29,500
the second argument should be an array.
46
00:02:29,500 --> 00:02:33,650
An array of dependencies off this Use Callback call.
47
00:02:33,650 --> 00:02:36,690
And the dependencies here are the same as they are
48
00:02:36,690 --> 00:02:37,823
for Use Effect.
49
00:02:39,240 --> 00:02:42,170
Anything you use in your function
50
00:02:42,170 --> 00:02:44,180
which is coming from the surrounding component
51
00:02:44,180 --> 00:02:48,870
so any state or props or context should be specified here.
52
00:02:48,870 --> 00:02:51,520
Now here we only have to state updating function
53
00:02:51,520 --> 00:02:55,060
and we could add this as a dependency but we don't have to
54
00:02:55,060 --> 00:02:58,950
because as mentioned early in the course React guarantees us
55
00:02:58,950 --> 00:03:01,470
actually with help of Use Callback probably
56
00:03:01,470 --> 00:03:03,820
that this will never change so that this will always
57
00:03:03,820 --> 00:03:05,450
be the same function object.
58
00:03:05,450 --> 00:03:08,680
Hence this is an exception which we don't have to add.
59
00:03:08,680 --> 00:03:11,860
And then in this simple callback we'd have nothing else.
60
00:03:11,860 --> 00:03:14,570
Keep in mind that this code here is just a function
61
00:03:14,570 --> 00:03:16,590
we pass to set show paragraph.
62
00:03:16,590 --> 00:03:18,510
So that is totally contained inside
63
00:03:18,510 --> 00:03:20,540
of our callback function.
64
00:03:20,540 --> 00:03:22,900
So therefore here we have an empty array of dependencies
65
00:03:22,900 --> 00:03:26,620
which basically tells React that this callback function
66
00:03:26,620 --> 00:03:28,830
this function here which we wanna store it
67
00:03:28,830 --> 00:03:32,380
in toggle paragraph handler ultimately will never change
68
00:03:32,380 --> 00:03:34,060
that it has no dependencies
69
00:03:34,060 --> 00:03:36,960
and the data for always the same function object
70
00:03:36,960 --> 00:03:40,253
should be reused when the app component rerenders.
71
00:03:42,200 --> 00:03:47,170
Hence now if I saved that, and I reload,
72
00:03:47,170 --> 00:03:52,170
we see button running once but then you never see it again
73
00:03:52,530 --> 00:03:56,030
because now React Memo is able to do its job
74
00:03:56,030 --> 00:03:59,090
because we ensure that all the prop values
75
00:03:59,090 --> 00:04:02,800
we pass in are a comparable in this case
76
00:04:02,800 --> 00:04:05,660
because this toggle paragraph handler function object
77
00:04:05,660 --> 00:04:08,500
is guaranteed to always be the exact same object
78
00:04:08,500 --> 00:04:11,233
in memory because of Use Callback.
6419
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.