How to Use the App Directory in Next.js?

Next.js is a popular react framework. It has introduced the App directory designed to refine the project organization by categorizing routes and components logically. This enhancement increases the developer’s workflow by simplifying the file management within the projects.

Steps to initialize a Next.js project

Step 1: Create a Next.js project using the following commands:

npx create-next-app appdirectory

Step 2: Select the options below while creating the app using the above command.

select above options using left right arrows and press enter.

Step 3: Change to that directory or just open the folder through file option in VS code.

cd appdirectory

Step 4: Create the folder structure and modify the json.config to handle routing.

appdirectory structure

Step 5: Configuring the json.config to handle imports in application.

// json.config
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@/*": ["*"],
"@/components/*": ["./components/*"]
//accordingly add all other routes to be used out there.
}
}
}

Step 6: To run the project follow the below command.

npm run dev

Understanding the App Directory

The App Directory is a new structural paradigm introduced in Next.js to enhance the developer experience. It provides a clear and logical organization for your project’s files, making it easier to manage large codebases.

Key Components of the App Directory

All the folders are optional to make the application more manageable and understandable, we use this structured format. In our project structure we have included api, component, styles and auth folders. Basically most used folders to structure the application is as follows:

  • api/: This directory is reserved for your server-side API routes.
  • components/: You will store all your reusable React components.
  • pages/: This is where your application’s pages and routing logic reside.
  • styles/: Use this directory for your global and component-specific stylesheets.
  • utils/: A place for utility functions and shared logic ( optional ).

But, before using these structured format, make sure to configure the routes as shown in installation step to handle routing in importing application components in each other.

Building an Application Using the App Directory

Step 1: Crafting the Home Page Inside the app directory, create an page.js file with the following content:

Step 2: Incorporating a Header Component In the app/components, create a header.js file. Then, update your app/page.js to include the Header component.

Step 3: Adding style to page.js and header.js as coded below and also shown in folder structure.

Step 4: Setting up API Routes in the app/api directory, add a hello.js file.

Example: This example shows the use or app directory.

CSS
/*page.module.css*/

.main {
    background-color: rgb(255, 255, 255);
    height: 100vh;
    color: "black";
}
CSS
/*  app/styles/header.module.css  */

.header {
    color: green;
    background-color: black;
}
JavaScript
"use client";

//app/page.js

import Header from "./components/header";
import style from "./page.module.css";

function Page() {
   return (
      <div className={style.main}>
         <Header />
         <h3>Hi Buddy! Have a Nice day :-)</h3>
         <h2>Above, header component is imported.</h2>
     </div>
   );
}

export default Page;
JavaScript
// app/components/header.jsx

import React from "react";
import style from "../styles/header.module.css";
function Header() {
   return (
     <div className={style.header}>
        <h2>GFG Header</h2>
     </div>
   );
}

export default Header;
JavaScript
//app/api/hello.js

function handler(req, res) {
  res.status(200).json({ message: "Welcome to the Next.js API" });
}

export default handler;

Output:

output of imported component

Best Practices When Using the App Directory

  • Modularity: Keep related files together for easier maintenance.
  • Consistency: Use consistent naming conventions for better readability.
  • Reusability: Store common components in the components directory.
  • Organization: Keep styles organized in the styles directory.
  • Security: Use environment variables for sensitive data.
  • Testing: Write tests for components and API routes to ensure reliability.

Conclusion

The App Directory in Next.js is a essential thing for developers who wants a more organized and efficient workflow. By following the steps and best practices given in this guide, you can take full advantage of this powerful feature and elevate your Next.js applications.



Contact Us