Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,130 --> 00:00:03,830
So now that we picked up those
2
00:00:03,830 --> 00:00:08,050
important React basics, it's time to dig a bit deeper
3
00:00:08,050 --> 00:00:11,830
to dive into some advanced concepts like routing
4
00:00:11,830 --> 00:00:16,830
sending HTTP requests and application wide state management
5
00:00:17,470 --> 00:00:19,870
and what all these things are.
6
00:00:19,870 --> 00:00:21,970
We're going to explore that.
7
00:00:21,970 --> 00:00:26,970
And for this, we're going to leave this basic project
8
00:00:27,376 --> 00:00:28,540
this basic demo we worked on.
9
00:00:28,540 --> 00:00:30,720
Instead, we're going to build this
10
00:00:30,720 --> 00:00:33,100
meet up demo application
11
00:00:33,100 --> 00:00:36,010
I showed you earlier already.
12
00:00:36,010 --> 00:00:38,260
And for that, I cleaned up my project.
13
00:00:38,260 --> 00:00:42,250
I removed basically all the imports and all the content
14
00:00:42,250 --> 00:00:47,250
from this app file here and index css has some new content.
15
00:00:47,970 --> 00:00:51,220
You'll find my updated project attached.
16
00:00:51,220 --> 00:00:55,500
So there you all to find the updated index css file.
17
00:00:55,500 --> 00:00:58,120
And I deleted all the components.
18
00:00:58,120 --> 00:00:59,180
Now, as I just said
19
00:00:59,180 --> 00:01:02,450
you'll find this updated starting state attached
20
00:01:02,450 --> 00:01:06,400
and you can therefore then start with that same project.
21
00:01:06,400 --> 00:01:07,233
In the end here,
22
00:01:07,233 --> 00:01:11,150
once you downloaded it, you need to run NPM install again.
23
00:01:11,150 --> 00:01:12,720
Then one step done.
24
00:01:12,720 --> 00:01:15,890
You can NPM start this project again to bring
25
00:01:15,890 --> 00:01:18,680
up that development server again.
26
00:01:18,680 --> 00:01:21,840
And that will give you a blank page.
27
00:01:21,840 --> 00:01:24,420
Because now we're going to build this project
28
00:01:24,420 --> 00:01:25,640
from the ground up.
29
00:01:25,640 --> 00:01:29,810
And this project will now involve more components
30
00:01:29,810 --> 00:01:31,090
than the previous one.
31
00:01:31,090 --> 00:01:35,660
And it will involve the important concept of routing.
32
00:01:35,660 --> 00:01:40,600
Now, what does routing mean, considered is Netflix example
33
00:01:40,600 --> 00:01:42,320
from before again.
34
00:01:42,320 --> 00:01:44,140
There I went to the
35
00:01:44,140 --> 00:01:49,140
my list page to view the items I added to my list.
36
00:01:49,570 --> 00:01:53,500
Now it looks like I visited a totally different page
37
00:01:53,500 --> 00:01:57,310
but if you watch this refresh icon in the top left corner
38
00:01:57,310 --> 00:01:59,470
you see that it never changes.
39
00:01:59,470 --> 00:02:02,860
It never goes into that loading mode
40
00:02:02,860 --> 00:02:07,780
because actually we never fetched a new HTML page.
41
00:02:07,780 --> 00:02:10,639
The page loaded by the browser never changed.
42
00:02:10,639 --> 00:02:13,977
We always stay on that single page.
43
00:02:13,977 --> 00:02:15,870
And that's the case for Netflix
44
00:02:15,870 --> 00:02:19,000
because they also have such a single page application.
45
00:02:19,000 --> 00:02:22,400
It should also be the case for our project now.
46
00:02:22,400 --> 00:02:25,050
I wanna give the user the illusion
47
00:02:25,050 --> 00:02:28,520
of having different pages to which we can navigate
48
00:02:28,520 --> 00:02:30,730
whilst actually staying on one
49
00:02:30,730 --> 00:02:34,840
and the same page and leaving it up to react, to change
50
00:02:34,840 --> 00:02:36,820
what's visible on the page.
51
00:02:36,820 --> 00:02:38,840
The advantage of this approach is
52
00:02:38,840 --> 00:02:42,360
that we never have to wait for a new page to be loaded.
53
00:02:42,360 --> 00:02:46,370
Instead, we can always stay fast, stay reactive
54
00:02:46,370 --> 00:02:50,300
because everything is handled by client side Java script
55
00:02:50,300 --> 00:02:54,470
which is faster than sending a new request to a server.
56
00:02:54,470 --> 00:02:58,380
And that can really enhance the user experience.
57
00:02:58,380 --> 00:03:00,840
Now, therefore we wanna add something which is
58
00:03:00,840 --> 00:03:02,960
called routing to our project.
59
00:03:02,960 --> 00:03:06,520
We wanna add a router, a special tool
60
00:03:06,520 --> 00:03:08,460
which actually watches changes
61
00:03:08,460 --> 00:03:12,400
in the URL and then changes what's visible
62
00:03:12,400 --> 00:03:16,120
on the screen based on the URL.
63
00:03:16,120 --> 00:03:18,580
So, which basically gives us a way
64
00:03:18,580 --> 00:03:23,580
of giving our users this illusion of routing.
65
00:03:23,690 --> 00:03:24,523
And for this
66
00:03:24,523 --> 00:03:27,400
I'll actually quit this development server again
67
00:03:27,400 --> 00:03:30,630
and install a new dependency,
68
00:03:30,630 --> 00:03:35,050
a new third party package into this project.
69
00:03:35,050 --> 00:03:39,130
We do this with NPM install with the NPM install command
70
00:03:39,130 --> 00:03:42,600
but now not just executed like this
71
00:03:42,600 --> 00:03:45,870
but instead we now add a package name thereafter.
72
00:03:45,870 --> 00:03:49,280
The name of the package, which should be installed
73
00:03:49,280 --> 00:03:52,950
so download it into this project here.
74
00:03:52,950 --> 00:03:57,443
And that's the react dash router dash dom package.
75
00:03:58,580 --> 00:04:02,130
That's a package which as the name kind of implies
76
00:04:02,130 --> 00:04:06,290
allows us to add routing functionality to react.
77
00:04:06,290 --> 00:04:09,540
So it allows us to handle URLs and change.
78
00:04:09,540 --> 00:04:11,150
What's visible on the screen
79
00:04:11,150 --> 00:04:15,070
without fetching new HTML pages.
80
00:04:15,070 --> 00:04:19,390
Simply hit Enter and this package will be installed.
81
00:04:19,390 --> 00:04:21,029
And in package dot json
82
00:04:21,029 --> 00:04:24,020
you'll see that a new entry was added.
83
00:04:24,020 --> 00:04:26,670
So that file was automatically updated
84
00:04:26,670 --> 00:04:29,570
which means that if you now share this project again
85
00:04:29,570 --> 00:04:32,170
without a node modules folder
86
00:04:32,170 --> 00:04:35,900
and someone then runs just NPM install again
87
00:04:35,900 --> 00:04:38,660
that newly added package will also be picked
88
00:04:38,660 --> 00:04:41,680
up and installed because it is part of this package.
89
00:04:41,680 --> 00:04:43,090
json file.
90
00:04:43,090 --> 00:04:44,610
Now we don't need to do this here
91
00:04:44,610 --> 00:04:47,340
because we just did install it.
92
00:04:47,340 --> 00:04:49,500
So we can run NPM start again to bring up
93
00:04:49,500 --> 00:04:51,700
that development server again.
94
00:04:51,700 --> 00:04:55,970
And now we can add routing, but what does this mean?
95
00:04:55,970 --> 00:04:59,320
What does add routing mean?
96
00:04:59,320 --> 00:05:00,890
In the end it means that we need
97
00:05:00,890 --> 00:05:05,070
to add this routing tool to our code
98
00:05:05,070 --> 00:05:09,540
so that we have this thing watching for URL changes.
99
00:05:09,540 --> 00:05:11,980
And we need to let this thing
100
00:05:11,980 --> 00:05:16,260
this tool know which component should be loaded
101
00:05:16,260 --> 00:05:21,260
as a page for which route, for which URL.
102
00:05:21,680 --> 00:05:25,010
And for this all start by adding a new folder next
103
00:05:25,010 --> 00:05:28,540
to the components folder inside the src folder
104
00:05:28,540 --> 00:05:30,910
and that's the pages folder.
105
00:05:30,910 --> 00:05:32,960
You don't have to add to this folder
106
00:05:32,960 --> 00:05:36,650
and you don't have to name it pages, but in the end here
107
00:05:36,650 --> 00:05:39,110
this is a good way of differentiating
108
00:05:39,110 --> 00:05:43,070
and of separating our components that are embedded
109
00:05:43,070 --> 00:05:45,990
in other components, which will be stored
110
00:05:45,990 --> 00:05:47,590
in the components folder,
111
00:05:47,590 --> 00:05:51,260
from the components that will be loaded as pages.
112
00:05:51,260 --> 00:05:54,590
Technically we'll build components in the same way
113
00:05:54,590 --> 00:05:56,590
no matter how we use them,
114
00:05:56,590 --> 00:05:59,580
but for as a developer, to have an easier time to
115
00:05:59,580 --> 00:06:02,490
quickly find the right components we wanna work on,
116
00:06:02,490 --> 00:06:05,200
I'll separate them into these two folders.
117
00:06:05,200 --> 00:06:08,190
And in the pages folder, we're now going to add a couple
118
00:06:08,190 --> 00:06:11,350
of react components, which again
119
00:06:11,350 --> 00:06:14,480
will be built just like all the other components
120
00:06:14,480 --> 00:06:17,490
but which will be loaded by this router,
121
00:06:17,490 --> 00:06:22,110
we just installed when certain URLs are visited.
122
00:06:22,110 --> 00:06:25,450
And here for this application for this project
123
00:06:25,450 --> 00:06:29,640
we're building here, I'll add all meet-ups js file.
124
00:06:29,640 --> 00:06:32,220
So a component that will be responsible
125
00:06:32,220 --> 00:06:35,983
for loading and displaying all the meetups we have.
126
00:06:37,220 --> 00:06:41,563
I'll add a new meetup js file and I'll add a favorites
127
00:06:41,563 --> 00:06:43,950
js file.
128
00:06:43,950 --> 00:06:47,750
New meetup will allow us to add a new meetup later
129
00:06:47,750 --> 00:06:52,750
and favorites js will allow us to view our favorite meetups
130
00:06:53,130 --> 00:06:55,920
because we'll add a functionality that allows us to
131
00:06:55,920 --> 00:06:59,683
bookmark, to favorite a meet-up as a user.
132
00:07:01,020 --> 00:07:04,130
But step-by-step for the moment, I'll just set up
133
00:07:04,130 --> 00:07:07,400
some dummy components in these three files.
134
00:07:07,400 --> 00:07:10,260
And I'll start with all meet-ups and I'll name
135
00:07:10,260 --> 00:07:13,670
this component function, all meetups page.
136
00:07:13,670 --> 00:07:15,670
You can name it however you want.
137
00:07:15,670 --> 00:07:18,350
I'll just choose this name to make it clear
138
00:07:18,350 --> 00:07:21,550
that this component will be used as a page
139
00:07:21,550 --> 00:07:23,253
that's loaded by this router.
140
00:07:24,140 --> 00:07:26,640
Now, just as all the other components we built
141
00:07:26,640 --> 00:07:30,180
before this component has to be exported
142
00:07:30,180 --> 00:07:32,700
and it needs to returns some JSX code.
143
00:07:32,700 --> 00:07:34,830
And for the moment, I'll just return a div where
144
00:07:34,830 --> 00:07:38,160
I say all meetups page, that's of course not
145
00:07:38,160 --> 00:07:41,203
the final content, but some starting content for now.
146
00:07:42,560 --> 00:07:47,010
Then we can copy this and add it into favorites file here.
147
00:07:47,010 --> 00:07:49,790
So this is the favorites page
148
00:07:49,790 --> 00:07:52,750
and we wanna export the favorites page.
149
00:07:52,750 --> 00:07:57,750
And here I'll then say favorites page, and then save this
150
00:07:58,190 --> 00:08:02,907
and copy this into new meetup and named his new meetup page
151
00:08:04,020 --> 00:08:07,040
and also use that name for exporting.
152
00:08:07,040 --> 00:08:10,500
And here we say, new meetup page.
153
00:08:10,500 --> 00:08:13,350
Now we've got these free page components.
154
00:08:13,350 --> 00:08:16,740
Now we need to use that router package to
155
00:08:16,740 --> 00:08:20,610
define when which page should be loaded.
156
00:08:20,610 --> 00:08:23,560
And for this be first of all, need to go to index js
157
00:08:23,560 --> 00:08:28,270
and to wrap our app component, which you render here
158
00:08:28,270 --> 00:08:30,750
with another component.
159
00:08:30,750 --> 00:08:33,980
For this, we need to import something
160
00:08:33,980 --> 00:08:38,010
from react dash router dash dom and that
161
00:08:38,010 --> 00:08:42,543
something which we import is the browser router, like this.
162
00:08:43,770 --> 00:08:44,880
Now it turns out
163
00:08:44,880 --> 00:08:49,110
that browser router is in the end a component itself
164
00:08:49,110 --> 00:08:53,360
and therefore we can use it as a HTML element, opening
165
00:08:53,360 --> 00:08:56,300
and closing tags, and wrap those tags
166
00:08:56,300 --> 00:08:58,403
around the app component.
167
00:08:59,630 --> 00:09:01,630
Now that is something we haven't seen
168
00:09:01,630 --> 00:09:05,420
up to this point that a non-standard component.
169
00:09:05,420 --> 00:09:10,360
So a non HTML component would be wrapped around something
170
00:09:10,360 --> 00:09:11,830
but it is something we can do.
171
00:09:11,830 --> 00:09:13,943
And it is something we are doing here.
172
00:09:15,180 --> 00:09:17,840
We're going to learn how we make our own components
173
00:09:17,840 --> 00:09:21,790
wrappable a little bit later in this course.
174
00:09:21,790 --> 00:09:25,330
So now we initialize this router package.
175
00:09:25,330 --> 00:09:27,520
We make it aware of this app
176
00:09:27,520 --> 00:09:31,623
and we ensure that it watches our URL so to say.
177
00:09:32,750 --> 00:09:34,030
Now, as a next step
178
00:09:34,030 --> 00:09:37,440
we need to define the URLs we wanna support
179
00:09:37,440 --> 00:09:41,123
and which pages should be loaded for the different URLs.
180
00:09:42,130 --> 00:09:44,570
And we do this in the app component now.
181
00:09:44,570 --> 00:09:46,723
So in this wrapped component.
182
00:09:47,740 --> 00:09:51,030
In there we return a div and we can leave it like this.
183
00:09:51,030 --> 00:09:53,050
But now in this div
184
00:09:53,050 --> 00:09:57,180
I wanna return or use other components
185
00:09:57,180 --> 00:10:02,180
which are also imported from react router dom.
186
00:10:02,410 --> 00:10:05,940
So we also import react router dom here and here
187
00:10:05,940 --> 00:10:09,240
we import the route component.
188
00:10:09,240 --> 00:10:13,100
So just like browser router route is a component
189
00:10:13,100 --> 00:10:16,100
and we use it like a component, but the job
190
00:10:16,100 --> 00:10:20,930
of the route component is to define different paths
191
00:10:20,930 --> 00:10:23,450
in the URL we wanna listen to
192
00:10:23,450 --> 00:10:25,870
and which component should be loaded
193
00:10:25,870 --> 00:10:27,413
for these different paths.
194
00:10:29,150 --> 00:10:31,970
And therefore I will also already import
195
00:10:31,970 --> 00:10:35,110
my three page components here
196
00:10:35,110 --> 00:10:40,110
that all meetups page from dot slash pages, all meetups.
197
00:10:42,460 --> 00:10:47,460
And then the new meetup page from dot pages, new meetup.
198
00:10:48,870 --> 00:10:52,120
And of course also the favorites page component
199
00:10:52,120 --> 00:10:54,923
from dot slash pages, favorites.
200
00:10:56,020 --> 00:11:00,730
And now in this div here, we can use the route component
201
00:11:00,730 --> 00:11:03,890
which I'm importing from react router dom
202
00:11:03,890 --> 00:11:08,023
and give it a path prop, a path attribute.
203
00:11:09,010 --> 00:11:10,890
Now path takes a string
204
00:11:10,890 --> 00:11:15,470
and that will be the path in the URL after your domain.
205
00:11:15,470 --> 00:11:16,470
So in our case
206
00:11:16,470 --> 00:11:20,140
our domain is for example, local hosts:3000.
207
00:11:20,140 --> 00:11:22,260
That's our domain, of course
208
00:11:22,260 --> 00:11:24,670
on a real server after deployment.
209
00:11:24,670 --> 00:11:27,900
That would be something like my page.com.
210
00:11:27,900 --> 00:11:29,390
And now the part
211
00:11:29,390 --> 00:11:33,620
after the domain is the path that can be just a slash
212
00:11:33,620 --> 00:11:36,580
so no path at all in the end
213
00:11:36,580 --> 00:11:39,540
or it can be something like slash favorites
214
00:11:39,540 --> 00:11:43,250
or slash products or anything like that.
215
00:11:43,250 --> 00:11:46,910
So the part after the domain is the path and here we define
216
00:11:46,910 --> 00:11:50,720
for which path, which component should be loaded.
217
00:11:50,720 --> 00:11:51,920
And here we can start
218
00:11:51,920 --> 00:11:55,960
with slash nothing to handle the default path
219
00:11:55,960 --> 00:12:00,300
that we just visited our domain slash nothing.
220
00:12:00,300 --> 00:12:03,610
And then between these route tags,
221
00:12:03,610 --> 00:12:08,540
we actually do now add the component that should be loaded,
222
00:12:08,540 --> 00:12:11,623
like the all meetups page like this,
223
00:12:12,800 --> 00:12:15,493
because that should be the starting page actually.
224
00:12:16,460 --> 00:12:20,650
We can add another route here for, let's say
225
00:12:20,650 --> 00:12:25,650
slash new dash meetup and the path it's up to you.
226
00:12:25,760 --> 00:12:29,900
If we now visit my domain.com/new-meetup
227
00:12:29,900 --> 00:12:32,140
then the component which we render
228
00:12:32,140 --> 00:12:35,420
between these route tags will be loaded.
229
00:12:35,420 --> 00:12:37,693
And that could be the new meetups page.
230
00:12:38,570 --> 00:12:40,000
And last but not least
231
00:12:40,000 --> 00:12:45,000
we can also define a route for the path slash favorites
232
00:12:45,430 --> 00:12:48,693
and then render the favorites page like this.
233
00:12:50,030 --> 00:12:52,620
Now we're defining three routes
234
00:12:52,620 --> 00:12:56,140
and instead of this content here
235
00:12:56,140 --> 00:12:59,070
the actual page component will be rendered
236
00:12:59,070 --> 00:13:01,050
if that route determines
237
00:13:01,050 --> 00:13:04,550
that the current URL matches this path.
238
00:13:04,550 --> 00:13:09,450
So if we are on my domain.com/new-meetup
239
00:13:09,450 --> 00:13:12,360
this new meetup page component would be rendered
240
00:13:12,360 --> 00:13:15,393
and not the favorites page, for example.
241
00:13:16,390 --> 00:13:19,580
So if we save this and we go back and reload
242
00:13:19,580 --> 00:13:24,083
localhost:3000, we should see the all meetups page here.
243
00:13:25,580 --> 00:13:27,720
But if we go to favorites, for example
244
00:13:27,720 --> 00:13:32,170
we see favorites page as well, but wait a second,
245
00:13:32,170 --> 00:13:37,170
is this the desired result that we see it as well?
246
00:13:37,220 --> 00:13:40,160
Well, it depends in some applications, depending
247
00:13:40,160 --> 00:13:44,030
on what you build, you might want such a nested page
248
00:13:44,030 --> 00:13:48,910
so that the page for just slash slash nothing is rendered
249
00:13:48,910 --> 00:13:53,190
but also this specific page for slash favorites.
250
00:13:53,190 --> 00:13:56,650
We see this output because by default
251
00:13:56,650 --> 00:14:01,650
react router matches all your paths and it simply checks
252
00:14:01,830 --> 00:14:06,260
if the current path includes these paths here
253
00:14:06,260 --> 00:14:11,260
and slash favorites includes both slash favorites, as well
254
00:14:11,450 --> 00:14:16,190
as just slash, which is why both components are rendered.
255
00:14:16,190 --> 00:14:20,760
Again, that could be what you want, but often it's not.
256
00:14:20,760 --> 00:14:24,190
That's why there is another component provided
257
00:14:24,190 --> 00:14:26,770
by react router dom, which helps us.
258
00:14:26,770 --> 00:14:29,460
And that's the switch component.
259
00:14:29,460 --> 00:14:32,790
We can use the switch component to wrap all our
260
00:14:32,790 --> 00:14:36,070
route components like this.
261
00:14:36,070 --> 00:14:38,750
And with that, we tell a react router
262
00:14:38,750 --> 00:14:42,460
that only one of these routes should be active.
263
00:14:42,460 --> 00:14:46,620
So at most one of these pages should be rendered.
264
00:14:46,620 --> 00:14:48,610
If we do that and save this,
265
00:14:48,610 --> 00:14:51,730
we don't see two components being rendered
266
00:14:51,730 --> 00:14:53,930
but we don't see the correct component
267
00:14:53,930 --> 00:14:55,480
being rendered either.
268
00:14:55,480 --> 00:14:57,290
Instead for a slash favorites,
269
00:14:57,290 --> 00:14:59,430
we now see all meetups.
270
00:14:59,430 --> 00:15:01,760
And the reason for this is now how your routes
271
00:15:01,760 --> 00:15:03,430
are matched.
272
00:15:03,430 --> 00:15:06,840
React router by default checks if the path
273
00:15:06,840 --> 00:15:11,840
of the current URL starts with the path to find here
274
00:15:12,350 --> 00:15:13,740
and because of switch
275
00:15:13,740 --> 00:15:16,422
it will then stop looking at the other routes as soon
276
00:15:16,422 --> 00:15:20,230
as it has a hit, as soon as it finds a match.
277
00:15:20,230 --> 00:15:25,090
Now, of course, slash favorites starts with just slash
278
00:15:25,090 --> 00:15:27,950
and then because of switch, it doesn't look any further,
279
00:15:27,950 --> 00:15:30,990
and therefore all meetups is rendered.
280
00:15:30,990 --> 00:15:32,200
That's not what we want.
281
00:15:32,200 --> 00:15:35,870
And we can prevent this by adding the exact prop here
282
00:15:35,870 --> 00:15:39,070
on route and setting this to true.
283
00:15:39,070 --> 00:15:40,660
Though you can also shorten this
284
00:15:40,660 --> 00:15:41,800
and if you have a prop
285
00:15:41,800 --> 00:15:45,520
which you just want to set to a true the value
286
00:15:45,520 --> 00:15:48,190
you can just add the prop name like this.
287
00:15:48,190 --> 00:15:50,010
So we add the exact prop
288
00:15:50,010 --> 00:15:53,060
and set it to true to this first route.
289
00:15:53,060 --> 00:15:58,060
And the exact prop tells react, router that for this route
290
00:15:58,240 --> 00:16:01,950
it should not check if the path begins with this path
291
00:16:01,950 --> 00:16:06,280
but if the full path matches this path and that's now
292
00:16:06,280 --> 00:16:08,940
of course not the case for slash favorites.
293
00:16:08,940 --> 00:16:13,040
So this will not create a match and it will continue looking
294
00:16:13,040 --> 00:16:17,253
and it will ultimately find this route four slash favorites.
295
00:16:18,540 --> 00:16:22,240
So now we see the favorites page or a new dash meetup,
296
00:16:22,240 --> 00:16:26,170
we see the new meetup page and for just local hosts:3000
297
00:16:26,170 --> 00:16:28,840
we see the all meetups page
298
00:16:28,840 --> 00:16:31,520
and that's how we can add routing.
299
00:16:31,520 --> 00:16:32,900
But of course at the moment
300
00:16:32,900 --> 00:16:37,030
we're just navigating around by entering URLs manually.
301
00:16:37,030 --> 00:16:39,580
And that's not very realistic.
302
00:16:39,580 --> 00:16:43,350
In reality, we would probably want some, some header,
303
00:16:43,350 --> 00:16:45,560
some navigation bar at the top
304
00:16:45,560 --> 00:16:47,730
which provides different links
305
00:16:47,730 --> 00:16:50,323
and therefore that is what we're going to add next.
24624
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.