Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,820 --> 00:00:06,940
All right so I know that I said we were finished up with the header but I've found a couple of things
2
00:00:06,950 --> 00:00:14,690
I just want to refactor real quick and clean up before we move on to the rest of the project so I'll
3
00:00:14,690 --> 00:00:15,980
try and be quick with this.
4
00:00:15,980 --> 00:00:23,840
So let's just jump on into it and we'll come on down over to our header dot J S file and we want to
5
00:00:23,840 --> 00:00:28,000
go to our use affect function.
6
00:00:28,070 --> 00:00:36,530
So where we are using the or setting up the active tab refresh fix and I realized that when we refactor
7
00:00:36,560 --> 00:00:43,760
this to use the switch statement we actually left in all of the if else statements from our previous
8
00:00:43,760 --> 00:00:44,510
approach.
9
00:00:44,510 --> 00:00:47,830
So right now let's just go ahead and get rid of that.
10
00:00:47,840 --> 00:00:54,530
So we'll close that up and now we're going to add some code that's really going to clean up the header
11
00:00:54,950 --> 00:01:02,350
and refactor everything in about the best way I think we can if you look throughout the header you'll
12
00:01:02,350 --> 00:01:10,000
see even here in the switch statement what looks to be a lot of similar code and same thing with the
13
00:01:10,120 --> 00:01:14,800
tabs here along with all of the list items.
14
00:01:14,800 --> 00:01:17,820
So here there is a lot of repetition.
15
00:01:17,830 --> 00:01:23,980
It looks like and I think that we could actually clean this up and do a bit of a better job at just
16
00:01:23,980 --> 00:01:31,100
making this code as efficient as possible so to begin this process I'm going to come right underneath
17
00:01:31,130 --> 00:01:35,200
the menu options and let's create another constant.
18
00:01:35,270 --> 00:01:40,720
But this time called our roots also equal to an array.
19
00:01:40,730 --> 00:01:47,390
This array is going to be of a similar structure to the menu options where we will use an object for
20
00:01:47,390 --> 00:01:52,830
each array item to hold our two pieces of root information.
21
00:01:52,850 --> 00:02:03,380
So let's start with a name for our home root and a link for that for just slash.
22
00:02:03,380 --> 00:02:13,910
I'm actually going to go ahead and copy this and then we'll just paste that three four and five times.
23
00:02:13,910 --> 00:02:18,780
And now let's go in and change each of these for each of our different routes.
24
00:02:18,800 --> 00:02:30,970
So the second one being services slash services the name for the revolution for Slash.
25
00:02:30,980 --> 00:02:46,990
Revolution Rev Lucian and then about us about us for Slash about and then contact us for Slash contact.
26
00:02:47,060 --> 00:02:52,910
I'm actually going to go ahead and leave the estimate real out of this and you'll see why here and a
27
00:02:52,910 --> 00:02:54,510
little bit.
28
00:02:54,570 --> 00:02:54,870
All right.
29
00:02:54,870 --> 00:03:01,620
With that in place let's come down into our use effect function and here we're actually going to start
30
00:03:01,620 --> 00:03:09,450
the first refactor with our switch statement because I see a lot of repeated code in here where we're
31
00:03:09,450 --> 00:03:16,650
just repeatedly checking the link that we want to mark as active and then we're making sure that that
32
00:03:16,650 --> 00:03:19,220
value is not already set.
33
00:03:19,260 --> 00:03:27,150
And then if it is not we go ahead and set the correct values so let's go ahead and start to try and
34
00:03:27,150 --> 00:03:28,640
generalize this.
35
00:03:28,740 --> 00:03:34,440
And what I'm gonna do is get a bunch of the code on the screen so we're just going to type it all out
36
00:03:34,710 --> 00:03:39,090
and then I'll go through and explain how all of it is working.
37
00:03:39,110 --> 00:03:39,440
All right.
38
00:03:39,500 --> 00:03:51,950
So I'm gonna start within array and here I'm going to spread the menu options and the roots then for
39
00:03:52,370 --> 00:03:55,950
each of the items within this new Array.
40
00:03:56,330 --> 00:04:05,600
Let's take the roots and then open up the arrow function to begin with a switch where we will switch
41
00:04:05,600 --> 00:04:19,340
over the window window dot location dot path name and inside of here our case will use the back ticks
42
00:04:19,370 --> 00:04:30,080
to inject javascript into the string and we will make this our route dot link then inside of the case
43
00:04:30,440 --> 00:04:43,610
if our current value does not equal our current route dot active index well then we're going to set
44
00:04:43,790 --> 00:04:49,240
our value to the route dot active index.
45
00:04:49,340 --> 00:05:06,930
And then if our route dot selected index exists and that route dot selected selected index is not equal
46
00:05:06,930 --> 00:05:18,660
to the already selected index well then we are going to set the selected index to our route dot selected
47
00:05:18,930 --> 00:05:27,750
index now what's come out of the case make sure we're outside of this pair of bracket and here will
48
00:05:27,750 --> 00:05:37,770
add or break statement break statement and then outside of that we're going to add our default case
49
00:05:38,130 --> 00:05:41,590
with a break statement there as well.
50
00:05:41,610 --> 00:05:41,970
All right.
51
00:05:42,000 --> 00:05:48,600
So now let's actually go ahead and delete the entire switch statement that we have down here below.
52
00:05:48,600 --> 00:05:52,110
So we'll get rid of that and forgot the little bracket.
53
00:05:52,110 --> 00:05:54,330
So get rid of the entire switch statement.
54
00:05:54,330 --> 00:06:02,760
So now are use effect only has this new piece of code that we added and then down here for our dependencies
55
00:06:02,760 --> 00:06:03,960
for the use hook.
56
00:06:03,960 --> 00:06:15,850
We need to add the menu options the selected selected index as well as the roots.
57
00:06:15,860 --> 00:06:16,190
All right.
58
00:06:16,220 --> 00:06:17,900
So just a few more changes.
59
00:06:17,930 --> 00:06:22,460
Let's come up to the menu options and the Roots arrays.
60
00:06:22,580 --> 00:06:27,220
And we're going to add a couple of pieces of information to each route.
61
00:06:27,230 --> 00:06:38,120
So up in our first menu option let's add an active index property and we'll set this to one and a selected
62
00:06:38,540 --> 00:06:42,460
index property which we'll set to 0.
63
00:06:42,470 --> 00:06:51,500
Let's go ahead and copy these two properties and we will paste them down on each of these routes here
64
00:06:51,500 --> 00:06:54,000
in the menu options.
65
00:06:54,050 --> 00:06:58,700
And now let's go and change the selected index for each one.
66
00:06:58,820 --> 00:07:05,140
But we're actually going to receive all of the active indexes to 1 for the menu options.
67
00:07:05,180 --> 00:07:11,400
But let's go and set our selected index for custom software development to 1.
68
00:07:11,450 --> 00:07:17,720
These selected index for mobile app development to add these selected index for Web site development
69
00:07:17,810 --> 00:07:19,370
to three.
70
00:07:19,470 --> 00:07:28,960
Then let's come to our roots array and inside of each route here we're going to just add in active index.
71
00:07:29,190 --> 00:07:32,070
And this time we'll set this one to zero.
72
00:07:32,160 --> 00:07:35,990
And now what's copy just the active index property.
73
00:07:36,000 --> 00:07:43,440
And we'll put that on the rest of these routes except the first active index for our services tab and
74
00:07:43,440 --> 00:07:56,510
then the second for the revolution third and the fourth active index.
75
00:07:56,520 --> 00:08:01,030
Let's go ahead and save this now so that the code will look a little bit cleaner.
76
00:08:01,110 --> 00:08:04,830
And now I'm going to walk you through what we actually just did.
77
00:08:05,870 --> 00:08:15,050
So here in our use affect function we started by combining the menu options and the routes arrays that
78
00:08:15,050 --> 00:08:20,540
we have up here then for each of the routes in this new Array.
79
00:08:20,600 --> 00:08:28,310
So now for every single option that we could have then we iterate over that and for each individual
80
00:08:28,310 --> 00:08:35,070
route we create a switch statement that checks the route dot link.
81
00:08:35,090 --> 00:08:43,360
So that is our link property right here as the case to check against our window dot location dot path
82
00:08:43,370 --> 00:08:49,810
name this is exactly what we were doing in the switch statement before except now we're getting the
83
00:08:49,810 --> 00:08:55,500
link from our arrays when we iterate instead of setting them explicitly.
84
00:08:55,580 --> 00:09:03,230
Next we do a check to see if our current value has already been set correctly by checking the active
85
00:09:03,230 --> 00:09:07,680
tab property on our route.
86
00:09:07,690 --> 00:09:14,380
This is why we went through and added an active tab property to each of the routes to specify which
87
00:09:14,410 --> 00:09:17,930
index they need to make sure is set to active.
88
00:09:17,950 --> 00:09:23,860
We do this because all of these services pages so the Web site development mobile app development and
89
00:09:23,860 --> 00:09:31,060
custom software development pages they all set these services tab equal to active and so we couldn't
90
00:09:31,060 --> 00:09:35,040
have just used the index from say mapping over it.
91
00:09:35,140 --> 00:09:41,230
So by setting that value explicitly we guarantee that we're always going to be maintaining the correct
92
00:09:41,260 --> 00:09:42,640
active tab.
93
00:09:42,640 --> 00:09:48,510
And then if we're not already on that active tab we're going to set it correctly.
94
00:09:48,510 --> 00:09:56,580
Lastly we have another check to see if our route dot selected index property even exists.
95
00:09:56,580 --> 00:10:03,300
And then if it does so if this is not undefined then we also check to see if these selected index has
96
00:10:03,300 --> 00:10:08,470
already been set on our selected index piece of state.
97
00:10:08,590 --> 00:10:15,730
This is for our menu items and that is why we went and added these selected index property to each of
98
00:10:15,730 --> 00:10:18,650
our menu option routes.
99
00:10:18,670 --> 00:10:26,440
This is the index of their menu option that will be used to determine which of them is selected again
100
00:10:26,490 --> 00:10:34,270
by setting this explicitly we maintain that we are always on the correct selected menu option if we
101
00:10:34,270 --> 00:10:34,960
are not.
102
00:10:35,020 --> 00:10:41,110
Then we go ahead and set that correctly which makes sure that everything is correct.
103
00:10:41,190 --> 00:10:48,180
This is a really clean piece of code that definitely shrunk down what was a pretty large switch statement
104
00:10:48,480 --> 00:10:54,930
and made it not only more efficient and smaller but also makes it more usable and generalized.
105
00:10:54,930 --> 00:11:01,920
If I wanted to use this same piece of code in a situation in a different project in the future or when
106
00:11:01,920 --> 00:11:04,220
I want to add roots to this project.
107
00:11:04,290 --> 00:11:10,890
This solution is already generalized so that all I would have to do is add roots to the roots array.
11946
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.