React Router is a standard library that enables navigation between different components in a React application while keeping the UI in sync with the URL.
It is the key to creating Single Page Applications (SPA) where the browser doesn't reload when you click a link.
The browser stays on the same index.html file, but the **content** inside changes dynamically.
| Component | Purpose |
|---|---|
BrowserRouter | The brain—wraps your whole app to enable routing. |
Routes | A container for all your possible page paths. |
Route | Maps a specific URL (path) to a Component (element). |
Link | The "React-way" to navigate (replaces <a> tag). |
Rule 1: Always wrap <App /> or your main content in <BrowserRouter>.
Rule 2: Use the to attribute for Links, not href.
Rule 3: Never use standard <a> tags for internal links—it will force a full page reload and break the state.
// ✅ Correct Link <Link to="/about">About Us</Link>
import { BrowserRouter, Routes, Route, Link } from "react-router-dom";
function App() {
return (
<BrowserRouter>
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
);
}
| Feature | Traditional Multi-Page | React SPA |
|---|---|---|
| Reload | Yes (Slow) | No (Instant) |
| Requests | Fresh HTML from Server | Client-side Component swap |
| State | Lost on refresh | Maintained across pages |
| Feel | Static Website | Dynamic Application |
Think of a professional dashboard:
Props → State → Events → Hooks → Router (Complete)
↓
Next Level: API Fetching → Redux/Context → Real World Project