React Router 中的巢狀路由
React Router 中的巢狀路由 可讓您在其他路由中定義路由,從而實現複雜的佈局並能夠根據路徑顯示不同的元件。此功能對於建立具有自己的子路由部分的應用程式特別有用,例如儀表板、設定檔或管理面板。
巢狀路由有助於建立分層 URL,其中每個路由都可以具有在其父元件內呈現特定內容的子路由。
如何建立巢狀路由
要在 React Router 中設定巢狀路由,您可以使用 Routes 和 Route 元件在父路由中定義路由。
實作巢狀路由的步驟
- 父路由:定義父元件的路由。
- 子路由:在父元件內,建立一個附加路由元件的路由元件來處理子路由。
-
渲染子元件:確保父元件包含
>元件,充當渲染子元件的佔位符。
巢狀路由的基本範例
這是一個基本範例,展示如何定義父路由和巢狀路由:
import React from 'react'; import { BrowserRouter, Routes, Route, Link, Outlet } from 'react-router-dom'; // Parent Component const Dashboard = () => { return ( <div> <h2 id="Dashboard">Dashboard</h2> <nav> <ul> <li> <link to="profile">Profile</li> <li> <link to="settings">Settings</li> </ul> </nav> <hr> <outlet></outlet> {/* Child route content will render here */} </div> ); }; // Child Components const Profile = () => <h3 id="Profile-Page">Profile Page</h3>; const Settings = () => <h3 id="Settings-Page">Settings Page</h3>; const App = () => { return ( <browserrouter> <routes> {/* Parent Route */} <route path="dashboard" element="{<Dashboard"></route>}> {/* Nested Routes */} <route path="profile" element="{<Profile"></route>} /> <route path="settings" element="{<Settings"></route>} /> </routes> </browserrouter> ); }; export default App;
說明:
- 儀表板元件是父路由,它呈現導航連結和
>。成分。 >用作將渲染子路由元件的佔位符。 - 設定檔和設定元件是儀表板內的巢狀路由。
- Link 元件用於導航,當點擊時,它們將更新 URL 並呈現相應的巢狀元件(例如 /dashboard/profile 或 /dashboard/settings)。
- 儀表板內的路由和路由元件定義巢狀路由,確保當 URL 符合 /dashboard/profile 或 /dashboard/settings 時,呈現適當的元件。
帶路徑參數的巢狀路由
您也可以使用動態參數建立巢狀路由。
import React from 'react'; import { BrowserRouter, Routes, Route, Link, Outlet } from 'react-router-dom'; // Parent Component const Dashboard = () => { return ( <div> <h2 id="Dashboard">Dashboard</h2> <nav> <ul> <li> <link to="profile">Profile</li> <li> <link to="settings">Settings</li> </ul> </nav> <hr> <outlet></outlet> {/* Child route content will render here */} </div> ); }; // Child Components const Profile = () => <h3 id="Profile-Page">Profile Page</h3>; const Settings = () => <h3 id="Settings-Page">Settings Page</h3>; const App = () => { return ( <browserrouter> <routes> {/* Parent Route */} <route path="dashboard" element="{<Dashboard"></route>}> {/* Nested Routes */} <route path="profile" element="{<Profile"></route>} /> <route path="settings" element="{<Settings"></route>} /> </routes> </browserrouter> ); }; export default App;
說明:
- Profile 元件從 URL (/dashboard/profile/:id) 接收 動態參數。
- useParams() 鉤子用於存取動態參數,在本例中為 id.
- 父路由 /dashboard 對於每個設定檔都有子路由,當 URL 發生變化時(例如 /dashboard/profile/1),Profile 元件將顯示使用者的 ID。
處理預設巢狀路由
React Router 提供了一種處理預設巢狀路由的方法。如果沒有符合到特定的子路由,則可以顯示預設元件。
import React from 'react'; import { BrowserRouter, Routes, Route, Link, Outlet, useParams } from 'react-router-dom'; const Dashboard = () => { return ( <div> <h2 id="Dashboard">Dashboard</h2> <nav> <ul> <li> <link to="profile/1">Profile 1</li> <li> <link to="profile/2">Profile 2</li> </ul> </nav> <hr> <outlet></outlet> {/* Child route content will render here */} </div> ); }; const Profile = () => { const { id } = useParams(); // Retrieve the 'id' parameter from the URL return <h3 id="Profile-Page-for-User-id">Profile Page for User: {id}</h3>; }; const App = () => { return ( <browserrouter> <routes> {/* Parent Route */} <route path="dashboard" element="{<Dashboard"></route>}> {/* Nested Route with Path Parameter */} <route path="profile/:id" element="{<Profile"></route>} /> </routes> </browserrouter> ); }; export default App;
說明:
- 索引路由是一種特殊的路由,用來定義當父路由符合但沒有提供子路徑時的預設元件。
- 在這種情況下,/dashboard 將預設渲染 DashboardHome 元件,但如果存取 /dashboard/profile 或 /dashboard/settings,則會顯示對應的元件。
結論
React Router 中的巢狀路由是建構具有分層結構的複雜 UI 的基本功能。它們允許您將應用程式分解為更小的、可管理的元件,同時仍保持導航的乾淨和動態。透過使用
以上是掌握 React Router 中的嵌套路由:建立動態佈局的詳細內容。更多資訊請關注PHP中文網其他相關文章!

javaandjavascriptaredistinctlanguages:javaisusedforenterpriseandmobileapps,while javascriptifforInteractiveWebpages.1)JavaisComcompoppored,statieldinglationallyTypted,statilly tater astrunsonjvm.2)

JavaScript核心數據類型在瀏覽器和Node.js中一致,但處理方式和額外類型有所不同。 1)全局對像在瀏覽器中為window,在Node.js中為global。 2)Node.js獨有Buffer對象,用於處理二進制數據。 3)性能和時間處理在兩者間也有差異,需根據環境調整代碼。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版
中文版,非常好用

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。