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:04,660
So, now, that we learned about useReducer
2
00:00:04,660 --> 00:00:08,160
let me, again, sum up when you should use useState
3
00:00:08,160 --> 00:00:10,763
and when it might be an idea to use useReducer.
4
00:00:12,130 --> 00:00:16,320
Now, generally, you will know when you need useReducer.
5
00:00:16,320 --> 00:00:19,780
That might not be the greatest rule of all times,
6
00:00:19,780 --> 00:00:22,560
but you will see when you need useReducer,
7
00:00:22,560 --> 00:00:26,840
for example, because using useState becomes too cumbersome,
8
00:00:26,840 --> 00:00:30,360
you find yourself dealing with a lot of kind
9
00:00:30,360 --> 00:00:33,156
of related state snapshots, which still are kind
10
00:00:33,156 --> 00:00:36,750
of independent and you start updating them together
11
00:00:36,750 --> 00:00:39,130
in a way that just doesn't work out.
12
00:00:39,130 --> 00:00:41,613
Then, you want to consider using useReducer.
13
00:00:43,050 --> 00:00:45,270
Now, to still give you a couple of ideas
14
00:00:45,270 --> 00:00:48,879
when to use useState versus useReducer,
15
00:00:48,879 --> 00:00:53,280
useState is your main state management tool.
16
00:00:53,280 --> 00:00:56,060
Typically, you start with useState
17
00:00:56,060 --> 00:00:58,570
and often it's all you need.
18
00:00:58,570 --> 00:01:02,420
It's great for independent pieces of state and data.
19
00:01:02,420 --> 00:01:05,280
It's great for simple state, you could say.
20
00:01:05,280 --> 00:01:08,140
It's great if state updates are easy
21
00:01:08,140 --> 00:01:11,870
and limited to a few kinds of updates.
22
00:01:11,870 --> 00:01:14,010
So, if you don't have a lot of different cases
23
00:01:14,010 --> 00:01:17,860
that will change a state and especially if you don't have,
24
00:01:17,860 --> 00:01:21,070
let's say, an object as a state or anything like that.
25
00:01:21,070 --> 00:01:23,750
Now, if you do have an object as a state
26
00:01:23,750 --> 00:01:27,110
or a more complex state useReducer might be interesting
27
00:01:27,110 --> 00:01:30,040
because, in general, useReducer is great
28
00:01:30,040 --> 00:01:33,570
if you need more power and with more power,
29
00:01:33,570 --> 00:01:37,020
I simply mean that you can write such a reducer function
30
00:01:37,020 --> 00:01:40,650
that can contain more complex state updating logic
31
00:01:40,650 --> 00:01:42,740
where you always are guaranteed to work
32
00:01:42,740 --> 00:01:44,850
with the latest state snapshot.
33
00:01:44,850 --> 00:01:48,990
And where you can move that potentially more complex logic
34
00:01:48,990 --> 00:01:52,170
out of your component function body
35
00:01:52,170 --> 00:01:54,700
into a separate reducer function.
36
00:01:54,700 --> 00:01:58,960
So, all the restructure code, that might be interesting.
37
00:01:58,960 --> 00:02:02,330
You should especially consider useReducer,
38
00:02:02,330 --> 00:02:05,420
if you're dealing with related data
39
00:02:05,420 --> 00:02:10,400
with state that is made up of related pieces of state.
40
00:02:10,400 --> 00:02:13,230
So when you, for example, have a scenario,
41
00:02:13,230 --> 00:02:16,310
as we had it with our form input state.
42
00:02:16,310 --> 00:02:18,850
And, in general, useReducer can be helpful
43
00:02:18,850 --> 00:02:21,130
if you have more complex state updates,
44
00:02:21,130 --> 00:02:24,600
if you have different cases, different actions
45
00:02:24,600 --> 00:02:27,740
that can change a state, cases like this.
46
00:02:27,740 --> 00:02:30,590
Now, there is no hard rule and there, as always
47
00:02:30,590 --> 00:02:33,720
in programming, is no clear wrong or right.
48
00:02:33,720 --> 00:02:36,980
You can certainly also handle cases
49
00:02:36,980 --> 00:02:40,810
where useReducer would be good with just useState,
50
00:02:40,810 --> 00:02:43,500
especially when combining that with useEffect.
51
00:02:43,500 --> 00:02:46,050
But sometimes using useReducer just might
52
00:02:46,050 --> 00:02:48,540
be more elegant, or simpler.
53
00:02:48,540 --> 00:02:50,460
On the other hand, you should absolutely
54
00:02:50,460 --> 00:02:55,160
not always useReducer because often that will be overkill.
55
00:02:55,160 --> 00:02:58,040
If you have just a simple state that just switches
56
00:02:58,040 --> 00:03:00,520
between two different values for example,
57
00:03:00,520 --> 00:03:03,260
using useReducer might be overkill.
58
00:03:03,260 --> 00:03:06,350
And that's it for useReducer.
59
00:03:06,350 --> 00:03:09,530
Now, you could absolutely also use useReducer
60
00:03:09,530 --> 00:03:13,160
to manage the form state as a whole with useReducer,
61
00:03:13,160 --> 00:03:16,290
instead of using useState for the form validity
62
00:03:16,290 --> 00:03:18,930
you could replace these two reducers
63
00:03:18,930 --> 00:03:21,940
and the form validity with one big reducer
64
00:03:21,940 --> 00:03:23,970
that manages the entire form,
65
00:03:23,970 --> 00:03:25,630
but that's something I will come back
66
00:03:25,630 --> 00:03:28,170
to later in the forms module.
67
00:03:28,170 --> 00:03:30,390
Since, for now, it's just important
68
00:03:30,390 --> 00:03:33,573
that you understand useReducer and when to use it.
5582
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.