02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Building out the site Navigation

Building out the Site Navigation

In this tutorial, we dive into creating a dynamic navigation system for a React-based single-page application (SPA). Navigation is essential for any SPA, and this lesson sets the foundation for building a reusable and scalable menu.


Key Steps

1. Setting Up the Navigation Component

  • Created a new Nav.js file to house our navigation logic.
  • Extracted the HTML structure from a previous layout and adapted it for JSX by replacing all class attributes with className.

2. Integrating the Navigation into the Application

  • Imported the Nav component into the App.js file.
  • Added the navigation as the first element inside the div to ensure it appears at the top.

3. Breaking Down Repetition

  • Observed repetitive HTML in navigation items (like li elements for Portfolio, About, and Contact sections).
  • Created a new NavItem component to encapsulate each individual navigation link.

4. Implementing Default Props

  • Introduced a defaultProps static method in the NavItem component to ensure a default className of page-scroll if no class name is provided.

5. Rendering Navigation Items Dynamically

  • Used the NavItem component to render each menu link dynamically:
    • Passed name and link as props for each navigation item.
    • Set a unique className for the hidden menu item.

Key Code Highlights

Setting Default Props for NavItem

static defaultProps = {
  className: 'page-scroll'
};

This ensures each NavItem has a consistent default style unless overridden.

Rendering Dynamic Navigation Links

<NavItem link="#portfolio" name="Portfolio" />
<NavItem link="#about" name="About" />
<NavItem link="#contact" name="Contact" />

Best Practices and Considerations

  1. Component Reusability
    By breaking down navigation into smaller components like NavItem, we achieve a higher degree of reusability and modularity.

  2. Error Prevention with Default Props
    Default props reduce the risk of runtime errors due to missing or undefined properties.

  3. Dynamic Link Handling
    Centralized navigation logic ensures that links can easily be updated or modified.


Recommendations for 2024

  1. Switch to Functional Components
    Consider using functional components with hooks like useState and useEffect for a more modern React approach.

  2. Leverage TypeScript
    TypeScript can enhance type safety for props, making the codebase more robust and easier to maintain.

  3. CSS-in-JS or Tailwind
    Use modern styling solutions like CSS-in-JS libraries (e.g., styled-components) or utility-first CSS frameworks (e.g., TailwindCSS) for scalable styling.


In the next tutorial, we'll explore bubbling events in React and how to manage menu click interactions effectively as we progress toward building a fully functional single-page application.

Video Transcript

00:01For us to build out a single page application,
00:03we really need our menu.
00:05The one thing we haven't really created yet.
00:07So let's start by creating our navigation
00:10and start thinking the planning process of
00:12how do we want our navigation to actually work?
00:15And we're not gonna be done with it
00:16because that's gonna be our main topic throughout the
00:19remaining lectures.
00:20But let's set up our first navigation element.
00:25Okay, let's continue and get our navigation in as well.
00:28And with the magic of uh, the internet
00:31and technology, I created the file already in advance.
00:34So let's go ahead. It's an empty file called nav js
00:37and I'm just gonna go ahead
00:38and copy our navigation out of the HTML, save the html,
00:43go back into our nav js, get rid of all the comments
00:46because they don't exist in JSX and get rid of that.
00:51Get rid of that. And of course go through
00:53and run any place that has a class find, replace
00:59and replace class with
01:01class name.
01:05Go ahead and replace all of those babies
01:07and I think that covers it.
01:09If we'll have any errors, we'll fix them as we see them.
01:12Um, next is let's go ahead
01:13and just copy from somewhere our default rendering
01:17class structure.
01:19And let me just go ahead and paste it right in here
01:23and let's grab that baby and do a return.
01:30Now, just by doing this, we should have our new nav render.
01:34Let's go ahead and oto,
01:35obviously give it the na a name on nav,
01:37it's default exporting.
01:39Let's go into our app imported and test it out.
01:41So I went ahead and already imported it, magic
01:45of technology again.
01:47And I'm gonna go into my nav
01:48and I'm gonna add it into our, as the first item
01:51inside of our diviv.
01:53And if I go ahead and save it, go back
01:55and click here on, well automatically we'll see
01:57that we got our portfolio.
01:58Alright, so the basics,
01:59these good stuff we've done already before.
02:01So let's continue down the line
02:03and if we take a look at our navigation again,
02:05we're gonna see a lot of repetition
02:06and that repetition is really happening in the area here,
02:10where we have actually the navigational elements themselves,
02:14our portfolio, our about section
02:18and so on and so forth.
02:19So what I wanna do is I want to extract this baby out
02:23and position it as our, I would say as our um,
02:28uh, single items I would call this,
02:30let's call this a nav item.
02:32So it's a, we're not gonna do, we're not gonna export it.
02:34I'm just gonna call it class nav item.
02:37And it's gonna extend not export, extends, react component
02:42and all the other greatness that we've seen previously.
02:45Only this time around, I'm gonna go ahead
02:48and attach to it basically one of these items.
02:51So I could see the class names change
02:54and I could see that class names change.
02:57Okay? So if the class names change,
02:59what I'm gonna do is I'm gonna have it by default set up
03:03to be page scroll, unless remember when we did our default.
03:06So let's go ahead and set a default class name.
03:10How do we set up that default class name?
03:12If you recall, when we were working just last section inside
03:15of our footer, what we've done was created default props,
03:19which was a static variable.
03:20And that's exactly what we want to do inside of our nav.
03:23So I'm just gonna copy it.
03:24But instead of a title or content, I'm just gonna have your,
03:27our class name
03:31and to set our class name, I'm gonna set that default
03:34to be a page scroll.
03:36Although technically we're not really doing much
03:37with it right now, it's just for the sake of practice.
03:40So we have your default for that class.
03:43Now that we have a default for the class, we could go ahead
03:45and copy one of those lis only this time
03:48around when we're export, when we're returning that at all.
03:51First of all, I don't have to put this in anymore,
03:53is a default, but what I could do is I could just go ahead
03:56and say here, hey, by the way, this stop props.
03:59So all the props that will come through will be assigned to
04:02that li and
04:03among them the class name will always be assigned.
04:06So if no props are sent in,
04:07besides that, so it's gonna send it in.
04:09And even if the properties are not being
04:10used, it's completely fine.
04:12Now when we're taking a look at here, we'll see
04:13that we have your two elements.
04:15One is an ID and the other is a title.
04:17So those don't really need any defaults,
04:19but we could definitely go ahead and set here.
04:22Um, for our about, I'm gonna go ahead here
04:25and just set here, uh, this dot props.
04:28And you know, let me take a look at our data structure
04:30that we already created in our app.
04:33Let's take a look. Where,
04:35was it in the app or is it in the client?
04:36Yeah, in the client. So we have link, we have name.
04:39So let's, let's use the same type
04:41of structuring link and name.
04:43So again, so let's call this link
04:46and name 'cause our name is gonna be visible,
04:56Beautiful, just to keep with some sort of a convention.
04:59Beautiful. So now that we have this item, let's go ahead
05:01and actually implement it.
05:03So if we go and try to implement it in our nav item.
05:06So first of all, for this original first item,
05:10we definitely wanna send in a class name
05:12because we're gonna set it to be hidden,
05:13which is different than the rest.
05:15The next item, we want to set a um, title.
05:19We don't need to or a name.
05:21We don't need to 'cause it's not using one in this one,
05:23but we definitely need a link.
05:24So let's go ahead and set here our link to be this
05:28top page item.
05:30And let's just have itself enclosed
05:34and we created our first nav item.
05:35Beautiful. Let's go ahead and create our next nav item.
05:39Our next nav item doesn't need a class name
05:41'cause it has already a default of page scroll.
05:44Our link is portfolio
05:48and we have your Otto a name
05:50and that name will be portfolio fabulous.
05:55And let's do the next one. It's gonna be very similar.
05:58Let's duplicate it twice
06:02and we have your about in one,
06:07We have your contact in the other
06:13And we could delete all that and let's give it a run
06:16and see if everything is still working save
06:20and everything is still doing exactly the same thing,
06:22which is exactly what we would hope for it to do.
06:26Now in the next lecture we're gonna move on
06:28to another topic, which is really exciting,
06:29which is about event delegation in many ways.
06:32'cause in this case, I really don't want my nav item
06:36to be in charge of managing.
06:37What happens with links maybe more naturally is
06:40to have it bandage inside of our nav.
06:43Moreover than that, it probably may even make sense
06:45to move it all the way to the top, into our client
06:48or our app to decide
06:49what will happen whenever a click happens.
06:51'cause it affects all the other sections.
06:53As we're going to build a single page application
06:58in this lecture, we set up our navigation,
07:00our starting point to our navigation.
07:02In the next lecture we'll talk about
07:04how do we bubble events in React
07:06and where do we wanna monitor that state of change
07:09whenever the menu is being clicked on.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!