Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,180 --> 00:00:05,100
So, in the backward counter component
2
00:00:05,100 --> 00:00:08,570
we got the same logic as in the forward counter,
3
00:00:08,570 --> 00:00:12,473
but we are dedacting, here, instead of adding.
4
00:00:13,500 --> 00:00:16,200
Nonetheless, we can use our custom hook here,
5
00:00:16,200 --> 00:00:17,940
we just need to do something
6
00:00:17,940 --> 00:00:21,880
which we often do with functions that we write.
7
00:00:21,880 --> 00:00:25,790
We make them reusable and configurable,
8
00:00:25,790 --> 00:00:30,460
you could say, by accepting arguments, parameters.
9
00:00:30,460 --> 00:00:33,170
Just as our components receive props
10
00:00:33,170 --> 00:00:37,120
and just as any function in JavaScript,
11
00:00:37,120 --> 00:00:40,920
or programming in general, can accept parameters,
12
00:00:40,920 --> 00:00:44,190
custom hooks, since they are just functions
13
00:00:44,190 --> 00:00:47,510
can also accept parameters.
14
00:00:47,510 --> 00:00:51,370
And for example, the built in useState hook
15
00:00:51,370 --> 00:00:55,010
also accepts a parameter where we set the initial state.
16
00:00:55,010 --> 00:00:59,360
The built in useEffect hook even accepts two parameters
17
00:00:59,360 --> 00:01:02,870
where we have our EFFECT function as a first argument,
18
00:01:02,870 --> 00:01:06,960
and our dependencies array as a second argument.
19
00:01:06,960 --> 00:01:11,700
So, that is something which we do already, implicitly,
20
00:01:11,700 --> 00:01:14,373
and which we can now also do for our custom hook.
21
00:01:15,280 --> 00:01:18,300
So, which parameter could we accept here?
22
00:01:18,300 --> 00:01:22,240
Well, in the end we want some indicator that controls
23
00:01:22,240 --> 00:01:25,420
how the counter should be incremented.
24
00:01:25,420 --> 00:01:28,670
We could accept this entire updating function, here,
25
00:01:28,670 --> 00:01:30,620
to make this re-flexible.
26
00:01:30,620 --> 00:01:34,470
So, here, we could have our counterUpdate function
27
00:01:34,470 --> 00:01:38,180
as a parameter and then in our custom hook
28
00:01:38,180 --> 00:01:41,530
just execute counterUpdate function, like this,
29
00:01:41,530 --> 00:01:44,850
and expect that the argument we get here
30
00:01:44,850 --> 00:01:47,570
is then a function that can be executed
31
00:01:47,570 --> 00:01:50,832
which gets the previous counter and accepts the new counter.
32
00:01:50,832 --> 00:01:55,832
Or, we don't make our custom hook that flexible
33
00:01:56,210 --> 00:02:01,210
but instead we accept some flag, some Boolean flag, here,
34
00:02:01,300 --> 00:02:05,270
which controls whether that should be a plus or a minus.
35
00:02:05,270 --> 00:02:06,750
So we could, for the example,
36
00:02:06,750 --> 00:02:11,750
accept forwards parameter, the name is up to you,
37
00:02:11,850 --> 00:02:14,290
which is true if we wanna add
38
00:02:14,290 --> 00:02:16,600
and false if we wanna subtract.
39
00:02:16,600 --> 00:02:19,800
And we could even set a default value
40
00:02:19,800 --> 00:02:23,080
so that this parameter is optional.
41
00:02:23,080 --> 00:02:26,580
And then here, inside set interval we just check
42
00:02:26,580 --> 00:02:30,710
if (forwards) if that is true
43
00:02:30,710 --> 00:02:33,360
then we update setCounter, like this,
44
00:02:33,360 --> 00:02:37,250
else, if it's not true we update setCounter
45
00:02:37,250 --> 00:02:40,120
by well, basically, just having
46
00:02:40,120 --> 00:02:42,347
the opposite logic of dedacting.
47
00:02:43,590 --> 00:02:45,510
And that's the approach I will use here
48
00:02:45,510 --> 00:02:48,233
but you could also accept the entire function.
49
00:02:49,230 --> 00:02:52,460
Either way, we now have a new dependency here
50
00:02:52,460 --> 00:02:55,020
in this effect in our custom hook
51
00:02:55,020 --> 00:02:58,770
because the parameter which I'm using in here
52
00:02:58,770 --> 00:03:00,380
that is a dependency.
53
00:03:00,380 --> 00:03:04,060
It's not defined inside of the EFFECT function,
54
00:03:04,060 --> 00:03:07,950
it's not defined outside of our custom hook
55
00:03:07,950 --> 00:03:11,930
instead, it is a value that we get as a parameter, here,
56
00:03:11,930 --> 00:03:14,900
and we have to add it as a dependency, therefore.
57
00:03:14,900 --> 00:03:18,730
So, we should add forwards as a dependency
58
00:03:18,730 --> 00:03:21,030
so that this EFFECT will rerun
59
00:03:21,030 --> 00:03:23,810
whenever this dependency changes.
60
00:03:23,810 --> 00:03:28,100
Of course, depending on the code we write in our components
61
00:03:28,100 --> 00:03:30,590
where we use the custom hook later,
62
00:03:30,590 --> 00:03:33,680
this might never change, it might always be true or false
63
00:03:33,680 --> 00:03:36,530
and not change for one at the same component.
64
00:03:36,530 --> 00:03:38,420
But that's fine, if it never changes
65
00:03:38,420 --> 00:03:40,080
this simply never runs again.
66
00:03:40,080 --> 00:03:42,940
We still have to add it as a dependency
67
00:03:42,940 --> 00:03:45,310
to follow all best practices
68
00:03:45,310 --> 00:03:48,410
and to ensure that if we would have a component
69
00:03:48,410 --> 00:03:52,110
where useCounter is used with different values
70
00:03:52,110 --> 00:03:55,170
for the argument, this EFFECT would rerun
71
00:03:55,170 --> 00:03:57,140
if the argument changes.
72
00:03:57,140 --> 00:03:59,140
And that makes a lot of sense, by the way,
73
00:03:59,140 --> 00:04:01,000
because if we had a component
74
00:04:01,000 --> 00:04:04,740
where the counter can switch between forwards and backwards
75
00:04:04,740 --> 00:04:08,940
we, of course, would want to restart the counter
76
00:04:08,940 --> 00:04:12,560
with the new logic if the value changed.
77
00:04:12,560 --> 00:04:16,029
But that's just some site theory, here, for the moment
78
00:04:16,029 --> 00:04:17,630
it doesn't matter too much.
79
00:04:17,630 --> 00:04:20,200
We can just go to the backward counter
80
00:04:20,200 --> 00:04:23,493
and import useCounter there as well.
81
00:04:25,160 --> 00:04:28,180
By going to the hooks folder and importing it
82
00:04:28,180 --> 00:04:30,670
and then in there, in the backward counter
83
00:04:30,670 --> 00:04:35,670
we call useCounter and we pass false as our argument, here,
84
00:04:36,580 --> 00:04:38,870
for our forwards parameter
85
00:04:38,870 --> 00:04:41,380
because here we don't wanna go forwards
86
00:04:41,380 --> 00:04:42,853
we gonna go backwards.
87
00:04:43,820 --> 00:04:47,730
We didn't need an argument in the ForwardCounter,
88
00:04:47,730 --> 00:04:49,360
there we can call like this
89
00:04:49,360 --> 00:04:52,850
because we got a default value in the custom hook.
90
00:04:52,850 --> 00:04:54,410
But, if we want a different value
91
00:04:54,410 --> 00:04:57,530
then that default value we have to pass it.
92
00:04:57,530 --> 00:05:00,670
Well, and with that, we can again extract our counter
93
00:05:00,670 --> 00:05:04,720
by using that return value and then get rid of all that
94
00:05:04,720 --> 00:05:09,440
other logic in the backward counter component, like this.
95
00:05:09,440 --> 00:05:14,440
And if we now save that and we reload our application
96
00:05:15,120 --> 00:05:17,680
we still have both counters working
97
00:05:17,680 --> 00:05:20,550
but now with our custom hook.
98
00:05:20,550 --> 00:05:23,610
And, of course, this was a made up example, here,
99
00:05:23,610 --> 00:05:27,150
and, of course, besides the example on its own
100
00:05:27,150 --> 00:05:30,220
and not being too realistic, you could definitely
101
00:05:30,220 --> 00:05:33,223
also find more ways of reusing logic, here,
102
00:05:33,223 --> 00:05:35,220
and working with one component
103
00:05:35,220 --> 00:05:37,500
instead of two counter components,
104
00:05:37,500 --> 00:05:42,480
but this was just there to introduce you to custom hooks.
105
00:05:42,480 --> 00:05:46,330
Now, let's move on to a more realistic scenario
106
00:05:46,330 --> 00:05:48,943
where custom hooks can really shine.
8598
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.