What is React Router?
React Router is a client-side routing library that allows navigation between different UI views without reloading the page. It keeps the UI in sync with the browser URL and enables Single Page Application (SPA) behavior.
Instead of requesting new HTML pages from the server, React Router swaps components dynamically.
Why Do We Need React Router?
- Fast navigation without page reload
- Better user experience
- Clean and shareable URLs
- Supports nested and protected routes
Almost all real-world applications like dashboards, portals, and tools similar to multi-step form platforms rely on client-side routing.
Installing React Router
React Router v6 is the latest stable version.
npm install react-router-dom
Basic React Router Setup
Wrap Your App with BrowserRouter
import { BrowserRouter } from "react-router-dom";
import App from "./App";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<BrowserRouter>
<App />
</BrowserRouter>
);
BrowserRouter listens to URL changes and renders the matching component.
Defining Routes
import { Routes, Route } from "react-router-dom";
import Home from "./Home";
import About from "./About";
function App() {
return (
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
);
}
Each Route maps a URL path to a React component.
Navigation Using Link
import { Link } from "react-router-dom";
function Navbar() {
return (
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
</nav>
);
}
Unlike anchor tags, Link prevents full page reloads.
Nested Routes
<Route path="/dashboard" element={<Dashboard />}>
<Route path="profile" element={<Profile />} />
<Route path="settings" element={<Settings />} />
</Route>
import { Outlet } from "react-router-dom";
function Dashboard() {
return (
<div>
<h2>Dashboard</h2>
<Outlet />
</div>
);
}
Nested routes are commonly used in structured flows like step-based user journeys.
Protected Routes (Authentication)
import { Navigate } from "react-router-dom";
function ProtectedRoute({ isAuth, children }) {
if (!isAuth) {
return <Navigate to="/login" />;
}
return children;
}
<Route
path="/dashboard"
element={
<ProtectedRoute isAuth={true}>
<Dashboard />
</ProtectedRoute>
}
/>
This pattern is widely used in secure applications and admin dashboards.
Dynamic Routes (URL Params)
<Route path="/user/:id" element={<User />} />
import { useParams } from "react-router-dom";
function User() {
const { id } = useParams();
return <p>User ID: {id}</p>;
}
Programmatic Navigation
import { useNavigate } from "react-router-dom";
function Login() {
const navigate = useNavigate();
function handleLogin() {
navigate("/dashboard");
}
return <button onClick={handleLogin}>Login</button>;
}
404 Page (Fallback Route)
<Route path="*" element={<NotFound />} />
Always include a fallback route for better UX.
Common Mistakes
- Forgetting to wrap app with BrowserRouter
- Using <a> instead of Link
- Overusing global route guards
- Mixing v5 and v6 syntax
Many of these mistakes are highlighted in frontend best-practice articles.
Best Practices & Special Notes
- Keep routes centralized
- Lazy load route components
- Group routes by feature
- Use loaders for data fetching (v6.4+)
To test routing knowledge and edge cases, scenario-based challenges on interactive assessments are highly effective.
Final Takeaway
React Router is the backbone of navigation in React applications. A clean and well-structured router setup improves performance, security, and scalability. Mastering React Router is essential for building real-world, production-ready React apps.