ReactJS-The Beginner Master Class
  • React In the Beginning
    • Lesson 1 - Demo: Build a Simple React App - Fast
    • Lesson 2 - HTML Review
    • Lesson 3 - CSS Review
    • Lesson 4 - Modern JavaScript (JSX) Patterns
    • Lesson 5 - Set up Dev Environment
    • Hands-on Practice
  • React Fundamentals
    • Lesson 1 - Understanding Old vs New Way of Building Web Apps - SPAs
    • Lesson 2 - Motivation for Using React as the Solution to Vanilla JS
    • Lesson 3 - What is ReactJS - How it Works
    • React Code Along from Scratch
    • Lesson 4 - Create and Run a React Project with Vite - Full Overview
    • Lesson 5 - Create Hook by State Management in React
    • Lesson 6 - React Project File and Folder Walkthrough
    • Lesson 7 - JSX and How React Treats the DOM & JSX Compilation(by Babeljs) - Overview
    • Lesson 8 - Understanding the Main Files - App.jsx and main.jsx
    • Lesson 9 - Props and One-Way Data Flow - Overview
    • Lesson 10 - Google Bookshelf App - Ver 1.0
    • Hands-on Practice I
    • Hands-on Practice II
  • React State and Styling
    • Lesson 1 - Pulling Book Data from a Different Data File
    • Lesson 2 - Overview of How State Works in React
    • Lesson 3 - RandomQuote App
    • Lesson 4 - Lifting State Up - React Pattern Overview
    • Hands-On - Simple Counter
  • Forms and Interactivity - Grocery List App
    • Lesson 1 - Setup a Simple Form and Input
    • Lesson 2 - Build Form Profile App Using Multi-input Form Data
    • Hands-on : Build a Grocery List App
  • Connecting to the Backend - Consuming APIs - UseEffect Hook
    • Lesson 1 - Connecting to the Back End - Understanding Side Effects, Hooks and useEffect - Overview
    • Lesson 2 - Fetching Data from the Backend the Right Way with useEffect Hook
    • Lesson 3 - Setting Up Loading State
    • Hands-on :Use Dependency Array and Adding Values that Control Side Effects
  • Solo Project 1
  • RESTful APIs :Build a BookSearch App -Ver 2.0
    • Lesson 1: Build and test RESTful APIs with Postman
    • Lesson 2 - BookShelf App Structure
    • Lesson 3 - Create NavBar.jsx Component
    • Lesson 4 - Create Footer Component
    • Lesson 5 - Create BookList.jsx Component
    • Lesson 6 - Create BookCard.jsx Component
    • Lesson 7 - Creating Custom Hooks - useBooks and api-client
    • Lesson 8 - Controlling Network Activities in React with AbortController
    • Lesson 9 - Show Book Details in a Modal - Working
    • Lesson 10 - Bookshelf App Summary
  • Multi-Page Applications (MPAs)
    • Build a Multi-Page React Application
    • Multi-Page React Application
    • Hands-on Practice
  • Backend Frameworks-NEXT.JS
    • Migrating from React to Next.js
    • Lesson 1: Key Concepts of NodeJS and Express for Backend Web Development
    • Lesson 2: How to set up a new Next.js project
    • Lesson 3: How to create Layouts and Pages
    • Hands-on Practice 1
    • Hands on Practice 2
      • New Project & Folder Structure
      • File-Based Routing
      • Server vs Client Components & Router Hooks
      • Start On The Navbar
      • Navbar Links, Dropdowns & React Icons
      • Active Links & Conditional Rendering
      • Homepage Components
      • Properties Page
      • Property Card Dynamic Data
      • Home Property Listings
      • Custom Not Found & Loading Pages
  • Git and GitHubs
    • Git Installation
    • Git Commands
    • GitHub Repository
    • Hands-on Practice
  • Database in Application
    • React Supabase CRUD
    • Hands-on: Note Taking App
  • NoSQL Database
    • Installing MongoDB Community Edition
    • System Env Path Setting
    • How to use MongoDB Shell
    • How to Connect and Use Mongosh in VS Code via MongoDB Extension
    • MongoDB Guide
  • Solo Project 2
  • Deployment and Web Hosting
    • Lesson 1. React+Vite+TS+Shadcn Project
    • Lesson 2. Deploying a React Vite App to Vercel from Vercel CLI
    • Lesson 3 Connecting to GitHub Repo and Automate Deployment
  • Solo Project 3
  • Final Term Project
    • Level 1 Team Project
    • Level 1 Team Project
    • Level 1 Team Project
    • Level 1 Team Project
    • Level 2 Team Project
    • Level 2 Team Project
    • Level 3 Team Project
    • Level 3 Team Project
Powered by GitBook
On this page
  • Navbar Building
  • 1. Setting Up the Components Folder
  • 2. Building the Navbar Component
  • 3. Converting HTML to Valid JSX
  • 4. Integrating the Navbar into Your Layout
  • 5. Handling Images and Assets
  • 6. Testing and Debugging
  • 7. Recap and Best Practices
  1. Backend Frameworks-NEXT.JS
  2. Hands on Practice 2

Start On The Navbar

PreviousServer vs Client Components & Router HooksNextNavbar Links, Dropdowns & React Icons

Last updated 2 months ago

Navbar Building

Code base for Navbar component Creation

// nav element
└── body
            ├── nav (class="bg-blue-700 border-b border-blue-500")
            │   ├── div (container: mx-auto max-w-7xl px-2 sm:px-6 lg:px-8)
            │   │   └── div (flex container: relative flex h-20 items-center justify-between)
            │   │       ├── div (mobile menu button container: absolute inset-y-0 left-0 flex items-center md:hidden)
            │   │       │   └── button (#mobile-dropdown-button)
            │   │       │       ├── span (overlay)
            │   │       │       ├── span (sr-only, "Open main menu")
            │   │       │       └── svg (menu icon)
            │   │       ├── div (logo & desktop menu container: flex flex-1 items-center justify-center md:items-stretch md:justify-start)
            │   │       │   ├── a (logo link)
            │   │       │   │   ├── img (logo image)
            │   │       │   │   └── span (logo text: "PropertyPulse")
            │   │       │   └── div (desktop menu: hidden md:ml-6 md:block)
            │   │       │       └── div (menu links container: flex space-x-2)
            │   │       │           ├── a ("Home")
            │   │       │           ├── a ("Properties")
            │   │       │           └── a ("Add Property")
            │   │       ├── div (right side menu for logged-out users: hidden md:block md:ml-6)
            │   │       │   └── div (flex items-center)
            │   │       │       └── button ("Login or Register")
            │   │       └── div (right side menu for logged-in users: absolute inset-y-0 right-0 flex items-center pr-2 md:static md:inset-auto md:ml-6 md:pr-0)
            │   │           ├── a (messages link with notification)
            │   │           │   ├── button (notification icon)
            │   │           │   └── span (notification count, "2")
            │   │           └── div (profile dropdown container: relative ml-3)
            │   │               ├── div
            │   │               │   └── button (#user-menu-button with profile image)
            │   │               └── div (dropdown menu: hidden absolute right-0, etc.)
            │   │                   ├── a ("Your Profile")
            │   │                   ├── a ("Saved Properties")
            │   │                   └── a ("Sign Out")
            │   └── div (mobile menu: hidden, id="mobile-menu")
            │       └── div (mobile menu links container: space-y-1 px-2 pb-3 pt-2)
            │           ├── a ("Home")
            │           ├── a ("Properties")
            │           ├── a ("Add Property")
            │           └── button ("Login or Register")

1. Setting Up the Components Folder

  1. Create a components/ Folder:

    • In the root of your project (outside the app/ directory), create a new folder named components.

    • This folder will store all non-page components, keeping your app directory reserved for page components.

  2. Create the Navbar File:

    • Inside the components/ folder, create a new file called Navbar.jsx.


2. Building the Navbar Component

a. Define the Component Structure

  1. Start with the Boilerplate Code:

    • Open Navbar.jsx and add the following code structure. Since the Navbar will eventually include interactive elements (like dropdowns and toggles), you can mark it as a client component by adding the "use client" directive at the top:

    "use client";
    
    export default function Navbar() {
      return (
        <nav>
          {/* Navbar content will go here */}
        </nav>
      );
    }

b. Copy and Paste the HTML Markup

  1. Extract the HTML:

    • Open your provided index.html file and locate the <nav> element (from the opening <nav> tag to its closing tag).

    • Copy the complete markup for the navigation bar.

  2. Paste into the Component:

    • Replace the placeholder comment inside <nav> with the copied HTML.

    • Your component will now look similar to:

    "use client";
    
    export default function Navbar() {
      return (
        <nav className="bg-blue-700 border-b border-blue-500">
          {/* Paste the complete copied markup from index.html here */}
        </nav>
      );
    }

3. Converting HTML to Valid JSX

After pasting, you’ll need to update the code to meet JSX requirements:

a. Update Attribute Names

  • Replace class with className: Use your editor’s multi-cursor feature (e.g., Command/Control + Shift + L) to change all instances.

  • Convert HTML attributes to camelCase: For example, change:

    • tabindex to tabIndex

    • stroke-width to strokeWidth

    • stroke-linecap to strokeLinecap

    • stroke-linejoin to strokeLinejoin

b. Commenting in JSX

  • Convert HTML Comments: HTML comments (<!-- comment -->) are not allowed in JSX. Replace them with JSX comments:

    // jsx comments
    {/* This is a comment */}

c. Verify and Save

  • Save the file and check the browser’s console for any warnings or errors related to JSX syntax.

  • Fix any issues (e.g., missing closing tags, incorrect attribute names).


4. Integrating the Navbar into Your Layout

a. Update the App Layout

  1. Locate Your Layout File:

    • In the app/ folder, open your main layout file (typically layout.jsx or layout.js).

  2. Import the Navbar:

    • At the top of your layout file, import the Navbar component:

    import Navbar from '@/components/Navbar';
  3. Insert the Navbar into the Layout:

    • Place the <Navbar /> component above the <main> tag (or wherever appropriate) so that it appears on every page:

    //app/layout.jsx
    import Navbar from '@/components/Navbar';
    import '@/app/globals.css'
    
    export default function RootLayout({ children }) {
      return (
        <html lang="en">
          <body>
            <Navbar />
            <main>{children}</main>
          </body>
        </html>
      );
    }

5. Handling Images and Assets

a. Prepare Your Image Assets

  1. Move Images to the Public Folder:

    • Copy the property images (if needed) into the public/images folder so they’re directly accessible.

  2. Create an Assets Folder for Logos/Icons:

    • For static assets like the logo or profile images, create an assets/images folder in the root of your project.

b. Use Next.js Image Component

  1. Import the Image Component:

    • In your Navbar, import Next.js’ Image component:

    import Image from 'next/image';
  2. Import Specific Images:

    • For example, if you have a logo:

    import logo from '@/assets/images/logo-white.png';
    import profileDefault from '@/assets/images/profile.png';
  3. Replace <img> Tags:

    • Locate the <img> tags in your Navbar code and replace them with the <Image /> component, updating the src property to the imported images:

    <Image src={logo} alt="PropertyPulse" className="h-10 w-auto" />

6. Testing and Debugging

  1. Run Your Development Server:

    • In your terminal, execute:

      npm run dev
    • Open http://localhost:3000 to view your site.

  1. Verify the Navbar:

    • Ensure the navigation bar appears correctly on every page.

    • Check the browser console for any JSX or image-related errors, and adjust as necessary.

  2. Interactive Elements:

    • In future steps, you can add interactivity (e.g., dropdowns, toggles) by utilizing client-side state and hooks in this component.


7. Recap and Best Practices

  • Component Isolation: Building the Navbar as a separate component promotes reusability and maintainability.

  • JSX Conversion: Always update HTML attributes and comments when converting HTML to JSX.

  • Asset Management: Utilize the Next.js Image component for optimized image handling, and organize your assets in dedicated folders (public for publicly served files, assets for imported assets).

25KB
index.html
index.html