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
  • New Project & Folder Structure
  • 1. Setting Up the Next.js Application
  • 2. Running and Opening Your Application
  • 3. Exploring the Optimal Folder Structure
  • 4. Summary
  1. Backend Frameworks-NEXT.JS
  2. Hands on Practice 2

New Project & Folder Structure

New Project & Folder Structure

1. Setting Up the Next.js Application

a. Prerequisites

  • Node.js (18+ above) must be installed on your system.

  • A terminal or command prompt is required.

  • (Optional) An IDE like Visual Studio Code for editing your code.

b. Create the App with Create Next App

  1. Open your terminal and navigate to the working folder where you want your project. For example:

    cd ~/Your-working-directory
  2. Run the create-next-app command using NPX:

    npx create-next-app@latest next-property-shop-app
  3. Explain the interactive prompts:

    • Project Name: It should be next-property-shop-app (the folder will be created with this name).

    • TypeScript: You can choose No if you prefer JavaScript (as done in the sample).

    • ESLint: Choose No to keep default settings (unless you want custom linting).

    • Tailwind CSS: Choose Yes if you want to include Tailwind for styling.

    • Use Source Directory: Choose No (this means your code will live in the default structure; using a src folder is optional).

    • Use App Router: Choose Yes to use the modern file-based routing provided in Next.js 13+.

    • Customize the Alias: Choose No for default settings.

  4. Wait for the dependencies to install. The tool will install core dependencies like react, react-dom, next, and, if opted, Tailwind CSS.


2. Running and Opening Your Application

  1. Navigate into your new project directory:

    cd property-shop-next-app
  2. Open the project in your code editor. For example, if using Visual Studio Code:

    code .
  3. Start the development server:

    npm run dev
  4. View your app: Open your browser and go to http://localhost:3000 to see your running Next.js application.


3. Exploring the Optimal Folder Structure

Next.js (especially with the new App Router) uses a file-based routing system that makes organizing your code straightforward. Here’s an overview of the recommended folder structure:

a. Core Directories and Files

  • app/ Directory:

    • Purpose: Contains all route-specific code and layouts.

    • Files:

      • layout.js (or layout.tsx): Wraps all your pages. Use this file to add common elements such as headers, footers, or global styles.

      • page.js (or page.tsx): Represents your home page.

      • Additional folders: Create folders inside app/ to represent additional routes (e.g., /about/page.js for the About page).

b. Additional Recommended Folders for Scalability

  • components/

    • Purpose: Contains reusable UI components (e.g., navigation bars, buttons, cards).

    • Benefit: Promotes code reusability and separation of concerns.

  • styles/

    • Purpose: Houses global CSS files or Tailwind configuration files.

    • Benefit: Centralizes style definitions making it easier to update and maintain design consistency.

  • public/

    • Purpose: Stores static assets like images, fonts, and icons.

    • Benefit: These files are served directly, keeping your source code clean.

  • lib/ or utils/

    • Purpose: Contains utility functions, helper modules, or API-related code.

    • Benefit: Keeps business logic and reusable functions separate from UI components.

  • hooks/ (if needed)

    • Purpose: For custom React hooks that manage stateful logic.

    • Benefit: Encapsulates complex logic into easy-to-use hooks.

c. Hands-On Exercise: Rebuilding the App Folder

The document suggests deleting the auto-generated app folder to rebuild it from scratch. This is an excellent exercise to understand:

  • File-Based Routing: How each file corresponds to a route.

  • Layout and Page Separation: How the layout wraps each page.

  • Modularity: How to keep your project organized as it grows.

Steps for the exercise:

  1. Delete the existing app/ folder from your project.

  2. Create a new app/ folder manually.

  3. Recreate the essential files:

    • layout.jsx: Create a simple layout that includes basic HTML structure (e.g., <html>, <body>, header/footer).

// app/layout.jsx

function RootLayout({ children }) {
  return (
    <html lang="en">
      <body
        className={`${geistSans.variable} ${geistMono.variable} antialiased`}
      >
        RootLayout : {children}
      </body>
    </html>
  );
}
export default RootLayout
 
  • page.jsx: Build a basic home page.

// app/page.jsx - children

import Image from "next/image";

export default function Home() {
  return (
(omitted)
);
}
  1. NEXT.JS 14 : First create assets folder at root and then create styles flder inside assets folder and add globals.css in styles folder.

  2. include tailwind libraries in globals.css file as bellow:

// assets/styles/globls.css - NEXT.js 14
@tailwind base;
@tailwind components;
@tailwind utilities;
  1. import globals.css file in app/layout.jsx as

// app/layout.jsx
import '@/assets/styles/globals.css';
(omitted)
  1. Test your app by running npm run dev and verifying that the new structure is working.

This manual process reinforces understanding of how Next.js handles routing and layouts, which is key to building scalable and maintainable applications

4. Summary

  • Create the App: Use NPX to run create-next-app@latest property-shop-next-app and follow the prompts.

  • Run the Dev Server: Navigate into your project, open it in your editor, and run npm run dev.

  • Understand the Folder Structure: Focus on the app/ directory for routing and layout, and add additional folders like components/, styles/, public/, lib/, and hooks/ to organize your code.

  • Hands-On Practice: Rebuild the app/ folder manually to better understand the routing and file structure.

By following these steps and exploring the structure, you'll set a strong foundation for building scalable and maintainable Next.js applications.

PreviousHands on Practice 2NextFile-Based Routing

Last updated 2 months ago