React Tutorial 0/120 lessons ~6 min read Lesson 51

    Navigation Components

    Navigation Components cover Link, NavLink, useNavigate, replace history, query strings with useSearchParams, and accessible navigation patterns.

    Course progress0%
    Focus
    11 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Course: React — Routing & API Integration Lesson: 06

    Learning objectives

    Use Link, NavLink, and useNavigate appropriately; build accessible navigation; and preserve expected browser behavior.

    Use Link for navigation to another route in the same React application:

    javascript
    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 is designed for navigation items that need an active state:

    javascript
    <NavLink
    to="/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:

    javascript
    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 useNavigate for 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

    2
    1. 1Difference between Link and NavLink?
      Beginner

      Model answer

      Link performs in-app navigation. NavLink adds active-state awareness for styling and accessibility in menus, sidebars, and route-backed tabs.
    2. 2How would you make filters bookmarkable?
      Beginner

      Model answer

      Store filters, sort, and pagination in the query string with useSearchParams so the URL can be shared and restored.

    Intermediate

    2
    1. 3When should useNavigate be used?
      Intermediate

      Model answer

      Use it for event-driven transitions such as after a successful form submit—not as a replacement for ordinary declarative links.

    2. 4Why should navigation use semantic elements?
      Intermediate

      Model answer

      Semantic nav, links, and buttons preserve keyboard access, focus, and meaning for assistive tech—clickable divs do not.

    Advanced

    1
    1. 5What does replace: true do?
      Advanced

      Model 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.

    Ready to mark this lesson complete?Track your journey across the entire course.