2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【react-router-dom v7】ルーティングのネスト

2
Posted at

はじめに

  • SwitchではなくRoutesを使うようになっていたので、今の方法を調べた

問題

  • ネストしたルーティングを書きたいが書き方不明

解決

親コンポーネント

  • 以下のようにすると /dashboard/settings のパスで見た時に子コンポーネントがレンダリングされる
<Routes>
  <Route path="dashboard" element={<Dashboard />}>
    <Route path="settings" element={<Settings />} />
  </Route>
</Routes>

子コンポーネント

import { Outlet } from "react-router";

export default function Dashboard() {
  return (
    <div>
      <Outlet />
    </div>
  );
}

終わりに

  • 結局公式ドキュメントを読むのがシンプルに書かれていて早い。今はAI slopも多いので特に根拠とできないような質の高くない記事については注意が必要。
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?