What is React Router?
Learn what React Router is, how client-side routing works, dynamic routes, useParams and useNavigate, with code examples and common interview questions.
Expected Interview Answer
React Router is the standard client-side routing library for React that maps URL paths to components, letting a single-page app render different views and navigate between them without full page reloads.
It keeps the UI in sync with the browser URL using the History API. You declare routes (path to component mappings), and React Router swaps the matching component into the tree as the URL changes. It supports nested routes, dynamic URL parameters, programmatic navigation, redirects, and data loading, so complex apps feel like traditional multi-page sites while staying a fast SPA.
- Client-side navigation with no full-page reload
- Deep-linkable, bookmarkable URLs
- Nested and dynamic routes
- Programmatic navigation and redirects
- Working browser back/forward buttons
- Code-splitting per route
AI Mentor Explanation
React Router is like the umpire signalling which format of play the match switches to. The scoreboard URL says 'over 12', and instantly the right fielding positions and rules render on the ground. Change the over and the whole field rearranges, but the stadium never empties and reloads — only the on-field arrangement swaps, exactly how a route changes the view without reloading the page.
Step-by-Step Explanation
Step 1
Install the router
Add react-router-dom and wrap your app in a router provider such as BrowserRouter or createBrowserRouter.
Step 2
Declare routes
Map each URL path to the component that should render, using Routes and Route (or a route object array).
Step 3
Add navigation
Use Link or NavLink instead of anchor tags so clicks navigate without a full-page reload.
Step 4
Read URL params
Use useParams to read dynamic segments like /users/:id inside the rendered component.
Step 5
Navigate programmatically
Use the useNavigate hook to redirect after events such as a successful form submit or login.
What Interviewer Expects
- Understands client-side vs server-side routing
- Knows BrowserRouter, Routes, Route and Link
- Can describe dynamic route params
- Knows nested routes and Outlet
- Mentions programmatic navigation with useNavigate
Common Mistakes
- Using plain anchor tags that trigger a full reload
- Confusing Switch (old API) with the current Routes component
- Forgetting the leading slash or mismatching path patterns
- Not using Outlet to render nested route children
- Assuming routing requires a server round trip
Best Answer (HR Friendly)
“React Router is the tool that lets a React app show different pages for different web addresses without reloading the browser. It keeps the URL and the on-screen content in sync, so users can bookmark links and use the back button just like a normal website.”
Code Example
import { BrowserRouter, Routes, Route, Link, useParams } from 'react-router-dom'
function User() {
const { id } = useParams()
return <h2>User {id}</h2>
}
export default function App() {
return (
<BrowserRouter>
<nav>
<Link to="/">Home</Link>
<Link to="/users/42">User 42</Link>
</nav>
<Routes>
<Route path="/" element={<h2>Home</h2>} />
<Route path="/users/:id" element={<User />} />
</Routes>
</BrowserRouter>
)
}Follow-up Questions
- What is the difference between BrowserRouter and HashRouter?
- How do nested routes and Outlet work?
- How do you read query parameters in React Router?
- What is the difference between Link and NavLink?
- How do you protect a route so only logged-in users can see it?
MCQ Practice
1. Which component should you use for in-app navigation without a full reload?
Link uses the History API to change the URL and render the matching route without reloading the whole page, unlike a plain anchor tag.
2. Which hook reads dynamic segments like :id from the current URL?
useParams returns an object of the dynamic route parameters matched from the URL pattern.
3. Which component renders child routes inside a nested route layout?
Outlet marks where a parent route should render its matched child route element.
Flash Cards
What does React Router do? — Maps URL paths to components so a single-page app can navigate between views without full reloads.
Which element replaces anchor tags? — Link (or NavLink), which navigates via the History API instead of reloading the page.
How do you read a URL param? — Call useParams(); it returns the dynamic segments matched from the path pattern.
What renders nested route children? — The Outlet component inside the parent route's layout element.