Nearby lessons
31 of 44🛣️ React Router – Nested Routes
📌 What are Nested Routes?
What are Nested Routes in React Router?
Nested Routes allow you to define routes inside other routes. This helps in building hierarchical navigation where child components are displayed within a parent component's layout.
Parent Component with Outlet component acts as a placeholder where child routes will render.
In our case, when we visit /products/shirts, the Shirts component
will be displayed inside Products.
📌 What are Nested Routes?
- Nested Routes let you render child routes inside a parent route.
- They help create layouts where sub-pages share common UI elements (like a navbar).
- React Router provides Outlet for rendering child routes.
- Useful for dashboards, product categories, settings pages, etc.
⚙️ Example of Nested Routes
Below is an example where /products is the parent route, and it has
child routes /products/shirts and /products/jeans.
React Playground
import React from 'react' import { Route, Routes } from 'react-router-dom' import Home from './Home.jsx' import About from './About.jsx' import Contact from './Contact.jsx' import Navbar from './Navbar.jsx' import PageNotFound from './PageNotFound.jsx' import Products from './Products.jsx' import Shirts from './Shirts.jsx' import Jeans from './Jeans.jsx' const App = () => { return ( <> <Navbar /> <Routes> <Route path='/' element={<Home />} /> <Route path='/products' element={<Products />}> <Route path='shirts' element={<Shirts />} /> <Route path='jeans' element={<Jeans />} /> </Route> <Route path='/about' element={<About />} /> <Route path='/contact' element={<Contact />} /> <Route path='*' element={<PageNotFound />} /> </Routes> </> ) } export default App;
🎨 Navigation Flow
- Visit
/products→ Products page loads with links. - Click Shirts →
/products/shirtsloads inside Products. - Click Jeans →
/products/jeansloads inside Products.
The parent layout (Products page) stays consistent while child content changes.
🧠 Test Your Knowledge
4 QuestionsProgress: 0 / 4
Keep Going!React - Index in Route Router