Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,120 --> 00:00:05,100
This here is basically an example
2
00:00:05,100 --> 00:00:08,440
I showed you earlier in this module already.
3
00:00:08,440 --> 00:00:11,070
This is a standard React App,
4
00:00:11,070 --> 00:00:13,670
not using Next.js at all,
5
00:00:13,670 --> 00:00:16,280
which uses a very traditional approach
6
00:00:16,280 --> 00:00:19,030
of sending a request inside of useEffect
7
00:00:19,030 --> 00:00:21,290
once this component is rendered,
8
00:00:21,290 --> 00:00:22,950
setting some loading state
9
00:00:22,950 --> 00:00:26,170
and then displaying some data down there.
10
00:00:26,170 --> 00:00:29,490
Again, that's a very standard React's way.
11
00:00:29,490 --> 00:00:32,940
And of course there are ways of changing that,
12
00:00:32,940 --> 00:00:34,300
of handling errors.
13
00:00:34,300 --> 00:00:37,560
Of using custom hoax and a lot of other things.
14
00:00:37,560 --> 00:00:40,550
But this is the most basic form
15
00:00:40,550 --> 00:00:45,550
of sending a request from inside React on the client side.
16
00:00:45,860 --> 00:00:46,770
And in the end,
17
00:00:46,770 --> 00:00:49,420
that's kind of what we now wanna build again
18
00:00:49,420 --> 00:00:52,450
with Next.js here.
19
00:00:52,450 --> 00:00:55,640
Now, for this I'll add a new page here,
20
00:00:55,640 --> 00:00:57,590
a new route therefore.
21
00:00:57,590 --> 00:01:01,810
And I'll name it last-sales.js.
22
00:01:01,810 --> 00:01:04,590
Of course, this will just be a dummy page here.
23
00:01:04,590 --> 00:01:07,870
This overall project is just a dummy project.
24
00:01:07,870 --> 00:01:10,320
We're going to have a more realistic example
25
00:01:10,320 --> 00:01:11,940
in the next course section,
26
00:01:11,940 --> 00:01:15,320
when we applied as to our events project again.
27
00:01:15,320 --> 00:01:19,750
But in this dummy file here, in this dummy page.
28
00:01:19,750 --> 00:01:21,940
I want to show you how we could implement
29
00:01:21,940 --> 00:01:25,900
client side data fetching with Next.js.
30
00:01:25,900 --> 00:01:27,300
And of course it starts
31
00:01:27,300 --> 00:01:31,320
with defining our page component here.
32
00:01:31,320 --> 00:01:35,020
And of course we also want to export that component
33
00:01:35,020 --> 00:01:36,883
so that it can be found.
34
00:01:37,860 --> 00:01:40,000
Now in here, in that component,
35
00:01:40,000 --> 00:01:42,710
we wanna return some JS Xcode.
36
00:01:42,710 --> 00:01:44,760
Let's say some sales data
37
00:01:44,760 --> 00:01:47,290
which should be rendered as a list,
38
00:01:47,290 --> 00:01:48,840
but we wanna fetch that data
39
00:01:48,840 --> 00:01:52,210
from inside this component function as well.
40
00:01:52,210 --> 00:01:54,940
We don't wanna add get static props
41
00:01:54,940 --> 00:01:56,720
or get server-side props here,
42
00:01:56,720 --> 00:01:59,500
because that's exactly not our goal here.
43
00:01:59,500 --> 00:02:02,550
We don't want to pre-fetch the data on the server
44
00:02:02,550 --> 00:02:04,770
or during the build time.
45
00:02:04,770 --> 00:02:08,592
Inside here, we can now write useEffect.
46
00:02:08,592 --> 00:02:10,310
So use to useEffect hook,
47
00:02:10,310 --> 00:02:13,170
which we import from React therefore.
48
00:02:13,170 --> 00:02:15,920
And then in there, in this effect function
49
00:02:15,920 --> 00:02:20,000
we can execute some code, which sends our HTTP request.
50
00:02:20,000 --> 00:02:21,130
And for a useEffect
51
00:02:21,130 --> 00:02:23,860
we also need to add this dependencies array,
52
00:02:23,860 --> 00:02:26,920
listing all the dependencies of this useEffect function.
53
00:02:26,920 --> 00:02:28,910
And in this case this function
54
00:02:28,910 --> 00:02:30,700
won't have any dependencies
55
00:02:30,700 --> 00:02:34,473
since I won't use any other component specific data here.
56
00:02:35,930 --> 00:02:39,680
So, now in here, we can use the built-in fetch API
57
00:02:39,680 --> 00:02:41,810
which is available in the browser.
58
00:02:41,810 --> 00:02:44,390
And as a side note, which could also be used
59
00:02:44,390 --> 00:02:47,810
in get static props or get service I props.
60
00:02:47,810 --> 00:02:49,610
It is available there as well.
61
00:02:49,610 --> 00:02:51,570
We don't need it there right now,
62
00:02:51,570 --> 00:02:53,800
but you could use it there as well.
63
00:02:53,800 --> 00:02:57,410
But here we use it on the client side instead of useEffect,
64
00:02:57,410 --> 00:02:59,890
to send the request to some API,
65
00:02:59,890 --> 00:03:01,730
to some web server.
66
00:03:01,730 --> 00:03:03,050
Now to test this,
67
00:03:03,050 --> 00:03:05,300
we need some dummy back-end,
68
00:03:05,300 --> 00:03:07,540
we need some web server.
69
00:03:07,540 --> 00:03:10,100
And I will use Firebase for that.
70
00:03:10,100 --> 00:03:12,060
It sounds like it's just a database,
71
00:03:12,060 --> 00:03:15,620
but actually Firebase is a service offered by Google,
72
00:03:15,620 --> 00:03:18,140
which gives you an entire back-end
73
00:03:18,140 --> 00:03:20,050
with a lot of different features.
74
00:03:20,050 --> 00:03:24,300
But one of the features is a database with an attached API
75
00:03:24,300 --> 00:03:26,600
which we can use as a dummy backend here.
76
00:03:26,600 --> 00:03:30,000
So we're not going to build anything complex with Firebase.
77
00:03:30,000 --> 00:03:33,180
We're just going to use it as a dummy back-end.
78
00:03:33,180 --> 00:03:35,050
For this you need to go to the console
79
00:03:35,050 --> 00:03:36,590
and for this you need a Google account
80
00:03:36,590 --> 00:03:38,233
with which you should log in.
81
00:03:39,300 --> 00:03:40,820
And once you did log in,
82
00:03:40,820 --> 00:03:44,030
you can add a new Firebase project
83
00:03:44,030 --> 00:03:47,000
and I'll just name it nextjs-course.
84
00:03:47,000 --> 00:03:47,833
But you can of course
85
00:03:47,833 --> 00:03:51,190
pick any name you want, click continue.
86
00:03:51,190 --> 00:03:52,870
You can disable Google Analytics,
87
00:03:52,870 --> 00:03:53,870
we don't need that.
88
00:03:53,870 --> 00:03:55,920
And create that project.
89
00:03:55,920 --> 00:04:00,720
Again Firebase is a service totally detached from Next.js
90
00:04:00,720 --> 00:04:04,400
which is actually made up of a lot of different services
91
00:04:04,400 --> 00:04:06,830
and tools and utilities,
92
00:04:06,830 --> 00:04:09,710
which you can use to build powerful back-ends
93
00:04:09,710 --> 00:04:12,720
for your application front-ends.
94
00:04:12,720 --> 00:04:15,880
Here, I really just wanna use its built-in database
95
00:04:15,880 --> 00:04:18,880
with a connected API.
96
00:04:18,880 --> 00:04:21,550
And that will be the real time database
97
00:04:21,550 --> 00:04:23,200
which I wanna use.
98
00:04:23,200 --> 00:04:26,300
So, here I'll click on real-time database on the left.
99
00:04:26,300 --> 00:04:30,650
And there, we then can click on create database.
100
00:04:30,650 --> 00:04:33,210
Now you can leave the default here, click on next.
101
00:04:33,210 --> 00:04:35,030
And on security rules
102
00:04:35,030 --> 00:04:37,820
you wanna start in test mode actually,
103
00:04:37,820 --> 00:04:40,060
which ensures that we do have access
104
00:04:40,060 --> 00:04:42,403
to the database from outside.
105
00:04:43,330 --> 00:04:46,530
Now this will create such a database for you.
106
00:04:46,530 --> 00:04:48,780
The great thing is that it will be a database
107
00:04:48,780 --> 00:04:51,650
into which we can look into here.
108
00:04:51,650 --> 00:04:55,704
And we also get an API here to which we can send requests
109
00:04:55,704 --> 00:04:59,130
which will then automatically be translated
110
00:04:59,130 --> 00:05:02,460
into database operations by Firebase.
111
00:05:02,460 --> 00:05:06,750
So, we send regular HTTP requests to this API here
112
00:05:06,750 --> 00:05:09,980
and then automatically data gets written, deleted
113
00:05:09,980 --> 00:05:13,270
whatever we wanna do here in Firebase.
114
00:05:13,270 --> 00:05:16,630
Now, here I actually wanna prepare some data though.
115
00:05:16,630 --> 00:05:18,960
So that we have some data we can fetch
116
00:05:18,960 --> 00:05:21,403
with our Next.js code here.
117
00:05:22,260 --> 00:05:24,630
Therefore we can click on the plus here,
118
00:05:24,630 --> 00:05:28,660
when we hover over this database name.
119
00:05:28,660 --> 00:05:31,510
And then add any key of your choice.
120
00:05:31,510 --> 00:05:33,800
I'll go with sales here,
121
00:05:33,800 --> 00:05:35,510
and then click on the plus here.
122
00:05:35,510 --> 00:05:38,690
And now we basically created a sales node
123
00:05:38,690 --> 00:05:41,320
a sales table kind off,
124
00:05:41,320 --> 00:05:44,550
and we can now add a different pieces of information
125
00:05:44,550 --> 00:05:46,990
that belong to a single sale.
126
00:05:46,990 --> 00:05:50,510
For example, here, we could name this first sale
127
00:05:50,510 --> 00:05:53,690
which you wanna add s1 and click on the plus again
128
00:05:53,690 --> 00:05:56,810
and now add the data that belongs to that sale.
129
00:05:56,810 --> 00:06:00,303
Like for example, the username, which could be Max,
130
00:06:01,220 --> 00:06:02,720
click on add now.
131
00:06:02,720 --> 00:06:05,420
And then click on the plus next to s1 again,
132
00:06:05,420 --> 00:06:08,773
and also add a volume of let's say 100.
133
00:06:10,120 --> 00:06:13,350
And now let's click on the plus next to sales again,
134
00:06:13,350 --> 00:06:17,185
add a sale with a name of s2 and click on this plus.
135
00:06:17,185 --> 00:06:21,410
And here add a username of manual,
136
00:06:21,410 --> 00:06:24,230
and then click on the plus next to s2,
137
00:06:24,230 --> 00:06:26,870
and add a volume of 50.
138
00:06:26,870 --> 00:06:28,270
Click on add.
139
00:06:28,270 --> 00:06:32,883
And now we added two sales entries to this dummy database.
140
00:06:34,350 --> 00:06:36,400
Now I mentioned that it would be this URL
141
00:06:37,310 --> 00:06:39,480
which we can use to send the request to,
142
00:06:39,480 --> 00:06:41,930
to interact with that database.
143
00:06:41,930 --> 00:06:43,300
And that is correct.
144
00:06:43,300 --> 00:06:45,260
I'll copy that URL.
145
00:06:45,260 --> 00:06:47,270
And now here in fetch,
146
00:06:47,270 --> 00:06:51,280
we can use that URL at least as a starter.
147
00:06:51,280 --> 00:06:54,380
And then here, we wanna send the request to the sales node
148
00:06:54,380 --> 00:06:56,240
and fetch all the sales.
149
00:06:56,240 --> 00:06:58,680
So, here I'll then add slash sales.
150
00:06:58,680 --> 00:07:00,520
And now in Firebase world,
151
00:07:00,520 --> 00:07:03,410
we also need to add dot json at the end.
152
00:07:03,410 --> 00:07:06,330
This is not a Next.js requirement.
153
00:07:06,330 --> 00:07:08,020
This is really just required
154
00:07:08,020 --> 00:07:11,110
by this Firebase API we're using.
155
00:07:11,110 --> 00:07:14,190
And this will send a request to this sales node
156
00:07:14,190 --> 00:07:16,300
in this Firebase database.
157
00:07:16,300 --> 00:07:18,973
Again, we just use it as a dummy back-end here.
158
00:07:20,550 --> 00:07:24,200
Now, fetch this built-in browser API,
159
00:07:24,200 --> 00:07:26,690
this function which is built into the browser,
160
00:07:26,690 --> 00:07:28,580
then returns a promise.
161
00:07:28,580 --> 00:07:31,240
So, here we can add a then block
162
00:07:31,240 --> 00:07:33,610
to work with the response
163
00:07:33,610 --> 00:07:35,800
which we actually get back here.
164
00:07:35,800 --> 00:07:37,120
And on this response,
165
00:07:37,120 --> 00:07:42,120
we can call dot json to read the data from that response.
166
00:07:42,190 --> 00:07:44,240
That's how this fetch API works.
167
00:07:44,240 --> 00:07:46,960
Again, nothing Next.js specific.
168
00:07:46,960 --> 00:07:49,760
This is the standard browser fetch API
169
00:07:49,760 --> 00:07:50,963
and how we can use it.
170
00:07:52,050 --> 00:07:54,670
So now with that, we're taking the response
171
00:07:54,670 --> 00:07:57,230
and extracting the response data.
172
00:07:57,230 --> 00:07:59,690
This again, then returns a promise.
173
00:07:59,690 --> 00:08:02,150
So we can chain another then call here
174
00:08:02,150 --> 00:08:04,330
and this will then give us the actual data
175
00:08:04,330 --> 00:08:06,403
which we parsed from the response.
176
00:08:07,290 --> 00:08:11,030
And it's now in here in this function here,
177
00:08:11,030 --> 00:08:13,993
where we can use that data which we received.
178
00:08:15,080 --> 00:08:18,730
Now, we might want to update the state of this component
179
00:08:18,730 --> 00:08:20,680
to output the data down there,
180
00:08:20,680 --> 00:08:22,610
once we did get it.
181
00:08:22,610 --> 00:08:26,500
And for this we can import to useState hook here.
182
00:08:26,500 --> 00:08:29,250
And register a state in this component.
183
00:08:29,250 --> 00:08:34,169
Let's say the data state or the sales state
184
00:08:36,270 --> 00:08:40,620
with a setSales state updating function.
185
00:08:40,620 --> 00:08:43,809
And we call useState to initialize this state.
186
00:08:43,809 --> 00:08:46,620
And initially let's say that's undefined
187
00:08:46,620 --> 00:08:49,500
because initially we have no sales.
188
00:08:49,500 --> 00:08:52,290
And maybe we also want a loading state,
189
00:08:52,290 --> 00:08:54,070
the isLoading state,
190
00:08:54,070 --> 00:08:56,680
so that we can show some loading fallback
191
00:08:56,680 --> 00:08:59,900
whilst we're waiting for that data to arrive.
192
00:08:59,900 --> 00:09:03,600
So here I'll then also set up this state
193
00:09:03,600 --> 00:09:05,943
and initially it's false.
194
00:09:07,260 --> 00:09:10,060
And now in useEffect in this function,
195
00:09:10,060 --> 00:09:13,762
we wanna set isLoading to true here at the beginning
196
00:09:13,762 --> 00:09:16,040
to set this loading state to true,
197
00:09:16,040 --> 00:09:18,520
because we are now fetching data.
198
00:09:18,520 --> 00:09:22,600
And once we got the data here in this second then block,
199
00:09:22,600 --> 00:09:26,360
we wanna setSales equal to our sales data.
200
00:09:26,360 --> 00:09:28,480
And, balloon that in a second.
201
00:09:28,480 --> 00:09:33,480
And we also want to set isLoading to false here again,
202
00:09:34,010 --> 00:09:35,773
because we're not loading anymore.
203
00:09:36,690 --> 00:09:38,410
Now regarding the sales data,
204
00:09:38,410 --> 00:09:40,100
it is important to know
205
00:09:40,100 --> 00:09:42,660
that the data we'll get back from Firebase
206
00:09:42,660 --> 00:09:44,450
will not be an array,
207
00:09:44,450 --> 00:09:47,690
but it will be an object where these Ids here,
208
00:09:47,690 --> 00:09:50,570
s1 and s2 will be keys,
209
00:09:50,570 --> 00:09:54,560
which then holds the detailed data as nested objects.
210
00:09:54,560 --> 00:09:58,200
So, if we want an array, we need to transform this data.
211
00:09:58,200 --> 00:10:01,187
So, here we can have our transformedSales
212
00:10:02,860 --> 00:10:04,400
which is an array.
213
00:10:04,400 --> 00:10:07,440
And we built that with a four in loop,
214
00:10:07,440 --> 00:10:10,510
by looping through all the keys in data.
215
00:10:10,510 --> 00:10:12,780
Because again, data will be an object
216
00:10:12,780 --> 00:10:17,163
where our Ids s1 and s2 will be keys.
217
00:10:18,260 --> 00:10:20,100
So then for transformedSales,
218
00:10:20,100 --> 00:10:22,540
we push an object into this array.
219
00:10:22,540 --> 00:10:25,720
And that object maybe has an Id property,
220
00:10:25,720 --> 00:10:27,840
which has the key as a value,
221
00:10:27,840 --> 00:10:30,943
because again, the keys are these Ids.
222
00:10:32,850 --> 00:10:35,960
And then we have our username field
223
00:10:35,960 --> 00:10:38,810
which is data for the given key.
224
00:10:38,810 --> 00:10:41,460
Dynamically accessing it with the square brackets,
225
00:10:41,460 --> 00:10:45,230
which is standard JavaScript dot username.
226
00:10:45,230 --> 00:10:48,580
So now we're diving into that nested object here,
227
00:10:48,580 --> 00:10:50,600
accessing the username.
228
00:10:50,600 --> 00:10:52,450
And we wanna do the same for the volume.
229
00:10:52,450 --> 00:10:55,020
So, I'll set a volume key here
230
00:10:55,020 --> 00:11:00,020
and set this equal to data for the given key dot volume.
231
00:11:01,320 --> 00:11:03,973
Now I'll reformat this and it looks like this.
232
00:11:05,470 --> 00:11:08,870
So now this is how we transform our sales
233
00:11:08,870 --> 00:11:11,290
from an object to an array.
234
00:11:11,290 --> 00:11:16,290
And then I can set these transformedSales as my new sales.
235
00:11:17,420 --> 00:11:20,840
And that would be a very standard reactive way
236
00:11:20,840 --> 00:11:22,633
of sending such a request.
237
00:11:23,650 --> 00:11:28,020
We can now use it and check if we are loading here.
238
00:11:28,020 --> 00:11:31,400
And if we are, I return a paragraph where I say,
239
00:11:31,400 --> 00:11:33,160
loading let's say,
240
00:11:33,160 --> 00:11:36,260
or where we show some loading spinner off course.
241
00:11:36,260 --> 00:11:38,890
And otherwise here in the unordered list,
242
00:11:38,890 --> 00:11:43,890
we can also access our sales then, our sales state.
243
00:11:43,970 --> 00:11:45,960
And map our sales
244
00:11:45,960 --> 00:11:50,010
so that every sale is mapped into a list item
245
00:11:50,010 --> 00:11:53,390
where the key is set to sale.id
246
00:11:53,390 --> 00:11:57,606
and where we then output the sale username.
247
00:11:57,606 --> 00:11:59,623
Then a dash maybe,
248
00:12:00,850 --> 00:12:04,610
and then after that the sale volume
249
00:12:04,610 --> 00:12:06,470
and maybe a dollar sign in front of it
250
00:12:06,470 --> 00:12:08,283
but that's just cosmetics.
251
00:12:10,180 --> 00:12:12,220
With all of that in place,
252
00:12:12,220 --> 00:12:15,200
we now got this client-side code.
253
00:12:15,200 --> 00:12:17,820
Now we can spin up our development server again
254
00:12:17,820 --> 00:12:19,350
to preview this.
255
00:12:19,350 --> 00:12:23,400
For this I'll go to local host last dash sales,
256
00:12:23,400 --> 00:12:26,310
since that last a page name we added.
257
00:12:26,310 --> 00:12:27,470
And here I get an error
258
00:12:27,470 --> 00:12:30,600
cannot read property map of undefined.
259
00:12:30,600 --> 00:12:33,110
That makes a lot of sense because initially
260
00:12:33,110 --> 00:12:35,163
isLoading is false.
261
00:12:36,220 --> 00:12:38,520
And our sales state
262
00:12:38,520 --> 00:12:41,610
which we try to use down there is undefined.
263
00:12:41,610 --> 00:12:44,520
Now in useEffect we change this loading state,
264
00:12:44,520 --> 00:12:46,000
but the way React works
265
00:12:46,000 --> 00:12:49,825
is such that useEffect only executes the effect function
266
00:12:49,825 --> 00:12:52,790
after the whole component was evaluated
267
00:12:52,790 --> 00:12:55,320
and rendered for the first time.
268
00:12:55,320 --> 00:12:57,600
And for this first render cycle
269
00:12:57,600 --> 00:12:59,640
sales therefore will be undefined
270
00:12:59,640 --> 00:13:01,300
and state will be false.
271
00:13:01,300 --> 00:13:02,860
So we'll reach that code
272
00:13:02,860 --> 00:13:04,680
and we'll then try to call map
273
00:13:04,680 --> 00:13:06,383
on something which is undefined.
274
00:13:07,370 --> 00:13:08,850
So therefore I'll add another check
275
00:13:08,850 --> 00:13:10,950
and check if not sales.
276
00:13:10,950 --> 00:13:13,210
So if we have no sales yet,
277
00:13:13,210 --> 00:13:18,210
in which case I return no data yet here as a paragraph.
278
00:13:19,460 --> 00:13:21,240
Now with data, if we save this,
279
00:13:21,240 --> 00:13:23,100
with this extra check added,
280
00:13:23,100 --> 00:13:25,150
now if I reload you see loading
281
00:13:25,150 --> 00:13:27,940
and then you see that data eventually.
282
00:13:27,940 --> 00:13:30,793
And that is how we could fetch data on the client side.
283
00:13:31,670 --> 00:13:34,480
Now, if we inspect the page source here,
284
00:13:34,480 --> 00:13:38,540
then the interesting thing is that we see no data yet.
285
00:13:38,540 --> 00:13:41,150
And that hopefully makes sense.
286
00:13:41,150 --> 00:13:45,110
The pages still pre-rendered by Next.js.
287
00:13:45,110 --> 00:13:47,640
Because I explained earlier in this module
288
00:13:47,640 --> 00:13:50,440
that that would be the default page
289
00:13:50,440 --> 00:13:53,230
which does not use get server side props,
290
00:13:53,230 --> 00:13:57,550
effectively will be pre-rendered by Next.js.
291
00:13:57,550 --> 00:14:01,480
The key thing here just is that the data used by this page
292
00:14:01,480 --> 00:14:04,300
will not be prepared by Next.js
293
00:14:04,300 --> 00:14:06,273
with get static props for example.
294
00:14:07,600 --> 00:14:11,030
Hence, when Next.js pre renders this page,
295
00:14:11,030 --> 00:14:13,360
it will not execute useEffect.
296
00:14:13,360 --> 00:14:15,130
It will not wait for this.
297
00:14:15,130 --> 00:14:16,780
It will not care about that.
298
00:14:16,780 --> 00:14:18,420
It will just return
299
00:14:18,420 --> 00:14:22,330
and pre-render the very basic first version
300
00:14:22,330 --> 00:14:24,820
of what this component spits out.
301
00:14:24,820 --> 00:14:26,590
And that has no data yet
302
00:14:26,590 --> 00:14:29,880
for the reasons I explained a couple of seconds ago.
303
00:14:29,880 --> 00:14:32,680
That is the initial state of this page.
304
00:14:32,680 --> 00:14:36,750
And that is what gets pre-rendered therefore.
305
00:14:36,750 --> 00:14:39,310
So we still have pre-rendering going on,
306
00:14:39,310 --> 00:14:40,980
but without the data.
307
00:14:40,980 --> 00:14:44,320
Because we're fetching the data on the client side now,
308
00:14:44,320 --> 00:14:45,583
with this approach.
23543
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.