What are Nested Routes?
Nested routes, in the context of React.js applications (especially those using a routing library like React Router), refer to routes that are defined within other routes. This allows you to create a hierarchical structure for your application's navigation, mirroring a hierarchical relationship in your application's data or user interface. Think of it like folders and subfolders on your computer; each folder can contain more folders and files. Similarly, a parent route can render a component, and that component can then render *another* set of routes.
Consider a scenario where you have an e-commerce website with a /products
page. You might want sub-pages for individual product categories (e.g., /products/electronics
, /products/clothing
), and then even further sub-pages for specific products within those categories (e.g., /products/electronics/laptop123
). Nested routes let you elegantly define this structure.
How to Create Nested Routes (with React Router v6 Example)
Here's a conceptual example of how you might implement nested routes using React Router v6. Note that this is a simplified example for illustrative purposes; you'll need to install React Router (npm install react-router-dom
or yarn add react-router-dom
) and have a basic React project set up to run this code.
import React from 'react';
import { BrowserRouter, Routes, Route, Link, Outlet, useParams } from 'react-router-dom';
// Components
function Home() {
return <h1>Home Page</h1>;
}
function Products() {
return (
<div>
<h2>Products</h2>
<ul>
<li><Link to="/products/electronics">Electronics</Link></li>
<li><Link to="/products/clothing">Clothing</Link></li>
</ul>
<Outlet /> {/* This is where child routes are rendered */}
</div>
);
}
function Electronics() {
return (
<div>
<h3>Electronics Products</h3>
<ul>
<li><Link to="/products/electronics/laptop123">Laptop 123</Link></li>
<li><Link to="/products/electronics/smartphone456">Smartphone 456</Link></li>
</ul>
<Outlet />
</div>
);
}
function Clothing() {
return <h3>Clothing Products</h3>;
}
function ProductDetail() {
const { productId } = useParams(); // Access the productId from the URL
return <h4>Product Detail: {productId}</h4>;
}
function Team() {
return <h1>Our Team</h1>;
}
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/products" element={<Products />}>
<Route path="electronics" element={<Electronics />}>
<Route path=":productId" element={<ProductDetail />} />
</Route>
<Route path="clothing" element={<Clothing />} />
</Route>
<Route path="/team" element={<Team />} />
</Routes>
</BrowserRouter>
);
}
export default App;
- Install React Router:
npm install react-router-dom
or yarn add react-router-dom
- Import Necessary Components: Import
BrowserRouter
, Routes
, Route
, Link
, Outlet
and useParams
from react-router-dom
. - Define Routes: Use the
<Routes>
and <Route>
components to define your application's routes. - Create Nested Routes: To nest routes, define a
<Route>
element within another <Route>
. The parent route's component will render, and then React Router will determine which of the child routes to render *within* that parent component. - Use Outlet: The
<Outlet />
component is crucial. It acts as a placeholder in the parent route's component where the matched child route's component will be rendered. Without <Outlet />
, the child route won't be displayed. - Access Parameters: You can access dynamic segments of the URL (e.g.,
/products/electronics/:productId
) using the useParams
hook. This hook returns an object containing the key-value pairs of the parameters defined in the route.
Explanation of the Example:
- The
/products
route renders the Products
component. - The
Products
component includes an <Outlet />
, which is where the child routes will be rendered. - The
/products/electronics
route renders the Electronics
component *within* the Products
component. - The
/products/clothing
route renders the Clothing
component *within* the Products
component. - The
/products/electronics/:productId
route renders the ProductDetail
component *within* the Electronics
component, and you can access the productId
using useParams
. For example if you visit /products/electronics/laptop123
, the ProductDetail component will display "Product Detail: laptop123"