LibraryLinking and navigation with `next/link` and `next/navigation`

Linking and navigation with `next/link` and `next/navigation`

Learn about Linking and navigation with `next/link` and `next/navigation` as part of Next.js 14 Full-Stack Development

Navigating Your Next.js App: Linking and Routing

In Next.js, efficient navigation is key to a smooth user experience. This module dives into the core components for handling links and routing within your application, focusing on the modern

code
next/navigation
and the foundational
code
next/link
.

code
next/link
is the primary component for declarative navigation in Next.js. It allows you to create links to different routes within your application, ensuring optimized performance and accessibility.

`next/link` enables client-side navigation for faster page transitions.

The <Link> component from next/link wraps your anchor (<a>) tags. It handles prefetching of linked pages in the background, so when a user clicks, the transition is instantaneous. This is a significant performance boost over traditional browser navigation.

When you use <Link href="/about">About Us</Link>, Next.js automatically intercepts the click event. Instead of a full page reload, it fetches the necessary data and renders the new page client-side. This creates a seamless, app-like feel. It also automatically adds the <a> tag for you, so you don't need to include it explicitly unless you need to pass specific attributes to it.

What is the primary benefit of using next/link for navigation?

Client-side navigation and prefetching for faster page transitions.

Introducing `next/navigation`

The

code
next/navigation
module provides imperative navigation functions, allowing you to programmatically redirect users or trigger navigation actions based on certain events, such as form submissions or button clicks.

Imperative navigation functions like `useRouter` and `usePathname` offer programmatic control.

The useRouter hook from next/navigation gives you access to methods like push() and replace(). push() adds a new entry to the browser's history stack, while replace() replaces the current history entry. This is useful for redirects after actions.

In the App Router, you'll primarily use hooks like useRouter and usePathname within your React Server Components (RSCs) or Client Components. For example, to redirect a user after a successful login, you might use router.push('/dashboard'). The usePathname hook is useful for conditionally styling active links based on the current URL.

Featurenext/linknext/navigation (e.g., useRouter)
Usage TypeDeclarative (Component-based)Imperative (Function-based)
Primary Use CaseCreating navigable links in UIProgrammatic redirects, conditional navigation
Client-SideYes (Prefetching, seamless transitions)Yes (Programmatic control)
Browser HistoryAdds new entry by defaultpush() adds, replace() replaces

App Router Specifics

With the introduction of the App Router in Next.js 13 and beyond, the way you handle navigation has evolved. While

code
next/link
remains largely the same, the imperative navigation functions are now part of
code
next/navigation
.

In the App Router, next/link is used for creating links. For programmatic navigation, you'll import hooks like useRouter and usePathname from next/navigation. The useRouter hook provides methods like push() to navigate to a new URL and replace() to navigate without adding to the history stack. This allows for dynamic routing based on user actions or application state.

📚

Text-based content

Library pages focus on text content

Remember to import Link from next/link and useRouter, usePathname from next/navigation.

Practical Examples

Let's look at how these are used in practice.

Declarative Navigation with

code
next/link
:

jsx
import Link from 'next/link';
function Navigation() {
return (
Home
About
Contact
);
}
export default Navigation;

Imperative Navigation with

code
useRouter
:

jsx
'use client'; // Required for Client Components
import { useRouter } from 'next/navigation';
function SubmitButton() {
const router = useRouter();
const handleSubmit = () => {
// ... perform some action ...
router.push('/success'); // Navigate to success page
};
return ;
}
export default SubmitButton;
When would you use router.replace('/dashboard') instead of router.push('/dashboard')?

When you don't want the previous page to be accessible via the back button, such as after a login or form submission where going back would be illogical.

Learning Resources

Next.js Link Component Documentation(documentation)

Official documentation detailing the `next/link` component, its props, and best practices for creating links in Next.js applications.

Next.js Navigation Documentation(documentation)

Comprehensive guide to the `next/navigation` module, covering hooks like `useRouter`, `usePathname`, and `useSearchParams` for programmatic navigation.

Next.js Routing: App Router vs. Pages Router(video)

A video explaining the differences and nuances of routing between the App Router and the older Pages Router in Next.js.

Mastering Next.js Navigation(video)

A tutorial video demonstrating practical examples of using `next/link` and `next/navigation` for effective routing in Next.js.

Next.js App Router: A Deep Dive(blog)

An in-depth blog post exploring the features of the Next.js App Router, including its impact on navigation and routing strategies.

Understanding Next.js Routing with the App Router(blog)

A beginner-friendly guide to understanding and implementing routing within the Next.js App Router.

Next.js Navigation Patterns(tutorial)

A course module that covers various navigation patterns and best practices in Next.js development.

React Router vs. Next.js Router(documentation)

While not directly Next.js, understanding React Router's concepts can provide context for Next.js's routing mechanisms.

Next.js Official Examples: Routing(documentation)

Official Next.js examples showcasing how to implement routing and navigation within the App Router structure.

The Evolution of Next.js Routing(blog)

An article discussing the evolution of Next.js routing, highlighting the benefits and changes introduced with the App Router.