Skip to content

Routing

In React:

👉 Routing = showing different UI based on the URL without reloading the page


/ -> Home Page
/about -> About Page
/contact -> Contact Page

👉 React changes components, not the whole page


👉 Most common library:

React Router



Terminal window
npm install react-router-dom

import { BrowserRouter, Routes, Route } from "react-router-dom";
function Home() {
return <h1>Home</h1>;
}
function About() {
return <h1>About</h1>;
}
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
);
}

URL changes -> Router matches path -> renders component


import { Link } from "react-router-dom";
<Link to="/">Home</Link>
<Link to="/about">About</Link>

👉 Prevents page reload (SPA behavior)


<a href="/about">About</a>

👉 Causes full page reload



👉 Fixed routes

<Route path="/about" element={<About />} />

👉 URL with variable

<Route path="/user/:id" element={<User />} />

import { useParams } from "react-router-dom";
function User() {
const { id } = useParams();
return <h1>User {id}</h1>;
}

👉 Routes inside 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>
<h1>Dashboard</h1>
<Outlet />
</div>
);
}

👉 Restrict access

function ProtectedRoute({ children }) {
const isAuth = true;
return isAuth ? children : <h1>Login First</h1>;
}

<Route
path="/dashboard"
element={
<ProtectedRoute>
<Dashboard />
</ProtectedRoute>
}
/>

👉 Navigate using code

import { useNavigate } from "react-router-dom";
function App() {
const navigate = useNavigate();
return (
<button onClick={() => navigate("/about")}>
Go to About
</button>
);
}

👉 URL like:

/products?category=electronics

import { useSearchParams } from "react-router-dom";
function Products() {
const [params] = useSearchParams();
const category = params.get("category");
return <h1>{category}</h1>;
}

👉 Load pages only when needed

const About = React.lazy(() => import("./About"));
<Suspense fallback={<p>Loading...</p>}>
<About />
</Suspense>


👉 React doesn’t reload page 👉 Only updates component


👉 React checks URL -> finds matching route


<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route path="about" element={<About />} />
</Route>

function Layout() {
return (
<>
<nav>
<Link to="/">Home</Link>
<Link to="/dashboard">Dashboard</Link>
</nav>
<Outlet />
</>
);
}
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<h1>Home</h1>} />
<Route path="dashboard" element={<h1>Dashboard</h1>} />
</Route>
</Routes>
</BrowserRouter>
);
}

  • Using <a> instead of <Link>
  • Forgetting <Outlet /> in nested routes
  • Not handling 404 page

URL -> Route match -> Component render -> UI update

“React Router enables navigation in a SPA by mapping URLs to components. It supports static, dynamic, nested, and protected routes, along with programmatic navigation and query handling.”