Loading...

Difference between router in next/navigation and next/router in Next.js?

question nextjs front-end
Ram Patra Published on December 21, 2024

In Next.js, there are two main ways to handle routing programmatically:

1. next/router

  • This is the older routing API used in Next.js.
  • It relies on the useRouter hook or Router object for client-side navigation.
  • Commonly used in Next.js versions before the introduction of the App Router.

Key Features:

  • Primarily designed for pages directory-based routing.
  • Works well in Next.js projects with the Pages Router (the default routing system in versions before Next.js 13).
  • Includes methods for programmatic navigation (push, replace, etc.).

Example:

import { useRouter } from 'next/router';

export default function MyComponent() {
  const router = useRouter();

  const handleNavigation = () => {
    router.push('/about'); // Navigate to /about
  };

  return <button onClick={handleNavigation}>Go to About</button>;
}

Common Use Cases:

  • Navigating between pages in the Pages Router system.
  • Accessing router query parameters (router.query).

2. next/navigation

  • This is the newer routing API introduced with the App Router in Next.js 13.
  • Designed to work with the new file-based App Router system.
  • Focuses on a more declarative and streamlined approach to routing.

Key Features:

  • Provides hooks and utilities like useRouter (similar to next/router) but tailored for the App Router.
  • Includes new utilities like usePathname, useSearchParams, and redirect for modern app routing needs.
  • Works seamlessly with React Server Components (RSC).

Example:

'use client'; // Required for client-side logic

import { useRouter } from 'next/navigation';

export default function MyComponent() {
  const router = useRouter();

  const handleNavigation = () => {
    router.push('/about'); // Navigate to /about
  };

  return <button onClick={handleNavigation}>Go to About</button>;
}

Common Use Cases:

  • Navigating in App Router-based projects.
  • Accessing and manipulating URL paths or search parameters (useSearchParams, usePathname).

Key Differences:

Feature next/router next/navigation
Designed For Pages Router App Router
Client/Server Support Client-side only Tailored for both client and server
Routing Hooks useRouter, Router object useRouter, usePathname, etc.
Support for RSC Not designed for React Server Components Fully compatible with React Server Components
URL Handling Query-based (router.query) Pathname and search parameter-based

When to Use Which?

  • If you’re using the Pages Router: Use next/router.
  • If you’re using the App Router: Use next/navigation.

For new projects, especially those starting with Next.js 13 or later, it’s recommended to use the App Router and the next/navigation API for a more modern, flexible approach to routing.

Take your presentation to the next level.

Put your face and name on your screen.

Your to-dos on your menu bar.

Fill forms using your right-click menu.

Ram Patra Published on December 21, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question nextjs react February 9, 2024 How to make an HTML button element behave like a Link component in Next.js?

In Next.js, you can use the Link component from next/link to create client-side navigation. However, if you want to use an HTML button element (<button>) to behave like a link, you can wrap it with the Link component. Here’s how you can do it:

question nextjs cloudflare March 19, 2025 How to run a website built with Next.js 15 on Cloudflare Pages?

If you simply connect a website’s code on GitHub with Cloudflare Pages that’s built using Next.js 15+, it may not build successfully out of the box. If this is the case with you, make sure to do the following.

question typescript react September 28, 2024 How to add a custom element to a Next.js/Typescript project?

Let’s say I have a custom element setapp-badge that I want to use in my tsx files. If I go ahead and use it, the compiler will throw an error and Next.js will fail to build. It seems the problem might be a combination of how Next.js, TypeScript, and custom elements work together. Therefore, let’s try an approach that avoids the namespace/module issues while ensuring custom elements are recognized in a Next.js/TypeScript project.

Like my work?

Please, feel free to reach out. I would be more than happy to chat.