Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,050 --> 00:00:04,150
Now with the basic routing working,
2
00:00:04,150 --> 00:00:06,640
we got one clear issue.
3
00:00:06,640 --> 00:00:08,860
At the moment we can only navigate
4
00:00:08,860 --> 00:00:13,080
between those pages by changing the URL manually.
5
00:00:13,080 --> 00:00:16,110
And whilst we, of course sometimes might do that.
6
00:00:16,110 --> 00:00:19,350
And whilst we might share a link to a page,
7
00:00:19,350 --> 00:00:21,000
it's all the pretty comments
8
00:00:21,000 --> 00:00:23,750
that we want to have links on our page
9
00:00:23,750 --> 00:00:27,640
which we can click, which will then take us somewhere else.
10
00:00:27,640 --> 00:00:29,120
And therefore let's now have a look
11
00:00:29,120 --> 00:00:32,860
at links and how we would add links to our page.
12
00:00:32,860 --> 00:00:35,530
Let's say on our app JS file
13
00:00:35,530 --> 00:00:40,530
we want to have a header above our main section.
14
00:00:40,740 --> 00:00:42,590
And it's in this main section
15
00:00:42,590 --> 00:00:44,580
where we wanna have the routes
16
00:00:44,580 --> 00:00:47,680
and it's this header where we want to have the navigation.
17
00:00:47,680 --> 00:00:48,650
And for this, of course
18
00:00:48,650 --> 00:00:51,250
we could also create a separate component
19
00:00:51,250 --> 00:00:55,620
in a separate components, folder components, and not pages
20
00:00:55,620 --> 00:00:58,510
because this will be a component which is embedded
21
00:00:58,510 --> 00:01:02,020
in JSX and not loaded as a route.
22
00:01:02,020 --> 00:01:06,790
And in there we could have our main header component
23
00:01:06,790 --> 00:01:08,900
or whatever you want to call it.
24
00:01:08,900 --> 00:01:13,650
So as simple component, regular component function
25
00:01:13,650 --> 00:01:15,700
as we created it multiple times
26
00:01:15,700 --> 00:01:19,780
in this course already, and here, we then return our header.
27
00:01:19,780 --> 00:01:22,180
And in there, we could have our navigation
28
00:01:22,180 --> 00:01:25,180
with an unordered list with our different links.
29
00:01:25,180 --> 00:01:27,760
That is a setup we could have here.
30
00:01:27,760 --> 00:01:31,200
Now at the moment, we have two routes here, a welcome
31
00:01:31,200 --> 00:01:34,420
and a products page and Henson does first list item.
32
00:01:34,420 --> 00:01:38,350
We could add a link which leads to slash welcome
33
00:01:39,210 --> 00:01:40,939
where we say welcome.
34
00:01:40,939 --> 00:01:43,600
And then we duplicate this list item
35
00:01:43,600 --> 00:01:46,750
and add a second link to slash products
36
00:01:46,750 --> 00:01:49,850
which a load step products page
37
00:01:49,850 --> 00:01:53,400
that is a basic navigation we could add here.
38
00:01:53,400 --> 00:01:58,250
Now we can go to app JS and import our main header
39
00:01:59,300 --> 00:02:02,910
from dot slash components slash main header.
40
00:02:02,910 --> 00:02:05,390
And we just use it here
41
00:02:05,390 --> 00:02:07,893
like this as a self-closing component.
42
00:02:09,960 --> 00:02:12,440
Now we see this navigation here.
43
00:02:12,440 --> 00:02:14,630
Now the styling is totally off.
44
00:02:14,630 --> 00:02:17,660
We'll work on this in a second, but we can see
45
00:02:17,660 --> 00:02:20,680
that we do have these two list items at the top here.
46
00:02:20,680 --> 00:02:22,490
So our header is here.
47
00:02:22,490 --> 00:02:24,110
And if I click them,
48
00:02:24,110 --> 00:02:28,000
indeed we do navigate between those two pages.
49
00:02:28,000 --> 00:02:32,010
But this approach has a big flaw.
50
00:02:32,010 --> 00:02:33,670
And you can see this flaw.
51
00:02:33,670 --> 00:02:37,840
If you take a closer look at the top left corner here
52
00:02:37,840 --> 00:02:39,970
whenever I click these links,
53
00:02:39,970 --> 00:02:44,850
you can see that does refresh icon turns to a cross.
54
00:02:44,850 --> 00:02:48,590
And that means that we actually load a new page.
55
00:02:48,590 --> 00:02:50,460
We send a new request.
56
00:02:50,460 --> 00:02:51,790
We can see this even better.
57
00:02:51,790 --> 00:02:54,730
If we opened the developer tools in India
58
00:02:54,730 --> 00:02:56,730
we go to the network tab.
59
00:02:56,730 --> 00:02:59,230
Whenever I click one of these items
60
00:02:59,230 --> 00:03:03,470
you see that new requests are sent brand new requests
61
00:03:03,470 --> 00:03:08,470
including a brand new request to a full HTML file.
62
00:03:09,120 --> 00:03:12,344
This HTML file here is loaded.
63
00:03:12,344 --> 00:03:15,440
Every time we click on one of these links.
64
00:03:15,440 --> 00:03:18,810
So we do send a brand new request to the server.
65
00:03:18,810 --> 00:03:20,760
Serving does app.
66
00:03:20,760 --> 00:03:24,070
This works, we get the different pages, but it means
67
00:03:24,070 --> 00:03:27,300
that we leave the active single-page application.
68
00:03:27,300 --> 00:03:28,930
We start a new one.
69
00:03:28,930 --> 00:03:30,690
When we click one of these links
70
00:03:30,690 --> 00:03:34,970
we lose any application state that we might have
71
00:03:34,970 --> 00:03:36,690
in this application.
72
00:03:36,690 --> 00:03:39,010
And that is not ideal.
73
00:03:39,010 --> 00:03:40,720
That goes against the idea
74
00:03:40,720 --> 00:03:43,700
of building a single page application.
75
00:03:43,700 --> 00:03:46,368
If we would be managing some cart,
76
00:03:46,368 --> 00:03:49,090
some shopping cart for a user,
77
00:03:49,090 --> 00:03:51,020
and we would lose all that state.
78
00:03:51,020 --> 00:03:53,230
Whenever we navigate between pages
79
00:03:53,230 --> 00:03:56,936
this would be a sup-optimal user experience.
80
00:03:56,936 --> 00:03:58,542
So it would be better
81
00:03:58,542 --> 00:04:02,790
if this browser default of sending a request
82
00:04:02,790 --> 00:04:07,790
and requesting a new HTML page would be prevented
83
00:04:08,220 --> 00:04:10,950
and we could write our own code for debt.
84
00:04:10,950 --> 00:04:14,300
And we could add click listeners to these anchor tags
85
00:04:14,300 --> 00:04:17,399
and prevent the browser default and manually
86
00:04:17,399 --> 00:04:21,420
try to get react router to load the right component
87
00:04:21,420 --> 00:04:23,943
but we don't need to do this manually.
88
00:04:24,870 --> 00:04:27,540
Instead. There's a number of components which we can import
89
00:04:27,540 --> 00:04:32,540
from react, router Dom, and that is the link component.
90
00:04:34,210 --> 00:04:37,390
The link component does what the name implies.
91
00:04:37,390 --> 00:04:39,683
It allows us to create a link.
92
00:04:40,760 --> 00:04:45,310
Now we can replace the anchor tag with this link component
93
00:04:45,310 --> 00:04:48,020
for both the opening and the closing tag
94
00:04:49,780 --> 00:04:51,220
like this.
95
00:04:51,220 --> 00:04:53,266
And we then have to replace the ref
96
00:04:53,266 --> 00:04:57,400
attribute the ref prop here with the two prop
97
00:04:57,400 --> 00:05:00,410
because this link component wants a two prop
98
00:05:00,410 --> 00:05:03,983
which then holds the actual address we want to navigate to.
99
00:05:05,320 --> 00:05:07,850
Now, if we do this and save this
100
00:05:08,720 --> 00:05:11,580
visually we'll get the same output as before.
101
00:05:11,580 --> 00:05:13,640
And if we inspect these links
102
00:05:13,640 --> 00:05:16,880
we'll see that we still have anchor tags here
103
00:05:16,880 --> 00:05:19,730
but these are now anchor tags rendered
104
00:05:19,730 --> 00:05:24,250
by this link component and internally react router.
105
00:05:24,250 --> 00:05:27,280
This package we're using will actually listen
106
00:05:27,280 --> 00:05:31,190
to clicks on those links, prevent the browser default
107
00:05:31,190 --> 00:05:35,020
and instead manually update the URL for us
108
00:05:35,020 --> 00:05:38,000
so that it looks like if we switched the page
109
00:05:38,000 --> 00:05:41,660
and it will then also change what we see on the screen.
110
00:05:41,660 --> 00:05:45,160
So we have this fake navigation you could say
111
00:05:45,160 --> 00:05:46,850
and that's what we want here.
112
00:05:46,850 --> 00:05:49,290
Because now with that, if I click here
113
00:05:49,290 --> 00:05:51,200
we still switched to page.
114
00:05:51,200 --> 00:05:54,860
We still switched between products and welcome page.
115
00:05:54,860 --> 00:05:59,530
But if you watch this top left corner and this refresh icon
116
00:05:59,530 --> 00:06:01,710
you see that it never spins
117
00:06:01,710 --> 00:06:05,550
simply because now we don't send new requests
118
00:06:05,550 --> 00:06:08,190
and we can see this in the dev tools as well
119
00:06:08,190 --> 00:06:09,550
in the network tab.
120
00:06:09,550 --> 00:06:12,530
Now we don't see any requests being sent
121
00:06:12,530 --> 00:06:14,340
because we don't send any
122
00:06:14,340 --> 00:06:17,400
we stay on this load at single page application.
123
00:06:17,400 --> 00:06:19,050
We don't lose any state.
124
00:06:19,050 --> 00:06:22,010
And we still give the user the illusion
125
00:06:22,010 --> 00:06:24,800
of switching between different pages
126
00:06:24,800 --> 00:06:29,020
and there for we now also got this shareable URL
127
00:06:29,020 --> 00:06:31,440
which will then lead us to the right page
128
00:06:31,440 --> 00:06:34,340
when we enter it in the browser
129
00:06:34,340 --> 00:06:37,030
and that is how we want to use routing.
130
00:06:37,030 --> 00:06:40,570
We wanted to find our routes as we did it before
131
00:06:40,570 --> 00:06:43,560
but if we are on the page and we want to allow
132
00:06:43,560 --> 00:06:46,150
the user to navigate between these pages,
133
00:06:46,150 --> 00:06:48,757
then we want to use this link component
134
00:06:48,757 --> 00:06:51,473
instead of the standard anchor tag.
10550
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.