Now it’s time to build the page that displays information for a specific art gallery. You’ll create a GalleryView component to render the /galleries/:galleryId route. This component will use useParams to access the URL parameter and display the matching gallery’s details.
GalleryView folder with a GalleryView.jsx file<h1> and export it/galleries/:galleryId in App.jsxuseParams to get the galleryId from the routegalleries prop into GalleryViewArray.find<Navigate /> to home if not// src/components/GalleryView/GalleryView.jsx
import { useParams, Navigate } from 'react-router-dom';
function GalleryView({ galleries }) {
const { galleryId } = useParams();
const gallery = galleries.find(g => g.id === Number(galleryId));
if (!gallery) return <Navigate to="/" />;
return <h2>{gallery.name}</h2>;
}
export default GalleryView;
import GalleryView from './GalleryView';
export default GalleryView;
Import GalleryView and add the route under your layout’s children:
import GalleryView from './components/GalleryView';
{
path: 'galleries/:galleryId',
element: <GalleryView galleries={harvardArt.records} />
}
You now have a dynamic detail view for each art gallery. The route path handles matching, and useParams handles retrieval. You also included a fallback redirect with <Navigate /> for invalid routes — nice and user-friendly!
Just like choosing a room from a museum map and walking in to see its exhibits, the URL tells the app which gallery to open. If the room doesn’t exist, you’re taken back to the main entrance — the homepage.