Logicoya
12

ルーティング

App Router

Next.jsのルーティング

Next.jsのルーティングは、App RouterとPages Routerの2種類ありますが、Next.js 13以降はApp Routerが推奨となりました。

Pages RouterApp Router
URLファイル名フォルダ名
方式Pages DirecotryApp Direcotry
ファイルpages/URLパス/[slug].tsxapp/[slug]/page.tsx
  • slugは、コンテンツを識別するためのURLパスの文字列

ダイナミックルーティング

App Routerでは、URLの一部を変数として扱い、ページコンポーネントに動的にデータを渡すことができます。

ディレクトリ

「App Directory」では「app/」に作成したディレクトリでダイナミックルーティングが設定されます。

pageファイル

ルーティングに対応したフォルダに、「page.js」または「page.tsx」を作成すると、ページが表示されます。

ルーティングの例
ディレクトリファイルURL
app/page.jshttp://localhost:3000/
app/dashboard/page.jshttp://localhost:3000/dashboard/
app/dashboard/settingspage.jshttp://localhost:3000/dashboard/settings
app/dashboard/analytics404 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」にアクセスできるか確認します。

結果