Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,290 --> 00:00:04,170
So we are fetching our data
2
00:00:04,170 --> 00:00:06,760
but as I mentioned at the end of the last lecture,
3
00:00:06,760 --> 00:00:08,960
we are of course not showing any loading
4
00:00:08,960 --> 00:00:10,780
or error state at the moment
5
00:00:10,780 --> 00:00:14,633
and that's not necessarily the behavior we want.
6
00:00:15,580 --> 00:00:19,810
Instead, we might want to only load this list of meals here,
7
00:00:19,810 --> 00:00:22,890
once we really got a list of meals.
8
00:00:22,890 --> 00:00:25,910
And to achieve this, we need to manage more our state
9
00:00:25,910 --> 00:00:29,160
and now since we will have related state,
10
00:00:29,160 --> 00:00:32,880
we could also consider using use reducer for that
11
00:00:32,880 --> 00:00:36,713
but I'll stick to three individual use state pieces.
12
00:00:37,600 --> 00:00:40,460
I will add isLoading
13
00:00:42,060 --> 00:00:42,893
state
14
00:00:42,893 --> 00:00:45,810
and a set IsLoading state updating function
15
00:00:45,810 --> 00:00:49,330
and initially it's false, initially we're not loading.
16
00:00:49,330 --> 00:00:50,970
But in use of fact,
17
00:00:50,970 --> 00:00:53,680
at the beginning of this fetchMeals function,
18
00:00:53,680 --> 00:00:56,940
I wanna call setIsLoading and set this to true,
19
00:00:56,940 --> 00:01:00,340
because now we clearly are loading.
20
00:01:00,340 --> 00:01:02,200
Of course, since we know that
21
00:01:02,200 --> 00:01:04,519
right when this component will be rendered,
22
00:01:04,519 --> 00:01:05,820
we will be loading,
23
00:01:05,820 --> 00:01:09,530
we could also consider not setIsLoading to true here,
24
00:01:09,530 --> 00:01:12,510
but instead setting the initial state to true,
25
00:01:12,510 --> 00:01:15,140
because we know that we always will start
26
00:01:15,140 --> 00:01:17,490
with loading data in this component
27
00:01:17,490 --> 00:01:19,240
when the component is rendered.
28
00:01:19,240 --> 00:01:21,170
And therefore in this case, that's fine.
29
00:01:21,170 --> 00:01:22,860
Of course, if you have a component
30
00:01:22,860 --> 00:01:25,260
where that's not necessarily the case,
31
00:01:25,260 --> 00:01:27,590
where you're not necessarily loading
32
00:01:27,590 --> 00:01:31,170
just because the component was created for the first time,
33
00:01:31,170 --> 00:01:33,210
there you might not want to set this
34
00:01:33,210 --> 00:01:34,653
but here, it should be fine.
35
00:01:35,930 --> 00:01:38,890
Now we wanna set IsLoading to false once we're done.
36
00:01:38,890 --> 00:01:42,800
So in the end, once we also set the meals,
37
00:01:42,800 --> 00:01:44,930
we wanna set IsLoading to false
38
00:01:44,930 --> 00:01:48,970
because we now definitely are done loading.
39
00:01:48,970 --> 00:01:52,980
And now we can use setIsLoading to control what we render.
40
00:01:52,980 --> 00:01:55,380
And we could say, that we don't wanna render
41
00:01:55,380 --> 00:01:57,080
our meals at all.
42
00:01:57,080 --> 00:02:00,150
So this entire section should not be rendered at all
43
00:02:00,150 --> 00:02:01,800
if we're loading.
44
00:02:01,800 --> 00:02:06,800
So, for that, we can simply check, if IsLoading is true
45
00:02:07,730 --> 00:02:10,900
and I checked this before I tried to map my meals.
46
00:02:10,900 --> 00:02:14,180
Before I do any of that I check if IsLoading is true.
47
00:02:14,180 --> 00:02:16,970
And if it is true, I will return something else
48
00:02:16,970 --> 00:02:19,030
in this functional component.
49
00:02:19,030 --> 00:02:22,123
So then we don't even make it to the code down there.
50
00:02:23,490 --> 00:02:25,900
And I'll return a section here,
51
00:02:25,900 --> 00:02:29,543
where I simply have a paragraph where I say, "loading"
52
00:02:30,970 --> 00:02:31,803
like this.
53
00:02:33,310 --> 00:02:35,660
If we add that code,
54
00:02:35,660 --> 00:02:38,810
you will see, there is this loading text on the left,
55
00:02:38,810 --> 00:02:42,810
briefly showing up when we reload this.
56
00:02:42,810 --> 00:02:46,850
And that's of course, not the best way to to show it
57
00:02:46,850 --> 00:02:51,470
and therefore let's style it by adding a class name here
58
00:02:51,470 --> 00:02:54,870
of classes.MealsLoading,
59
00:02:54,870 --> 00:02:55,800
like this maybe
60
00:02:56,950 --> 00:02:58,160
and then we can dive into this
61
00:02:58,160 --> 00:03:01,498
available meals module CSS file
62
00:03:01,498 --> 00:03:05,000
and quickly add a class selector
63
00:03:05,000 --> 00:03:06,870
for the meals loading class,
64
00:03:06,870 --> 00:03:09,110
where we set text align to center
65
00:03:09,110 --> 00:03:10,973
and set the color to white.
66
00:03:11,810 --> 00:03:13,250
Of course, you can definitely give this
67
00:03:13,250 --> 00:03:15,200
a more elaborate styling
68
00:03:15,200 --> 00:03:17,370
but it's better than nothing.
69
00:03:17,370 --> 00:03:20,420
Because with this class added here
70
00:03:20,420 --> 00:03:23,040
and assigned here to this section,
71
00:03:23,040 --> 00:03:25,120
with those changes made if I reload,
72
00:03:25,120 --> 00:03:27,720
we at least have the loading text here.
73
00:03:27,720 --> 00:03:30,600
And it's still only flashing for a brief moment
74
00:03:30,600 --> 00:03:32,980
because Firebase has a quick back end
75
00:03:32,980 --> 00:03:34,690
but it's better than nothing.
76
00:03:34,690 --> 00:03:36,730
Of course here, we could consider
77
00:03:36,730 --> 00:03:38,600
omitting this loading state
78
00:03:38,600 --> 00:03:41,530
since the data is loading rapidly quickly
79
00:03:41,530 --> 00:03:44,570
but of course, we don't know which internet connection
80
00:03:44,570 --> 00:03:46,610
our end users might have,
81
00:03:46,610 --> 00:03:49,490
so showing this loading state might not be too bad
82
00:03:49,490 --> 00:03:52,730
but actually there it was showing up a bit longer.
83
00:03:52,730 --> 00:03:55,410
So that is not too bad.
84
00:03:55,410 --> 00:03:58,683
But what if we have an error we should also work on that.
6471
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.