In traditional multi-page web applications, clicking a navigation link sends an HTTP GET request to the server, resulting in a white-screen flash while the browser fetches and parses a completely new HTML page. React Router v6 enables client-side routing: URLs change dynamically, browser history works seamlessly, but transitions happen instantaneously in milliseconds without reloading the browser.
Figure 9: React Router v6 Architecture: Persistent Layouts with Dynamic Nested Outlets.
npm install react-router-dom
// src/App.jsx
import React from 'react';
import { BrowserRouter, Routes, Route, Link, Outlet, useParams, useNavigate } from 'react-router-dom';
// 1. Root Layout (Header and Footer persist across all pages)
function RootLayout() {
return (
<div className="layout-container">
<header style={{ background: '#5dbe59', padding: '15px', color: '#fff' }}>
<h2>Veewom Academy</h2>
<nav>
<Link to="/" style={{ color: '#fff', marginRight: '15px' }}>Home</Link>
<Link to="/students" style={{ color: '#fff', marginRight: '15px' }}>Students</Link>
<Link to="/contact" style={{ color: '#fff' }}>Contact</Link>
</nav>
</header>
<main style={{ padding: '20px' }}>
{/* <Outlet /> renders the matched child page component */}
<Outlet />
</main>
<footer style={{ textAlign: 'center', padding: '15px', borderTop: '1px solid #e2e8f0' }}>
© 2026 Veewom.com
</footer>
</div>
);
}
// 2. Dynamic Route with useParams()
function StudentDetails() {
const { id } = useParams(); // Extracts :id from URL (e.g. /students/42)
const navigate = useNavigate();
return (
<div>
<h3>Viewing Profile for Student ID: #{id}</h3>
<button onClick={() => navigate('/students')}>
← Back to Directory
</button>
</div>
);
}
// 3. Main Router Setup
export default function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<RootLayout />}>
<Route index element={<h2>Home: Welcome to React Router v6!</h2>} />
<Route path="students" element={<h2>Student Directory Page</h2>} />
<Route path="students/:id" element={<StudentDetails />} />
<Route path="*" element={<h2>404: Page Not Found</h2>} />
</Route>
</Routes>
</BrowserRouter>
);
}
Now let us learn how to render modal dialogs outside the component DOM tree in React Portals & Modals.
Accelerate your React mastery by studying these tightly connected core tutorials:
Composing nested pages with Route and Outlet.
Lazy loading page bundles on demand.
File-system based server routing ecosystem.