How do you create a dynamic route like /products/:productId in React Router?

Tests React Router v6 dynamic segments and useParams. A strong answer declares path="products/:productId" on a Route, then extracts productId with useParams in the component. Red flag: using v5 props.match.params or manual URL parsing instead of the hook.
What's really being asked
This question checks whether you understand React Router v6 dynamic routing syntax and the hook-based API for reading URL parameters. At the senior level, interviewers want to see that you know the modern declarative approach rather than legacy patterns, and that you can connect route configuration to component logic cleanly.
The full answer
First, route definition using a colon prefix in the path prop, such as path="products/:productId" inside a Route component nested within Routes. Second, accessing the parameter inside the rendered component with the useParams hook, which returns an object where keys correspond to the dynamic segment names. Third, mentioning that useParams only works inside the component tree rendered by the matching route, because it relies on React context provided by the router. Fourth, optionally noting that multiple dynamic segments like path="products/:productId/reviews/:reviewId" are supported and all appear in the useParams object.
The mistakes people make
Using props.match.params from React Router v5 is the most common red flag and shows outdated knowledge. Another mistake is trying to parse window.location.pathname manually with string splitting or regex, which ignores the router's built-in abstraction. Some candidates forget the colon prefix and write path="products/productId" as a static literal, which only matches that exact string. Finally, calling useParams outside the router context, such as in a utility file not rendered by a Route, will return an empty object and indicates a misunderstanding of how the hook receives data.
What usually comes next
The interviewer might ask how to make a parameter optional, which in v6 is done with an outlet or a separate route rather than a question mark in the path. They could ask how to navigate programmatically to a dynamic route, which is answered with useNavigate and template literals. They might also ask how to validate that productId is a number before rendering, which leads to discussing loaders, route guards, or runtime validation inside the component.
A concrete example
In an e-commerce app, you would define the route as Route path="products/:productId" element={ProductDetail} inside Routes. Inside ProductDetail, you write const { productId } = useParams(). You then fetch product data in a useEffect or a route loader using that productId. If the user visits products/42, useParams returns { productId: "42" }. You should guard against undefined by checking the key exists or by using TypeScript to enforce param types.
Interview question
In React Router v6, how does a component rendered by /products/:productId correctly read the productId URL parameter?
- a.Define the route path without the colon prefix and read the value from route props
- b.Parse the value from window.location.pathname using string methods
- c.Access props.match.params.productId
- d.Call useParams inside the component and read the productId propertyCorrect
Why? this is the answer
useParams is the idiomatic v6 hook for reading dynamic segments, returning an object with keys like productId. props.match.params is the legacy v5 approach, manual parsing bypasses the router, and omitting the colon prefix creates a static route that only matches the literal string.
Just read this? Test yourself on what you have been reading.
Read the original → reactrouter.com
- #react router
- #dynamic routes
- #useparams
- #frontend routing
- #react
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles