Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,040 --> 00:00:03,880
So how do we then get access
2
00:00:03,880 --> 00:00:06,860
to the concrete value that was entered by the user
3
00:00:06,860 --> 00:00:08,330
in the URL?
4
00:00:08,330 --> 00:00:13,330
For this, Next.js gives us a special hook we can use.
5
00:00:13,520 --> 00:00:16,760
For this, we need to import something from
6
00:00:16,760 --> 00:00:19,090
Next slash router.
7
00:00:19,090 --> 00:00:22,260
So from the Next library
8
00:00:22,260 --> 00:00:25,320
and there from the router sub package.
9
00:00:25,320 --> 00:00:29,680
And we need to import the use router hook.
10
00:00:29,680 --> 00:00:33,920
So that's a React hook defined by the Next.js team
11
00:00:33,920 --> 00:00:37,910
which we can use an any functional component.
12
00:00:37,910 --> 00:00:42,470
Alternatively, if you're working with class-based components
13
00:00:42,470 --> 00:00:46,500
there also is a width router, higher order component
14
00:00:46,500 --> 00:00:49,660
which you can wrap around your class based component
15
00:00:49,660 --> 00:00:54,660
to get a special router prop injected into your component.
16
00:00:55,250 --> 00:00:58,010
But we're using the more modern
17
00:00:58,010 --> 00:01:00,750
functional component only approach here
18
00:01:00,750 --> 00:01:04,093
and therefore, we will use this use router hook.
19
00:01:05,060 --> 00:01:07,040
Now inside of the functional component
20
00:01:07,040 --> 00:01:09,780
we can then call use router like this
21
00:01:09,780 --> 00:01:14,360
and this will return a router object to us.
22
00:01:14,360 --> 00:01:17,120
Again, if you would be using width router
23
00:01:17,120 --> 00:01:20,260
you would be getting that router key here
24
00:01:20,260 --> 00:01:21,993
on your props object.
25
00:01:23,000 --> 00:01:25,090
Now, what does this router
26
00:01:25,090 --> 00:01:27,650
object give us which we're getting here?
27
00:01:27,650 --> 00:01:32,290
This router object exposes various pieces of data
28
00:01:32,290 --> 00:01:34,750
and various methods we can use.
29
00:01:34,750 --> 00:01:38,070
Now we'll have a look at the methods later when we dive into
30
00:01:38,070 --> 00:01:40,750
navigating between pages, but when it comes
31
00:01:40,750 --> 00:01:45,710
to the data properties, we, for example, get the path name
32
00:01:45,710 --> 00:01:47,570
and let's see what's inside of that
33
00:01:47,570 --> 00:01:50,490
by simply console logging it here.
34
00:01:50,490 --> 00:01:54,320
So that we can see what we get there as a value.
35
00:01:54,320 --> 00:01:57,150
And that's also console log something else.
36
00:01:57,150 --> 00:01:59,920
Let's console log, router dot query.
37
00:01:59,920 --> 00:02:02,780
That's another interesting property we can access
38
00:02:02,780 --> 00:02:04,523
on that router object.
39
00:02:05,920 --> 00:02:10,780
With that, if we saved as updated project ID page here.
40
00:02:10,780 --> 00:02:15,620
If we go to slash portfolio slash something
41
00:02:15,620 --> 00:02:16,990
or anything like that,
42
00:02:16,990 --> 00:02:19,950
and we then open up the developer tools,
43
00:02:19,950 --> 00:02:21,410
we see the path here.
44
00:02:21,410 --> 00:02:23,610
And the interesting thing here is that we don't
45
00:02:23,610 --> 00:02:27,770
see the path we entered into URL slash portfolio
46
00:02:27,770 --> 00:02:32,630
slash something, but the path inferred by Next.js.
47
00:02:32,630 --> 00:02:35,360
So the path to component file,
48
00:02:35,360 --> 00:02:38,690
that had to be loaded to bring this to the screen,
49
00:02:38,690 --> 00:02:42,000
the portfolio folder and then to square bracket
50
00:02:42,000 --> 00:02:45,960
project ID JS file if you want to read it like this.
51
00:02:45,960 --> 00:02:49,750
And that of course is the path Next.js had to take
52
00:02:49,750 --> 00:02:51,310
to reach this component,
53
00:02:51,310 --> 00:02:53,663
which is responsible for what we see here.
54
00:02:54,550 --> 00:02:59,550
So that is the encoded, the inferred path Next.js took.
55
00:03:00,890 --> 00:03:03,210
Then here, we have an empty object
56
00:03:03,210 --> 00:03:06,770
but we can ignore this double output we're getting here
57
00:03:06,770 --> 00:03:07,690
for the moment.
58
00:03:07,690 --> 00:03:10,220
Instead, if we have a look at the last log here,
59
00:03:10,220 --> 00:03:13,150
we see that it's actually not an empty object
60
00:03:13,150 --> 00:03:16,260
but it is an object with a project ID property
61
00:03:16,260 --> 00:03:19,080
and then a value of something.
62
00:03:19,080 --> 00:03:21,530
And that's the interesting part now.
63
00:03:21,530 --> 00:03:24,700
This query property on the router
64
00:03:24,700 --> 00:03:27,940
gives us access to the concrete data
65
00:03:27,940 --> 00:03:31,170
that is encoded in the URL.
66
00:03:31,170 --> 00:03:34,210
So it gives us access to the concrete value
67
00:03:34,210 --> 00:03:37,410
we get for this dynamic path segment.
68
00:03:37,410 --> 00:03:40,620
So we entered portfolio slash something
69
00:03:40,620 --> 00:03:43,680
and therefore something is the concrete value
70
00:03:43,680 --> 00:03:46,090
for this project ID placeholder.
71
00:03:46,090 --> 00:03:48,590
And that's why on this query object,
72
00:03:48,590 --> 00:03:51,400
we get project ideas a key name
73
00:03:51,400 --> 00:03:53,500
because I picked project ID here
74
00:03:53,500 --> 00:03:56,320
as a value between the square brackets
75
00:03:56,320 --> 00:04:01,320
and then something so what we entered into URL as a value.
76
00:04:01,540 --> 00:04:06,010
And that means that we can use this router query property
77
00:04:06,010 --> 00:04:10,690
to get access to the concrete value encoded in the URL.
78
00:04:10,690 --> 00:04:14,640
And if this would be the ID of some database object,
79
00:04:14,640 --> 00:04:19,640
we could then send a request to some backend server
80
00:04:19,800 --> 00:04:21,510
to fetch the
81
00:04:22,420 --> 00:04:26,100
piece of data with an ID of
82
00:04:26,100 --> 00:04:30,163
router dot query dot project ID in this case.
83
00:04:31,120 --> 00:04:33,670
That's how we could use this encoded value.
84
00:04:33,670 --> 00:04:36,580
And that of course, is how we will use it later
85
00:04:36,580 --> 00:04:39,110
once we also dive into data fetching.
86
00:04:39,110 --> 00:04:41,470
Here I just want you to understand
87
00:04:41,470 --> 00:04:44,540
how that routing works and how we can get access
88
00:04:44,540 --> 00:04:47,400
to the different pieces of data we might need.
89
00:04:47,400 --> 00:04:50,100
And that is how we can get access
90
00:04:50,100 --> 00:04:52,743
to this dynamic segment value.
7126
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.