Facebook Pixel

Why does my dynamic route not refetch when the id changes?

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.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Dynamic Routing in React: useParams and Route Parameters

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.

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.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.