In this phase, you will build a component named GalleryNavigation that renders links to all art galleries. It should show up on every page, and each link will route to a gallery detail view using React Router's NavLink. This is your first reusable component in the project!
components folder inside srcGalleryNavigation folder with a GalleryNavigation.jsx and optional index.jsGalleryNavigation.jsx, define and export a component that initially displays an h1 with the text "Galleries"GalleryNavigation from App.jsx at the /galleries routeharvardArt.records as a prop to the componentgalleries to render a NavLink to each gallery pageh1 and links in a <nav> elementCreate this structure:
src/
├── components/
│ └── GalleryNavigation/
│ ├── GalleryNavigation.jsx
│ └── index.js (optional)
import { NavLink } from 'react-router-dom';
function GalleryNavigation({ galleries }) {
return (
<nav>
<h1>Galleries</h1>
<NavLink to="/">Home</NavLink>
{galleries.map(gallery => (
<NavLink key={gallery.id} to={`/galleries/${gallery.id}`}>
{gallery.name}
</NavLink>
))}
</nav>
);
}
export default GalleryNavigation;
import GalleryNavigation from './GalleryNavigation';
export default GalleryNavigation;
Inside your router array:
import GalleryNavigation from './components/GalleryNavigation';
import harvardArt from './data/harvardArt';
const router = createBrowserRouter([
{
path: '/',
element: (
<>
<h2>Harvard Art Museum</h2>
<p>Look, but Don't Touch. Please select a Gallery in the navigation bar.</p>
</>
)
},
{
path: '/galleries',
element: <GalleryNavigation galleries={harvardArt.records} />
},
{
path: '*',
element: <h2>Page Not Found</h2>
}
]);
Test by visiting http://localhost:5173/galleries. You should see a nav bar listing the galleries and a Home link.
You now have a working GalleryNavigation component that dynamically lists galleries using props and maps them into NavLinks. In the next phase, you’ll make this appear on every route by introducing layout routes using Outlet.
This is like the menu of a museum tour app — no matter where the user is exploring, they should always have access to jump between rooms (galleries). You’ll soon make that happen globally using a layout component.