What if your app could switch pages instantly without you writing complex URL checks?
Why Defining routes in React? - Purpose & Use Cases
Imagine building a website where you manually show and hide different page content by writing lots of if-else checks for URLs.
Every time a user clicks a link, you have to write code to detect the URL and then update the page content yourself.
This manual way is slow and messy. You must write repetitive code for each page, and it's easy to make mistakes.
It's hard to keep track of all the URLs and what content to show, especially as your site grows.
Defining routes lets you map URLs to components in a clear, organized way.
React routing libraries automatically handle showing the right page when the URL changes, so you don't have to write that logic yourself.
if (window.location.pathname === '/home') { showHome(); } else if (window.location.pathname === '/about') { showAbout(); }
<Route path='/home' element={<Home />} /> <Route path='/about' element={<About />} />
You can build multi-page apps that feel smooth and fast, with clean code that's easy to manage and update.
Think of a shopping website where clicking 'Products' or 'Cart' changes the page without reloading the whole site, thanks to defined routes.
Manual URL checks are hard to maintain and error-prone.
Defining routes maps URLs to components automatically.
This makes building and scaling web apps easier and cleaner.
Practice
Solution
Step 1: Understand routing setup
React Router requires a top-level component to manage routing context.Step 2: Identify the wrapper component
BrowserRouterwraps the app to enable routing features.Final Answer:
<BrowserRouter> -> Option CQuick Check:
Routing wrapper = BrowserRouter [OK]
- Using <Routes> as the top wrapper
- Confusing <Link> with the router wrapper
- Using <Route> to wrap the app
Solution
Step 1: Recall Route syntax in React Router v6+
Routes usepathfor URL andelementfor component to render.Step 2: Match correct syntax
<Route path='/home' element={<Home />} /> usespath='/home'andelement={<Home />}, which is correct.Final Answer:
<Route path='/home' element={<Home />} /> -> Option AQuick Check:
Route syntax = path + element [OK]
- Using component instead of element
- Using to instead of path
- Passing component as JSX instead of element prop
import { BrowserRouter, Routes, Route } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<Routes>
<Route path='/' element={Home
} />
<Route path='/about' element={About Us
} />
<Route path='/contact' element={Contact
} />
</Routes>
</BrowserRouter>
);
}Solution
Step 1: Match URL path to Route path
The URL '/about' matches the Route withpath='/about'.Step 2: Identify rendered element
The matched Route renders<h1>About Us</h1>.Final Answer:
<h1>About Us</h1> -> Option DQuick Check:
URL '/about' renders About Us [OK]
- Confusing '/' with '/about'
- Assuming default route renders for all paths
- Ignoring exact path matching
<Route path='/profile' component={} />Solution
Step 1: Recall correct Route props in React Router v6+
Routes useelementprop to pass JSX, notcomponent.Step 2: Identify the error
Usingcomponentcauses the route not to render correctly.Final Answer:
The prop 'component' should be 'element' with JSX -> Option BQuick Check:
Use element, not component, for Route JSX [OK]
- Using component prop from older React Router versions
- Passing component as string
- Omitting element or component prop
Solution
Step 1: Understand nested route path rules
Nested routes use relative paths inside parent Route; child path 'settings' appends to '/dashboard'.Step 2: Check each option for correct nesting
<Routes> <Route path='/dashboard' element={<Dashboard />}> <Route path='settings' element={<Settings />} /> </Route> </Routes> correctly nestssettingsas child path without repeating parent path.Final Answer:
<Routes> <Route path='/dashboard' element={<Dashboard />}> <Route path='settings' element={<Settings />} /> </Route> </Routes> -> Option AQuick Check:
Nested child paths are relative, no leading slash [OK]
- Using absolute paths in nested routes
- Defining nested routes as siblings
- Repeating parent path in child route
Ready to prove it?
Pass the test and this pattern moves unknown
