LearnFrenzy
Sign in Take a free test

Layouts and Routing · Lesson 6 of 24

Navigation in Next.js: Link, useRouter and redirect

6 min read Updated Nov 8, 2026 Saurabh Samir

Most navigation in a Next.js app happens through <Link>. For everything else – going to a page after a form is saved, redirecting logged-out users, updating filters in the URL – Next.js provides useRouter, redirect and the search-params APIs, all from next/navigation.

In this lesson

  • Link and prefetching
  • useRouter in Client Components
  • redirect on the server
  • Search params: on the server and with useSearchParams
import Link from "next/link";

<Link href="/courses">Courses</Link>
<Link href={{ pathname: "/search", query: { q: "hooks" } }}>Search hooks</Link>   {/* /search?q=hooks */}
<Link href="/terms" prefetch={false}>Terms</Link>                                   {/* don't prefetch */}
<Link href="/checkout" replace>Checkout</Link>                                      {/* replace history entry */}
<Link href="/courses#faq" scroll={false}>FAQ</Link>

In production, links that scroll into view are prefetched: Next.js loads the static part of the target page in the background, so clicking feels instant. With Partial Prefetching (on by default in new apps) only the static shell is prefetched, and request-specific parts load after the click.

useRouter: navigating in code

// app/page.tsx
import { QuizStart } from "./quiz-start";

export default function Home() {
  return (
    <main>
      <h1>React quiz</h1>
      <QuizStart />
    </main>
  );
}
// app/quiz-start.tsx
"use client";

import { useRouter } from "next/navigation";      // not "next/router" – that's the old Pages Router
import { useState } from "react";

export function QuizStart() {
  const router = useRouter();
  const [name, setName] = useState("");
  return (
    <form
      onSubmit={(e) => {
        e.preventDefault();
        router.push(`/quiz?player=${encodeURIComponent(name)}`);   // go there in code
      }}
    >
      <input aria-label="Your name" placeholder="Your name" value={name} onChange={(e) => setName(e.target.value)} />
      <button>Start quiz</button>
    </form>
  );
}
// app/quiz/page.tsx
import { Suspense } from "react";

async function Greeting({ searchParams }: { searchParams: Promise<{ player?: string }> }) {
  const { player = "guest" } = await searchParams;
  return <p>Good luck, {player}! Question 1 of 10.</p>;
}

export default function QuizPage({ searchParams }: PageProps<"/quiz">) {
  return (
    <main>
      <h1>Quiz</h1>
      <Suspense fallback={<p>Loading…</p>}>
        <Greeting searchParams={searchParams} />
      </Suspense>
    </main>
  );
}

Visiting the site

URL: /
React quiz
[Your name…] [Start quiz]

› type “Asha” into Your name
URL: /
React quiz
[Asha] [Start quiz]

› click “Start quiz”
URL: /quiz?player=Asha
Quiz
Good luck, Asha! Question 1 of 10.
MethodDoes
router.push(href)navigate and add a history entry
router.replace(href)navigate without a new history entry
router.back() / forward()history navigation
router.refresh()re-fetch the current page's server data, keeping client state
router.prefetch(href)prefetch a route you expect the user to visit

A page's searchParams prop is request data, so – like params without generateStaticParams – the part that reads it sits inside <Suspense>.

redirect on the server

In Server Components, Server Actions and Route Handlers, call redirect(). It throws internally, so nothing after it runs:

// app/old-courses/page.tsx
import { redirect } from "next/navigation";

export default function OldCourses() {
  redirect("/courses");          // temporary (307); use permanentRedirect() for 308
}
// app/courses/page.tsx
export default function Courses() {
  return <h1>All courses</h1>;
}

HTTP response and visit

GET /old-courses
→ 307 (redirect to /courses) text/html
(HTML page)

URL: /courses
All courses

For fixed, site-wide redirects (old URLs after a migration), the redirects option in next.config.ts is simpler and runs before any page code.

useSearchParams: filters on the client

// app/page.tsx
import { Suspense } from "react";
import { CourseFilter } from "./course-filter";

export default function Home() {
  return (
    <main>
      <h1>Courses</h1>
      <Suspense fallback={<p>Loading filters…</p>}>
        <CourseFilter />
      </Suspense>
    </main>
  );
}
// app/course-filter.tsx
"use client";

import { usePathname, useRouter, useSearchParams } from "next/navigation";

const all = [
  { title: "React.js", level: "beginner" },
  { title: "Next.js", level: "intermediate" },
  { title: "System Design", level: "advanced" },
];

export function CourseFilter() {
  const searchParams = useSearchParams();
  const router = useRouter();
  const pathname = usePathname();
  const level = searchParams.get("level") ?? "all";

  function setLevel(next: string) {
    const params = new URLSearchParams(searchParams);
    if (next === "all") params.delete("level");
    else params.set("level", next);
    router.replace(`${pathname}?${params}`, { scroll: false });
  }

  const shown = level === "all" ? all : all.filter((c) => c.level === level);
  return (
    <>
      <label>
        Level
        <select value={level} onChange={(e) => setLevel(e.target.value)}>
          <option value="all">All levels</option>
          <option value="beginner">Beginner</option>
          <option value="advanced">Advanced</option>
        </select>
      </label>
      <ul>{shown.map((c) => <li key={c.title}>{c.title}</li>)}</ul>
    </>
  );
}

Visiting the site

URL: /
Courses
Level [All levels ▾]
• React.js
• Next.js
• System Design

› choose “Beginner” in Level
URL: /?level=beginner
Courses
Level [Beginner ▾]
• React.js

› choose “Advanced” in Level
URL: /?level=advanced
Courses
Level [Advanced ▾]
• System Design

searchParams prop or useSearchParams?

  • Use the page's searchParams prop when the query decides what data to load on the server (pagination, database filters).
  • Use useSearchParams in a Client Component when you only filter or display data already on the client. Wrap that component in <Suspense>, as above.

Practice

Try it yourself

  1. Make a "Back to courses" button that uses router.back() on a course page.

    Show solution
    // app/back-button.tsx
    "use client";
    
    import { useRouter } from "next/navigation";
    
    export function BackButton() {
      const router = useRouter();
      return <button onClick={() => router.back()}>← Back to courses</button>;
    }
    // app/page.tsx
    import Link from "next/link";
    
    export default function Home() {
      return <Link href="/courses/react">Open React course</Link>;
    }
    // app/courses/react/page.tsx
    import { BackButton } from "../../back-button";
    
    export default function ReactCourse() {
      return (
        <main>
          <h1>React.js</h1>
          <BackButton />
        </main>
      );
    }

    Visiting the site

    URL: /
    Open React course
    
    › click “Open React course”
    URL: /courses/react
    React.js
    [← Back to courses]
    
    › click “← Back to courses”
    URL: /
    Open React course

Quick check

Which package do App Router hooks come from?

next/navigation (not next/router).

What status does redirect() send?

307 (temporary); permanentRedirect() sends 308.

What does router.refresh() do?

Re-fetches the server data for the current route while keeping client state.

Summary

  • Link navigates and prefetches; useRouter navigates from Client Components.
  • redirect() and permanentRedirect() work in Server Components, actions and route handlers.
  • Read the query with the searchParams prop on the server or useSearchParams on the client – both inside Suspense.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Search LearnFrenzy

Esc

Quick links