Fullstack CourseLearn by building
Back to week 3

Topic

Link and navigate

Definition

Next.js Link performs client-side navigation to internal routes, while useRouter exposes imperative navigation methods from a Client Component.

In simpler words

Use Link for a normal in-app destination and router methods after a user action decides where to go.

Navigation should preserve the app experience while retaining normal browser behavior for external destinations.

After this you can

  • Choose Link or useRouter
  • Link internally without a full document reload
  • Navigate after a successful user action

Choose declarative or imperative navigation

A link expresses a destination in the UI and remains accessible to keyboard and assistive-technology users. It is the default for navigation menus, breadcrumbs, and item rows.

Imperative navigation belongs in an event handler, for example after a completed create action. Because handlers run in the browser, the component using useRouter needs the use client directive.

Link and router in their roles

import Link from 'next/link';

<Link href="/course/frontend">Frontend path</Link>

'use client';
const router = useRouter();
router.push('/course/frontend/week/1');

The link describes a destination; router.push follows an event-driven decision.

Mistake: using an anchor for an internal route

// Wrong — requests a new document
<a href="/course/frontend">Frontend</a>

// Right — Next manages internal navigation
<Link href="/course/frontend">Frontend</Link>

Use a normal anchor for external URLs, downloads, and intentionally full navigations.

Navigate after state succeeds

Definition

Navigation after a write should follow a confirmed success state, not just a button click.

In simpler words

Do not send a learner to a detail page for a ticket Nest rejected.

Use router.replace when the old history entry should not return users to a completed sign-in or setup step.

Preserve query parameters deliberately when filters or pagination are part of the destination state.

Live playground

Linking and navigating playground

Compare a Link destination with router.push after an event.

Prefetch + client transition for internal routes.

Keep in mind

  • Prefer Link for visible internal destinations.
  • Use useRouter only in a Client Component and usually inside an event handler.
  • Use anchors for external destinations.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥80% (enforced by the API) to save progress.

Checking your session…

15 questions · concept 5 · syntax 3 · practical 4 · logic 3

1. Client navigation component?
Concept
2. Programmatic navigation in client components?
Syntax
3. Why Link over raw anchor for internal routes?
Practical
4. Dynamic segment href?
Logic
5. replace vs push?
Concept
6. External URLs?
Practical
7. Active link styling?
Syntax
8. Prefetch default behavior?
Logic
9. Redirect in Server Component/route?
Concept
10. Which navigation anti-pattern?
Practical
11. Why prefer Link over a raw anchor for internal routes?
Conceptintermediate
import Link from 'next/link';
<Link href="/tickets">Tickets</Link>
12. When is this navigation approach appropriate?
Syntaxintermediate
'use client';
import { useRouter } from 'next/navigation';
const router = useRouter();
router.push('/tickets');
13. What does this build?
Practicalintermediate
<Link href={'/tickets/' + id}>Open</Link>
14. How does replace differ from push?
Logicadvanced
router.replace('/login');
15. Why is this an anti-pattern for internal navigation?
Conceptadvanced
onClick={() => { window.location.href = '/tickets'; }}

Checking your session…