In Next.js, page.js is a file commonly used to define individual pages of your application. Each page is a React component that represents a route in your application. The page.js file is crucial for routing and rendering content in your Next.js project.In this article, we will see about the pages and how we can dynamically manipulate them.
Prerequisites:
Page.js File In Next.js
The page.js file in Next.js is used to create and manage different pages within your application. Each file in the pages directory corresponds to a route, and page.js files are key to defining what gets rendered for each route.
Pages are associated with the routes based on their file name. For example, in development:
- pages/index.js is associated with the / route.
- pages/posts/first-post.js is associated with the /posts/first-post route.
To create the pages in Next.js, we are introduced to the "page.js" file by the new app router in Next.js. Basically, while using the app router, whenever you want to create a new page, you can create a folder and inside that folder, create a file named "page.js" which exports your page and Next.js will render that page whenever somebody hits that route. The page inside the app folder serves as the homepage of the application.
Syntax:
For static Page:
To create a new page, create a folder (which will tell the route for which the page would be rendered) and inside that folder export a function rendering your component as shown below:
export default function YourPage(){
return (
// your code...
)
}
Whenever user hits the route of the folder, the user is presented with the UI in the "page.js".
For Dynamic Pages:
To create a dynamic page, you can create a folder with square brackets (for example: [user_id]) and then create a page.js file inside that folder. Then you can get the user_id from the props inside the page component as shown below:
export default function Page({params}){
const user_id = params.user_id
return // your component0
}
For page with search parameter:
To get the search parmas (/?name=123&age=5) from the user, you can also add another prop, in your component, named "searchParams" which would return a javascript object containing all the search params for that page.
export default function Page({searchParams}){
const name = searchParams.name
return // your component0
}
Steps to create the project
Step 1: Open your terminal and type the following command:
npx create-next-app@latest
Step 2: Enter the details about your project and continue further.
Enter the details to create your next appStep 3: Get inside the folder and run the npm script to start your application.
npm run dev
Folder structure:
folder structure of our applicationThe updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "14.1.0"
},
"devDependencies": {
"autoprefixer": "^10.0.1",
"postcss": "^8",
"tailwindcss": "^3.3.0",
"eslint": "^8",
"eslint-config-next": "14.1.0"
}
Example 1: Let us create an about page for our application by creating a folder named "about" in our app directory and then exporting our component inside "page.js"
JavaScript
// inside src/app/about/page.js
export default function AboutPage() {
return (
<div className="p-5">
<h1 className="text-2xl font-bold text-green-500">
GeeksforGeeks
</h1>
<h3 className="text-lg font-bold">
page.js Next.js
</h3>
<p>This is our About Page</p>
</div>
);
}
Output: Now, when you go to the route "localhost:3000/about", you can see the about page as shown below:
Output of example 1 of page.js Next.jsExample 2: In this example, let's create a dynamic page using the square brackets and name the folder as "[id]" and then print this id entered by the user in our page. For this, create this folder inside the "app" directory and then export the below component inside its "page.js" file.
JavaScript
// inside src/app/[id]/page.js
export default function IdPage({ params }) {
return (
<div className="p-5">
<h1 className="text-2xl font-bold text-green-500">
GeeksforGeeks
</h1>
<h3 className="text-lg font-bold">
page.js Next.js
</h3>
<p>The id entered by user is: {params.id}</p>
</div>
);
}
Output: So, now if go to the route "localhost:3000/13", it will return "13" in "params.id" and will be shown on our page as shown in the below output.
Output of example 2 of page.js Nextj.jsExample 3: In this example, let us create a profile page by adding a new folder in our app directory named "profile" and inside it's "page.js" show the user name and user age retrieved through the search params.
JavaScript
// inside src/app/profile/page.js
export default function ProfilePage({ searchParams }) {
return (
<div className="p-5">
<h1 className="text-2xl font-bold text-green-500">
GeeksforGeeks
</h1>
<h3 className="text-lg font-bold">
page.js Next.js
</h3>
<p>This is our Profile Page</p>
<p className="mt-5">
user name:
<span className="font-bold">
{searchParams.name}
</span>
</p>
<p>
user age:
<span className="font-bold">
{searchParams.age}
</span>
</p>
</div>
);
}
Output: Now if we go to the route "localhost:3000/profile?name=Vikas&age=19", we can see the following output.
Output of example 3 of page.js Next.jsSummary
Next.js, page.js
files define pages and routes in your application. Each file exports a React component that represents the content for a specific route. You can handle dynamic routes, static generation, server-side rendering, and API routes using the appropriate Next.js methods and conventions.
Similar Reads
template.js in Next JS
In Next.js, the template.js file can serve various purposes depending on the context and project requirements. It can be used to create reusable templates for components, pages, or even configuration settings. Utilizing a template.js file helps to maintain a consistent structure and reduce repetitiv
4 min read
Next.js Pages
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 wil
3 min read
SEO in Next JS
Search Engine Optimization (SEO) is crucial for enhancing the visibility and ranking of your website in search engine results. Next.js provides robust features and tools that make optimizing your site for SEO easier and more effective.In this article, we'll explore how to use Next.js for SEO to opti
5 min read
Next.js File Conventions: page.js
Next.js File Conventions are practices and rules for organizing files in a Next.js application to manage routing and components efficiently. In these conventions, the page.js file represents a specific route in the application, where each file in the pages or app directory corresponds to a route. Th
4 min read
MDX in Next JS
MDXÂ is a lightweight markup language used to format text. It allows you to write using plain text syntax and convert it to structurally valid HTML. It's commonly used for writing content on websites and blogs. In this article we will see more about MDX in Next JS What is MDX?MDX stands for Multidime
4 min read
loading.js in Next JS
Next JS is a React framework that provides a number of features to help you build fast and scalable web applications. One of these features is loading.js which allows you to create a loading UI for your application. Prerequisites:JavaScript/TypeScriptReactJS BasicsNextJSLoading UI is important becau
3 min read
Next.js Custom Error Page
Creating a custom error page in Next.js allows you to provide a better user experience when an error occurs, such as a 404 (Not Found) or a 500 (Server Error). Custom error pages can match your site's design and offer helpful information or navigation options. In this article, we will learn how to c
8 min read
Why Next.js is Popular?
Next.js is a robust React framework designed to enhance web development with capabilities like server-side rendering (SSR), static site generation (SSG), and incremental static regeneration (ISR). Its comprehensive feature set simplifies the development process, improves performance, and boosts SEO,
4 min read
Linking between pages in Next.js
In this article, we are going to see how we can link one page to another in Next.js. Follow the below steps to set up the linking between pages in the Next.js application: To create a new NextJs App run the below command in your terminal: npx create-next-app GFGAfter creating your project folder (i.
3 min read
Next.js Functions: usePathname
Next.js is a JavaScript framework library used to build fast and scalable web applications with server-side rendering and static site generation capabilities. One of the useful hooks provided by Next.js is usePathname, which allows developers to easily access and manipulate the current pathname of t
3 min read