Routing
Routing
Section titled “Routing”What is React Routing?
Section titled “What is React Routing?”In React:
👉 Routing = showing different UI based on the URL without reloading the page
Simple Example
Section titled “Simple Example”/ -> Home Page/about -> About Page/contact -> Contact Page👉 React changes components, not the whole page
Library Used
Section titled “Library Used”👉 Most common library:
React Router
Basic Setup
Section titled “Basic Setup”Step 1: Install
Section titled “Step 1: Install”npm install react-router-domStep 2: Basic Routing Example
Section titled “Step 2: Basic Routing Example”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 componentNavigation (VERY IMPORTANT)
Section titled “Navigation (VERY IMPORTANT)”Using Link
Section titled “Using Link”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
Types of Routing (Important)
Section titled “Types of Routing (Important)”1. Static Routing
Section titled “1. Static Routing”👉 Fixed routes
<Route path="/about" element={<About />} />2. Dynamic Routing (VERY IMPORTANT )
Section titled “2. Dynamic Routing (VERY IMPORTANT )”👉 URL with variable
<Route path="/user/:id" element={<User />} />Access param
Section titled “Access param”import { useParams } from "react-router-dom";
function User() { const { id } = useParams(); return <h1>User {id}</h1>;}3. Nested Routing
Section titled “3. Nested Routing”👉 Routes inside routes
Example
Section titled “Example”<Route path="/dashboard" element={<Dashboard />}> <Route path="profile" element={<Profile />} /> <Route path="settings" element={<Settings />} /></Route>Layout
Section titled “Layout”import { Outlet } from "react-router-dom";
function Dashboard() { return ( <div> <h1>Dashboard</h1> <Outlet /> </div> );}4. Protected Routes (Auth)
Section titled “4. Protected Routes (Auth)”👉 Restrict access
function ProtectedRoute({ children }) { const isAuth = true;
return isAuth ? children : <h1>Login First</h1>;}<Route path="/dashboard" element={ <ProtectedRoute> <Dashboard /> </ProtectedRoute> }/>5. Programmatic Navigation
Section titled “5. Programmatic Navigation”👉 Navigate using code
import { useNavigate } from "react-router-dom";
function App() { const navigate = useNavigate();
return ( <button onClick={() => navigate("/about")}> Go to About </button> );}6. Query Parameters
Section titled “6. Query Parameters”👉 URL like:
/products?category=electronicsAccess it
Section titled “Access it”import { useSearchParams } from "react-router-dom";
function Products() { const [params] = useSearchParams(); const category = params.get("category");
return <h1>{category}</h1>;}7. Lazy Loading (Performance)
Section titled “7. Lazy Loading (Performance)”👉 Load pages only when needed
const About = React.lazy(() => import("./About"));
<Suspense fallback={<p>Loading...</p>}> <About /></Suspense>Important Concepts
Section titled “Important Concepts”1. SPA (Single Page Application)
Section titled “1. SPA (Single Page Application)”👉 React doesn’t reload page 👉 Only updates component
2. Route Matching
Section titled “2. Route Matching”👉 React checks URL -> finds matching route
3. Layout Pattern (VERY IMPORTANT)
Section titled “3. Layout Pattern (VERY IMPORTANT)”<Route path="/" element={<Layout />}> <Route index element={<Home />} /> <Route path="about" element={<About />} /></Route>Real-World Example
Section titled “Real-World Example”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> );}Common Mistakes
Section titled “Common Mistakes”- Using
<a>instead of<Link> - Forgetting
<Outlet />in nested routes - Not handling 404 page
Final Mental Model
Section titled “Final Mental Model”URL -> Route match -> Component render -> UI updateInterview Summary
Section titled “Interview Summary”“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.”