Building Dynamic Movie Detail Pages With React Router
Movie detail pages use dynamic routing. Here is how to build them with React Router.
Building Dynamic Movie Detail Pages With React Router
Movie detail pages use dynamic routing so one component handles any movie. Here is how to build them with React Router.
Define the Dynamic Route
Create a route like /movie/:id where :id is a parameter. The same detail component renders for any movie id.
Read the ID With useParams
Inside the detail component, use useParams to read the id. This id is what you pass to the movie API to fetch the right movie.
Fetch in useEffect
In useEffect with the id as a dependency, fetch the movie by id. Including the id as a dependency means the fetch re-runs when you navigate between movies.
Handle Loading and Error
Show a spinner while loading, an error message on failure, and the movie details on success. Always handle all three states so the page never looks broken.
Render the Details
Show the backdrop, title, overview, cast, genres, and a trailer if available. Render related movies at the bottom, fetched from the same or a different endpoint.
Navigate With Link or useNavigate
Use Link to navigate from a movie card to its detail page, building the URL with the movie id. For programmatic navigation, use useNavigate.
The Common Mistake
Forgetting to add the id to the useEffect dependency array, so navigating between two movies does not refetch and shows stale data. Always include the id.
The Takeaway
Dynamic movie pages use a /movie/:id route, useParams to read the id, a fetch in useEffect with the id as a dependency, full loading and error handling, and Link or useNavigate for navigation. Watch the dependency array.
Define a dynamic route like /movie/:id, read the id with useParams, fetch the movie in useEffect with the id as a dependency, handle loading and error, render the details, and navigate with Link or useNavigate building the URL with the id.
Because you forgot to add the id to the useEffect dependency array, so the fetch does not re-run when navigating between movies. Always include the id so the detail refetches on every navigation.
Use the useParams hook inside the component. It returns an object with the parameter name and its value from the URL, which you pass to your fetch call to load the right movie.
Use Link with a dynamic path, like <Link to={`/movie/${movie.id}`}>, or useNavigate for programmatic navigation. Build the URL with the movie id so the route matches the dynamic /movie/:id pattern.
Loading state with a spinner, error state with a message and retry, and success state with the details like backdrop, title, overview, cast, genres, and a trailer. Always handle all three states so the page never looks broken.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

