App Router
Next.jsのルーティング
Next.jsのルーティングは、App RouterとPages Routerの2種類ありますが、Next.js 13以降はApp Routerが推奨となりました。
| Pages Router | App Router | |
|---|---|---|
| URL | ファイル名 | フォルダ名 |
| 方式 | Pages Direcotry | App Direcotry |
| ファイル | pages/URLパス/[slug].tsx | app/[slug]/page.tsx |
- slugは、コンテンツを識別するためのURLパスの文字列
ダイナミックルーティング
App Routerでは、URLの一部を変数として扱い、ページコンポーネントに動的にデータを渡すことができます。
ディレクトリ
「App Directory」では「app/」に作成したディレクトリでダイナミックルーティングが設定されます。
pageファイル
ルーティングに対応したフォルダに、「page.js」または「page.tsx」を作成すると、ページが表示されます。

ルーティングの例
| ディレクトリ | ファイル | URL |
|---|---|---|
| app/ | page.js | http://localhost:3000/ |
| app/dashboard/ | page.js | http://localhost:3000/dashboard/ |
| app/dashboard/settings | page.js | http://localhost:3000/dashboard/settings |
| app/dashboard/analytics | 404 Not Found |
プロフィールページ
ファイル構成
├── app/
│ └── user/
│ └── profile/
│ └── page.tsx
├── public/
│ └── images/
│ └── me.png
ProfilePageコンポーネント
ページファイル作成
「profile/page.tsx」を作成します。
コンポーネント作成
ProfilePageコンポーネント(Client Component)を作成します。
app/profile/page.tsx
"use client"
const ProfilePage = () => {
}
export default ProfilePage;
JSX
プロフィールページ用のJSXを作成します。
app/profile/page.tsx
"use client"
import { User } from '@/app/models/User';
import Image from 'next/image';
import imageMe from "@/public/images/me.png";
const ProfilePage = () => {
return (
<div className="mx-auto w-1/2">
<h1 className="text-3xl text-center font-bold">Profile</h1>
<div className="flex justify-center p-3">
<Image className="rounded-full h-[100px] w-[100px]" src={imageMe} alt="" />
</div>
<div className="flex justify-center p-3">
{user.name}
</div>
</div>
)
}
export default ProfilePage;
ページ確認
「/profile」にアクセスしてページが表示されるか確認します。
結果
ユーザ登録ページ
ファイル構成
app/
├── auth/
│ ├── layout.tsx
│ └── regist/
│ └── page.tsx
├── components/
│ ├── Input.tsx
│ └── Navbar.tsx
ページファイル作成
「auth/regist/page.tsx」を作成します。
RegistPageコンポーネント
コンポーネント作成
RegistPage(Client Component)を作成します。
app/auth/regist/page.tsx
"use client";
function RegistPage() {
return (
<div>
Enter
</div>
);
}
export default RegistPage;
タイトル
タイトルを作成し、「React Icons」のFaUserをインポートしてアイコン表示します。
app/auth/regist/page.tsx
"use client";
import { FaUser } from "react-icons/fa";
function RegistPage() {
return (
<div className="mx-auto w-1/3">
<h1 className="flex p-3 me-3 text-2xl justify-center">
<FaUser className='mt-1 me-3' />
Register
</h1>
</div>
);
}
export default RegistPage;
「auth/regist/」にアセクセスしてページを確認します。
結果
カスタムコンポーネント利用
ユーザ登録画面でカスタムコンポーネント利用します。
app/auth/regist/page.tsx
"use client";
import { FaUser } from "react-icons/fa";
import Link from 'next/link';
// カスタムコンポーネントのインポート
import Input from '@/app/components/Input';
import Input from '@/app/components/ClickButton';
import Input from '@/app/components/LinkButton';
function RegistPage() {
return (
<div className="mx-auto w-1/3">
<h1 className="flex p-3 me-3 text-2xl justify-center">
<FaUser className='mt-1 me-3' />
Register
</h1>
<div>
<Input
type="text"
value=""
placeholder="Your Name"
/>
<Input
type="password"
value=""
placeholder="Email"
/>
<Input
type="text"
value=""
placeholder="Email"
/>
</div>
<div>
<ClickButton label="Sign up" onClick={()=>{}} />
<LinkButton label="Sign in" href="/auth/login" />
</div>
</div>
);
}
export default RegistPage;
結果
ログインページ
ファイル構成
app/
├── auth/
│ ├── layout.tsx
│ ├── login/
│ │ └── page.tsx
├── components/
│ ├── Input.tsx
│ └── Navbar.tsx
LoginPageコンポーネント
「auth/login/page.tsx」にLoginPage(Client Component)を作成します。内容はユーザ登録画面と同様です。
app/auth/regist/page.tsx
"use client";
import { RiLockPasswordFill } from "react-icons/ri";
import Input from '@/app/components/Input';
function RegistPage() {
return (
<div className="mx-auto w-1/3">
<h1 className="flex p-3 me-3 text-2xl justify-center">
<RiLockPasswordFill className='mt-1 me-3' />
Sign in
</h1>
<div>
<Input
type="text"
value=""
placeholder='Email'
/>
<Input
type="password"
value=""
placeholder='Email'
/>
</div>
<div>
<ClickButton label="Sign in" onClick={() => {}} />
<LinkButton label="Register" href="/auth/regist/" />
</div>
</div>
);
}
export default RegistPage;
ページ確認
「auth/login」にアクセスできるか確認します。
結果

コース内レッスン
- 1
Lesson 1
はじめるまえに
レッスンを見る - 2
Lesson 2
プロジェクト作成 - Laravel -
レッスンを見る - 3
Lesson 3
API処理 1
レッスンを見る - 4
Lesson 4
Postman
レッスンを見る - 5
Lesson 5
ユーザ認証API
レッスンを見る - 6
Lesson 6
API処理 2
レッスンを見る - 7
Lesson 7
プロジェクト作成 - Next.js -
レッスンを見る - 8
Lesson 8
レイアウト
レッスンを見る - 9
Lesson 9
コンポーネントの基本1
レッスンを見る - 10
Lesson 10
コンポーネントの基本2
レッスンを見る - 11
Lesson 11
コンポーネントの基本3
レッスンを見る - 12
Lesson 12
ルーティング
レッスンを見る - 13
Lesson 13
useState
レッスンを見る - 14
Lesson 14
モデルとサービス
レッスンを見る - 15
Lesson 15
useEffect
レッスンを見る - 16
Lesson 16
useRouter
レッスンを見る - 16
Lesson 16
プロパティ(props)
レッスンを見る - 17
Lesson 17
Cookie
レッスンを見る - 18
Lesson 18
useContext
レッスンを見る - 19
Lesson 19
NextAuth
レッスンを見る - 20
Lesson 20
NextAuthで認証
レッスンを見る