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
  • Creating Layouts and Pages
  • Prerequisites:
  • Step-by-Step Procedure:
  1. Backend Frameworks-NEXT.JS

Lesson 3: How to create Layouts and Pages

PreviousLesson 2: How to set up a new Next.js projectNextHands-on Practice 1

Last updated 3 months ago

Creating Layouts and Pages

Prerequisites:

  • Next.js Project: You should have a Next.js project set up. If you haven't already, follow the steps I provided in Lesson 2 to create a new Next.js project using create-next-app.

Step-by-Step Procedure:

1. Creating Pages:

  • Next.js uses a file-system routing system. Pages are created by adding files to the app directory.

  • Basic Page Creation:

    • To create a new page, create a new .js, .jsx, .ts, or .tsx file inside the app directory.

    • For example, to create an "about" page, create a file named app/about/page.jsx (or .tsx if you're using TypeScript).

  • Page Content:

    • Inside the page file, you need to export a React component (usually a functional component) as the default export.

    • Here's an example:

      JavaScript

      // app/about/page.jsx
      import React from 'react';
      
      function AboutPage() {
        return (
          <div>
            <h1>About Us</h1>
            <p>This is the about page.</p>
          </div>
        );
      }
      
      export default AboutPage;
  • Accessing Pages:

    • You can access the page in your browser by navigating to the corresponding URL path. For example, the "about" page would be accessible at http://localhost:3000/about.

2. Creating Layouts:

  • Layouts are used to share UI components between pages. They wrap your pages and can contain things like headers, footers, navigation menus, etc.

  • Layout Creation:

    • To create a layout, create a file named layout.jsx (or .tsx) inside a directory within the app directory.

    • Layouts apply to all pages within the directory and its subdirectories.

    • For example, to create a layout for a dashboard section, you might create a file at app/dashboard/layout.jsx.

  • Layout Content:

    • Inside the layout file, you need to export a React component as the default export.

    • Layout components receive a children prop, which represents the page or nested layout being rendered within it.

    • Here's an example:

      JavaScript

      // app/layout.jsx (Root layout)
      import React from 'react';
      import './globals.css'; // Import global styles
      
      function RootLayout({ children }) {
        return (
          <html lang="en">
            <head>
              <title>My App</title>
            </head>
            <body>
              <nav>
                {/* Navigation menu */}
                <a href="/">Home</a> | <a href="/about">About</a>
              </nav>
              <main>{children}</main> {/* Render the page content */}
              <footer>
                {/* Footer */}
                <p>&copy; 2025 My App</p>
              </footer>
            </body>
          </html>
        );
      }
      
      export default RootLayout;

      JavaScript

      // app/dashboard/layout.jsx (Dashboard layout)
      import React from 'react';
      
      function DashboardLayout({ children }) {
        return (
          <div>
            <aside>
              {/* Dashboard sidebar */}
              <a href="/dashboard">Dashboard Home</a> | <a href="/dashboard/settings">Settings</a>
            </aside>
            <main>{children}</main> {/* Render the page content */}
          </div>
        );
      }
      
      export default DashboardLayout;
  • Root Layout (app/layout.js):

    • Every Next.js app must have a root layout defined at the top level of the app directory (app/layout.js or app/layout.tsx).

    • This layout typically includes the <html>, <head>, and <body> tags.

    • It's where you should include global styles, metadata, and other elements that are consistent across your entire application.

3. Linking Between Pages:

  • Next.js provides the <Link> component from next/link for client-side navigation between pages.

  • Using <Link> is important for performance and accessibility. It prevents full page reloads and provides a smoother user experience.

  • Example:

    JavaScript

    import React from 'react';
    import Link from 'next/link';
    
    function HomePage() {
      return (
        <div>
          <h1>Welcome to My Next.js App!</h1>
          <p>
            Go to the <Link href="/about">About Page</Link>
          </p>
          <p>
            Go to the <Link href="/dashboard">Dashboard</Link>
          </p>
        </div>
      );
    }
    
    export default HomePage;

Sample Example: Nested Layouts and Linking Pages

Let's create a scenario with nested layouts and linking:

  • Project Structure:

    app/
    ├── layout.js       (Root layout)
    ├── page.js         (Home page)
    ├── about/
    │   └── page.js     (About page)
    └── dashboard/
        ├── layout.js   (Dashboard layout)
        ├── page.js     (Dashboard home page)
        └── settings/
            └── page.js (Dashboard settings page)
  • Code:

    JavaScript

    // app/layout.jsx (Root layout)
    import React from 'react';
    import Link from 'next/link';
    import '../styles/globals.css'; // You might have global styles
    
    function RootLayout({ children }) {
      return (
        <html lang="en">
          <head>
            <title>My App</title>
          </head>
          <body>
            <nav>
              <Link href="/">Home</Link> | <Link href="/about">About</Link> | <Link href="/dashboard">Dashboard</Link>
            </nav>
            <main>{children}</main>
            <footer>
              <p>&copy; 2024 My App</p>
            </footer>
          </body>
        </html>
      );
    }
    
    export default RootLayout;

    JavaScript

    // app/page.jsx (Home page)
    import React from 'react';
    import Link from 'next/link';
    
    function HomePage() {
      return (
        <div>
          <h1>Welcome Home!</h1>
          <p>This is the home page.</p>
            Go to the <Link href="/about">About Page</Link>
          </p>
          <p>
            Go to the <Link href="/dashboard">Dashboard</Link>
          </p>
          
          
        </div>
      );
    }
    
    export default HomePage;

    JavaScript

    // app/about/page.jsx (About page)
    import React from 'react';
    
    function AboutPage() {
      return (
        <div>
          <h1>About Us</h1>
          <p>This is the about page.</p>
        </div>
      );
    }
    
    export default AboutPage;

    JavaScript

    // app/dashboard/layout.jsx (Dashboard layout)
    import React from 'react';
    import Link from 'next/link';
    
    function DashboardLayout({ children }) {
      return (
        <div>
          <aside>
            <h3>Dashboard Navigation</h3>
            <Link href="/dashboard">Dashboard Home</Link> | <Link href="/dashboard/settings">Settings</Link>
          </aside>
          <main>{children}</main>
        </div>
      );
    }
    
    export default DashboardLayout;

    JavaScript

    // app/dashboard/page.jsx (Dashboard home page)
    import React from 'react';
    
    function DashboardHomePage() {
      return (
        <div>
          <h1>Dashboard Home</h1>
          <p>Welcome to your dashboard!</p>
        </div>
      );
    }
    
    export default DashboardHomePage;

    JavaScript

    // app/dashboard/settings/page.jsx (Dashboard settings page)
    import React from 'react';
    
    function DashboardSettingsPage() {
      return (
        <div>
          <h1>Dashboard Settings</h1>
          <p>Manage your dashboard settings here.</p>
        </div>
      );
    }
    
    export default DashboardSettingsPage;
  • Testing:

    1. Run the development server: npm run dev, yarn dev, or pnpm dev.

    2. Navigate to http://localhost:3000: You should see the Home page with links.

    3. Click the "About Page" link: You should see the About page content, and the navigation and footer from the root layout should be present.

    4. Click the "Dashboard" link: You should see the Dashboard Home page, and both the root layout's navigation/footer and the dashboard layout's sidebar should be present.

    5. Click the "Settings" link in the dashboard sidebar: You should see the Dashboard Settings page, with both layouts still applied.

Explanation of the Example:

  • Root Layout: The app/layout.js provides the basic HTML structure, navigation, and footer for the entire application.

  • Dashboard Layout: The app/dashboard/layout.js provides a specific layout for the dashboard section, including a sidebar navigation.

  • Nested Layouts: The Dashboard pages (/dashboard and /dashboard/settings) are wrapped by both the root layout and the dashboard layout, demonstrating nested layouts.

  • Linking: The <Link> component is used to navigate between pages, providing client-side routing.

By following these steps and the example, you can create and structure layouts and pages in your Next.js application effectively. Remember to organize your files within the app directory to leverage Next.js's file-system routing.