# The page.js Convention

> The mandatory file that brings a route to life.

Subject: Next.js | Lesson 5 of 65 | 3 min read
Interactive lesson: https://codewithanimation.in/nextjs/page-file
Previous: [Server Component Payloads & Serialization](https://codewithanimation.in/nextjs/rsc-payloads.md) | Next: [The layout.js Convention](https://codewithanimation.in/nextjs/layout-file.md)

## The Leaf of the Route

In the App Router, folders define the paths of your URLs, but folders alone do not create web pages. A route is only publicly accessible if it contains a `page.js` (or `.tsx`) file.

The `page.js` file is the 'leaf' of your routing tree. It defines the unique UI that belongs exclusively to that specific URL path.

## Separation of concerns

By separating the unique UI (`page.js`) from the shared UI (`layout.js`), Next.js achieves incredibly efficient rendering.

When a user navigates between pages, Next.js knows that it only needs to swap out the `page.js` content. The surrounding layouts remain completely untouched, preventing unnecessary re-renders and maintaining state (like scroll position or form inputs) in the layout.

## Exporting a component

To create a page, you simply create a file exactly named `page.tsx` inside a folder, and export a default React component.

By default, this component is a React Server Component. This is a massive shift from traditional React: your `page.tsx` runs on the server, meaning it has direct access to your backend infrastructure.

## Common mistake: The async/useState trap

Because `page.tsx` is a Server Component, developers often trip up when they try to make the page interactive.

If you try to use `useState` or `onClick` inside a standard `page.tsx`, the app will crash because those are client-side features. To fix this, you must either move the interactivity to a smaller child component (recommended) or add `'use client'` to the top of the page.

## Tip: Async pages

One of the best features of `page.tsx` being a Server Component is that it can be an `async` function.

You can write `export default async function Page() { const data = await db.query(); ... }`. There is no need for `useEffect` or `getServerSideProps`. You can fetch data exactly where you render the UI.

## Try it: The Leaf Node

A conceptual view of how `page.js` slots into the routing tree.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/page-file

## Check yourself

### Question 1

What is the specific role of the page.js file in Next.js?

- A. It defines the shared layout for a folder
- B. It makes a route publicly accessible and defines its unique UI
- C. It configures the URL path string
- D. It is used exclusively for API routes

Correct answer: B

A folder defines a route path, but the route is not publicly accessible to users until a `page.js` file is placed inside it to provide the UI.

- Why not A: That is the job of layout.js.
- Why not C: The folder name configures the URL path, not the page file.
- Why not D: API routes use the route.js convention.

### Question 2

Which of the following is a valid way to fetch data in a Next.js App Router page?

- A. export default async function Page() { ... }
- B. export function getServerSideProps() { ... }
- C. You must always use useEffect to fetch data
- D. Pages cannot fetch data directly

Correct answer: A

By default, `page.js` is a Server Component. This means you can simply make the component `async` and `await` your database or API calls directly in the component body.

- Why not B: This was the data fetching method in the older Pages Router.
- Why not C: Server Components can fetch data directly without useEffect.
- Why not D: Because pages are Server Components by default, they are excellent places to fetch data.

## Remember this

- A route segment requires a `page.js` file to be publicly accessible.
- The `page.js` file defines the unique UI for that specific path.
- By default, `page.js` is a React Server Component.
- Because it is a Server Component, a page can be an `async` function that fetches data directly.
