What is the purpose of defining routes in a React application?
Defining routes lets you decide which component to show when the user visits a specific URL. It helps create different pages or views in a single app.
Click to reveal answer
beginner
Which React library is commonly used for defining routes?
React Router is the most popular library to define routes in React apps. It helps manage navigation and URL changes easily.
Click to reveal answer
beginner
What does the <Route> component do in React Router?
The <Route> component tells React Router which component to show when the URL matches a path you specify.
Click to reveal answer
beginner
How do you define a route for the home page '/' in React Router?
You use <Route path='/' element={<Home />} /> inside a <Routes> component to show the Home component at the root URL.
Click to reveal answer
intermediate
Why do we wrap <Route> components inside a <Routes> component?
The <Routes> component groups all routes and ensures only the best matching route is shown. It manages route matching logic.
Click to reveal answer
Which component from React Router wraps all route definitions?
A<Switch>
B<Routes>
C<Route>
D<Router>
✗ Incorrect
wraps all components and manages which one to display.
How do you specify which component to render for a route?
AUsing the 'render' prop
BUsing the 'component' prop
CUsing the 'element' prop
DUsing the 'view' prop
✗ Incorrect
In React Router v6+, use the 'element' prop with JSX to specify the component.
What happens if no route matches the current URL?
AA fallback or 'not found' route can be shown
BThe first route is shown
CNothing is rendered
DThe app crashes
✗ Incorrect
You can define a fallback route with path='*' to show a 'not found' page.
Which hook lets you navigate programmatically in React Router?
AuseNavigate
BuseRoute
CuseHistory
DuseLocation
✗ Incorrect
useNavigate returns a function to change routes in code.
What is the correct way to import routing components in React Router v6?
Aimport { BrowserRouter, Switch, Route } from 'react-router-dom';
Bimport { Router, Switch, Route } from 'react-router-dom';
Cimport { Router, Routes, Route } from 'react-router-dom';
Dimport { BrowserRouter, Routes, Route } from 'react-router-dom';
✗ Incorrect
React Router v6 uses BrowserRouter, Routes, and Route components.
Explain how to define a route for a 'Profile' page in React Router.
Think about wrapping routes and specifying the path and component.
You got /4 concepts.
Describe why we use the component and how it affects route matching.
Consider how React Router decides which page to show.
You got /4 concepts.
Practice
(1/5)
1. Which component is used to wrap your entire React app to enable routing?
easy
A. Link
B. Route
C. BrowserRouter
D. Routes
Solution
Step 1: Understand routing setup
React Router requires a top-level component to manage routing context.
Step 2: Identify the wrapper component
BrowserRouter wraps the app to enable routing features.
Final Answer:
<BrowserRouter> -> Option C
Quick Check:
Routing wrapper = BrowserRouter [OK]
Hint: Remember: BrowserRouter wraps the whole app for routing [OK]
Common Mistakes:
Using <Routes> as the top wrapper
Confusing <Link> with the router wrapper
Using <Route> to wrap the app
2. Which of the following is the correct syntax to define a route for path '/home' rendering component?
easy
A. } />
B.
C. } />
D. } />
Solution
Step 1: Recall Route syntax in React Router v6+
Routes use path for URL and element for component to render.
Step 2: Match correct syntax
<Route path='/home' element={<Home />} /> uses path='/home' and element={<Home />}, which is correct.
Final Answer:
<Route path='/home' element={<Home />} /> -> Option A
Quick Check:
Route syntax = path + element [OK]
Hint: Use path for URL and element for component in Route [OK]
Common Mistakes:
Using component instead of element
Using to instead of path
Passing component as JSX instead of element prop
3. Given this code snippet, what will be rendered when the URL path is '/about'?
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>
);
}
medium
A.
Home
B. Nothing renders
C.
Contact
D.
About Us
Solution
Step 1: Match URL path to Route path
The URL '/about' matches the Route with path='/about'.
Step 2: Identify rendered element
The matched Route renders <h1>About Us</h1>.
Final Answer:
<h1>About Us</h1> -> Option D
Quick Check:
URL '/about' renders About Us [OK]
Hint: Match URL path exactly to Route path to find output [OK]
Common Mistakes:
Confusing '/' with '/about'
Assuming default route renders for all paths
Ignoring exact path matching
4. What is wrong with this route definition?
<Route path='/profile' component={} />
medium
A. The path should be '/profiles' instead of '/profile'
B. The prop 'component' should be 'element' with JSX
C. The component should be passed as a string, not JSX
D. Route cannot have a path prop
Solution
Step 1: Recall correct Route props in React Router v6+
Routes use element prop to pass JSX, not component.
Step 2: Identify the error
Using component causes the route not to render correctly.
Final Answer:
The prop 'component' should be 'element' with JSX -> Option B
Quick Check:
Use element, not component, for Route JSX [OK]
Hint: Use element prop with JSX in Route, not component [OK]
Common Mistakes:
Using component prop from older React Router versions
Passing component as string
Omitting element or component prop
5. You want to create nested routes where '/dashboard' shows a component and '/dashboard/settings' shows inside it. Which is the correct way to define these routes?
hard
A.
}>
} />
B.
} />
} />
C.
}>
} />
D.
}>
} />
Solution
Step 1: Understand nested route path rules
Nested routes use relative paths inside parent Route; child path 'settings' appends to '/dashboard'.