We want to provide users a way to move between pages using navigation links. These links should be visible on all pages and clearly indicate which page is currently active. We'll use NavLink from react-router-dom to style active links and route the user correctly.
Navigation componentNavLink elements for each routeApp.jsx above all routesFile: src/Navigation.jsx
// Pseudocode:
// 1. Import NavLink from react-router-dom
// 2. Return a <nav> element containing NavLinks to each route
// 3. Use descriptive text for each navigation item
import { NavLink } from 'react-router-dom';
function Navigation() {
return (
<nav>
<NavLink to="/">Home</NavLink>
<NavLink to="/fruits/new">Enter a Fruit</NavLink>
<NavLink to="/fav-fruit">Favorite Fruit</NavLink>
<NavLink to="/set-fav-fruit">Set Favorite Fruit</NavLink>
</nav>
);
}
export default Navigation;
/fav-fruit and highlights the link/NavLink works just like an anchor tag (<a>), but it also knows when it's the active link and can automatically apply styling for it. This helps users know where they are in the app.
The <nav> element groups our links and helps with accessibility and semantic layout.
Imagine your app is like a multi-room gallery. The navigation is the map on the wall — it not only shows where the rooms are but also highlights which room you're currently standing in.
You can customize the style of active links with the className or style prop using a callback in NavLink to enhance user experience even more.