React Router とは
これまでの Anime Station は1つの画面だけで構成されていました。URLが変わらないためリンクの共有やブラウザの戻るボタンが使えません。
React Router を使うと、URLに応じて表示するコンポーネントを切り替えるルーティングが実現できます。
| URL | ページ | 内容 |
|---|---|---|
/ | ホーム | サイトのトップ |
/articles | 記事一覧 | ブログ記事の一覧 |
/about | サイトについて | サイトの説明 |
プロジェクトの作成
React プロジェクト作成
npm create vite@latest react-blog -- --template react
パッケージインストール
新しいウィンドウで `react-blog' を開いて、ターミナルでパッケージインストールを実行します。
npm install
npm install react-router-dom
ページ構成
このLessonでは、シンプルなブログサイトを作ります。
| URL | ページ | 内容 |
|---|---|---|
/ | ホーム | サイトのトップ |
/articles | 記事一覧 | ブログ記事の一覧 |
/about | サイトについて | サイトの説明 |
src/
├── pages/
│ ├── HomePage.jsx
│ ├── ArticlesPage.jsx
│ └── AboutPage.jsx
├── components/
│ ├── Header.jsx
│ └── Header.module.css
├── App.jsx
└── main.jsx
💡 ページコンポーネントは
pages/フォルダにまとめるのが一般的です。
BrowserRouter / Routes / Route
BrowserRouter
アプリ全体をルーティング機能で包みます。main.jsx に追加します。
/* src/main.jsx */
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { BrowserRouter } from 'react-router-dom'
import App from './App'
import './index.css'
createRoot(document.getElementById('root')).render(
<StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</StrictMode>
)
Routes / Route
URLに応じて表示するコンポーネントを定義します。
import { Routes, Route } from 'react-router-dom'
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/articles" element={<ArticlesPage />} />
<Route path="/about" element={<AboutPage />} />
</Routes>
| 属性 | 説明 |
|---|---|
path | URLのパターン |
element | 表示するコンポーネント |
Link と NavLink
Link
ページ遷移のリンクを作ります。<a> タグの代わりに使います。
import { Link } from 'react-router-dom'
<Link to="/">ホーム</Link>
<Link to="/articles">記事一覧</Link>
⚠️
<a href>を使うとページ全体がリロードされます。React Router では必ず<Link>を使います。
NavLink
Link と同じですが、現在のURLと一致するときアクティブなスタイルを自動で付与できます。ナビゲーションメニューに使います。
import { NavLink } from 'react-router-dom'
<NavLink
to="/"
className={({ isActive }) => isActive ? styles.active : styles.link}
>
ホーム
</NavLink>
className に関数を渡すと、isActive が true のときアクティブ用のクラスを適用できます。
| 状態 | isActive | 適用されるクラス |
|---|---|---|
| 現在のURLと一致 | true | styles.active |
| 一致しない | false | styles.link |
実装
Header コンポーネント
ナビゲーションメニューに NavLink を使います。
/* src/components/Header.jsx */
import { NavLink } from 'react-router-dom'
import styles from './Header.module.css'
function Header() {
return (
<header className={styles.header}>
<NavLink to="/" className={styles.logo}>
React Blog
</NavLink>
<nav className={styles.nav}>
<NavLink
to="/"
end
className={({ isActive }) => isActive ? styles.active : styles.link}
>
ホーム
</NavLink>
<NavLink
to="/articles"
className={({ isActive }) => isActive ? styles.active : styles.link}
>
記事一覧
</NavLink>
<NavLink
to="/about"
className={({ isActive }) => isActive ? styles.active : styles.link}
>
サイトについて
</NavLink>
</nav>
</header>
)
}
export default Header
💡
/のリンクにはendを追加します。endがないと/articlesにいるときも/が一致してアクティブになってしまいます。
/* src/components/Header.module.css */
.header {
background: white;
border-bottom: 1px solid #e5e7eb;
padding: 0 1.5rem;
display: flex;
align-items: center;
justify-content: space-between;
height: 56px;
}
.logo {
font-size: 1.1rem;
font-weight: bold;
color: #111827;
text-decoration: none;
}
.nav {
display: flex;
gap: 0.25rem;
}
.link {
padding: 0.4rem 0.75rem;
border-radius: 6px;
color: #6b7280;
text-decoration: none;
font-size: 0.9rem;
}
.link:hover {
background: #f3f4f6;
color: #111827;
}
.active {
padding: 0.4rem 0.75rem;
border-radius: 6px;
background: #f3f4f6;
color: #111827;
text-decoration: none;
font-size: 0.9rem;
font-weight: bold;
}
App.jsx
/* src/App.jsx */
import { Routes, Route } from 'react-router-dom'
import styles from './App.module.css'
import Header from './components/Header'
import HomePage from './pages/HomePage'
import ArticlesPage from './pages/ArticlesPage'
import AboutPage from './pages/AboutPage'
function App() {
return (
<div className={styles.app}>
<Header />
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/articles" element={<ArticlesPage />} />
<Route path="/about" element={<AboutPage />} />
</Routes>
</div>
)
}
export default App
/* src/App.module.css */
.app {
min-height: 100vh;
background: #f9fafb;
display: flex;
flex-direction: column;
}
ページコンポーネント
/* src/pages/HomePage.jsx */
import styles from './Page.module.css'
function HomePage() {
return (
<main className={styles.main}>
<h1 className={styles.title}>React Blog へようこそ</h1>
<p className={styles.lead}>React Router を使ったブログサイトのサンプルです。</p>
</main>
)
}
export default HomePage
/* src/pages/ArticlesPage.jsx */
import styles from './Page.module.css'
function ArticlesPage() {
return (
<main className={styles.main}>
<h1 className={styles.title}>記事一覧</h1>
<p className={styles.lead}>ここに記事の一覧が入ります。</p>
</main>
)
}
export default ArticlesPage
/* src/pages/AboutPage.jsx */
import styles from './Page.module.css'
function AboutPage() {
return (
<main className={styles.main}>
<h1 className={styles.title}>サイトについて</h1>
<p className={styles.lead}>React Router の学習用サンプルサイトです。</p>
</main>
)
}
export default AboutPage
/* src/pages/Page.module.css */
.main {
flex: 1;
padding: 3rem 1.5rem;
max-width: 760px;
margin: 0 auto;
width: 100%;
}
.title {
font-size: 1.75rem;
font-weight: bold;
color: #111827;
margin-bottom: 1rem;
}
.lead {
font-size: 1rem;
color: #6b7280;
line-height: 1.7;
}
💡 3つのページで同じスタイルを使うため
Page.module.cssを共有しています。
ファイル構成
src/
├── pages/
│ ├── HomePage.jsx
│ ├── ArticlesPage.jsx
│ ├── AboutPage.jsx
│ └── Page.module.css
├── components/
│ ├── Header.jsx
│ └── Header.module.css
├── App.jsx
├── App.module.css
└── main.jsx
まとめ
| ポイント | 内容 |
|---|---|
| React Router | URLに応じてコンポーネントを切り替えるライブラリ |
| BrowserRouter | ルーティング機能をアプリ全体に提供する(main.jsx に追加) |
| Routes / Route | URLとコンポーネントの対応を定義する |
| Link | ページリロードなしで遷移する・<a> の代わりに使う |
| NavLink | アクティブ状態のスタイルを自動で付与できる Link |
end | / のリンクが他のURLと誤マッチするのを防ぐ |
pages/ | ページコンポーネントをまとめるフォルダ |
コース内レッスン
- 1
Lesson 1
Reactとは
レッスンを見る - 2
Lesson 2
React開発環境
レッスンを見る - 3
Lesson 3
プロジェクト作成
レッスンを見る - 4
Lesson 4
App コンポーネント
レッスンを見る - 5
Lesson 5
カスタムコンポーネント
レッスンを見る - 6
Lesson 6
Props
レッスンを見る - 7
Lesson 7
React Hooks
レッスンを見る - 8
Lesson 8
useStateの応用
レッスンを見る - 9
Lesson 9
非同期通信
レッスンを見る - 10
Lesson 10
useEffect
レッスンを見る - 11
Lesson 11
ローディングとエラー
レッスンを見る - 12
Lesson 12
useContext
レッスンを見る - 13
Lesson 13
Layoutコンポーネント
レッスンを見る - 14
Lesson 14
lucide-react
レッスンを見る - 15
Lesson 15
Motion
レッスンを見る - 16
Lesson 16
認証・認可
レッスンを見る - 17
Lesson 17
Firebaseセットアップ
レッスンを見る - 18
Lesson 18
Firebase認証
レッスンを見る - 19
Lesson 19
ReactRouter
レッスンを見る - 20
Lesson 20
shadcn/ui
レッスンを見る - 21
Lesson 21
アイコン
レッスンを見る