Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 39 ยท Routes and Nested Layouts

Links, Navigation and Meta

Navigate in React Router v7 framework mode with Link and NavLink, set page titles with the meta export, add stylesheets with links, and show pending navigation state.

Once an app has several routes, users need ways to move between them, and search engines need a title and description for every page. React Router gives you components for navigation (Link, NavLink, Form) and two route module exports for the document head (meta and links).

All code in this lesson is read-only because the framework cannot run on this site. Each idea comes with a runnable stand-in written in plain JavaScript so you can see the logic.

<Link> renders a normal <a href>, so it works before JavaScript loads, can be opened in a new tab, and is crawlable. Once the app has hydrated, a click is intercepted and the router changes the URL and loads data without a full page reload.

TSX
import { Link } from "react-router";

export default function Nav() {
  return (
    <nav>
      <Link to="/">Home</Link>
      <Link to="/products">Products</Link>
      <Link to="/products/42?tab=reviews">Reviews</Link>
      <Link to="..">Up one level</Link>
      <Link to="/search" replace>Replace history entry</Link>
    </nav>
  );
}

Relative links (to="edit", to="..") resolve against the route hierarchy, not the URL, so they keep working when you move a route. Use relative="path" if you want URL-based resolution instead.

NavLink is a Link that knows whether it matches the current location. It adds an active class and aria-current="page" automatically, and className/style/children can be functions of its state.

TSX
import { NavLink } from "react-router";

<NavLink
  to="/messages"
  className={({ isActive, isPending }) =>
    isPending ? "pending" : isActive ? "active" : ""
  }
>
  {({ isPending }) => <span>Messages {isPending ? "..." : ""}</span>}
</NavLink>
  • isActive: the link's URL matches the current URL (a parent link stays active for child URLs unless you add the end prop).
  • isPending: a navigation to this link is in flight, which is perfect for a small spinner.
JS Stand-in: how NavLink decides it is active

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that / without end matches everything. That is why the home link usually gets the end prop.

Programmatic navigation

Prefer links and forms because they work without JavaScript. When you need code to navigate (after a timer, after a non-form event), use the hook:

TSX
import { useNavigate } from "react-router";

function Countdown() {
  const navigate = useNavigate();
  // ...when finished:
  // navigate("/done", { replace: true });
  return null;
}

Inside loaders and actions, return redirect("/path") instead (see Actions).

Showing a pending state

useNavigation() reports the current navigation: state is "idle", "loading" or "submitting", and location is where the user is headed.

TSX
import { Outlet, useNavigation } from "react-router";

export default function Layout() {
  const navigation = useNavigation();
  const busy = navigation.state !== "idle";
  return (
    <div className={busy ? "dim" : ""}>
      {busy && <p role="status">Loading...</p>}
      <Outlet />
    </div>
  );
}

The meta export

Every route module can export meta. It returns an array of descriptors that become tags in <head>. It receives the loader data, route params, and the matches of parent routes.

TSX
import type { Route } from "./+types/product";

export function meta({ data, params }: Route.MetaArgs) {
  if (!data) return [{ title: "Product not found" }];
  return [
    { title: `${data.name} | Shop` },
    { name: "description", content: data.summary },
    { property: "og:title", content: data.name },
    { tagName: "link", rel: "canonical", href: `https://example.com/products/${params.id}` },
  ];
}

The deepest matching route wins: if a child returns a title, it replaces the parent's. To merge, read matches from the args and spread the parent's tags in. The root route must render <Meta /> inside <head> for any of this to appear (see Root and Entry Files).

JS Stand-in: merging meta from the route hierarchy

Press Run (or Ctrl + Enter) to see the console output here.

No output.

links adds <link> tags, mostly stylesheets and preloads, scoped to the route: the CSS is only loaded when the route is rendered.

TSX
import type { Route } from "./+types/dashboard";
import styles from "./dashboard.css?url";

export function links(): Route.LinkDescriptors {
  return [
    { rel: "stylesheet", href: styles },
    { rel: "preload", href: "/fonts/inter.woff2", as: "font", type: "font/woff2", crossOrigin: "anonymous" },
  ];
}

In Vite-based apps you can also import CSS directly in a component file, which is simpler; links is handy when you want explicit control of preloads and ordering.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is <Link> preferred over a <button onClick={navigate}> for moving between pages?

    Link renders an a element with an href. The router enhances it after hydration.
  2. 2. Which prop stops the / NavLink from being active on every page?

    end makes the match exact instead of also matching descendant URLs.
  3. 3. Two routes in the hierarchy both return a title from meta. Which one is used?

    Later (deeper) matches override earlier ones unless you merge manually.
  4. 4. What does useNavigation().state return while a loader for the next page is running?

    The states are idle, loading and submitting. A link navigation is loading; a form post is submitting.

Summary

  • Link and NavLink render real anchors and navigate client-side after hydration.
  • NavLink supplies isActive and isPending; use end for exact matches.
  • useNavigation powers global pending indicators.
  • meta builds the head tags and the deepest route wins; links adds route-scoped stylesheets and preloads.

Next: Index, Layout and Pathless Routes