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:08,930
So let's make sure we show a loading spinner if we are well, waiting for the HTTP request to complete.
2
00:00:10,160 --> 00:00:16,129
To do that, we, of course, need to manage an extra piece of state maybe is loading and set is loading
3
00:00:16,129 --> 00:00:20,450
our appropriate names with U.S. state and initially that's false.
4
00:00:21,140 --> 00:00:26,240
I need to manage state here because we'll change does and every change should lead to this ingredients
5
00:00:26,240 --> 00:00:31,130
component to Remender so that we can update our loading state and show something in the UI.
6
00:00:32,180 --> 00:00:40,070
Now, when we're adding an ingredient, let's say then I want to set is loading to True Judah's right
7
00:00:40,070 --> 00:00:41,090
before we call fetch.
8
00:00:42,200 --> 00:00:46,700
Once we got a response, however, I want to set it to false because now we're done.
9
00:00:47,150 --> 00:00:50,780
So I'm updating my state here with these calls to set this loading.
10
00:00:51,710 --> 00:00:55,370
This, of course, will also mean that we rendered the component.
11
00:00:56,460 --> 00:01:03,300
And now we can use our loading indicator to, well, output, whether we're loading or not, we could
12
00:01:03,300 --> 00:01:05,820
display this indicator in all kinds of places.
13
00:01:06,000 --> 00:01:10,710
I like to display it in my ingredient form, but it's up to you.
14
00:01:11,100 --> 00:01:16,830
I will add loading prop here and point at is loading here.
15
00:01:17,800 --> 00:01:27,610
So 40 is loading state to the ingredient form and in the ingredient form there, we can import the loading
16
00:01:27,940 --> 00:01:32,070
indicator from UI loading indicator.
17
00:01:32,110 --> 00:01:33,960
So that's a component I prepared for you.
18
00:01:34,270 --> 00:01:35,140
It's a little spinner.
19
00:01:35,590 --> 00:01:41,140
And then down there next to the button, we can check props loading.
20
00:01:41,350 --> 00:01:46,090
And if that's true, then I want to render the loading Enriqueta.
21
00:01:47,440 --> 00:01:53,830
Otherwise, I want to return nothing, by the way, a shortcut for this is that you replaced questionmark
22
00:01:53,830 --> 00:01:58,810
with and so with two ampersand signs and then you omitted us Colen else case.
23
00:01:58,850 --> 00:02:00,820
Now, this means this will only get rendered.
24
00:02:00,820 --> 00:02:01,660
If this is true.
25
00:02:02,170 --> 00:02:04,140
It's a little JavaScript syntax, but it does a check.
26
00:02:04,150 --> 00:02:07,110
And if this is true, this gets rendered or diskettes return.
27
00:02:07,120 --> 00:02:09,400
And in this case, Synthron react does gets rendered.
28
00:02:10,180 --> 00:02:11,290
So now loading is true.
29
00:02:11,320 --> 00:02:16,150
We should see an indicator where a setting loading when we add ingredients, let's all do it when we
30
00:02:16,150 --> 00:02:19,300
remove ingredients there I call set is loading.
31
00:02:19,300 --> 00:02:20,620
Said it to true initially.
32
00:02:21,550 --> 00:02:23,140
And only once it is deleted.
33
00:02:23,170 --> 00:02:26,850
So here in the then block, I'll set it to false because we're not loading anymore.
34
00:02:27,640 --> 00:02:30,400
So if we save this now, if we add.
35
00:02:31,700 --> 00:02:32,910
Chocolate one.
36
00:02:33,680 --> 00:02:38,600
We should see a loading spinner here, and we do if for now the lead something, we also see one.
37
00:02:38,810 --> 00:02:39,710
So that's a bit better.
38
00:02:39,950 --> 00:02:43,670
And of course, we could do something similar when we're loading all the ingredients initially.
39
00:02:44,150 --> 00:02:46,150
We'll take care about this later.
40
00:02:46,430 --> 00:02:49,160
For now, this is what I wanted to show you, though.
41
00:02:50,270 --> 00:02:55,850
Let's next handle errors, things all that could go wrong, let's say, for removing.
42
00:02:56,210 --> 00:02:57,230
We get an error.
43
00:02:57,230 --> 00:03:00,590
And Firebase, unfortunately, kind of is really stable.
44
00:03:00,590 --> 00:03:02,300
So we probably won't get an error there.
45
00:03:02,540 --> 00:03:08,330
But if I remove this here and Jason will force a technical error there for now, if I click on chocolate,
46
00:03:08,900 --> 00:03:11,570
actually just keeps on spinning, this doesn't get removed.
47
00:03:11,570 --> 00:03:13,580
And here we see we have an erroneous request.
48
00:03:14,210 --> 00:03:16,420
We got an error, but we're not handling it.
49
00:03:16,430 --> 00:03:20,840
And that's not a great user experience because it's not obvious to the user who hasn't.
50
00:03:21,140 --> 00:03:23,540
The developer tools open what went wrong.
51
00:03:24,320 --> 00:03:30,080
So we want to handle errors like this now with fetch, since it returns a promise we can handle errors
52
00:03:30,080 --> 00:03:33,500
by adding catch here and catch.
53
00:03:33,500 --> 00:03:37,220
We'll get the error and then allow us to do whatever we want with that error.
54
00:03:37,660 --> 00:03:42,830
Now, here, I want to output my error model and show a nice error message if something goes wrong.
55
00:03:43,190 --> 00:03:47,540
Therefore, I'll import the error model from.
56
00:03:50,330 --> 00:03:56,720
DUI folder and there error modal jazz and rendered as if we have an error, hence we need to manage
57
00:03:56,720 --> 00:04:02,480
an extra piece of state error and set error with use state.
58
00:04:02,480 --> 00:04:06,130
And initially, it has no value in there because we have no error.
59
00:04:06,140 --> 00:04:09,020
Initially, it's just undefined or now initially.
60
00:04:09,830 --> 00:04:12,590
But now we can call set error whenever we got an error.
61
00:04:12,860 --> 00:04:18,680
So let's say because here I noted it, have one in the remove ingredient handler into the catch block.
62
00:04:18,680 --> 00:04:25,310
Here we call set error and I set error message as a value.
63
00:04:25,310 --> 00:04:28,130
Every error object by default has a message property.
64
00:04:28,130 --> 00:04:32,510
So I'm sending this message as a field or as a value for for error.
65
00:04:32,510 --> 00:04:38,180
And you can of course, manage whatever you want in here, can also set your own message like something
66
00:04:38,930 --> 00:04:41,630
went wrong or anything like that.
67
00:04:42,660 --> 00:04:47,760
And I'll do that here, because that pre-defined message might be really cryptic to users, so I showed
68
00:04:47,760 --> 00:04:48,240
this one.
69
00:04:49,910 --> 00:04:55,020
Now we can output the error model if error is set.
70
00:04:55,190 --> 00:04:57,590
So if that error state is set.
71
00:04:58,750 --> 00:05:00,790
For this year in this Divx.
72
00:05:01,740 --> 00:05:07,080
I want to check if Arora is true, remember, Arora is my error state.
73
00:05:07,930 --> 00:05:13,660
So if error is true and if it is using the same shortcut as before or rendered the error modals, it
74
00:05:13,660 --> 00:05:20,800
is only gets rendered if error is set and then to the error model, I pass my error message and that's
75
00:05:20,800 --> 00:05:22,450
just what a story in error.
76
00:05:23,980 --> 00:05:29,470
No air remotely, actually, is a component which tries to call on close if we click the backdrop audio
77
00:05:29,530 --> 00:05:33,790
OK button, so I'll add on close here and there.
78
00:05:33,790 --> 00:05:40,090
I want to execute a function that clears the error so that a new function, clear error or whatever
79
00:05:40,090 --> 00:05:40,800
you want to name it.
80
00:05:41,380 --> 00:05:45,720
And in the end, what this does is it calls set error and sets us back to null.
81
00:05:45,730 --> 00:05:50,890
And if that is now, this error model will all disappear because this condition will not be satisfied
82
00:05:50,890 --> 00:05:51,270
anymore.
83
00:05:51,400 --> 00:05:54,310
NULL is treated as false and therefore this won't get rendered.
84
00:05:55,570 --> 00:05:58,510
So here on clothes, I will point at clear error.
85
00:05:58,510 --> 00:06:04,330
So at this function, which should get executed if on Close's triggered, in addition to setting the
86
00:06:04,330 --> 00:06:11,560
error here, I also want to set is loading to false here because even with that error, we are not loading
87
00:06:11,560 --> 00:06:12,000
anymore.
88
00:06:12,040 --> 00:06:14,230
So the loading spinner here should disappear.
89
00:06:14,320 --> 00:06:15,980
Otherwise that will keep on going.
90
00:06:17,050 --> 00:06:22,150
Now if we save this and I click on that, I get that error model for click.
91
00:06:22,180 --> 00:06:23,800
OK, we're done.
92
00:06:25,040 --> 00:06:31,520
That's also another important thing you have to understand regarding how react, works, react also
93
00:06:31,520 --> 00:06:33,710
Bachus state updates.
94
00:06:34,010 --> 00:06:39,860
I'm naming a sad state here because whenever you call a set, whatever it is function from use state,
95
00:06:40,040 --> 00:06:41,870
this will, of course, trigger a state update.
96
00:06:42,080 --> 00:06:47,870
And their react has a mechanism for batching multiple updates together to avoid unnecessary renderers
97
00:06:47,870 --> 00:06:48,410
cycles.
98
00:06:49,070 --> 00:06:50,630
So when you set your new state.
99
00:06:52,110 --> 00:06:56,970
No matter which approach you use, react batches, these state updates.
100
00:06:58,490 --> 00:07:03,800
So that also means that riding the next line after calling said you said you can't immediately use the
101
00:07:03,800 --> 00:07:11,030
new state because we won't go ahead and update the state and skip all the other code instead will execute
102
00:07:11,030 --> 00:07:12,890
all the other code in the same function.
103
00:07:13,040 --> 00:07:16,880
And Bache, all state updates you scheduled there together.
104
00:07:17,540 --> 00:07:21,010
So all state updates from one at the same synchronous event handler.
105
00:07:21,020 --> 00:07:21,710
That's important.
106
00:07:21,710 --> 00:07:23,870
Synchronoss are batched together.
107
00:07:24,830 --> 00:07:25,720
That's a bit theoretic.
108
00:07:25,730 --> 00:07:26,420
What does this mean?
109
00:07:26,840 --> 00:07:30,710
It means that here in clear error, for example, I have set error and said is loading.
110
00:07:30,710 --> 00:07:31,970
This isn't the same function.
111
00:07:32,120 --> 00:07:35,560
These two steps are executed synchronously after each other.
112
00:07:35,780 --> 00:07:40,820
So even if that were in the then block, if both are in the same, then block, then these two always
113
00:07:40,820 --> 00:07:45,670
run right after each other and then react Bachus D to state updates together.
114
00:07:45,950 --> 00:07:52,220
So this will not mean that set error causes a remender cycle and set is loading causes another one,
115
00:07:52,670 --> 00:07:59,180
but will only have one render cycle here, which updates both states or which reflects both state updates
116
00:07:59,180 --> 00:07:59,840
to be precise.
117
00:08:00,350 --> 00:08:04,640
And looking at that settles loading is actually wrong here and clear error or not wrong.
118
00:08:04,640 --> 00:08:09,470
But you might also argue that you want to move it here where you set the error initially then.
119
00:08:11,010 --> 00:08:18,240
Then this spinning icon also disappears right when Mole opens, of course, because we're setting loading
120
00:08:18,240 --> 00:08:19,890
at the same point of time, we're setting the error.
121
00:08:19,900 --> 00:08:22,200
But these two, again, will be batched together.
122
00:08:22,410 --> 00:08:29,010
So it's not to render cycles that are executed to show the error model and to remove the spinner.
123
00:08:29,040 --> 00:08:34,159
But it's one of the same render cycles, which already has both state updates taken into account.
124
00:08:35,159 --> 00:08:42,390
So that's how REAC Bachus state updates and how you can handle error with a separate state, of course,
125
00:08:42,539 --> 00:08:46,140
where you store errors and then react to them appropriately.
12689
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.