React Router 是一個用於在 React 應用程式中處理路由的函式庫。它允許您的應用程式在不同的元件和視圖之間導航,而無需重新載入整頁,從而實現無縫的用戶體驗。
先安裝react-router-dom:
npm install react-router-dom
使用 BrowserRouter、Routes 和 Route 設定基本路由:
import React from 'react'; import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom'; const Home = () => <h2>Home</h2>; const About = () => <h2>About</h2>; const App = () => { return ( <Router> <nav> <Link to="/">Home</Link> <Link to="/about">About</Link> </nav> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> </Routes> </Router> ); }; export default App;
對於更複雜的應用程序,您可以嵌套路由。以下是如何在父元件中設定巢狀路由:
import React from 'react'; import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom'; const Dashboard = () => <h2>Dashboard Home</h2>; const Profile = () => <h2>Your Profile</h2>; const DashboardLayout = () => { return ( <div> <nav> <Link to="/dashboard">Home</Link> <Link to="/dashboard/profile">Profile</Link> </nav> <Routes> <Route path="/" element={<Dashboard />} /> <Route path="profile" element={<Profile />} /> </Routes> </div> ); }; const App = () => { return ( <Router> <Routes> <Route path="/dashboard/*" element={<DashboardLayout />} /> </Routes> </Router> ); }; export default App;
動態路由可讓您在 URL 中傳遞參數。以下是定義和存取動態路線的方法:
import { useParams } from 'react-router-dom'; const User = () => { const { id } = useParams(); return <h2>User ID: {id}</h2>; }; const App = () => { return ( <Router> <nav> <Link to="/user/1">User 1</Link> <Link to="/user/2">User 2</Link> </nav> <Routes> <Route path="/user/:id" element={<User />} /> </Routes> </Router> ); }; export default App;
要實現受保護的路由,您可以建立自訂 PrivateRoute 元件:
import { Navigate, Outlet } from 'react-router-dom'; const useAuth = () => { const user = { loggedIn: true }; // Replace with actual auth logic return user && user.loggedIn; }; const PrivateRoute = () => { const isAuth = useAuth(); return isAuth ? <Outlet /> : <Navigate to="/login" />; }; const App = () => { return ( <Router> <Routes> <Route path="/login" element={<Login />} /> <Route path="/dashboard" element={<PrivateRoute />}> <Route path="" element={<Dashboard />} /> </Route> </Routes> </Router> ); }; export default App;
有時,您可能想要以程式設計方式導航,例如在提交表單後。在 React Router v6 中使用 useNavigate 鉤子:
import { useNavigate } from 'react-router-dom'; const Login = () => { const navigate = useNavigate(); const handleLogin = () => { // Login logic here... navigate('/dashboard'); }; return ( <div> <h2>Login</h2> <button onClick={handleLogin}>Login</button> </div> ); }; export default Login;
透過建立包羅萬象的路由來處理 404(找不到)錯誤:
const NotFound = () => <h2>404 - Page Not Found</h2>; const App = () => { return ( <Router> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> <Route path="*" element={<NotFound />} /> </Routes> </Router> ); }; export default App;
對於大型應用程序,延遲載入路由可以提高效能。以下是如何使用 React.lazy() 和 Suspense 實現延遲載入:
import React, { Suspense, lazy } from 'react'; import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; const Home = lazy(() => import('./Home')); const About = lazy(() => import('./About')); const App = () => { return ( <Router> <Suspense fallback={<div>Loading...</div>}> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> </Routes> </Suspense> </Router> ); }; export default App;
以上是掌握 React 路由:應用程式導航完整指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!