Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,840 --> 00:00:07,800
OK I know it's been a lot for this menu but we really have it looking pretty good right now and we're
2
00:00:07,800 --> 00:00:14,430
just going to go ahead and add a little bit of extra functionality to maintain which menu item we are
3
00:00:14,430 --> 00:00:17,060
actually on now.
4
00:00:17,070 --> 00:00:23,760
If you remember when I walked through the menu documentation they actually had an example of how to
5
00:00:23,760 --> 00:00:33,060
do this by setting a selected prop to the menu item and checking against a selected index state to determine
6
00:00:33,090 --> 00:00:34,930
which value is active.
7
00:00:34,980 --> 00:00:42,640
And so then which menu item deserves the selected styling to do this we're going to have to change a
8
00:00:42,640 --> 00:00:48,470
few things but then it will result in some really clean looking functionality.
9
00:00:48,520 --> 00:00:55,660
So let's go ahead and get started by coming over to our header function and underneath the open and
10
00:00:55,720 --> 00:01:00,910
set open hooks we're going to create a another set of state.
11
00:01:00,970 --> 00:01:14,710
This will be the selected index and set selected index hooks with the use state function with a default
12
00:01:14,710 --> 00:01:17,320
state of 0.
13
00:01:17,330 --> 00:01:26,600
Now we actually need to be iterating over a list of our menu options so that we have access to an index
14
00:01:26,600 --> 00:01:36,250
for each one where currently we have no way of tracking an index of each menu item so to take care of
15
00:01:36,250 --> 00:01:42,340
this we're going to refactor this a little bit because you can see actually that there is a lot of a
16
00:01:42,340 --> 00:01:45,630
repeated code going on between each of these.
17
00:01:45,670 --> 00:01:48,430
They each have the exact same on click handler.
18
00:01:48,430 --> 00:01:55,100
They all have the component equals link and the classes equals classes dollar menu item.
19
00:01:55,100 --> 00:02:02,110
So the only things that are different are the path for their route in the text that they have inside
20
00:02:02,140 --> 00:02:04,210
of the menu item.
21
00:02:04,230 --> 00:02:09,760
I think that we can refactor this pretty easily and create some pretty clean code.
22
00:02:09,810 --> 00:02:19,530
So let's go up here to the top and underneath handle close what's come down here and create our menu
23
00:02:19,680 --> 00:02:25,070
options and we're gonna set this equal to an array.
24
00:02:25,120 --> 00:02:34,890
This will allow us to map over each element in the array and have an index assigned to each one so each
25
00:02:34,950 --> 00:02:41,970
element in this array actually needs to contain two pieces of information so the elements in this array
26
00:02:42,060 --> 00:02:45,710
we're going to make an object in this object.
27
00:02:45,810 --> 00:02:53,670
We're first going to have a name property set equal to the name or the text that we want set inside
28
00:02:53,670 --> 00:02:54,990
of each menu item.
29
00:02:55,140 --> 00:03:03,810
So services in this case and then a link property where we will set the route that we want.
30
00:03:03,840 --> 00:03:12,090
Each menu item to go to in this case slash services every then go ahead and make one of these objects
31
00:03:12,150 --> 00:03:13,910
for each path.
32
00:03:13,980 --> 00:03:23,250
So name custom software development and then the link of slash custom software.
33
00:03:24,540 --> 00:03:26,700
And another one for loops.
34
00:03:26,730 --> 00:03:39,350
Didn't mean to open that up for name of mobile app development and then a link of slash mobile apps
35
00:03:39,900 --> 00:03:42,620
and then comb and fix things.
36
00:03:43,050 --> 00:03:45,400
Mobile apps and then the last one.
37
00:03:45,420 --> 00:03:54,870
A name of Web site development and a link of slash Web sites.
38
00:03:54,870 --> 00:04:00,870
So that is all we need set up to implement this along with a click handler.
39
00:04:00,900 --> 00:04:10,370
So actually underneath this handle click let's create a concept handle handle menu item click.
40
00:04:10,470 --> 00:04:18,720
And to set this equal to not only a function taking any event but also we need an index passed through
41
00:04:18,790 --> 00:04:19,690
as well.
42
00:04:19,860 --> 00:04:22,940
And then we'll create an arrow function opening it up.
43
00:04:23,160 --> 00:04:31,470
And if we're clicking on a menu item we still want to set the anchor element to null because the menu
44
00:04:31,470 --> 00:04:41,210
will then go away which means we also need set open to false but then the most important piece here
45
00:04:41,540 --> 00:04:52,840
is we need to set our set selected index to pass in the index of the menu item we just clicked on.
46
00:04:52,840 --> 00:05:02,110
So now if we come down and we're going to remove all of these menu items so all of our menu items let's
47
00:05:02,110 --> 00:05:10,390
go ahead and get rid of them and we're going to open up a javascript function or inject some javascript
48
00:05:10,390 --> 00:05:20,080
here and we're going to take our menu options constant and we're going to map over it then for each
49
00:05:20,170 --> 00:05:24,960
element inside of here we're going to take in option.
50
00:05:24,970 --> 00:05:33,340
So this will be our individual option as well as an index which we then turn into an arrow function
51
00:05:34,240 --> 00:05:37,180
and open it up inside of here.
52
00:05:37,300 --> 00:05:42,580
We'll create our menu item and open it up.
53
00:05:42,580 --> 00:05:48,370
And on the inside we're going to pass Oh it's like we have a problem here.
54
00:05:48,430 --> 00:05:54,270
I think actually and get rid of these brackets and I think it should have been a parentheses.
55
00:05:54,280 --> 00:05:57,540
So make sure that you get those put in place.
56
00:05:57,550 --> 00:05:58,620
Yep that faced it.
57
00:05:58,690 --> 00:06:03,420
Because by having the parentheses we won't need a return statement.
58
00:06:03,430 --> 00:06:05,820
And this is just returned directly.
59
00:06:05,890 --> 00:06:12,850
So make sure you change those brackets to a parentheses and inside of our menu item we're going to render
60
00:06:12,850 --> 00:06:16,510
the option dot name.
61
00:06:16,690 --> 00:06:23,550
And that is coming from the name a property that we set here on our menu options.
62
00:06:23,590 --> 00:06:32,920
So for each option in there it's going to set the name that we have set inside of the menu item if we
63
00:06:32,920 --> 00:06:35,140
went ahead and just saved this.
64
00:06:35,140 --> 00:06:43,540
Now we would see when we hover over the services tab there we still have all of our menu items but now
65
00:06:43,540 --> 00:06:50,020
they're not styled the same as they were before because we haven't put the class back on and they don't
66
00:06:50,020 --> 00:06:54,520
actually navigate us anywhere because we haven't set that up either.
67
00:06:54,520 --> 00:07:02,020
But you can see that it is successfully rendering the names for each menu item from our menu options
68
00:07:02,140 --> 00:07:11,760
array so let's continue getting this set back up how it was and we'll assign the component prop to our
69
00:07:11,760 --> 00:07:22,670
menu item of link and set that to our option dot link so now for every option that we've passed in and
70
00:07:22,670 --> 00:07:27,240
we'll have the appropriate link to the route that it needs to go to.
71
00:07:27,440 --> 00:07:36,650
And then we need to set our styling again by setting our collapses of the object with root and then
72
00:07:37,010 --> 00:07:48,320
classes dot menu item and then of course set are on click equal to our parentheses or brackets and then
73
00:07:48,320 --> 00:07:57,830
turn that into an arrow function which is then going to call a set of functions first being the handle
74
00:07:58,250 --> 00:08:00,320
menu item click.
75
00:08:00,320 --> 00:08:07,430
And if you come up to the handle menu item click function you'll remember it takes the Click event as
76
00:08:07,430 --> 00:08:14,020
well as an index because we need access to the index after the event that's passed through.
77
00:08:14,030 --> 00:08:21,860
So we actually need to change our arrow function to take the event property that is being passed and
78
00:08:21,860 --> 00:08:31,850
pass that along to the handle menu item click function as the event and then pass are index which is
79
00:08:31,940 --> 00:08:36,200
coming from mapping over the menu options.
80
00:08:36,260 --> 00:08:42,890
This will now set the correct menu item to being selected whenever we click on it.
81
00:08:42,950 --> 00:08:51,050
But we then also need to set the selected prop which will apply the styling if the current menu item
82
00:08:51,080 --> 00:09:00,110
is the one selected will determine this by setting this equal to check if the index the index that we're
83
00:09:00,110 --> 00:09:10,360
currently on of IE is equal to the selected index state value the selected index state value is going
84
00:09:10,360 --> 00:09:16,140
to be called with this index value any time we click on the menu item.
85
00:09:16,180 --> 00:09:24,790
And so if that index is set for these selected index and that is the index we're currently on then that
86
00:09:24,790 --> 00:09:29,090
menu item will receive the selected styling.
87
00:09:29,100 --> 00:09:39,240
Now remember the on click handler also actually needed to call the set value function of set to 1 to
88
00:09:39,240 --> 00:09:43,370
set the currently selected tab to the services tab.
89
00:09:43,410 --> 00:09:51,570
We also need to handle closing the menu whenever this is clicked because it is then obviously navigating
90
00:09:51,570 --> 00:09:56,710
to a new page since we're iterating over an array.
91
00:09:56,710 --> 00:10:05,710
We also because of react we have to add the key component to any of the elements that are being rendered
92
00:10:05,800 --> 00:10:06,850
dynamically.
93
00:10:06,970 --> 00:10:14,500
And we need to set this to a unique identifier and to do that we'll just pass the entire option through
94
00:10:14,710 --> 00:10:19,490
because that will never be the same for any of the components.
95
00:10:19,500 --> 00:10:26,940
Now if we go ahead and save this it will refresh the application and reorganize my code here to be a
96
00:10:26,940 --> 00:10:28,410
little bit cleaner.
97
00:10:28,410 --> 00:10:36,630
And now if we come over and we hover over our services tab we can hover over these and you'll notice
98
00:10:36,640 --> 00:10:45,250
the services tab at the top now has a darkened background to indicate its selection.
99
00:10:45,270 --> 00:10:48,630
Now it looks like our routes are a little messed up right now.
100
00:10:48,630 --> 00:10:52,550
So let's go back to the home page and I'll show you why in just a minute.
101
00:10:52,800 --> 00:11:00,240
But if we are on the home page and we hover over the services tab you can see that the services tab
102
00:11:00,450 --> 00:11:07,340
is actually still showing that is being selected even though we're on the home screen.
103
00:11:07,410 --> 00:11:14,670
So even though the the services tab was previously selected we don't want that to still be shown if
104
00:11:14,670 --> 00:11:16,870
we're not actually on the page.
105
00:11:17,040 --> 00:11:23,220
So to make sure that that doesn't happen when we come over here and on the menu item when we set these
106
00:11:23,220 --> 00:11:33,450
selected property we should also check that we are actually on the value of 1 equals 1 and that is making
107
00:11:33,450 --> 00:11:39,210
sure that the value of the active tab is set to the services page.
108
00:11:39,240 --> 00:11:45,540
So if we go ahead and save that we'll come over here and now still on the home page if we hover over
109
00:11:45,540 --> 00:11:49,960
the services tab you'll see that none of the tabs are selected.
110
00:11:50,100 --> 00:11:56,880
And if we come over to the mobile app page and now hover you'll see that it does show selection.
12628
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.