What are dynamic routes and what are route parameters?
Assesses fundamental understanding of React conventions, runtime behavior, and memory/performance considerations.
Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.
A dynamic route is a route path that contains a placeholder segment (a *route parameter*) instead of a fixed, hard-coded value, so a single route definition can match many different URLs. Route parameters are the named placeholder segments (prefixed with : in React Router) whose actual values are extracted from the matched URL at runtime.
import { BrowserRouter, Routes, Route, useParams } from "react-router-dom";
function UserProfile() {
// useParams() returns an object of all dynamic segments matched for this route
const { userId } = useParams();
return <div>Profile for user: {userId}</div>;
}
function App() {
return (
<BrowserRouter>
<Routes>
{/* :userId is a route parameter — matches /users/1, /users/abc, etc. */}
<Route path="/users/:userId" element={<UserProfile />} />
</Routes>
</BrowserRouter>
);
}
A route can also define multiple parameters and an optional catch-all (splat) parameter:
// Multiple params: /posts/2024/react-hooks-guide
<Route path="/posts/:year/:slug" element={<Post />} />
// Optional segment (v6.4+): matches /shop and /shop/electronics
<Route path="/shop/:category?" element={<Shop />} />
// Splat/wildcard param: matches any depth, e.g. /docs/a/b/c
<Route path="/docs/*" element={<Docs />} />
function Post() {
const { year, slug } = useParams();
// year -> "2024", slug -> "react-hooks-guide"
return <h1>{slug} ({year})</h1>;
}
Key points:
- Route params are always returned as strings by
useParams()— convert them (e.g.,Number(id)) if you need a number. - Static routes (
/about) are matched exactly; dynamic routes (/users/:userId) match a whole family of URLs and are essential for detail/edit pages, pagination, category filters, etc. - Route params are different from query/search params (
?sort=asc), which are read withuseSearchParams()instead and are meant for optional, non-hierarchical data like filters or sorting. - Because dynamic segments can match unexpected values, always validate/guard params (e.g., check the fetched resource exists) rather than assuming the value is always well-formed.
Candidate Response Strategy & Interview Tips
- Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
- Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
- Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
- Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.