Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,810 --> 00:00:09,670
We just learned how to navigate through our application using the link component and the history property
2
00:00:09,670 --> 00:00:11,740
that we get from Mauriac router dom.
3
00:00:12,680 --> 00:00:19,490
Now, how would we actually go about setting up our menu item so that it allows us to navigate to different
4
00:00:19,490 --> 00:00:20,090
pages?
5
00:00:20,750 --> 00:00:26,660
More specifically, we want to use our has menu item to navigate to our new hats page.
6
00:00:28,030 --> 00:00:36,280
Well, one of the caveats of using those properties that we get from route, which is the match, the
7
00:00:36,280 --> 00:00:44,950
history and the location property, is that we actually only get access to them from the first component
8
00:00:44,950 --> 00:00:47,080
that gets passed into our route.
9
00:00:47,890 --> 00:00:50,710
So even though directory and.
10
00:00:51,690 --> 00:00:59,430
Menu item, our children of our home page component, only our home page component gets access to the
11
00:00:59,430 --> 00:01:04,920
history propp that we need because it's the home page component that we're passing into root.
12
00:01:05,340 --> 00:01:09,770
So Raoh only passes it one child in which is its child.
13
00:01:09,930 --> 00:01:10,890
So our home page.
14
00:01:12,640 --> 00:01:19,390
One solution we could do is we could pass these props down into our directory, we could pass the history
15
00:01:19,840 --> 00:01:27,250
into our directory, and then we can pass that history from our directory down into our menu item.
16
00:01:30,090 --> 00:01:38,100
This props taught history, and by doing so, our menu item now has access to the history and its props.
17
00:01:39,150 --> 00:01:48,090
The problem with doing it this way is that this is actually a bad pattern called prop tunneling, because
18
00:01:48,090 --> 00:01:54,270
we're tunneling these props, multiple children deep in order to get them to the component that needs
19
00:01:54,270 --> 00:01:54,450
them.
20
00:01:55,230 --> 00:02:03,750
But the children in between don't actually need the history property for any reason other than to pass
21
00:02:03,750 --> 00:02:04,580
it to their children.
22
00:02:06,030 --> 00:02:13,050
Now, luckily, there's a solution that recruiter Dom provides us that allows us to not have to do this.
23
00:02:14,180 --> 00:02:15,330
So let's take a look.
24
00:02:15,920 --> 00:02:17,660
So first, let's undo.
25
00:02:18,760 --> 00:02:19,240
That.
26
00:02:20,190 --> 00:02:21,900
Prop tunneling code that we just wrote.
27
00:02:24,270 --> 00:02:25,730
We don't need that either.
28
00:02:27,060 --> 00:02:36,420
And what we're going to use is actually a nother component from react router dom, and this component
29
00:02:36,630 --> 00:02:38,460
is called with the router.
30
00:02:43,820 --> 00:02:52,280
Now, what with router is is it's a higher order component and a higher order component is essentially
31
00:02:52,280 --> 00:02:59,470
a function that takes a component as an argument and we're turned you a modified component.
32
00:03:00,260 --> 00:03:08,840
So just like how our menu item is a function that takes props and renders out a component, a higher
33
00:03:08,840 --> 00:03:16,850
order component is a function that takes any component and modifies it in some way and then returns
34
00:03:16,850 --> 00:03:18,990
you that new modified component.
35
00:03:19,550 --> 00:03:28,310
So it's kind of like a function that gives you back a powered up component in this case, where powering
36
00:03:28,310 --> 00:03:35,750
up our menu item component to have access to those things related to our router.
37
00:03:36,810 --> 00:03:46,050
So what we would do is we just pass our ups, not history, we just pass our menu item component into
38
00:03:46,050 --> 00:03:47,040
this with Rueter.
39
00:03:47,910 --> 00:03:55,080
And with Rueter will then return us back with the same name of this menu item, right, what we're whatever
40
00:03:55,080 --> 00:04:03,360
it is that we're exporting by default, it will return us back a super powered menu item component with
41
00:04:03,360 --> 00:04:10,790
access now to those location match and history props that we need access to.
42
00:04:11,460 --> 00:04:15,120
So by wrapping now, we now have access to history.
43
00:04:18,260 --> 00:04:25,340
And what we want to do with history is we want to wrap our whole div items so that on unclick.
44
00:04:27,080 --> 00:04:28,190
We're going to push.
45
00:04:30,500 --> 00:04:31,730
History not push.
46
00:04:33,230 --> 00:04:34,130
To some Yoro.
47
00:04:35,250 --> 00:04:37,350
Now, how do we do this?
48
00:04:37,740 --> 00:04:45,960
Well, what we want to be able to do is have these you are al IDs almost be dynamically generated as
49
00:04:45,960 --> 00:04:46,260
well.
50
00:04:47,750 --> 00:04:54,980
Because we know that where we're going is going to be the hats page, so we know that our route needs
51
00:04:54,980 --> 00:04:56,190
a hats route, right.
52
00:04:56,510 --> 00:05:06,320
So what we can do for now, just to hardcoded, is we can go into our directory component.
53
00:05:08,500 --> 00:05:14,620
And add a neutral parameter here, like a link of sorts, right link URL.
54
00:05:15,670 --> 00:05:18,310
And we're just going to set this to hats.
55
00:05:19,820 --> 00:05:27,830
So after this, we want to add the same value, but to every other section, but for now, we'll just
56
00:05:27,830 --> 00:05:35,500
keep it as an empty string because we don't actually have those other routes set up yet.
57
00:05:37,450 --> 00:05:40,810
Now we're going to pass this link, you are all into our menu item.
58
00:05:41,880 --> 00:05:49,080
And it is on our section, right, that we're mapping through, but because we're at we're getting more
59
00:05:49,080 --> 00:05:54,360
and more verbose, it's very easy for us to just do this again.
60
00:05:54,360 --> 00:05:55,230
Link you URL.
61
00:05:56,490 --> 00:06:02,790
And then to pass it in as link you all, but you'll notice that these keys that were structuring off
62
00:06:02,790 --> 00:06:10,460
are essentially equivalent to the exact same property name that we're also passing in to our menu item.
63
00:06:11,130 --> 00:06:12,630
So there's actually a little yes.
64
00:06:12,630 --> 00:06:23,010
Six trick we can do by spreading the rest of these values together that we don't need for key because
65
00:06:23,010 --> 00:06:28,170
ID we're not actually passing it as ID, we can remove that.
66
00:06:29,820 --> 00:06:36,390
And then set the rest of them as section props or other section props.
67
00:06:37,520 --> 00:06:42,050
And now we can spread those values in like this.
68
00:06:43,030 --> 00:06:49,750
And this is equivalent to us writing out what we did before by spreading these values through, we're
69
00:06:49,750 --> 00:06:56,680
saying that every other key value pair we want to pass in where the key of title goes to the value of
70
00:06:56,680 --> 00:06:57,110
men's.
71
00:06:57,670 --> 00:07:01,450
So this is equivalent to us having done that.
72
00:07:04,600 --> 00:07:10,480
And now in our menu item, we will have access to the link URL.
73
00:07:12,040 --> 00:07:14,680
Which we now want to push.
74
00:07:16,020 --> 00:07:24,960
However, we also don't know in our menu item where in our directory we will be, so what we can do
75
00:07:25,920 --> 00:07:28,080
is we can take the match.
76
00:07:30,860 --> 00:07:31,370
As well.
77
00:07:33,350 --> 00:07:39,260
And just like in our demonstration earlier, we can do match dot you URL.
78
00:07:40,600 --> 00:07:41,650
Into.
79
00:07:44,340 --> 00:07:45,480
Link Yoro.
80
00:07:47,080 --> 00:07:48,550
And now if we save this.
81
00:07:49,950 --> 00:07:56,430
We should see if we click our hats page, it navigates us to our hearts page and if we were to change
82
00:07:56,430 --> 00:08:04,440
our You URL at any point, our component is still fully aware and functional because it knows that it's
83
00:08:04,440 --> 00:08:09,810
just going to base where it's been matched up to and then append the URL.
84
00:08:12,120 --> 00:08:19,110
And with that, we have some basic navigation setup inside of our menu item component.
85
00:08:20,710 --> 00:08:22,570
So now with this in mind.
86
00:08:23,560 --> 00:08:25,060
Let's commit our code.
87
00:08:26,090 --> 00:08:27,410
So let's see what we have to commit.
88
00:08:28,560 --> 00:08:30,780
So we have to now.
89
00:08:31,920 --> 00:08:36,299
Update our menu items.
90
00:08:38,190 --> 00:08:39,210
To navigate.
91
00:08:40,590 --> 00:08:41,100
With.
92
00:08:42,340 --> 00:08:44,290
Rueter, HHC.
93
00:08:46,440 --> 00:08:52,650
Now we're going to explore this concept of higher order component further a little later when we write
94
00:08:52,650 --> 00:09:01,080
our own, but for now, just remember, our higher order component is just a function that takes another
95
00:09:01,620 --> 00:09:11,250
component as an argument, transforms it into another component, and returns that transformed component
96
00:09:11,250 --> 00:09:12,090
out of itself.
97
00:09:13,420 --> 00:09:15,590
So we'll definitely explore that further.
98
00:09:16,420 --> 00:09:19,290
But for now, just think about it simply like that.
99
00:09:19,780 --> 00:09:22,630
So let's get Push or jam master our code.
100
00:09:23,590 --> 00:09:26,200
And let's move on to the next lesson.
9496
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.