Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Next.js Pages and Routing

Next.js Pages and Routing

Next.js uses file-system routing, meaning the pages in your application are automatically determined by the file system structure; each file and folder corresponds to a route, without needing to explicitly configure each path as in traditional React routing, which makes the development process more intuitive and simple.

Next.js's routing system is very flexible, supporting static pages, dynamic routes, and nested routes, enabling developers to easily implement complex application structures.

Next.js 13 introduced the new app directory, which coexists with the traditional pages directory and provides more flexible routing and layout organization.

Assume the project directory structure is as follows:

app/

├── page.tsx             # 对应根路径 /

├── about/

│   └── page.tsx         # 对应 /about

├── [slug]/

│   └── page.tsx         # 对应动态路径,如 /[slug]

Default Pages and Routes

Next.js automatically creates routes for files in your app/ or pages/ folder.

The following are some common routing examples:

Dynamic Route

Dynamic routing allows you to dynamically render pages based on different parts of the URL.

In Next.js, you can implement dynamic routing by creating folders or files with square brackets.

For example, suppose you want to render different content based on different article IDs; you can do this:



app/

├── [id]/

│   └── page.tsx        # 动态路由,匹配任何 `/[id]` 的路径

[id] is a placeholder for dynamic routing; it can match any id parameter was added.

Get Dynamic Route Parameters

In Next.js, you can access dynamic route parameters via the useParams hook. For example:

Example

'use client'

;

import

{

useParams

}

from

'next/navigation'

;

export

default

function

Post

(

)

{

 

const

params

=

useParams

(

)

;

 

const

{

id

}

=

params

;

 

// Get dynamic route parameters id

 

return

<

div

Post ID

:

{

id

}

div

;

}

When a user visits /post/123, id is automatically assigned the value 123 and displayed on the page.

Nested Routing

Next.js also supports nested routing, that is, creating sub-routes under a route. For example:



app/

├── about/

│   ├── page.tsx          # /about 路由页面

│   ├── team/

│   │   └── page.tsx      # /about/team 子路由页面

│   └── contact/

│       └── page.tsx      # /about/contact 子路由页面

This approach gives your application a hierarchical structure while preserving the readability of URL paths.

Custom Routing Rules

If you want to customize routing behavior, you can configure it via the next.config.js file. But for most common routing needs, Next.js already provides out-of-the-box functionality without extra configuration.

Special Pages and Routing Features


Example

Create a Static Page

Let's take creating a simple static page as an example: create an about/page.tsx file with the following content:

Example

export

default

function

AboutPage

(

)

{

 

return

<

h1

About Us

h1

;

}

When you visit the /about path, Next.js automatically renders this page.

Using Dynamic Routes

Suppose you want to render a page based on the article ID; create an app/[id]/page.tsx file with the following content:

Example

import

{

useParams

}

from

'next/navigation'

;

export

default

function

PostPage

(

)

{

 

const

{

id

}

=

useParams

(

)

;

 

return

<

h1

Post ID

:

{

id

}

h1

;

}

Visiting /123 will display "Post ID: 123".

Route Navigation

Next.js uses the component provided by next/link for route navigation. You can add links in a page to jump to other pages.

Example

import

Link from

'next/link'

;

export

default

function

HomePage

(

)

{

 

return

(

   

<

div

<

h1

Welcome to Next.

js

!

h1

     

<

Link href

=

"/about"

About Us

Link

   

div

 

)

;

}

The component is used to navigate between pages while maintaining the single-page application (SPA) experience.