HOME › オンライン教材 › React & Next.js 超入門 › Lesson 4 App Router
App Router の基本
App Router とは
App Router は、「フォルダ/ファイル = URL」をルールとしたダイナミックルーティングです。Next.js 13 から プロジェクト直下の app/ を基準に page.tsx ファイルを配置します。
ルーティングの大原則
よくある構成例
app/
├── api/
│ └── route.tsx
├── layout.tsx
├── page.tsx
├── chat/
│ └── page.tsx
└── chat/
├── page.tsx
└── [slug]/
├── page.tsx
├── loading.tsx
└── error.tsx
基本ルーティング
| 項目 | 説明 | |
|---|---|---|
| フォルダ | app/chat/ | /chat でアクセス |
| ページ | page.tsx | フォルダ内のページファイル |
| 共通レイアウト1 | layout.tsx | HTMLの共通枠を利用 |
| 共通レイアウト2 | template.tsx | ページ遷移ごとに状態リセットされるレイアウト |
| ネスト(階層 URL) | app/blog/[slug]/page.tsx | /blog/abc でアクセス |
| 動的パラメータ | [param]、可変長は [...param] | パラメーター取得 |
動的 & ネストルーティング
| 形式 | フォルダ名 | 例 URL | searchParams / params 値 |
|---|---|---|---|
| 単一 | [id] | /post/123 | { id: '123' } |
| 可変長 | [...slug] | /docs/a/b/c | { slug: ['a','b','c'] } |
| オプション | [[lang]] | /en/blog or /blog | { lang?: string } |
ルートグループ & URL クリーン化
フォルダ名 (group) を使うと「URLに影響しない」グルーピングが可能です。
| ファイル | URL |
|---|---|
app/(marketing)/landing/page.tsx | / |
app/(app)/landing/page.tsx | /dashboard |
ページ固有の補助ファイル
| ファイル | タイミング / 目的 |
|---|---|
loading.tsx | データ取得待ち中に表示 |
error.tsx | 例外捕捉してユーザフレンドリ表示 |
not-found.tsx | notFound() 呼び出し時に表示 |
トップページ
トップページの修正
サイトのトップページはすでに作成されています。 app/page.tsx を修正してみましょう。
app/page.tsx
"use client"
export default function Home() {
return (
<div className="text-center space-y-6">
<h1 className="text-4xl md:text-6xl font-extrabold text-gray-800">
React & Next.js
</h1>
<p className="text-lg md:text-xl text-gray-600">
サンプルアプリを作成しよう!
</p>
</div>
);
}
"use client"
import Link from "next/link";
export default function Home() {
return (
<div className="text-center space-y-6">
<h1 className="text-4xl md:text-6xl font-extrabold text-gray-800">
React & Next.js
</h1>
<p className="text-lg md:text-xl text-gray-600">
サンプルアプリを作成しよう!
</p>
<div className="space-x-4">
<Link href="counter/"
className="px-6 py-3 bg-sky-600 text-white rounded-xl shadow hover:bg-sky-700 transition">
はじめる
</Link>
</div>
</div>
);
}
ページ追加
カウンターページ作成
app/counter/page.tsx を作成します。
React用スニペット
ES7 React/Redux/GraphQL/React-Native プラグインを利用すると、rfce または rfc で、Reactのファンクションコンポーネントが簡単に作成できます。
rfce スニペット
rfc スニペット
ソースコード
基本コードが自動入力されました。
app/counter/page.tsx
import React from 'react'
export default function page() {
return (
<div>page</div>
)
}
ページの修正
コンテンツの中身を変更します。
app/counter/page.tsx
import React from 'react'
export default function page() {
return (
<div>
<h1 className="text-3xl font-bold text-center">Counter</h1>
</div>
)
}
ページの確認
ブラウザで counter/ にアクセスしてページが表示されるか確認します。

ページリンク
Link
Next.js では <a> ではなく <Link> でページリンクを推奨しています。
Link と a の違い
| 特徴 | <Link> | <a> |
|---|---|---|
| ルーティング制御 | ✅ SPAとして動作(ページ遷移が速い・状態保持) | ❌ フルリロードが発生(状態が初期化) |
| プリフェッチ(事前読み込み) | ✅ 自動で先読みされる(UX向上) | ❌ なし |
| Next.js の最適化対象 | ✅ 対象になる | ❌ 無視される |
| ページ遷移の速度 | ✅ 比較的高速 | ❌ HTMLベース |
<a> だけ使うケース(例外)
| 使用シーン | 理由 |
|---|---|
| 外部リンク(例: Googleなど) | Link は内部ルーティング専用。外部は通常の <a> でOK |
| ファイルダウンロードリンク | a href="/file.pdf" download のような場合 |
| メール・電話リンク | mailto:[email protected] や tel:090-xxxx-xxxx |
Link の使い方
インポート
next/link から Link をインポートします。
app/page.tsx
import Link from "next/link";
テキストリンク
<Link> の中に直接テキストや要素を記述します。
<Link href="/counter">はじめ</Link>
リンクの追加
<Link> を利用して counter/ にリンクしてみましょう。
app/page.tsx
"use client"
import Link from "next/link";
export default function Home() {
return (
<div className="text-center space-y-6">
<h1 className="text-4xl md:text-6xl font-extrabold text-gray-800">
React & Next.js
</h1>
<p className="text-lg md:text-xl text-gray-600">
サンプルアプリを作成しよう!
</p>
<div className="space-x-4">
<Link href="counter/"
className="px-6 py-3 bg-sky-600 text-white rounded-xl shadow hover:bg-sky-700 transition">
はじめる
</Link>
</div>
</div>
);
}
カウンターページ確認
トップページから counter/ にリンクして表示されるか確認してみましょう。

コース内レッスン
- 1
Lesson 1
はじめに
レッスンを見る - 2
Lesson 2
プロジェクト作成
レッスンを見る - 3
Lesson 3
レイアウト
レッスンを見る - 4
Lesson 4
App Router
レッスンを見る - 5
Lesson 5
コンポーネント
レッスンを見る - 6
Lesson 6
React Hook
レッスンを見る - 7
Lesson 7
Memoアプリプロジェクト
レッスンを見る - 8
Lesson 8
APIルーティング
レッスンを見る - 9
Lesson 9
useEffect
レッスンを見る - 10
Lesson 10
ファイル操作
レッスンを見る - 11
Lesson 11
Gemini API でプロンプト
レッスンを見る - 13
Lesson 13
Gemini API で画像操作
レッスンを見る - 21
Lesson 21
Postman
レッスンを見る