Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,140 --> 00:00:03,170
Now at the moment,
2
00:00:03,170 --> 00:00:06,160
we are using this dummy meetups array
3
00:00:06,160 --> 00:00:08,280
for rendering our list of meetups.
4
00:00:08,280 --> 00:00:09,820
And on the meetup detailed page,
5
00:00:09,820 --> 00:00:12,050
we just have some hard coded dummy data
6
00:00:12,050 --> 00:00:14,030
and that's not realistic.
7
00:00:14,030 --> 00:00:16,180
In reality, we would have a backend,
8
00:00:16,180 --> 00:00:19,100
some database which holds that data.
9
00:00:19,100 --> 00:00:21,100
Now let's simulate that we do
10
00:00:21,100 --> 00:00:23,380
and later we'll actually have one,
11
00:00:23,380 --> 00:00:26,230
but first of all let's simulate that we do have one
12
00:00:26,230 --> 00:00:28,160
and then the homepage component,
13
00:00:28,160 --> 00:00:32,400
and as a route index.js file, let's not pass dummy meetups
14
00:00:32,400 --> 00:00:34,300
into the meetups prop like this,
15
00:00:34,300 --> 00:00:37,200
but instead let's change this component
16
00:00:37,200 --> 00:00:39,750
to behave the way it would behave
17
00:00:39,750 --> 00:00:42,710
if we would reach out to a backend.
18
00:00:42,710 --> 00:00:45,520
And how would we typically do this in React?
19
00:00:45,520 --> 00:00:48,400
Well, if we wanna send a HTTP request
20
00:00:48,400 --> 00:00:50,250
once this page is rendered,
21
00:00:50,250 --> 00:00:54,110
we would typically use the useEffect hook to control this.
22
00:00:54,110 --> 00:00:55,730
So we would import useEffect
23
00:00:55,730 --> 00:00:59,840
from React and then execute useEffect here
24
00:01:00,870 --> 00:01:03,870
and have an empty dependencies array, probably,
25
00:01:03,870 --> 00:01:06,720
which means that this affect function runs whenever
26
00:01:06,720 --> 00:01:10,440
the component is first rendered, but never thereafter.
27
00:01:10,440 --> 00:01:13,280
And then in here we could fetch our data
28
00:01:13,280 --> 00:01:15,770
and we could manage some state for this component
29
00:01:15,770 --> 00:01:17,924
with the useState hook, which we therefore,
30
00:01:17,924 --> 00:01:21,120
also need to import from React.
31
00:01:21,120 --> 00:01:24,900
And then here, we could manage our list of meetups.
32
00:01:24,900 --> 00:01:28,650
Let's say the loadedMeetups
33
00:01:28,650 --> 00:01:33,400
and we have our setLoadedMeetups state updating function
34
00:01:33,400 --> 00:01:34,760
and in useEffect,
35
00:01:34,760 --> 00:01:39,760
we would send that HTTP request and fetch data.
36
00:01:39,990 --> 00:01:41,920
And then once that's done,
37
00:01:41,920 --> 00:01:44,120
it would be an asynchronous task, of course,
38
00:01:44,120 --> 00:01:48,372
but once that's done, we would call setLoadedMeetups
39
00:01:48,372 --> 00:01:51,522
like this and set the meetups that we fetched
40
00:01:51,522 --> 00:01:55,500
from a server has the meetups for this component.
41
00:01:55,500 --> 00:01:56,380
And for the moment,
42
00:01:56,380 --> 00:02:00,270
let's just simulate that we fetched the dummy meetups.
43
00:02:00,270 --> 00:02:03,150
Of course, they are available right from the start here,
44
00:02:03,150 --> 00:02:06,350
but let's assume we just fetched them from a server.
45
00:02:06,350 --> 00:02:10,139
So some promise completed here and we got back the response.
46
00:02:10,139 --> 00:02:14,210
And now I set my dummy meetups as the loaded meetups
47
00:02:14,210 --> 00:02:18,650
and here in the JSX code, we passed the loaded meetups,
48
00:02:18,650 --> 00:02:21,463
so our state into meetup list.
49
00:02:22,470 --> 00:02:24,950
If we do all of that,
50
00:02:24,950 --> 00:02:28,920
if we visit the starting page with all the meetups,
51
00:02:28,920 --> 00:02:30,750
we don't see any difference there.
52
00:02:30,750 --> 00:02:32,610
When I reload, all the meetups are there,
53
00:02:32,610 --> 00:02:34,040
right from the start
54
00:02:34,040 --> 00:02:37,720
because we never really send a HTTP request.
55
00:02:37,720 --> 00:02:40,090
But technically there is a difference
56
00:02:40,090 --> 00:02:43,240
because it is important to note that useEffect
57
00:02:43,240 --> 00:02:46,515
works such that it executes this function
58
00:02:46,515 --> 00:02:51,515
after important, after the component function was executed.
59
00:02:52,270 --> 00:02:53,310
So that means that,
60
00:02:53,310 --> 00:02:56,675
the first time this homepage component is rendered,
61
00:02:56,675 --> 00:02:59,180
loadedMeetups will be an empty array.
62
00:02:59,180 --> 00:03:01,526
Then this effect function will execute,
63
00:03:01,526 --> 00:03:03,500
it will then update the state
64
00:03:03,500 --> 00:03:06,480
and then this component function will execute again
65
00:03:06,480 --> 00:03:08,150
because the state changed
66
00:03:08,150 --> 00:03:10,540
and it will then re-render the list
67
00:03:10,540 --> 00:03:12,720
with the actual data
68
00:03:12,720 --> 00:03:15,340
but we'll have two component renders cycles.
69
00:03:15,340 --> 00:03:17,320
And in the first render cycle,
70
00:03:17,320 --> 00:03:19,383
the first time this component renders,
71
00:03:19,383 --> 00:03:23,130
loadedMeetups state will be this initial state,
72
00:03:23,130 --> 00:03:24,403
this empty array.
73
00:03:25,340 --> 00:03:27,720
Now, why am I emphasizing this?
74
00:03:27,720 --> 00:03:30,500
Because if we would fetch this from a backend,
75
00:03:30,500 --> 00:03:32,976
our users might see a loading spinner briefly,
76
00:03:32,976 --> 00:03:36,290
which could or could not be the user experience
77
00:03:36,290 --> 00:03:37,880
we wanna offer.
78
00:03:37,880 --> 00:03:41,360
But in addition, even here where we don't really
79
00:03:41,360 --> 00:03:45,063
send the request and where the response,
80
00:03:45,063 --> 00:03:49,240
"arrives basically, instantly" even in this case.
81
00:03:49,240 --> 00:03:51,740
Because of these two render cycles,
82
00:03:51,740 --> 00:03:55,010
we have a problem with search engine optimization.
83
00:03:55,010 --> 00:03:57,094
If I viewed a page source, you will notice
84
00:03:57,094 --> 00:04:01,800
that in there, the actual meetup data is missing.
85
00:04:01,800 --> 00:04:03,660
I got my unordered list here
86
00:04:03,660 --> 00:04:06,203
and this unordered list is empty.
87
00:04:07,130 --> 00:04:09,810
So the items which we see on the screen here,
88
00:04:09,810 --> 00:04:13,740
these items are missing in the HTML content.
89
00:04:13,740 --> 00:04:17,769
In the HTML page we fetched from the server
90
00:04:17,769 --> 00:04:20,420
and they are missing because they are only rendered
91
00:04:20,420 --> 00:04:23,233
in the second component execution cycle.
92
00:04:24,200 --> 00:04:28,230
But the pre-rendered HTML page generated
93
00:04:28,230 --> 00:04:31,040
by NextJS automatically does not wait
94
00:04:31,040 --> 00:04:32,900
for this second cycle.
95
00:04:32,900 --> 00:04:37,300
It always takes the result of the first render cycle
96
00:04:37,300 --> 00:04:41,690
and return stat as the pre-rendered HTML code.
97
00:04:41,690 --> 00:04:44,430
And there, this data is missing.
98
00:04:44,430 --> 00:04:45,490
Now, of course, here,
99
00:04:45,490 --> 00:04:46,460
with the dummy data,
100
00:04:46,460 --> 00:04:48,720
it's is redundant to do it like this.
101
00:04:48,720 --> 00:04:51,450
But as I said, we are basically just simulating
102
00:04:51,450 --> 00:04:54,200
that we do fetch this data from a server
103
00:04:54,200 --> 00:04:58,360
and then we'll face the problem that NextJS does not wait
104
00:04:58,360 --> 00:05:00,220
for that data to be fetched,
105
00:05:00,220 --> 00:05:03,260
to then return the fully pre-rendered page,
106
00:05:03,260 --> 00:05:04,830
but it returns the result
107
00:05:04,830 --> 00:05:07,360
of the first component rendered cycle.
108
00:05:07,360 --> 00:05:10,450
And that might be a pretty empty page.
109
00:05:10,450 --> 00:05:13,430
And that now, was a lengthy explanation
110
00:05:13,430 --> 00:05:15,440
of the problem we're facing here,
111
00:05:15,440 --> 00:05:17,220
but it is an important problem
112
00:05:17,220 --> 00:05:21,520
and it is important to understand this problem properly.
113
00:05:21,520 --> 00:05:26,160
But thankfully, NextJS also has solutions to this problem.
114
00:05:26,160 --> 00:05:31,160
It has more core features built into NextJS that help us
115
00:05:31,920 --> 00:05:34,360
with precisely this problem
116
00:05:34,360 --> 00:05:37,950
that we wanna pre-render a page with data,
117
00:05:37,950 --> 00:05:41,120
but with data for which we have to wait.
118
00:05:41,120 --> 00:05:44,520
And we need to tell NextJS, once we're done waiting
119
00:05:44,520 --> 00:05:46,380
and therefore that's now what we're going
120
00:05:46,380 --> 00:05:47,490
to explore next,
121
00:05:47,490 --> 00:05:50,513
how we can fetch data for pre-rendering.
9485
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.