Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,090 --> 00:00:04,880
Dynamic routes and route parameters,
2
00:00:04,880 --> 00:00:07,910
are another crucial feature of React router,
3
00:00:07,910 --> 00:00:10,380
which you will use in almost any application,
4
00:00:10,380 --> 00:00:11,970
you are building.
5
00:00:11,970 --> 00:00:14,390
But there is another problem which you could face,
6
00:00:14,390 --> 00:00:16,470
with this current routes set up.
7
00:00:16,470 --> 00:00:18,970
At least if you may be already played around
8
00:00:18,970 --> 00:00:21,600
with those props a little bit.
9
00:00:21,600 --> 00:00:23,130
At the moment everything works
10
00:00:23,130 --> 00:00:25,198
and we load totally different pages
11
00:00:25,198 --> 00:00:28,240
and therefore show totally different contents here
12
00:00:28,240 --> 00:00:30,163
based on the different URLs.
13
00:00:32,910 --> 00:00:34,910
But of course one problem we have,
14
00:00:34,910 --> 00:00:38,170
is that we're not linking to the product detail page.
15
00:00:38,170 --> 00:00:39,950
We have to enter that manually.
16
00:00:39,950 --> 00:00:42,790
But another problem is maybe in general,
17
00:00:42,790 --> 00:00:45,650
how we structured our URLs.
18
00:00:45,650 --> 00:00:48,140
It is quite common on websites stat,
19
00:00:48,140 --> 00:00:50,640
if you have a list like page,
20
00:00:50,640 --> 00:00:53,970
you have something like slash tutorials as a path,
21
00:00:53,970 --> 00:00:56,400
but that if you then click on a list item,
22
00:00:56,400 --> 00:00:58,360
like a specific article here,
23
00:00:58,360 --> 00:01:01,260
you simply have slash tutorials.
24
00:01:01,260 --> 00:01:03,173
So the path I had before,
25
00:01:03,173 --> 00:01:06,410
and then another path added thereafter.
26
00:01:06,410 --> 00:01:09,280
This is not a must do but it's a common pattern,
27
00:01:09,280 --> 00:01:11,440
which is intuitive for users
28
00:01:11,440 --> 00:01:13,970
and which is hopefully also intuitive for you,
29
00:01:13,970 --> 00:01:15,650
as a developer.
30
00:01:15,650 --> 00:01:18,160
So here instead of slash product,
31
00:01:18,160 --> 00:01:20,530
dash detailed slash some ID,
32
00:01:20,530 --> 00:01:24,760
we might want to use slash products, slash some ID.
33
00:01:24,760 --> 00:01:27,200
So that for just slash products,
34
00:01:27,200 --> 00:01:29,670
we load the list of products.
35
00:01:29,670 --> 00:01:32,640
And for slash products, slash some ID,
36
00:01:32,640 --> 00:01:35,313
we load the details for that product.
37
00:01:36,360 --> 00:01:38,460
Now on the products component,
38
00:01:38,460 --> 00:01:41,250
I also want to turn my products into links
39
00:01:41,250 --> 00:01:43,710
with help of the link component.
40
00:01:43,710 --> 00:01:45,730
Link instead of nav link,
41
00:01:45,730 --> 00:01:49,200
because here I don't want some active link styling.
42
00:01:49,200 --> 00:01:51,480
I just want standard links.
43
00:01:51,480 --> 00:01:54,500
And then we can wrap this text here
44
00:01:54,500 --> 00:01:58,900
with that link component and add the two prop.
45
00:01:58,900 --> 00:02:03,080
And now for example navigate to slash products slash P one,
46
00:02:03,080 --> 00:02:04,563
for the first item.
47
00:02:05,670 --> 00:02:10,220
And then replicate that for the other items,
48
00:02:10,220 --> 00:02:13,600
wrap all that text inside of links
49
00:02:13,600 --> 00:02:16,840
and just change the IDs here.
50
00:02:16,840 --> 00:02:19,850
And of course, we could also loop for some data here
51
00:02:19,850 --> 00:02:21,400
and generate this list
52
00:02:21,400 --> 00:02:24,530
and the prop values for the two prop dynamically.
53
00:02:24,530 --> 00:02:27,860
That's also something we're going to do a little bit later.
54
00:02:27,860 --> 00:02:30,380
For the moment I have these hard coded links.
55
00:02:30,380 --> 00:02:32,820
And with that, if I'm now on slash products
56
00:02:32,820 --> 00:02:34,640
I got these clickable items.
57
00:02:34,640 --> 00:02:37,991
But watch what happens if I click on a book.
58
00:02:37,991 --> 00:02:41,630
We load to product detailed data
59
00:02:41,630 --> 00:02:45,810
but we loaded below this list.
60
00:02:45,810 --> 00:02:47,810
And why is that happening?
61
00:02:47,810 --> 00:02:48,900
I mean, it is working
62
00:02:48,900 --> 00:02:52,210
but we're not leaving the products page.
63
00:02:52,210 --> 00:02:56,870
That's happening because this is how React router works.
64
00:02:56,870 --> 00:02:59,000
We define our routes here,
65
00:02:59,000 --> 00:03:02,420
but by default these routes are not parsed,
66
00:03:02,420 --> 00:03:05,830
such that only one of them is loaded at the same time.
67
00:03:05,830 --> 00:03:09,340
But instead all routes that match the current path
68
00:03:09,340 --> 00:03:11,200
will be loaded.
69
00:03:11,200 --> 00:03:15,460
Now you might say, okay, but still only this route
70
00:03:15,460 --> 00:03:18,290
the last route should be matching the current path.
71
00:03:18,290 --> 00:03:23,140
Because the current path is slash products slash some ID.
72
00:03:23,140 --> 00:03:26,350
And that's clearly not this path of the second route.
73
00:03:26,350 --> 00:03:27,430
And that is correct.
74
00:03:27,430 --> 00:03:30,930
But matching in React routers world,
75
00:03:30,930 --> 00:03:35,930
means your path starts with the path defined here.
76
00:03:36,430 --> 00:03:40,550
And indeed, if we are on slash products slash p3,
77
00:03:40,550 --> 00:03:42,000
as I am here,
78
00:03:42,000 --> 00:03:46,370
then this path starts with slash products.
79
00:03:46,370 --> 00:03:50,490
It also starts with slash products slash some ID.
80
00:03:50,490 --> 00:03:52,420
That's that actually the entire path
81
00:03:52,420 --> 00:03:55,860
but it also starts with just slash products.
82
00:03:55,860 --> 00:03:58,660
And that's why both routes are active now.
83
00:03:58,660 --> 00:04:01,950
This is simply how React router works.
84
00:04:01,950 --> 00:04:04,310
Now, sometimes that is what you want.
85
00:04:04,310 --> 00:04:08,290
You might want to show more details below that list here
86
00:04:08,290 --> 00:04:10,348
and then this is how you could make it work.
87
00:04:10,348 --> 00:04:11,360
But sometimes,
88
00:04:11,360 --> 00:04:14,713
you only want to have one active route at the same time.
89
00:04:15,650 --> 00:04:16,730
And to make this work,
90
00:04:16,730 --> 00:04:20,160
you can utilize another component offered by React router,
91
00:04:20,160 --> 00:04:22,283
DOM, the switch component.
92
00:04:23,180 --> 00:04:24,480
The switch component,
93
00:04:24,480 --> 00:04:27,580
can be wrapped around your route components.
94
00:04:27,580 --> 00:04:30,360
So you add the opening and closing tags
95
00:04:30,360 --> 00:04:32,610
around all these routes.
96
00:04:32,610 --> 00:04:35,410
And then only one of these routes,
97
00:04:35,410 --> 00:04:37,740
will be active at the same time.
98
00:04:37,740 --> 00:04:41,100
And it will be the route which is matched first.
99
00:04:41,100 --> 00:04:44,270
So if I now save this, if I click these links
100
00:04:44,270 --> 00:04:46,660
we don't see the detail page anymore.
101
00:04:46,660 --> 00:04:48,900
That's not the desired result,
102
00:04:48,900 --> 00:04:51,610
but at least we don't have multiple pages on the screen,
103
00:04:51,610 --> 00:04:53,430
simultaneously.
104
00:04:53,430 --> 00:04:56,320
But why do we see the list here,
105
00:04:56,320 --> 00:04:58,500
instead of the detail page?
106
00:04:58,500 --> 00:05:01,790
After all the detailed pages is the more specific one.
107
00:05:01,790 --> 00:05:05,240
Well, as of React router version five,
108
00:05:05,240 --> 00:05:08,760
it doesn't matter if a certain route is more specific.
109
00:05:08,760 --> 00:05:12,940
React router simply goes through your routes top to bottom.
110
00:05:12,940 --> 00:05:15,970
And when it finds a match, and keep in mind,
111
00:05:15,970 --> 00:05:20,690
that it matches the start of a path, not the entire path.
112
00:05:20,690 --> 00:05:24,280
If it finds a match, it will then stop because of switch,
113
00:05:24,280 --> 00:05:25,980
not look at the other routes,
114
00:05:25,980 --> 00:05:30,660
and rendered that one route for which it did find a match.
115
00:05:30,660 --> 00:05:32,370
Now that is how it behaves.
116
00:05:32,370 --> 00:05:36,150
But of course here, it's not giving us the result we want.
117
00:05:36,150 --> 00:05:39,160
One solution is to change the order.
118
00:05:39,160 --> 00:05:41,520
We can move the products route
119
00:05:41,520 --> 00:05:43,440
after the product detail route
120
00:05:43,440 --> 00:05:45,730
and therefore this will be the first match.
121
00:05:45,730 --> 00:05:47,980
And this route will be ignored.
122
00:05:47,980 --> 00:05:51,570
If I do that, we would see the route detail page.
123
00:05:51,570 --> 00:05:52,973
Now this would be working.
124
00:05:53,990 --> 00:05:55,760
And alternative would be,
125
00:05:55,760 --> 00:05:58,150
that we leave the original order
126
00:05:58,150 --> 00:06:01,870
but we add another prop here on this route.
127
00:06:01,870 --> 00:06:04,300
The exact prop.
128
00:06:04,300 --> 00:06:06,180
This tells React router,
129
00:06:06,180 --> 00:06:09,830
that this should only lead to a match
130
00:06:09,830 --> 00:06:11,810
if we have an exact match.
131
00:06:11,810 --> 00:06:15,520
So then it switches from matching the beginning of the path,
132
00:06:15,520 --> 00:06:19,220
with this path to matching the full path.
133
00:06:19,220 --> 00:06:22,270
And therefore now again, if we saved this,
134
00:06:22,270 --> 00:06:26,030
the product detail page would not match slash products,
135
00:06:26,030 --> 00:06:28,318
because it's not a full match,
136
00:06:28,318 --> 00:06:31,280
slash products on the other end would.
137
00:06:31,280 --> 00:06:33,440
And that would then be ways of making it work
138
00:06:33,440 --> 00:06:34,890
in the way we want.
139
00:06:34,890 --> 00:06:37,060
Again, I'm showing this in great detail,
140
00:06:37,060 --> 00:06:40,350
because these other behaviors are not wrong
141
00:06:40,350 --> 00:06:41,420
or a problem.
142
00:06:41,420 --> 00:06:43,690
Sometimes you want to build applications,
143
00:06:43,690 --> 00:06:47,600
where multiple routes can be active at the same time.
144
00:06:47,600 --> 00:06:49,510
But sometimes you don't want to do that.
145
00:06:49,510 --> 00:06:52,693
And then this here is a solution you can use.
11207
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.