What is dynamic routing in React?
A dynamic route uses a parameter in the path, like /product/:id. The same component renders for any id, and you read the id with useParams to fetch the right data. This lets one component handle many similar URLs.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Dynamic Routing in React: useParams and Route Parameters
Use the useParams hook inside the component. It returns an object with the parameter name and its value from the URL, which you can then use to fetch the right data.
In the route path, use a colon followed by the parameter name, like /restaurant/:id. React Router matches any value in that position and makes it available as a parameter through useParams.
Because you forgot to add the parameter to the useEffect dependency array. Without it, the effect does not re-run when navigating between two dynamic routes, so the component shows stale data for the previous id.
Still have questions?
Browse all our FAQs or reach out to our support team
