Open In App

Next.js Pages

Last Updated : 29 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Next.js Pages are the components used to define routes in the next application. Next.js uses a file-based routing system that automatically maps files in the pages directory to application routes, supporting static, dynamic, and nested routes for seamless web development. In this article, we will learn to create different types of routes with the help of next.js pages.

Next.js Pages

Each file with the js, ts, jsx, tsx extensions automatically renders as a route in Next.js. We can create multiple types of routes with the help of pages in Next.js.

  • Static Pages: the code files inside /pages directory having some name and the above mentioned types create static pages.
  • Dynamic Pages: The files with names in single square brackets create dynamic routes in Next.js application e.g., /pages/users/[user].js
  • API Routes: Next.js allows us to create API endpoints in the project within the pages/api directory. It is directly mapped to /api/... route and is treated as api.

Steps to Create NextJS Application

You can create a new NextJS project using the below command:

npx create-next-app gfg

After creating your project folder (i.e. gfg), move to it by using the following command.

cd gfg

Project Structure:

It will look like this.

Creating a Static Page

We can create static pages without using router in next.js

Approach:

To create a static page in Next.js, add a file in the pages directory, export a React component, and optionally use getStaticProps for pre-fetching data at build time. Run npm run build and npm start to serve the static page.

Example: This example demonstrate creating static page on /gfg route.

JavaScript
// pages/gfg.js

export default function GfgPage() {
    return <div>Welcome to the GFG Page!</div>;
}
JavaScript
// pages/index.js

export default function HomePage() {
  return <div>Welcome to the Home Page!</div>;
}

Now we can easily access both pages in our app by running the app in the browser.

Step to Run Application: Run the application using the following command from the root directory of the project.

npm run dev

Output:

Creating Dynamic Pages

For this, we are going to create a new folder with the name route and inside this folder, we are going to create our dynamic file with the name [gfg].js.

We have 2 approaches for dynamic pages in next js mentioned below:

  • with useRouter
  • with usePathname

using useRouter from next/router:

create a dynamic page in Next.js, use useRouter from next/router to display the current route dynamically.

JavaScript
//filename - pages/route/[gfg].js


import React from 'react'
import {useRouter} from 'next/router'; 

export default function getRoute() { 
    // Calling useRouter() hook 
    const router = useRouter() 
    return ( 
        <div> 
            <h1>GeeksforGeeks</h1> 
            <h2>pathname:- {router.asPath}</h2> 
        </div> 
    ) 
}

using usePathname from next/navigation:

Create a dynamic page in Next.js, use usePathname from next/navigation to display the current route dynamically.

JavaScript
// pages/route/[gfg].js
import { usePathname } from "next/navigation";

export default function DynamicPage() {
  const pathname = usePathname();
  return <div>Current route: {pathname}</div>;
}

Step to Run Application: Run the application using the following command from the root directory of the project.

npm run dev

Output:

Conclusion

Next.js pages provide a simple and powerful way to define the routes and structure of your application. With file-based routing, dynamic routes, and support for both SSR and SSG, Next.js offers a flexible and efficient way to build modern web applications. By following best practices and leveraging Next.js features, you can create robust, scalable, and high-performance web applications.


Next Article
Article Tags :

Similar Reads