Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,150 --> 00:00:03,210
So now let's continue
2
00:00:03,210 --> 00:00:05,160
working on this application.
3
00:00:05,160 --> 00:00:06,550
We are fetching the movies
4
00:00:06,550 --> 00:00:09,254
whenever we click this button and that's great.
5
00:00:09,254 --> 00:00:12,640
Now we're not really showing any loading state.
6
00:00:12,640 --> 00:00:15,437
If I reload this app and I click fetch movies
7
00:00:15,437 --> 00:00:19,510
it takes a second or so for the movies to show up.
8
00:00:19,510 --> 00:00:23,470
In reality, you typically want to show some loading spinner
9
00:00:23,470 --> 00:00:26,870
or at least some loading text in the meanwhile
10
00:00:26,870 --> 00:00:30,660
to signal to your user that data is on its way
11
00:00:30,660 --> 00:00:32,770
and you're doing some work.
12
00:00:32,770 --> 00:00:35,680
And of course we want to do this here as well.
13
00:00:35,680 --> 00:00:38,903
Now, how could we do that in React world?
14
00:00:39,970 --> 00:00:44,350
In the end, it's again, just about managing state.
15
00:00:44,350 --> 00:00:46,320
We get the movies state and we can,
16
00:00:46,320 --> 00:00:49,600
of course, tell whether we have movies or not.
17
00:00:49,600 --> 00:00:52,120
Now to tell whether we're waiting or not,
18
00:00:52,120 --> 00:00:55,130
we can introduce a second piece of state.
19
00:00:55,130 --> 00:00:57,280
We could name it isLoading and setIsLoading
20
00:00:59,320 --> 00:01:03,290
and then add useState and initially set this to false.
21
00:01:03,290 --> 00:01:05,590
Initially, we're not loading data.
22
00:01:05,590 --> 00:01:07,440
When this component is loaded,
23
00:01:07,440 --> 00:01:09,970
when the app component is rendered to the screen
24
00:01:09,970 --> 00:01:12,250
we're not loading data.
25
00:01:12,250 --> 00:01:15,100
But when we call fetchMoviesHandler,
26
00:01:15,100 --> 00:01:18,120
when this function starts to execute,
27
00:01:18,120 --> 00:01:21,540
we of course want to call setIsLoading here
28
00:01:21,540 --> 00:01:22,913
and set this to true.
29
00:01:23,941 --> 00:01:28,130
With that we changed the state when we start to load.
30
00:01:28,130 --> 00:01:31,140
Now, once we're done, so basically,
31
00:01:31,140 --> 00:01:35,340
after this await block or after we setMovies,
32
00:01:35,340 --> 00:01:37,090
but that doesn't really make a difference
33
00:01:37,090 --> 00:01:39,750
because these are synchronous tasks,
34
00:01:39,750 --> 00:01:43,900
but once we're done, we want to call setIsLoading again,
35
00:01:43,900 --> 00:01:45,430
and set it to false again,
36
00:01:45,430 --> 00:01:47,150
because we're not loading anymore,
37
00:01:47,150 --> 00:01:48,333
we got some data.
38
00:01:50,530 --> 00:01:53,800
And now we can use the isLoading state
39
00:01:53,800 --> 00:01:57,910
to render a loading spinner or some loading text.
40
00:01:57,910 --> 00:02:00,350
So here we could say that we only want to render
41
00:02:00,350 --> 00:02:03,970
the movies list if we are not loading.
42
00:02:03,970 --> 00:02:06,730
So of course we can do it as we learned it,
43
00:02:06,730 --> 00:02:09,363
wrap this in curly braces, check isLoading,
44
00:02:10,488 --> 00:02:14,660
and if we're not loading, then we want to render this,
45
00:02:14,660 --> 00:02:16,820
which we can do with this shortcut here.
46
00:02:16,820 --> 00:02:20,060
So for not loading, then we render this,
47
00:02:20,060 --> 00:02:23,030
alternatively, if we are loading,
48
00:02:23,030 --> 00:02:25,590
so there is no exclamation mark here,
49
00:02:25,590 --> 00:02:28,703
I will render a paragraph where I say loading.
50
00:02:30,291 --> 00:02:33,890
So if we save this and we click this button
51
00:02:33,890 --> 00:02:36,510
you see loading here, only briefly,
52
00:02:36,510 --> 00:02:38,870
because the data arrives super fast,
53
00:02:38,870 --> 00:02:40,930
but you see loading here for the fraction
54
00:02:40,930 --> 00:02:44,000
of a second until the data is there
55
00:02:44,000 --> 00:02:45,723
and also if we re-fetch.
56
00:02:46,620 --> 00:02:49,083
And that is how we can show a loading spinner,
57
00:02:49,083 --> 00:02:52,573
loading text, whatever you want to do whilst you're loading.
58
00:02:53,770 --> 00:02:55,810
Now, of course, loading and not loading
59
00:02:55,810 --> 00:02:58,650
are not the only states you might have.
60
00:02:58,650 --> 00:03:00,810
We might also want to show a different content
61
00:03:00,810 --> 00:03:04,370
if we're not loading, but we've got no movies yet,
62
00:03:04,370 --> 00:03:06,250
so if movies is an empty array
63
00:03:06,250 --> 00:03:08,113
and there is nothing to display.
64
00:03:09,330 --> 00:03:11,400
Of course we could also handle this.
65
00:03:11,400 --> 00:03:14,630
We could say that we only want to render the movies list
66
00:03:14,630 --> 00:03:19,630
if we're not loading and movies length is greater than zero.
67
00:03:21,170 --> 00:03:24,151
So only if we're not loading and we got movies,
68
00:03:24,151 --> 00:03:26,683
only then we display the movies list.
69
00:03:29,220 --> 00:03:31,320
Here, we want to show to loading text
70
00:03:31,320 --> 00:03:33,090
whenever we are loading though.
71
00:03:33,090 --> 00:03:36,160
No matter if we already have loaded movies or not
72
00:03:36,160 --> 00:03:38,250
we started a new loading process
73
00:03:38,250 --> 00:03:41,150
and therefore, of course, we want to show to loading text.
74
00:03:42,330 --> 00:03:45,200
Now, maybe we want to show some fallback content
75
00:03:45,200 --> 00:03:48,303
if we're not loading and we got no movies though.
76
00:03:49,180 --> 00:03:53,160
So here we could say, if we're not loading,
77
00:03:53,160 --> 00:03:55,290
so we want to display some content,
78
00:03:55,290 --> 00:03:58,560
but the movie's length is equal to zero,
79
00:03:58,560 --> 00:04:00,340
so we got no movies.
80
00:04:00,340 --> 00:04:03,340
Then we show a paragraph where we say
81
00:04:03,340 --> 00:04:07,180
found no movies or anything like that.
82
00:04:07,180 --> 00:04:10,183
This of course, is just a simple fallback example.
83
00:04:11,120 --> 00:04:14,320
Now you see when we reload this application
84
00:04:14,320 --> 00:04:16,990
and we therefore no movies initially,
85
00:04:16,990 --> 00:04:19,329
we see found no movies.
86
00:04:19,329 --> 00:04:21,680
If I click fetch movies, this will change
87
00:04:21,680 --> 00:04:23,913
to loading and then to the movies.
88
00:04:26,312 --> 00:04:28,561
And that's of course a crucial part
89
00:04:28,561 --> 00:04:31,010
of building user interfaces.
90
00:04:31,010 --> 00:04:34,210
You want to let your users know which state
91
00:04:34,210 --> 00:04:36,150
your application currently has.
92
00:04:36,150 --> 00:04:39,760
And there is a difference between we got no movies,
93
00:04:39,760 --> 00:04:43,143
we are loading, and we have movies, of course.
7352
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.