Home >Web Front-end >JS Tutorial >Mastering Nested Routes in React Router: Building Dynamic Layouts
Nested Routes in React Router allow you to define routes within other routes, enabling complex layouts and the ability to display different components depending on the path. This feature is particularly useful for building applications with sections that have their own sub-routes, such as dashboards, profiles, or admin panels.
Nested routes help create hierarchical URLs, where each route can have child routes that render specific content inside their parent component.
To set up nested routes in React Router, you define routes within a parent route using the Routes and Route components.
Here’s a basic example showing how to define a parent route and nested routes:
import React from 'react'; import { BrowserRouter, Routes, Route, Link, Outlet } from 'react-router-dom'; // Parent Component const Dashboard = () => { return ( <div> <h2>Dashboard</h2> <nav> <ul> <li><Link to="profile">Profile</Link></li> <li><Link to="settings">Settings</Link></li> </ul> </nav> <hr /> <Outlet /> {/* Child route content will render here */} </div> ); }; // Child Components const Profile = () => <h3>Profile Page</h3>; const Settings = () => <h3>Settings Page</h3>; const App = () => { return ( <BrowserRouter> <Routes> {/* Parent Route */} <Route path="dashboard" element={<Dashboard />}> {/* Nested Routes */} <Route path="profile" element={<Profile />} /> <Route path="settings" element={<Settings />} /> </Route> </Routes> </BrowserRouter> ); }; export default App;
You can also create nested routes with dynamic parameters.
import React from 'react'; import { BrowserRouter, Routes, Route, Link, Outlet } from 'react-router-dom'; // Parent Component const Dashboard = () => { return ( <div> <h2>Dashboard</h2> <nav> <ul> <li><Link to="profile">Profile</Link></li> <li><Link to="settings">Settings</Link></li> </ul> </nav> <hr /> <Outlet /> {/* Child route content will render here */} </div> ); }; // Child Components const Profile = () => <h3>Profile Page</h3>; const Settings = () => <h3>Settings Page</h3>; const App = () => { return ( <BrowserRouter> <Routes> {/* Parent Route */} <Route path="dashboard" element={<Dashboard />}> {/* Nested Routes */} <Route path="profile" element={<Profile />} /> <Route path="settings" element={<Settings />} /> </Route> </Routes> </BrowserRouter> ); }; export default App;
React Router provides a way to handle default nested routes. If no specific child route is matched, you can display a default component.
import React from 'react'; import { BrowserRouter, Routes, Route, Link, Outlet, useParams } from 'react-router-dom'; const Dashboard = () => { return ( <div> <h2>Dashboard</h2> <nav> <ul> <li><Link to="profile/1">Profile 1</Link></li> <li><Link to="profile/2">Profile 2</Link></li> </ul> </nav> <hr /> <Outlet /> {/* Child route content will render here */} </div> ); }; const Profile = () => { const { id } = useParams(); // Retrieve the 'id' parameter from the URL return <h3>Profile Page for User: {id}</h3>; }; const App = () => { return ( <BrowserRouter> <Routes> {/* Parent Route */} <Route path="dashboard" element={<Dashboard />}> {/* Nested Route with Path Parameter */} <Route path="profile/:id" element={<Profile />} /> </Route> </Routes> </BrowserRouter> ); }; export default App;
Nested routes in React Router are an essential feature for building complex UIs with hierarchical structures. They allow you to break down your application into smaller, manageable components while still keeping the navigation clean and dynamic. By using the
The above is the detailed content of Mastering Nested Routes in React Router: Building Dynamic Layouts. For more information, please follow other related articles on the PHP Chinese website!