ホームページ >ウェブフロントエンド >jsチュートリアル >React Router V6:初心者ガイド
<routes></routes>
<route></route>
<link>
高度なルーティング手法:useNavigate
Reactは、複数のビュー(ページ)で動的なWebアプリケーションを構築することに優れています。 従来のマルチページアプリとは異なり、ナビゲーションはページ全体をリロードしてはなりません。 代わりに、既存のページ内でビューをスムーズにレンダリングする必要があります。 Reactルーターはこれを宣言的に達成し、シームレスなユーザーエクスペリエンスを確保します。 ユーザーは期待しています:
各ビューの一意のURL:ブックマークを許可(例えば、
)。www.example.com/products
example.com/products/shoes/101
、重要なメモ:
React Routerは、公式のFacebook/Meta製品ではなく、Remixソフトウェアが管理するサードパーティライブラリです。<code class="language-jsx"><route path="/about" element="{<About"></route>} /></code>
概要:<route></route>
<route></route>
このチュートリアルカバー:<link>
基本的なルーティングの概念。 ネストされたルーティング。
パスパラメーターを備えた動的ネストルーティング。保護されたルートの実装。
完全なプロジェクトコードはGitHubで入手できます(ここに挿入するリンク)。 Reactルーターのセットアップ:node.jsがインストールされる必要があります。 そうでない場合は、公式node.jsのWebサイトからダウンロードしてください。 node.js管理を容易にするためにバージョンマネージャーを使用することを検討してください。 NPM(ノードパッケージマネージャー)はnode.jsにバンドルされています インストールの確認:
<code class="language-jsx"><route path="/about" element="{<About"></route>} /></code>
Create Reactアプリを使用して新しいReactプロジェクトを作成します:
<code class="language-bash">node -v npm -v</code>
React Router Dom:
をインストールします<code class="language-bash">npx create-react-app react-router-demo cd react-router-demo</code>
開発サーバーを起動:
<code class="language-bash">npm install react-router-dom</code>
React React Routerを使用したReactアプリがhttp://localhost:3000/
。
Reactルーターの基本:
ホーム、カテゴリ、製品の3つのビューを持つアプリを作成します。
ルーターコンポーネントまたはBrowserRouter
。 HashRouter
(HTML5 History APIを使用)は、一般的にクリーナーのURLよりも好まれます。
BrowserRouter
<code class="language-bash">npm start</code>
関数を提供します。useNavigate
navigate
リンクおよびルートコンポーネント
ページリロードなしでナビゲーションを提供します。<route></route>
<link>
update
App.js
<code class="language-jsx">// src/index.js import { BrowserRouter } from 'react-router-dom'; // ... root.render( <react.strictmode> <browserrouter> <app></app> </browserrouter> </react.strictmode> );</code>ネストされたルーティング:
ネストは、他の
コンポーネント内にコンポーネントを配置してルートします。 これは、ネストされたURL構造を反映しています
<route></route>
変更<route></route>
:
App.js
create
<code class="language-jsx">import { Link, Route, Routes } from 'react-router-dom'; // ... component definitions for Home, Categories, Products ... export default function App() { return ( <div> <nav> <ul> <li> <link to="/">Home</li> <li> <link to="/categories">Categories</li> <li> <link to="/products">Products</li> </ul> </nav> <routes> <route path="/" element="{<Home"></route>} /> <route path="/categories" element="{<Categories"></route>} /> <route path="/products" element="{<Products"></route>} /> </routes> </div> ); }</code>
Categories.js
<code class="language-jsx">import { Link, Route, Routes } from 'react-router-dom'; import { Categories, Desktops, Laptops } from './Categories'; // Import nested route components // ... other components ... export default function App() { return ( <div> {/* ... navigation ... */} <routes> <route path="/" element="{<Home"></route>} /> <route path="/categories" element="{<Categories"></route>}> <route path="desktops" element="{<Desktops"></route>} /> <route path="laptops" element="{<Laptops"></route>} /> <route path="/products/*" element="{<Products"></route>} /> {/* Note the trailing * */} </routes> </div> ); }</code>
ダイナミックネストルーティング:<outlet></outlet>
パスパラメーターを使用して、動的ルートを作成します。 子ルートを許可するために、親ルートにトレーリングを追加します。 パラメーターにアクセスするには、を使用してください。
update:*
useParams
Products.js
。
<code class="language-jsx">import { Link, Outlet } from 'react-router-dom'; export const Categories = () => ( <div> <h2>Categories</h2> <nav> <ul> <li> <link to="desktops">Desktops</li> <li> <link to="laptops">Laptops</li> </ul> </nav> <outlet></outlet> </div> ); export const Desktops = () => <h3>Desktop PC Page</h3>; export const Laptops = () => <h3>Laptops Page</h3>;</code>
ルートの保護::productId
Product
useParams
プログラマティックリダイレクトに
コンポーネントを作成します。 create:
useNavigate
aPrivateRoute
コンポーネントを追加し、
に統合します。 元の応答に記載されているセキュリティ上の考慮事項を覚えておいてください。PrivateRoute.js
<code class="language-jsx">// ... (import statements and productData) ... const Products = () => ( <div> <h2>Products</h2> <ul> {/* ... linkList (generated from productData) ... */} </ul> <routes> <route path=":productId" element="{<Product" data="{productData}"></route>} /> <route index element="{<p">Please select a product.} /> </route></routes> </div> ); // ... Product component ...</code>React Router v6.4以降:
Login
PrivateRoute
React Router v6.4は、データの負荷と突然変異API(リミックスに触発された)を導入しました。 これらのAPIは、ローダーとアクションを使用して、ルート内でデータの取得と管理を簡素化します。 このセクションでは、別のより詳細な説明が必要です。App.js
/admin
このチュートリアルは、React Router V6の包括的な概要を提供し、基本的および高度なルーティングの概念をカバーしています。 最新の情報と詳細については、公式のReactルーターのドキュメントを参照してください。
以上がReact Router V6:初心者ガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。