Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,370 --> 00:00:08,940
Now, here, I added use reducer instead of use state, not because we replaced so many state things
2
00:00:08,940 --> 00:00:14,640
with it, actually we only replace this one user ingredients state, but because we have multiple ways
3
00:00:14,640 --> 00:00:18,030
of changing that state and some of these ways depend on the previous state.
4
00:00:18,390 --> 00:00:23,490
And whilst the previous approach did, of course, all that worked fine using a producer oftenest cleaner
5
00:00:23,610 --> 00:00:29,490
because you have all your updating logic in one place in the producer, and then you just dispatch these
6
00:00:29,490 --> 00:00:34,720
actions, which is a very clear and easy to understand flow for how you manage your data.
7
00:00:35,160 --> 00:00:40,440
So whenever you have more complex states that can change in different ways instead of just, let's say,
8
00:00:40,560 --> 00:00:46,170
setting a boolean from true to false, but like here where we can add ingredients or remove ingredients,
9
00:00:46,200 --> 00:00:51,900
whenever you have such a state that is a bit more complex and that might also depend on previous state,
10
00:00:52,260 --> 00:00:59,190
then you should strongly consider using a producer to have a cleaner logic to have all in one place,
11
00:00:59,190 --> 00:00:59,940
basically.
12
00:01:00,210 --> 00:01:04,170
And ultimately, it's of course, up to your personal preference, though.
13
00:01:04,920 --> 00:01:10,500
Another scenario where using use reducer could make sense, as if you have multiple connected states
14
00:01:10,500 --> 00:01:12,360
like here is loading an error.
15
00:01:12,690 --> 00:01:18,510
They're both related to ascending HTTP requests, and whilst we managed them independently, they're
16
00:01:18,510 --> 00:01:19,960
kind of related.
17
00:01:20,460 --> 00:01:26,430
Now you can of course, manage it like this, but also here often a her makes her code easier to reason
18
00:01:26,430 --> 00:01:28,140
about, easier to understand.
19
00:01:29,000 --> 00:01:32,910
So I'll add an average user again outside of my component.
20
00:01:33,090 --> 00:01:34,380
Doesn't have to be all it said.
21
00:01:34,380 --> 00:01:39,210
You could move it in there, for example, if you use props and your reducer, but if you don't need
22
00:01:39,210 --> 00:01:44,280
to put it outside of the component to avoid unnecessary recreations of the user.
23
00:01:45,330 --> 00:01:54,960
So here I'll named is H to the P to reduce her, maybe again get my HTP state and my action.
24
00:01:55,740 --> 00:01:59,070
And just as before, you can name these arguments whatever you want, of course.
25
00:01:59,070 --> 00:02:02,250
But this will be your old state basically, and that's the action you're getting.
26
00:02:02,610 --> 00:02:08,430
And just as before, we can switch the action type because there will be different actions, we'll dispatch.
27
00:02:08,729 --> 00:02:14,100
And now let's think about which actions might we have when we work with TDP requests.
28
00:02:15,260 --> 00:02:20,990
Well, if we have a look at our code, then we always start by sending the request right, we start
29
00:02:20,990 --> 00:02:22,600
off by sending a request.
30
00:02:22,610 --> 00:02:24,950
So sending a request probably is one action.
31
00:02:25,930 --> 00:02:33,370
And action we then have is when the response is there and then case or when we have an error in this
32
00:02:33,760 --> 00:02:38,740
case, so we have three possible states here that makes sense.
33
00:02:38,950 --> 00:02:40,990
Three different cases, I should say.
34
00:02:41,590 --> 00:02:46,210
So the first case is send or send request or whatever you want to name it.
35
00:02:46,210 --> 00:02:46,840
I'll just name it.
36
00:02:46,840 --> 00:02:47,350
Send.
37
00:02:48,100 --> 00:02:50,950
The second case is response.
38
00:02:50,950 --> 00:02:57,640
So the response is there again, you can name does whatever you want and an error case and justice before
39
00:02:57,640 --> 00:03:05,260
a default case where I however, will only throw a new error should not be reached because we're the
40
00:03:05,260 --> 00:03:07,720
ones dispatching these actions.
41
00:03:07,720 --> 00:03:11,470
We should, of course, make sure we only dispatch actions with these identifiers.
42
00:03:11,650 --> 00:03:16,280
If we ever see the error, then we dispatched some action we're not handling and then we should ever
43
00:03:16,300 --> 00:03:17,020
fix a typo.
44
00:03:17,020 --> 00:03:19,180
We have or add a case for that action.
45
00:03:20,950 --> 00:03:25,690
Now, what happens if we send the request now, it's important to understand that here will not send
46
00:03:25,690 --> 00:03:26,980
the request itself.
47
00:03:27,280 --> 00:03:29,230
We're doing that down there.
48
00:03:30,040 --> 00:03:36,850
I just want to manage to state that is related to that request being sent that influences our UI.
49
00:03:37,810 --> 00:03:43,870
So the state there is basically where we show a loading spinner or whether we show an error.
50
00:03:44,230 --> 00:03:48,520
That's essentially the state I want to manage related to that request.
51
00:03:49,890 --> 00:03:58,530
So here, when we initialize this with use reducer and we point at our DP reducer, then the initial
52
00:03:58,530 --> 00:04:05,400
state could be an object where we have two fields and that is loading, which is falls initially and
53
00:04:05,400 --> 00:04:11,940
error, which is null initially here I'm using an object because I reducers could also just manage a
54
00:04:11,940 --> 00:04:12,630
single string.
55
00:04:12,630 --> 00:04:17,820
But here I want to have multiple connected fields and therefore I have an object connectome.
56
00:04:18,300 --> 00:04:25,920
Now when we send a request, we want to set loading to true and the error probably to null because we
57
00:04:25,920 --> 00:04:27,030
have no error yet.
58
00:04:27,030 --> 00:04:30,300
If we just sent the request and desist, new state will return.
59
00:04:30,300 --> 00:04:35,160
Of course, it should match the signature of our general state like the initial state we're managing
60
00:04:35,160 --> 00:04:35,400
here.
61
00:04:35,410 --> 00:04:36,620
And that is the case here.
62
00:04:36,630 --> 00:04:39,570
We have a loading property and we have an error property.
63
00:04:40,500 --> 00:04:46,590
If we got a response, then all said loading to Falls and al-Said set error to null.
64
00:04:46,590 --> 00:04:48,810
Or actually I just don't want to change the error.
65
00:04:49,080 --> 00:04:54,660
A common pattern dhafer for is that you actually set the property you want to set, but before you do
66
00:04:54,660 --> 00:05:01,080
that you simply take your existing state and use to spread operator to pull all key value pairs out
67
00:05:01,080 --> 00:05:01,380
of it.
68
00:05:01,560 --> 00:05:06,900
Merged with this new object which will be returned as your new state so that you don't lose anything
69
00:05:06,900 --> 00:05:12,600
from the old state and whatever you set thereafter will overwrite any properties of the same name that
70
00:05:12,600 --> 00:05:13,980
were pulled out of the old state.
71
00:05:14,280 --> 00:05:18,630
So if the old state we have a loading property in there, we're pulling that out and adding it to the
72
00:05:18,630 --> 00:05:19,110
object.
73
00:05:19,380 --> 00:05:25,170
But since we all to define it here, we then overrides that value with the new value of our properties,
74
00:05:25,170 --> 00:05:28,410
which we don't override here, will simply be kept in this new object.
75
00:05:30,010 --> 00:05:37,000
If we have an error, then I want to set loading to false because we're still not loading anymore,
76
00:05:37,000 --> 00:05:41,800
even if we have an error and set the error to action error.
77
00:05:41,800 --> 00:05:45,340
So we probably should get it here or error data.
78
00:05:45,490 --> 00:05:48,550
Whenever you want to have here, you're the one writing the code.
79
00:05:48,590 --> 00:05:52,140
You can define what you expect on your actions here.
80
00:05:52,150 --> 00:05:58,000
I'm not using to spread trickery, to spread approach, because here I'm setting both properties, which
81
00:05:58,000 --> 00:05:59,040
my state here has.
82
00:05:59,050 --> 00:06:01,900
So no need to spread existing properties.
83
00:06:01,900 --> 00:06:03,850
I would overwrite all of them anyways.
84
00:06:04,870 --> 00:06:06,510
So that's the age reducer.
85
00:06:06,510 --> 00:06:12,640
The producer use Reducer Dafa returns our H to the P state or whatever you want to name it.
86
00:06:12,640 --> 00:06:16,270
I'll name it if you state it's the same name as I have up there.
87
00:06:16,510 --> 00:06:21,100
So to avoid confusion, you don't have to change any of the two because they're in different scopes.
88
00:06:21,250 --> 00:06:28,870
But to avoid confusion, all named is priv htp or cur htp state because it's occurred in state before
89
00:06:28,870 --> 00:06:32,410
the update up there and here it's just htp state.
90
00:06:32,680 --> 00:06:34,300
And again we get dispatch.
91
00:06:34,840 --> 00:06:41,140
Now I already have a dispatch variable though, so I'll renamed this to dispatch H to the AP so that
92
00:06:41,140 --> 00:06:43,030
we have no name clash here and here.
93
00:06:43,030 --> 00:06:47,290
We need to rename it because they're in the same scope, hence we can't use the same name twice.
94
00:06:48,850 --> 00:06:55,060
So now we can get rid of these to use state statements and now fix all these errors we're getting,
95
00:06:55,210 --> 00:07:00,380
for example, here for loading, in the end, what's happening here is that I want to dispatch an HDP
96
00:07:00,400 --> 00:07:04,490
action and that action is of type send.
97
00:07:04,990 --> 00:07:06,820
So that's the identifier I define here.
98
00:07:06,820 --> 00:07:10,020
And that will then automatically set these state values.
99
00:07:10,150 --> 00:07:15,430
We don't need to pass any extra data to that action because that action doesn't need any external data
100
00:07:15,430 --> 00:07:15,880
to work.
101
00:07:16,370 --> 00:07:18,550
So we send a request here.
102
00:07:19,150 --> 00:07:26,450
Now, when we got a response, I'll sent the response request here, down there in removing Agent Handler.
103
00:07:26,500 --> 00:07:29,680
Again, it's send the request, which I'm sending.
104
00:07:29,920 --> 00:07:37,750
And then here, if we got a response, it's the response request only here if we have an error, well,
105
00:07:37,750 --> 00:07:41,200
then I want to dispatch my error action.
106
00:07:41,200 --> 00:07:47,710
And that's the only action here that that then takes an extra argument, because in our review, sir,
107
00:07:48,010 --> 00:07:51,520
we're accessing error data on the action to get the error.
108
00:07:53,490 --> 00:07:59,700
Actually, I'll rename this, too, error message, because I want to get the message and stored that
109
00:07:59,700 --> 00:08:01,950
an error, so that error always is a message.
110
00:08:02,580 --> 00:08:09,720
And then down there I pass error message as a key, and that is error message or again, a message of
111
00:08:09,720 --> 00:08:13,520
your choice, like something went wrong.
112
00:08:15,080 --> 00:08:21,260
Now in clear error there, we actually need to dispatch an action we don't have yet, so let's register
113
00:08:21,260 --> 00:08:26,600
a new case and that is clear maybe where I return.
114
00:08:27,740 --> 00:08:33,620
My old heard his state, but I said error to null, because that's what we're doing here.
115
00:08:33,890 --> 00:08:38,600
We're keeping all the old state in this new state object, returning the resetting error to null.
116
00:08:38,870 --> 00:08:41,299
So now we can dispatched as clear action here.
117
00:08:41,990 --> 00:08:49,840
So dispatch HTP with an object where the type is set to clear so that this clearing is happening well.
118
00:08:49,850 --> 00:08:54,140
And then here, instead of error, we access HTP state.
119
00:08:54,740 --> 00:08:59,970
Remember that HTP state is now what we're extracting from the result of use reducer here.
120
00:09:00,380 --> 00:09:05,220
So that is in the enties object with loading an error, which we're constantly updating.
121
00:09:05,690 --> 00:09:09,350
So we got our HTP state, which might contain an error.
122
00:09:09,950 --> 00:09:15,980
HTP state error is what I output here and HTP state error, all those what we need to check here.
123
00:09:17,450 --> 00:09:20,990
And then here it's HTP state loading.
124
00:09:21,880 --> 00:09:25,930
With that, if we save this -- again, we're correct me if I add.
125
00:09:28,960 --> 00:09:35,650
A new item here, you see the spinners floating works, we see that here and now if we add an error
126
00:09:35,650 --> 00:09:36,100
again.
127
00:09:37,480 --> 00:09:41,890
For removing, let's say, by again removing that is from Jason Vonow.
128
00:09:41,890 --> 00:09:42,610
Now click here.
129
00:09:42,610 --> 00:09:43,420
We see that.
130
00:09:43,420 --> 00:09:44,470
We see the error model.
131
00:09:44,470 --> 00:09:45,310
We can close it.
132
00:09:45,680 --> 00:09:47,650
So that is now all working.
133
00:09:47,860 --> 00:09:48,720
Let me repeat.
134
00:09:48,730 --> 00:09:50,020
Yes, with that.
135
00:09:50,020 --> 00:09:52,120
We're using a reducer for this to here.
136
00:09:52,120 --> 00:09:57,310
We're doing it because we have multiple connected pieces of state, which we now can manage in one place
137
00:09:57,550 --> 00:10:02,560
and still make sure that we always get the old state, that we update everything correctly, and that,
138
00:10:02,560 --> 00:10:08,710
again, we take some logic out of our component here and move it into the reducer functions so that
139
00:10:08,720 --> 00:10:15,720
we have cleaner code where we have a clearly defined flow of of data and of where we change our data.
140
00:10:16,300 --> 00:10:18,130
Now, we can also get rid of the.
141
00:10:18,550 --> 00:10:23,860
State import here in the ingredients chestful because now we're managing all our state with use reducer,
142
00:10:24,130 --> 00:10:26,050
which is an alternative to use state.
143
00:10:26,200 --> 00:10:33,850
If you got more complex state states that relies on the old state or if multiple state values work together
144
00:10:33,850 --> 00:10:38,020
and you want, you will manage them in one place and update them correctly.
15202
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.