Navigation Components
Navigation Components cover Link, NavLink, useNavigate, replace history, query strings with useSearchParams, and accessible navigation patterns.
Course: React — Routing & API Integration Lesson: 06
Learning objectives
Use Link, NavLink, and useNavigate appropriately; build accessible navigation; and preserve expected browser behavior.
Link for in-app navigation
Use Link for navigation to another route in the same React application:
import { Link } from "react-router-dom";function MainMenu() {return (<nav aria-label="Main navigation"><Link to="/">Home</Link><Link to="/products">Products</Link><Link to="/orders">Orders</Link></nav>);}
A router link updates the client-side location without performing a full document navigation.
NavLink for active navigation
NavLink is designed for navigation items that need an active state:
<NavLinkto="/orders"className={({ isActive }) => isActive ? "nav-link active" : "nav-link"}>Orders</NavLink>
The active state can drive styling and accessibility attributes. Use it for sidebars, tabs represented by routes, and primary navigation.
Programmatic navigation
Use useNavigate when navigation follows an application event, such as successful form submission:
import { useNavigate } from "react-router-dom";function CreateCustomerForm() {const navigate = useNavigate();async function handleSubmit(event) {event.preventDefault();const customer = await createCustomer();navigate(`/customers/${customer.id}`);}return <form onSubmit={handleSubmit}>{/* fields */}</form>;}
Use declarative links for user-initiated navigation where possible. Do not replace every link with a click handler.
Replace and history navigation
navigate("/dashboard", { replace: true }) replaces the current history entry. navigate(-1) moves backward in history, but use it carefully: the user may have arrived from outside the application. Provide a predictable fallback when a Back action is important.
Query strings and location
Filters, sort order, and pagination can be represented in query parameters, for example /orders?status=open&page=2. useSearchParams can read and update these values. This makes filtered views bookmarkable and shareable.
Accessibility
- Use a semantic
<nav>landmark with an accessible label. - Ensure links have meaningful text.
- Preserve keyboard navigation and visible focus styles.
- Do not use a clickable
<div>as a substitute for a link. - Use buttons for actions and links for navigation.
Common mistakes
- Using
<a href>for every internal route. - Using
useNavigatefor simple static links. - Making links inaccessible with vague labels such as “Click here”.
- Assuming
navigate(-1)always returns to a safe in-app page. - Storing important filter state only in component memory when it should survive refresh or be shareable.
Best practices
Choose the component based on intent: Link for navigation, NavLink for active navigation, and useNavigate for event-driven transitions. Keep URLs meaningful and preserve accessibility.
Interview questions & answers
Interview preparation
5 questions grouped by difficulty — expand any card for a model answer and follow-up probe.
Beginner
1Difference between Link and NavLink?
BeginnerModel answer
Linkperforms in-app navigation.NavLinkadds active-state awareness for styling and accessibility in menus, sidebars, and route-backed tabs.2How would you make filters bookmarkable?
BeginnerModel answer
Store filters, sort, and pagination in the query string withuseSearchParamsso the URL can be shared and restored.
Intermediate
3When should useNavigate be used?
IntermediateModel answer
Use it for event-driven transitions such as after a successful form submit—not as a replacement for ordinary declarative links.
4Why should navigation use semantic elements?
IntermediateModel answer
Semanticnav, links, and buttons preserve keyboard access, focus, and meaning for assistive tech—clickabledivs do not.
Advanced
5What does replace: true do?
AdvancedModel answer
It replaces the current history entry instead of pushing a new one, so Back does not return to the replaced location (useful after login).
Practice
Build a responsive SaaS sidebar with active links for Dashboard, Customers, Orders, and Settings. Add a form that navigates to a newly created customer's detail route.