Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,080 --> 00:00:04,320
So we've been creating routes and pages,
2
00:00:04,320 --> 00:00:06,960
and I've mentioned that we're using something
3
00:00:06,960 --> 00:00:09,480
called React Server Components,
4
00:00:09,480 --> 00:00:14,480
but actually we have no idea what those are right now.
5
00:00:14,580 --> 00:00:18,990
Now React Server Components are by far the biggest
6
00:00:18,990 --> 00:00:22,200
and hottest topic in React right now
7
00:00:22,200 --> 00:00:25,350
and will probably be for a long time.
8
00:00:25,350 --> 00:00:28,971
And so let's now dive really deep into this new paradigm.
9
00:00:28,971 --> 00:00:33,630
And I've divided this lecture into multiple parts.
10
00:00:33,630 --> 00:00:35,820
So in this first part we'll learn
11
00:00:35,820 --> 00:00:38,220
what React Server Components are,
12
00:00:38,220 --> 00:00:40,230
and then later on we're going to learn
13
00:00:40,230 --> 00:00:43,020
how they actually work behind the scenes
14
00:00:43,020 --> 00:00:45,990
and how React Server Components compare
15
00:00:45,990 --> 00:00:48,540
to server-side rendering.
16
00:00:48,540 --> 00:00:51,240
So this is gonna be a long ride,
17
00:00:51,240 --> 00:00:53,850
similar to the behind-the-scenes section
18
00:00:53,850 --> 00:00:55,590
earlier in the course.
19
00:00:55,590 --> 00:00:58,650
And so let's get started.
20
00:00:58,650 --> 00:01:02,610
And let's actually start by asking the question,
21
00:01:02,610 --> 00:01:05,850
why do we need something like server components
22
00:01:05,850 --> 00:01:07,740
in the first place?
23
00:01:07,740 --> 00:01:09,960
Well, to answer that question,
24
00:01:09,960 --> 00:01:13,230
let's go back to the very beginning of the course
25
00:01:13,230 --> 00:01:16,650
where we discussed that in React applications,
26
00:01:16,650 --> 00:01:20,070
we can imagine the UI as being a function
27
00:01:20,070 --> 00:01:23,220
of state changing over time.
28
00:01:23,220 --> 00:01:28,220
So in a typical 100% client-side rendered React app,
29
00:01:28,470 --> 00:01:33,030
as we keep updating state, the app simply keeps re rendering
30
00:01:33,030 --> 00:01:36,960
and showing different parts of the UI and layout.
31
00:01:36,960 --> 00:01:41,700
So that's what we mean by UI is a function of state.
32
00:01:41,700 --> 00:01:44,790
Now, we do hack this idea a little bit
33
00:01:44,790 --> 00:01:47,940
by also storing fetch data in state,
34
00:01:47,940 --> 00:01:51,000
but more about data in a moment.
35
00:01:51,000 --> 00:01:55,110
But anyway, this approach is great for users
36
00:01:55,110 --> 00:01:57,930
because apps are super interactive
37
00:01:57,930 --> 00:02:00,810
and it's great for us developers too
38
00:02:00,810 --> 00:02:04,200
because it lets us write all this interactivity
39
00:02:04,200 --> 00:02:06,510
inside these small, reusable
40
00:02:06,510 --> 00:02:10,800
and composable packages that we call components.
41
00:02:10,800 --> 00:02:13,380
But of course there are also downsides
42
00:02:13,380 --> 00:02:16,920
to 100% client-side rendered apps,
43
00:02:16,920 --> 00:02:19,170
and we already discussed some of them,
44
00:02:19,170 --> 00:02:22,830
but here I just want to focus on two facts.
45
00:02:22,830 --> 00:02:26,550
First, React apps require a lot of JavaScript
46
00:02:26,550 --> 00:02:29,160
to be downloaded to the user's computer,
47
00:02:29,160 --> 00:02:32,310
which can have huge impacts on performance
48
00:02:32,310 --> 00:02:35,850
and is always a constraint for us developers
49
00:02:35,850 --> 00:02:38,040
that we have to constantly think about
50
00:02:38,040 --> 00:02:40,470
when we're building our apps.
51
00:02:40,470 --> 00:02:44,760
And second, our client-server waterfalls.
52
00:02:44,760 --> 00:02:48,210
And a client-server waterfall is a problem
53
00:02:48,210 --> 00:02:51,690
that happens when multiple components on a page
54
00:02:51,690 --> 00:02:55,530
need to fetch different data one after another.
55
00:02:55,530 --> 00:02:59,490
So when data from one component depends on the data fetched
56
00:02:59,490 --> 00:03:01,560
in another component,
57
00:03:01,560 --> 00:03:05,340
this is a very common problem in large applications,
58
00:03:05,340 --> 00:03:08,130
caused by the fact that we have to fetch data
59
00:03:08,130 --> 00:03:13,130
on the client when we have a 100% client-side app.
60
00:03:13,560 --> 00:03:15,780
Now the alternative it seems
61
00:03:15,780 --> 00:03:19,800
is to have a 100% server-side rendered app
62
00:03:19,800 --> 00:03:24,120
like we used to have in the good old days of PHP.
63
00:03:24,120 --> 00:03:26,250
And maybe you're old enough to remember
64
00:03:26,250 --> 00:03:30,030
that back then we had no interactivity
65
00:03:30,030 --> 00:03:32,850
and no components at all.
66
00:03:32,850 --> 00:03:35,070
What we did have was an easy
67
00:03:35,070 --> 00:03:39,510
and fast way to fetch data directly from the data source,
68
00:03:39,510 --> 00:03:42,090
for example, from a database.
69
00:03:42,090 --> 00:03:46,410
Therefore there was no need to build an API to communicate
70
00:03:46,410 --> 00:03:48,690
between frontend and backend,
71
00:03:48,690 --> 00:03:52,260
because we were always on the backend anyway.
72
00:03:52,260 --> 00:03:54,570
And for displaying all this data,
73
00:03:54,570 --> 00:03:57,270
no JavaScript was needed at all,
74
00:03:57,270 --> 00:04:01,350
meaning faster page load for our users.
75
00:04:01,350 --> 00:04:03,108
So in this case, we could say that the UI
76
00:04:03,108 --> 00:04:08,108
was not at all a function of state, but of data.
77
00:04:10,320 --> 00:04:13,560
So in this approach, it's really the data
78
00:04:13,560 --> 00:04:16,620
that determines the content of the UI.
79
00:04:16,620 --> 00:04:19,623
And this actually also sounds great.
80
00:04:20,730 --> 00:04:24,210
It makes a lot of sense that data is at the center
81
00:04:24,210 --> 00:04:26,130
of generating pages,
82
00:04:26,130 --> 00:04:30,090
but the same is also true for state.
83
00:04:30,090 --> 00:04:33,690
It also makes sense for state to determine the UI
84
00:04:33,690 --> 00:04:35,700
as we just saw earlier.
85
00:04:35,700 --> 00:04:38,523
So both are completely valid approaches.
86
00:04:39,420 --> 00:04:43,290
So what do we do then with all this information?
87
00:04:43,290 --> 00:04:47,940
How do we achieve a UI that's a function both of state
88
00:04:47,940 --> 00:04:50,820
and of data, which is much closer
89
00:04:50,820 --> 00:04:54,660
to what real-world applications actually look like?
90
00:04:54,660 --> 00:04:58,650
Or in other words, what if this whole diagram didn't have
91
00:04:58,650 --> 00:05:02,580
to be so black and white, or in this case actually,
92
00:05:02,580 --> 00:05:04,380
so blue and yellow,
93
00:05:04,380 --> 00:05:07,680
what if there could be more of a gradient,
94
00:05:07,680 --> 00:05:12,000
something in between that's not 100% client
95
00:05:12,000 --> 00:05:15,480
or server-side, something that would give us
96
00:05:15,480 --> 00:05:20,480
all the advantages of both client-side and server-side.
97
00:05:20,850 --> 00:05:24,330
So what we're really asking here is this.
98
00:05:24,330 --> 00:05:26,550
What if we could take the best
99
00:05:26,550 --> 00:05:31,260
of both worlds having React components both on the server
100
00:05:31,260 --> 00:05:32,877
and on the client,
101
00:05:32,877 --> 00:05:36,870
and therefore being able to be either interactive
102
00:05:36,870 --> 00:05:39,150
or to be close to the data source
103
00:05:39,150 --> 00:05:42,150
and ship no JavaScript at all?
104
00:05:42,150 --> 00:05:44,700
Well, the answer to all this
105
00:05:44,700 --> 00:05:47,850
is a completely new React paradigm,
106
00:05:47,850 --> 00:05:51,570
a completely new way of building React apps
107
00:05:51,570 --> 00:05:53,670
that changes everything
108
00:05:53,670 --> 00:05:57,600
and it's called React Server Components.
109
00:05:57,600 --> 00:06:00,840
But what are React Server Components?
110
00:06:00,840 --> 00:06:02,553
Well, let's find out.
111
00:06:03,840 --> 00:06:07,020
Now before we even start this conversation,
112
00:06:07,020 --> 00:06:09,690
let me clarify that we're back to learning
113
00:06:09,690 --> 00:06:12,420
pure React in this lecture.
114
00:06:12,420 --> 00:06:16,410
So for now, please just forget about Next.js
115
00:06:16,410 --> 00:06:18,420
and server-side rendering.
116
00:06:18,420 --> 00:06:21,420
Don't even think about them for now.
117
00:06:21,420 --> 00:06:24,180
We're gonna bring all these concepts back together
118
00:06:24,180 --> 00:06:26,040
in the future, but for now,
119
00:06:26,040 --> 00:06:30,363
let's remember that we're just talking about React itself.
120
00:06:31,320 --> 00:06:33,450
So with that out of the way,
121
00:06:33,450 --> 00:06:36,930
React Server Components is a completely new
122
00:06:36,930 --> 00:06:41,070
full-stack architecture for building React apps.
123
00:06:41,070 --> 00:06:43,110
It introduces the server
124
00:06:43,110 --> 00:06:46,560
as a first class citizen in React apps,
125
00:06:46,560 --> 00:06:50,520
which means that the server is now an integral part
126
00:06:50,520 --> 00:06:54,630
of the React componentries of our applications.
127
00:06:54,630 --> 00:06:59,250
Or in other words, the Reactory now extends all the way
128
00:06:59,250 --> 00:07:02,550
to the server, like a bridge that closes the gap
129
00:07:02,550 --> 00:07:04,500
between client and server.
130
00:07:04,500 --> 00:07:07,500
And the way in which React does that
131
00:07:07,500 --> 00:07:10,890
is by introducing a new kind of component
132
00:07:10,890 --> 00:07:13,860
which are server components.
133
00:07:13,860 --> 00:07:17,100
Now this is already starting to become confusing,
134
00:07:17,100 --> 00:07:20,460
and so let's clear that confusion right away.
135
00:07:20,460 --> 00:07:23,010
The term React Server Components,
136
00:07:23,010 --> 00:07:26,580
which you'll often see abbreviated as RSC,
137
00:07:26,580 --> 00:07:29,310
is the name of this whole new paradigm,
138
00:07:29,310 --> 00:07:31,620
so, this new architecture.
139
00:07:31,620 --> 00:07:35,610
The term server component alone is then the name
140
00:07:35,610 --> 00:07:40,410
of the new type of component that RSC introduces.
141
00:07:40,410 --> 00:07:42,090
And these are components
142
00:07:42,090 --> 00:07:47,090
that are only rendered on the server, never on the client.
143
00:07:47,220 --> 00:07:48,810
They're usually responsible
144
00:07:48,810 --> 00:07:51,780
for fetching data right on the server.
145
00:07:51,780 --> 00:07:55,260
And so to use the idea of the previous slide,
146
00:07:55,260 --> 00:08:00,260
their goal is to make part of the UI a function of the data
147
00:08:00,630 --> 00:08:02,730
that powers the application.
148
00:08:02,730 --> 00:08:06,600
Now, since server components run only on the server,
149
00:08:06,600 --> 00:08:10,290
they have no interactivity, so no state,
150
00:08:10,290 --> 00:08:14,130
which means that they require exactly zero JavaScript
151
00:08:14,130 --> 00:08:17,670
in the downloadable bundle to do their job.
152
00:08:17,670 --> 00:08:21,750
And this is one of the main advantages of this model
153
00:08:21,750 --> 00:08:23,880
as we'll see later.
154
00:08:23,880 --> 00:08:27,270
So if we think about this, with server components,
155
00:08:27,270 --> 00:08:30,720
we can essentially build our application's backend
156
00:08:30,720 --> 00:08:35,010
also with React, which is pretty amazing.
157
00:08:35,010 --> 00:08:37,350
Now, besides server components,
158
00:08:37,350 --> 00:08:40,982
of course we still have our old regular components
159
00:08:40,982 --> 00:08:45,983
that we already know and that run entirely on the client.
160
00:08:46,020 --> 00:08:49,137
Therefore these are called client components
161
00:08:49,137 --> 00:08:52,440
and they are responsible for the interactivity.
162
00:08:52,440 --> 00:08:55,170
So for the part of the app where the UI
163
00:08:55,170 --> 00:08:59,610
is still a function of state, so together,
164
00:08:59,610 --> 00:09:02,100
client and server components allow us
165
00:09:02,100 --> 00:09:03,870
to write frontend code,
166
00:09:03,870 --> 00:09:06,270
right next to backend code
167
00:09:06,270 --> 00:09:09,060
in a way that feels completely natural
168
00:09:09,060 --> 00:09:12,510
because it feels just like the regular React apps
169
00:09:12,510 --> 00:09:14,850
that we've been building so far,
170
00:09:14,850 --> 00:09:17,460
we're still using just components
171
00:09:17,460 --> 00:09:19,500
and can even compose server
172
00:09:19,500 --> 00:09:23,700
and client components together to build full-stack apps
173
00:09:23,700 --> 00:09:28,230
that are really entirely controlled by React.
174
00:09:28,230 --> 00:09:30,810
Now here is one very important thing
175
00:09:30,810 --> 00:09:33,690
to understand about all this.
176
00:09:33,690 --> 00:09:36,870
React Server Components are not active
177
00:09:36,870 --> 00:09:40,110
by default in new React apps.
178
00:09:40,110 --> 00:09:41,550
So for example,
179
00:09:41,550 --> 00:09:45,030
if we just start a new React project in Vite
180
00:09:45,030 --> 00:09:49,590
like we did earlier, we will not get this architecture.
181
00:09:49,590 --> 00:09:51,150
And this makes sense
182
00:09:51,150 --> 00:09:53,610
because there is no server involved
183
00:09:53,610 --> 00:09:55,383
in a Vite application.
184
00:09:56,490 --> 00:09:59,250
So the server needs to be somewhere,
185
00:09:59,250 --> 00:10:02,790
it needs to be integrated into this whole idea.
186
00:10:02,790 --> 00:10:06,630
And so that's where full-stack frameworks like Next.js
187
00:10:06,630 --> 00:10:10,980
or Remix finally come into the picture again.
188
00:10:10,980 --> 00:10:15,210
So basically, React provides this architecture
189
00:10:15,210 --> 00:10:18,180
and frameworks can then choose to adopt
190
00:10:18,180 --> 00:10:20,850
and to implement it if they want.
191
00:10:20,850 --> 00:10:25,590
And that's exactly what Next.js did in the app router.
192
00:10:25,590 --> 00:10:29,550
So any app that we built in the Next.js app folder
193
00:10:29,550 --> 00:10:32,940
will use this new RSC architecture
194
00:10:32,940 --> 00:10:34,380
in which server components
195
00:10:34,380 --> 00:10:38,160
become the default components actually.
196
00:10:38,160 --> 00:10:39,510
So again,
197
00:10:39,510 --> 00:10:43,650
server components are now the new default components
198
00:10:43,650 --> 00:10:45,450
in this new model.
199
00:10:45,450 --> 00:10:49,380
Client components then basically become opt in,
200
00:10:49,380 --> 00:10:52,080
so we need to specifically tell a component
201
00:10:52,080 --> 00:10:56,490
that it should be a client component if that's what we need.
202
00:10:56,490 --> 00:11:00,510
And we do so by using the new use client directive
203
00:11:00,510 --> 00:11:02,160
at the top of the module
204
00:11:02,160 --> 00:11:04,653
that contains the component definition.
205
00:11:05,640 --> 00:11:10,440
Now, okay, so this is probably a lot of new stuff to absorb.
206
00:11:10,440 --> 00:11:12,330
And so let's look at an example
207
00:11:12,330 --> 00:11:15,033
to make this a bit more practical.
208
00:11:16,560 --> 00:11:21,560
So let's divide this UI from the Wild Oasis app into client
209
00:11:22,080 --> 00:11:25,620
and server components so that we understand the difference
210
00:11:25,620 --> 00:11:28,470
between them just a bit better.
211
00:11:28,470 --> 00:11:32,580
So on a high level we have this sidebar, header,
212
00:11:32,580 --> 00:11:34,710
and the main area.
213
00:11:34,710 --> 00:11:37,020
Now none of these are interactive,
214
00:11:37,020 --> 00:11:39,270
so we can't really click on them
215
00:11:39,270 --> 00:11:42,840
or at least they won't do anything if we do so.
216
00:11:42,840 --> 00:11:46,020
I mean we can of course click on some elements
217
00:11:46,020 --> 00:11:49,890
that are inside of them, but the sidebar, header
218
00:11:49,890 --> 00:11:54,890
and main elements themselves are not interactive at all.
219
00:11:55,140 --> 00:11:58,230
This means that they can be rendered on the server
220
00:11:58,230 --> 00:12:00,090
as server components,
221
00:12:00,090 --> 00:12:04,380
so without shipping any JavaScript to the browser.
222
00:12:04,380 --> 00:12:08,790
Okay, so let's build ourselves a componentry out of this,
223
00:12:08,790 --> 00:12:11,910
starting with the generic app on the top
224
00:12:11,910 --> 00:12:16,410
and then our three server components in yellow.
225
00:12:16,410 --> 00:12:18,600
All right, so let's keep going
226
00:12:18,600 --> 00:12:22,290
and look for a few more server components.
227
00:12:22,290 --> 00:12:25,860
So right in the header we have this Avatar,
228
00:12:25,860 --> 00:12:28,590
which has a photo and a name.
229
00:12:28,590 --> 00:12:31,620
And this component is also not interactive
230
00:12:31,620 --> 00:12:35,010
and its data needed to be fetched from somewhere,
231
00:12:35,010 --> 00:12:38,130
which means that this is another great candidate
232
00:12:38,130 --> 00:12:42,720
for a server component because in this new model,
233
00:12:42,720 --> 00:12:44,010
whenever possible,
234
00:12:44,010 --> 00:12:46,770
data should be fetched right on the server
235
00:12:46,770 --> 00:12:48,990
for the first render to be faster
236
00:12:48,990 --> 00:12:52,110
and to avoid client-server waterfalls.
237
00:12:52,110 --> 00:12:53,910
And so again,
238
00:12:53,910 --> 00:12:58,500
that's why this one should probably be a server component.
239
00:12:58,500 --> 00:13:01,620
And the same applies to this table here,
240
00:13:01,620 --> 00:13:03,810
which is also not interactive
241
00:13:03,810 --> 00:13:07,980
and is probably responsible for fetching all the cabin data
242
00:13:07,980 --> 00:13:09,540
and rendering it,
243
00:13:09,540 --> 00:13:14,037
also using this other server component CabinRow.
244
00:13:14,037 --> 00:13:17,550
All right, but here comes the interesting part
245
00:13:17,550 --> 00:13:20,970
where we now look for client components.
246
00:13:20,970 --> 00:13:23,910
And remember that client components are the ones
247
00:13:23,910 --> 00:13:26,130
that need interactivity.
248
00:13:26,130 --> 00:13:29,760
So they need JavaScript in the browser in order to work.
249
00:13:29,760 --> 00:13:33,600
So they are the React components that we already know.
250
00:13:33,600 --> 00:13:35,610
And starting again in the header,
251
00:13:35,610 --> 00:13:39,270
we have this DarkMode toggle that we can click,
252
00:13:39,270 --> 00:13:43,170
and therefore this one cannot be rendered on the server.
253
00:13:43,170 --> 00:13:46,530
It needs to be a client component.
254
00:13:46,530 --> 00:13:49,110
And the same happens with the filter
255
00:13:49,110 --> 00:13:51,270
and the SortBy components.
256
00:13:51,270 --> 00:13:54,540
The user actually needs to interact with these
257
00:13:54,540 --> 00:13:56,040
in order to make them work.
258
00:13:56,040 --> 00:14:00,810
And so once again, they cannot be rendered on the server.
259
00:14:00,810 --> 00:14:03,030
Now we're already starting to see
260
00:14:03,030 --> 00:14:06,840
that our component tree now contains backend code
261
00:14:06,840 --> 00:14:10,770
as well as frontend code, all mixed together
262
00:14:10,770 --> 00:14:12,990
and all controlled by React
263
00:14:12,990 --> 00:14:15,630
with the client components usually appearing
264
00:14:15,630 --> 00:14:20,010
at the end of the tree, so as the last children.
265
00:14:20,010 --> 00:14:21,240
But let's keep going,
266
00:14:21,240 --> 00:14:24,723
because it's about to get even more interesting.
267
00:14:25,560 --> 00:14:30,150
So right inside the CabinRow we have these three dots
268
00:14:30,150 --> 00:14:32,670
as a Menu, which if clicked,
269
00:14:32,670 --> 00:14:37,670
will then reveal the three buttons for different operations.
270
00:14:37,740 --> 00:14:41,820
Now remember how we need to opt into client components
271
00:14:41,820 --> 00:14:46,320
by using the use client directive at the top of the file.
272
00:14:46,320 --> 00:14:48,750
So that's necessary here in Menu,
273
00:14:48,750 --> 00:14:52,230
and also in DarkMode and SortBy,
274
00:14:52,230 --> 00:14:57,230
but notably not in the child components of Menu.
275
00:14:57,510 --> 00:15:01,860
So what this means is that child components of components
276
00:15:01,860 --> 00:15:04,440
that are already client components
277
00:15:04,440 --> 00:15:08,100
don't need to specify use client again,
278
00:15:08,100 --> 00:15:10,710
the reason for that is that these children
279
00:15:10,710 --> 00:15:15,710
are already inside the so-called server-client boundary.
280
00:15:16,290 --> 00:15:20,040
And this boundary is another super important concept
281
00:15:20,040 --> 00:15:22,470
in the RSC architecture.
282
00:15:22,470 --> 00:15:26,550
So React Server Components is all about boundaries
283
00:15:26,550 --> 00:15:30,870
because they are what marks the split points between code
284
00:15:30,870 --> 00:15:35,850
that runs on the server and code that runs on the client.
285
00:15:35,850 --> 00:15:39,150
So basically we use, use client,
286
00:15:39,150 --> 00:15:42,900
in order to create these client-server boundaries,
287
00:15:42,900 --> 00:15:46,680
which in turn will create client sub-trees.
288
00:15:46,680 --> 00:15:51,330
So sub-trees that will only be executed in the browser
289
00:15:51,330 --> 00:15:56,280
and technically SortBy and DarkMode are also sub-trees,
290
00:15:56,280 --> 00:15:58,860
but just very small ones.
291
00:15:58,860 --> 00:16:02,730
All right, so this practical example should give us
292
00:16:02,730 --> 00:16:05,730
a pretty good overview of what client
293
00:16:05,730 --> 00:16:10,650
and server components are and when to use each one of them.
294
00:16:10,650 --> 00:16:13,710
But now let's actually dive even deeper
295
00:16:13,710 --> 00:16:17,040
and compare these two types of components
296
00:16:17,040 --> 00:16:18,903
in a more structured way.
297
00:16:19,800 --> 00:16:21,150
So to start,
298
00:16:21,150 --> 00:16:24,120
server components are the default components
299
00:16:24,120 --> 00:16:25,860
in the RSC model.
300
00:16:25,860 --> 00:16:28,860
While we need to opt into client components
301
00:16:28,860 --> 00:16:33,570
by writing use client at the top of the module.
302
00:16:33,570 --> 00:16:37,590
Now, since client components are the interactive ones,
303
00:16:37,590 --> 00:16:41,850
only they can have state and use other hooks.
304
00:16:41,850 --> 00:16:44,820
Server components cannot be stateful
305
00:16:44,820 --> 00:16:48,870
and in fact they can't use any hooks whatsoever.
306
00:16:48,870 --> 00:16:51,540
It's pretty interesting why that is
307
00:16:51,540 --> 00:16:55,320
and we'll learn all about that in one of the next lectures
308
00:16:55,320 --> 00:16:59,073
where we look behind the scenes of React Server Components.
309
00:16:59,940 --> 00:17:02,370
Another important concept we learned about
310
00:17:02,370 --> 00:17:04,410
is lifting state up,
311
00:17:04,410 --> 00:17:06,960
which is of course not even applicable
312
00:17:06,960 --> 00:17:11,040
in server components since they cannot have state.
313
00:17:11,040 --> 00:17:13,200
Moving on to props now,
314
00:17:13,200 --> 00:17:16,140
we can pass props in server components
315
00:17:16,140 --> 00:17:18,839
just like in regular components
316
00:17:18,839 --> 00:17:22,170
and we can do so between two server components
317
00:17:22,170 --> 00:17:26,339
and also from server to client components,
318
00:17:26,339 --> 00:17:29,190
which is the extremely important way
319
00:17:29,190 --> 00:17:33,330
of passing data across the client-server boundary.
320
00:17:33,330 --> 00:17:36,810
There's just one small thing that we need to keep in mind
321
00:17:36,810 --> 00:17:38,250
in that scenario,
322
00:17:38,250 --> 00:17:42,390
which is the fact that props must be serializable.
323
00:17:42,390 --> 00:17:45,180
So they must be convertible to a format
324
00:17:45,180 --> 00:17:47,850
that can easily be transferred.
325
00:17:47,850 --> 00:17:52,050
Functions and classes are examples of data structures
326
00:17:52,050 --> 00:17:54,270
that are not serializable,
327
00:17:54,270 --> 00:17:58,320
and so we cannot pass those as props from server
328
00:17:58,320 --> 00:17:59,763
to client components.
329
00:18:00,660 --> 00:18:03,510
Now when it comes to data fetching,
330
00:18:03,510 --> 00:18:05,400
as we just learned previously,
331
00:18:05,400 --> 00:18:10,350
the preferred way of doing so is in server components.
332
00:18:10,350 --> 00:18:12,210
They are now the native,
333
00:18:12,210 --> 00:18:16,320
first-class way of fetching data in React.
334
00:18:16,320 --> 00:18:19,470
In fact, we can simply use async await
335
00:18:19,470 --> 00:18:23,310
right in the top-level code of a server component,
336
00:18:23,310 --> 00:18:27,660
which is something that won't work in client components.
337
00:18:27,660 --> 00:18:31,650
If we then need to make that data available to the client,
338
00:18:31,650 --> 00:18:36,030
we can just pass it as props as we saw a moment ago.
339
00:18:36,030 --> 00:18:40,380
Of course, we can also still fetch data in client components
340
00:18:40,380 --> 00:18:41,820
just like before,
341
00:18:41,820 --> 00:18:45,510
and preferably do so with a third party library
342
00:18:45,510 --> 00:18:47,310
like React Query.
343
00:18:47,310 --> 00:18:50,580
So just because we have server components now,
344
00:18:50,580 --> 00:18:53,280
doesn't mean that something like React Query
345
00:18:53,280 --> 00:18:55,383
is now completely obsolete.
346
00:18:56,490 --> 00:19:00,330
Next up, let's talk about what kind of component
347
00:19:00,330 --> 00:19:04,110
can be imported by each type of component.
348
00:19:04,110 --> 00:19:08,040
And here we need to actually distinguish between importing
349
00:19:08,040 --> 00:19:09,840
and rendering.
350
00:19:09,840 --> 00:19:13,560
So with import, we mean that the component module
351
00:19:13,560 --> 00:19:18,060
imports another module using the import syntax.
352
00:19:18,060 --> 00:19:20,520
On the other hand, with render,
353
00:19:20,520 --> 00:19:24,630
what we mean is that one component calls another one,
354
00:19:24,630 --> 00:19:29,630
so it uses another component inside its own JSX body.
355
00:19:29,880 --> 00:19:31,800
So keep that distinction in mind
356
00:19:31,800 --> 00:19:34,293
as it will be pretty important later on.
357
00:19:35,160 --> 00:19:36,390
But anyway,
358
00:19:36,390 --> 00:19:39,870
as we already noticed in the tree from the previous slide,
359
00:19:39,870 --> 00:19:43,500
several components can import both client
360
00:19:43,500 --> 00:19:45,390
and server components.
361
00:19:45,390 --> 00:19:50,100
And by the way, that tree was technically an import tree,
362
00:19:50,100 --> 00:19:53,250
so modules importing each other.
363
00:19:53,250 --> 00:19:55,710
Now, client components however
364
00:19:55,710 --> 00:19:59,220
can only import other client components
365
00:19:59,220 --> 00:20:02,040
but not server components.
366
00:20:02,040 --> 00:20:05,160
Once the client-server boundary has been passed,
367
00:20:05,160 --> 00:20:09,420
there is no way of going back to the server again,
368
00:20:09,420 --> 00:20:13,200
what is possible though is for client components
369
00:20:13,200 --> 00:20:15,720
to render server components,
370
00:20:15,720 --> 00:20:20,040
as long as these have been passed to them as props.
371
00:20:20,040 --> 00:20:23,280
And this is indeed extremely confusing.
372
00:20:23,280 --> 00:20:26,310
So I don't want to go into further detail here
373
00:20:26,310 --> 00:20:30,030
because we'll discuss this a bit better later.
374
00:20:30,030 --> 00:20:33,300
So here I just wanted to summarize everything.
375
00:20:33,300 --> 00:20:37,410
All right, but back to server components,
376
00:20:37,410 --> 00:20:40,590
they can also render everything.
377
00:20:40,590 --> 00:20:42,210
And now finally,
378
00:20:42,210 --> 00:20:45,210
one extremely important question is,
379
00:20:45,210 --> 00:20:48,990
when does this component type re-render?
380
00:20:48,990 --> 00:20:52,200
Well for client components it's very simple.
381
00:20:52,200 --> 00:20:57,200
It's whenever its own state or a parent state changes.
382
00:20:57,240 --> 00:20:59,580
So we've been using that idea
383
00:20:59,580 --> 00:21:01,293
throughout the entire course.
384
00:21:02,550 --> 00:21:06,990
But what about server components? They have no state.
385
00:21:06,990 --> 00:21:09,930
So do they ever get re-rendered?
386
00:21:09,930 --> 00:21:13,560
Well, as it turns out, the answer is yes,
387
00:21:13,560 --> 00:21:16,350
server components are executed again
388
00:21:16,350 --> 00:21:19,380
each time that the URL changes,
389
00:21:19,380 --> 00:21:21,930
so when the user navigates to the URL
390
00:21:21,930 --> 00:21:25,410
that has the server component in question,
391
00:21:25,410 --> 00:21:28,860
that's because usually server components are tied
392
00:21:28,860 --> 00:21:31,800
to specific routes in the framework,
393
00:21:31,800 --> 00:21:34,200
so in our case, Next.js.
394
00:21:34,200 --> 00:21:36,270
So when the URL changes
395
00:21:36,270 --> 00:21:38,700
and the framework moves to another route,
396
00:21:38,700 --> 00:21:42,990
all server components associated with that route re-render,
397
00:21:42,990 --> 00:21:47,283
so they are executed again and might even fetch data again.
398
00:21:48,210 --> 00:21:52,260
All right, so keep this summary handy as you start working
399
00:21:52,260 --> 00:21:54,570
with server components in the future
400
00:21:54,570 --> 00:21:58,200
because I belief it might be quite helpful.
401
00:21:58,200 --> 00:22:02,730
What's also gonna be helpful is to now get a mental model
402
00:22:02,730 --> 00:22:06,300
for how the RSC paradigm is different
403
00:22:06,300 --> 00:22:08,250
from traditional React,
404
00:22:08,250 --> 00:22:11,730
based on everything we learned so far.
405
00:22:11,730 --> 00:22:15,510
And let's start by looking at a simple mental model
406
00:22:15,510 --> 00:22:18,840
of traditional React on a very high level,
407
00:22:18,840 --> 00:22:22,560
which you should already be 100% familiar with
408
00:22:22,560 --> 00:22:24,480
at this point.
409
00:22:24,480 --> 00:22:27,000
So basically it all comes down
410
00:22:27,000 --> 00:22:31,440
to components which together display a view,
411
00:22:31,440 --> 00:22:34,200
so, a user interface.
412
00:22:34,200 --> 00:22:38,130
Now as the user interacts with this interface,
413
00:22:38,130 --> 00:22:40,350
for example by clicking a button,
414
00:22:40,350 --> 00:22:43,440
that interaction will update state,
415
00:22:43,440 --> 00:22:46,560
which in turn will re-render the components
416
00:22:46,560 --> 00:22:49,950
and display a new updated view.
417
00:22:49,950 --> 00:22:52,560
Components can also fetch data
418
00:22:52,560 --> 00:22:54,780
and store that data in state,
419
00:22:54,780 --> 00:22:59,430
which then again updates the view as the data changes.
420
00:22:59,430 --> 00:23:02,580
So this is nothing new, right?
421
00:23:02,580 --> 00:23:04,920
But now let's see how React
422
00:23:04,920 --> 00:23:08,700
with React Server Components enhances this model,
423
00:23:08,700 --> 00:23:11,880
how it gives this model an outer layer
424
00:23:11,880 --> 00:23:14,220
and makes it slightly more complex
425
00:23:14,220 --> 00:23:17,070
but still very manageable.
426
00:23:17,070 --> 00:23:18,360
So the core
427
00:23:18,360 --> 00:23:21,120
of the mental model we just talked about
428
00:23:21,120 --> 00:23:24,540
actually remains completely unchanged.
429
00:23:24,540 --> 00:23:28,380
All this is still valid in the RSC world
430
00:23:28,380 --> 00:23:30,180
with the only difference being
431
00:23:30,180 --> 00:23:33,000
that our regular components are now called
432
00:23:33,000 --> 00:23:34,860
client components.
433
00:23:34,860 --> 00:23:37,530
Now on top of client components,
434
00:23:37,530 --> 00:23:40,320
we now also have server components
435
00:23:40,320 --> 00:23:43,200
that run exclusively on the server
436
00:23:43,200 --> 00:23:46,863
but also participate in displaying their view.
437
00:23:47,730 --> 00:23:50,880
Now just to make sure we're all on the same page,
438
00:23:50,880 --> 00:23:55,740
this diagram is not about how React Server Components works
439
00:23:55,740 --> 00:23:57,120
behind the scenes.
440
00:23:57,120 --> 00:23:59,250
That's for a later lecture.
441
00:23:59,250 --> 00:24:03,060
Here, we're just trying to build up a model in our minds
442
00:24:03,060 --> 00:24:06,780
that will help us make sense of this new paradigm,
443
00:24:06,780 --> 00:24:09,060
so, to understand what it does
444
00:24:09,060 --> 00:24:11,820
and how we use it in practice.
445
00:24:11,820 --> 00:24:15,060
But anyway, back to our server components,
446
00:24:15,060 --> 00:24:19,200
these are now the primary way of fetching data.
447
00:24:19,200 --> 00:24:21,990
This data can then be rendered directly
448
00:24:21,990 --> 00:24:23,940
and displayed in the view,
449
00:24:23,940 --> 00:24:28,860
or it can be passed as props to client components.
450
00:24:28,860 --> 00:24:30,000
So server
451
00:24:30,000 --> 00:24:34,320
and client components are essentially connected by props.
452
00:24:34,320 --> 00:24:36,270
They are like the bridge
453
00:24:36,270 --> 00:24:39,360
across the client-server boundary.
454
00:24:39,360 --> 00:24:44,160
Okay, and now to finish this model, we just need to consider
455
00:24:44,160 --> 00:24:46,740
how server components re-render,
456
00:24:46,740 --> 00:24:50,400
which we already learned about in the previous slide.
457
00:24:50,400 --> 00:24:53,472
So as the user navigates around the page,
458
00:24:53,472 --> 00:24:56,070
the URL will be updated,
459
00:24:56,070 --> 00:24:59,430
which will then re-render the server component.
460
00:24:59,430 --> 00:25:02,670
This usually triggers fetching the data again,
461
00:25:02,670 --> 00:25:07,670
and therefore displaying an updated view with the new data.
462
00:25:07,920 --> 00:25:11,010
So in summary, in this new world,
463
00:25:11,010 --> 00:25:14,910
we have not only client components that re-render
464
00:25:14,910 --> 00:25:16,830
and display updated views,
465
00:25:16,830 --> 00:25:20,340
but also server components that re-render
466
00:25:20,340 --> 00:25:23,430
and display updated views as well.
467
00:25:23,430 --> 00:25:27,330
So we have two types of components that contribute
468
00:25:27,330 --> 00:25:28,830
to the same view,
469
00:25:28,830 --> 00:25:31,770
but by using different mechanisms.
470
00:25:31,770 --> 00:25:34,830
One type of component runs on the client
471
00:25:34,830 --> 00:25:36,330
and one on the server,
472
00:25:36,330 --> 00:25:39,900
and they can even be connected by props.
473
00:25:39,900 --> 00:25:42,420
And it's important to mention that one type
474
00:25:42,420 --> 00:25:46,260
of component is not better than the other one.
475
00:25:46,260 --> 00:25:50,250
They just have different purposes, but are both an important
476
00:25:50,250 --> 00:25:53,670
and integral part of this new model.
477
00:25:53,670 --> 00:25:54,503
Now, right.
478
00:25:54,503 --> 00:25:57,240
And now to finish this long lecture,
479
00:25:57,240 --> 00:25:59,280
which we're almost done with,
480
00:25:59,280 --> 00:26:02,070
let's just quickly look at the pros
481
00:26:02,070 --> 00:26:05,100
and cons of the RSC architecture
482
00:26:05,100 --> 00:26:08,730
as a kind of summary of this video.
483
00:26:08,730 --> 00:26:10,320
So in my opinion,
484
00:26:10,320 --> 00:26:13,830
the biggest selling point of the RSC model
485
00:26:13,830 --> 00:26:18,240
is that we can now write React components on the frontend
486
00:26:18,240 --> 00:26:20,160
and on the backend,
487
00:26:20,160 --> 00:26:22,980
meaning that we can now encapsulate
488
00:26:22,980 --> 00:26:27,750
all server-side concerns right into components as well.
489
00:26:27,750 --> 00:26:29,040
And by doing so,
490
00:26:29,040 --> 00:26:32,490
we can compose entire full-stack applications
491
00:26:32,490 --> 00:26:34,890
by only writing components,
492
00:26:34,890 --> 00:26:39,630
plus actually something called server actions for mutations,
493
00:26:39,630 --> 00:26:42,930
which we'll learn more about by the end of the course.
494
00:26:42,930 --> 00:26:46,020
And this just feels extremely natural
495
00:26:46,020 --> 00:26:49,980
and it also allows us to have just one single code base
496
00:26:49,980 --> 00:26:53,820
for the frontend and backend of our app.
497
00:26:53,820 --> 00:26:57,060
Also, we don't even have to build an API
498
00:26:57,060 --> 00:27:00,330
in order to access our data on the frontend
499
00:27:00,330 --> 00:27:02,070
in many situations,
500
00:27:02,070 --> 00:27:05,700
our server components are already on the backend,
501
00:27:05,700 --> 00:27:09,420
so they have a direct access to the data source.
502
00:27:09,420 --> 00:27:13,710
For example, we can directly grab some data from a database
503
00:27:13,710 --> 00:27:15,780
right in a server component
504
00:27:15,780 --> 00:27:18,960
and then simply send the already rendered page
505
00:27:18,960 --> 00:27:20,190
to the browser,
506
00:27:20,190 --> 00:27:24,120
so instead of sending only the data as JSON.
507
00:27:24,120 --> 00:27:27,570
Plus, if we need to access a third party API,
508
00:27:27,570 --> 00:27:29,160
we no longer need to worry
509
00:27:29,160 --> 00:27:32,490
about securely storing our API keys,
510
00:27:32,490 --> 00:27:36,630
which is always a problem in a pure client-side app.
511
00:27:36,630 --> 00:27:39,810
And staying on the topic of fetching data,
512
00:27:39,810 --> 00:27:42,030
doing so in server components
513
00:27:42,030 --> 00:27:46,380
can eliminate all kinds of client-server waterfalls,
514
00:27:46,380 --> 00:27:51,090
which is again, a huge win for performance.
515
00:27:51,090 --> 00:27:53,940
So instead of fetching one piece of data
516
00:27:53,940 --> 00:27:57,600
after another in different client components in the browser,
517
00:27:57,600 --> 00:27:59,580
we can just fetch all the data
518
00:27:59,580 --> 00:28:02,640
that we need on the server at once,
519
00:28:02,640 --> 00:28:06,030
and then send or stream the final result
520
00:28:06,030 --> 00:28:09,150
to the browser all in one go.
521
00:28:09,150 --> 00:28:13,080
Now server components don't require sending any JavaScript
522
00:28:13,080 --> 00:28:17,430
to the browser because that's not where they are executed,
523
00:28:17,430 --> 00:28:20,280
and we can call this disappearing code,
524
00:28:20,280 --> 00:28:23,190
because this fact allows us to import
525
00:28:23,190 --> 00:28:26,970
and use huge libraries in server components
526
00:28:26,970 --> 00:28:29,940
without increasing the bundle size at all,
527
00:28:29,940 --> 00:28:32,790
so basically for free.
528
00:28:32,790 --> 00:28:34,860
This is great for libraries
529
00:28:34,860 --> 00:28:37,710
that we really only need for rendering data,
530
00:28:37,710 --> 00:28:42,180
like libraries to connect your app to a CMS,
531
00:28:42,180 --> 00:28:47,180
embedding things like tweets, syntax highlighting and so on.
532
00:28:47,670 --> 00:28:50,610
So this is another win for performance,
533
00:28:50,610 --> 00:28:53,430
and therefore for the user experience,
534
00:28:53,430 --> 00:28:58,320
which is definitely a theme in all these RSC props.
535
00:28:58,320 --> 00:29:02,040
But now let's move on to some bad parts
536
00:29:02,040 --> 00:29:06,840
because they always exist in any technology as well.
537
00:29:06,840 --> 00:29:11,550
So first of all, it's undeniable that introducing RSC
538
00:29:11,550 --> 00:29:14,880
makes React a lot more complex.
539
00:29:14,880 --> 00:29:16,290
It's still manageable,
540
00:29:16,290 --> 00:29:19,410
but there are definitely more things to learn
541
00:29:19,410 --> 00:29:21,390
and to understand now.
542
00:29:21,390 --> 00:29:24,570
But luckily for you, you have this course,
543
00:29:24,570 --> 00:29:28,380
where I try to make it all as easy to understand as possible
544
00:29:28,380 --> 00:29:31,050
for you not only in this lecture
545
00:29:31,050 --> 00:29:33,960
but in some future ones as well.
546
00:29:33,960 --> 00:29:37,050
Then once we understand server components,
547
00:29:37,050 --> 00:29:39,510
using them is actually pretty easy
548
00:29:39,510 --> 00:29:44,510
and natural in practice, they have no state or effects,
549
00:29:44,760 --> 00:29:49,140
no stale closures or memorization-like client components,
550
00:29:49,140 --> 00:29:54,140
and therefore they're actually pretty easy to reason about.
551
00:29:54,150 --> 00:29:56,340
At some point we completely forget
552
00:29:56,340 --> 00:29:59,703
that we're even working on the server in some components.
553
00:30:00,600 --> 00:30:03,360
Now, some more practical cons are the fact
554
00:30:03,360 --> 00:30:06,300
that APIs like the context API
555
00:30:06,300 --> 00:30:10,920
don't work on server components just like all other hooks.
556
00:30:10,920 --> 00:30:15,720
And also that we need to make more decisions all the time.
557
00:30:15,720 --> 00:30:20,310
Things like should this be a client or a server component?
558
00:30:20,310 --> 00:30:23,040
Should I fetch this data on the server
559
00:30:23,040 --> 00:30:26,370
or on the client and so on.
560
00:30:26,370 --> 00:30:29,640
This again, comes from the added complexity
561
00:30:29,640 --> 00:30:31,230
of this architecture,
562
00:30:31,230 --> 00:30:34,740
but I believe it's still a good trade-off.
563
00:30:34,740 --> 00:30:39,740
Also, if on top of a web app, you also need a mobile app,
564
00:30:39,750 --> 00:30:43,200
you'll probably still need to build an API,
565
00:30:43,200 --> 00:30:46,950
even if you don't need one for the web application
566
00:30:46,950 --> 00:30:50,460
where you can just get data directly from the data source
567
00:30:50,460 --> 00:30:52,320
in server components.
568
00:30:52,320 --> 00:30:54,450
But if you're at that level,
569
00:30:54,450 --> 00:30:57,210
you're probably working in a larger team anyway,
570
00:30:57,210 --> 00:31:00,060
and then that should be no big deal.
571
00:31:00,060 --> 00:31:03,780
And finally, one thing that many people don't like
572
00:31:03,780 --> 00:31:05,760
about React Server Components
573
00:31:05,760 --> 00:31:09,090
is the fact that they can only be implemented
574
00:31:09,090 --> 00:31:11,850
and used within a framework.
575
00:31:11,850 --> 00:31:14,610
So you cannot just set up a Vite app
576
00:31:14,610 --> 00:31:17,490
and implement RSC yourself.
577
00:31:17,490 --> 00:31:20,760
I mean, you actually could in theory,
578
00:31:20,760 --> 00:31:23,190
but it's just so complex
579
00:31:23,190 --> 00:31:26,670
and would be so much work that no one will do that.
580
00:31:26,670 --> 00:31:28,470
And as a consequence,
581
00:31:28,470 --> 00:31:31,260
you always have to buy into a framework
582
00:31:31,260 --> 00:31:34,800
if you want to use the power of server components.
583
00:31:34,800 --> 00:31:38,160
But in any case, in my own personal opinion,
584
00:31:38,160 --> 00:31:40,890
I think server components are awesome
585
00:31:40,890 --> 00:31:45,750
and definitely worth it no matter how many cons there are.
586
00:31:45,750 --> 00:31:49,860
So the good parts absolutely outweigh the bad parts,
587
00:31:49,860 --> 00:31:53,700
and I'm really happy that React went this way.
588
00:31:53,700 --> 00:31:58,260
And now after all this talk, let's go back to our project
589
00:31:58,260 --> 00:32:01,113
and explore this with actual code.
47005
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.