Client-side routing lets your React app change pages without reloading the whole website. It makes the app feel faster and smoother.
Client-side routing concept in React
import { BrowserRouter, Routes, Route } from 'react-router-dom'; function App() { return ( <BrowserRouter> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> </Routes> </BrowserRouter> ); }
BrowserRouter wraps your app to enable routing.
Routes contains all your route definitions.
Route defines a path and which component to show.
import { BrowserRouter, Routes, Route } from 'react-router-dom'; function App() { return ( <BrowserRouter> <Routes> <Route path="/" element={<Home />} /> <Route path="/contact" element={<Contact />} /> </Routes> </BrowserRouter> ); }
:userId to show different profiles.import { BrowserRouter, Routes, Route } from 'react-router-dom'; function App() { return ( <BrowserRouter> <Routes> <Route path="/" element={<Home />} /> <Route path="/profile/:userId" element={<Profile />} /> </Routes> </BrowserRouter> ); }
Navigate.import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'; function App() { return ( <BrowserRouter> <Routes> <Route path="/" element={<Home />} /> <Route path="/old-path" element={<Navigate to="/new-path" replace />} /> <Route path="/new-path" element={<NewPage />} /> </Routes> </BrowserRouter> ); }
This React app uses client-side routing to switch between Home and About pages without reloading. The navigation links update the URL and show the correct page content instantly.
import React from 'react'; import { BrowserRouter, Routes, Route, Link } from 'react-router-dom'; function Home() { return <h2>Home Page</h2>; } function About() { return <h2>About Page</h2>; } export default function App() { return ( <BrowserRouter> <nav aria-label="Main navigation"> <ul> <li><Link to="/">Home</Link></li> <li><Link to="/about">About</Link></li> </ul> </nav> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> </Routes> </BrowserRouter> ); }
Client-side routing keeps the app fast by avoiding full page reloads.
Use semantic HTML and ARIA labels for accessible navigation.
Remember to install react-router-dom to use routing features.
Client-side routing changes views without reloading the page.
Use BrowserRouter, Routes, and Route to set up routes.
Navigation links update the URL and show the right content instantly.
Practice
Solution
Step 1: Understand client-side routing
Client-side routing allows changing the visible content without refreshing the whole page, making navigation faster and smoother.Step 2: Compare options
Options B, C, and D describe behaviors unrelated to client-side routing in React.Final Answer:
To change views without reloading the entire page -> Option CQuick Check:
Client-side routing = change views without reload [OK]
- Confusing client-side routing with server-side rendering
- Thinking navigation always reloads the page
- Assuming routing manages backend tasks
Solution
Step 1: Identify the correct package and components
The official React routing library is 'react-router-dom' and the main components are BrowserRouter, Routes, and Route.Step 2: Check each import statement
Only import { BrowserRouter, Routes, Route } from 'react-router-dom'; correctly imports BrowserRouter, Routes, and Route from 'react-router-dom'. Others use incorrect component names or wrong packages.Final Answer:
import { BrowserRouter, Routes, Route } from 'react-router-dom'; -> Option AQuick Check:
Correct import = import { BrowserRouter, Routes, Route } from 'react-router-dom'; [OK]
- Using 'react-router' instead of 'react-router-dom'
- Mixing component names like Router instead of BrowserRouter
- Forgetting to import Routes component
/about?
import { BrowserRouter, Routes, Route } from 'react-router-dom';
function App() {
return (
Home} />
About Us} />
Contact} />
);
}Solution
Step 1: Match URL path to Route path
The URL path '/about' matches the Route with path='/about'.Step 2: Identify the element rendered
The Route for '/about' renders <h1>About Us</h1>.Final Answer:
<h1>About Us</h1> -> Option BQuick Check:
Path '/about' renders About Us [OK]
- Choosing the home route for all paths
- Confusing element props with children
- Assuming multiple routes render simultaneously
import { BrowserRouter, Routes, Route } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<Route path='/' element={} />
<Routes>
<Route path='/about' element={} />
</Routes>
</BrowserRouter>
);
}Solution
Step 1: Check Route placement
All Route components must be children of a Routes component to work correctly.Step 2: Identify the misplaced Route
The Route with path='/' is outside the Routes component, which is incorrect.Final Answer:
Route components must be inside Routes component -> Option AQuick Check:
Routes wrap Route components [OK]
- Placing Route outside Routes component
- Thinking BrowserRouter cannot wrap Routes
- Confusing JSX elements with strings in element prop
Solution
Step 1: Understand navigation in React Router
The <Link> component updates the URL and renders the new route without reloading the page.Step 2: Evaluate options for SPA navigation
<a> tags with href attribute causes full page reloads. <button> with onClick calling window.location uses window.location which reloads. <NavLink> component but without BrowserRouter requires BrowserRouter to work properly.Final Answer:
<Link> component from react-router-dom -> Option DQuick Check:
Use Link for client-side navigation [OK]
- Using <a> tags causing full page reload
- Forgetting BrowserRouter wrapper for NavLink
- Using buttons with window.location causing reload
Ready to prove it?
Pass the test and this pattern moves unknown
