Skip to content
codeaihub.in
Menu
Menu

Using next/navigation Hooks in Next.js 13+: A Beginner-Friendly Guide

Posted on August 25, 2025 by Tech Writer

When working with Next.js 13+ (App Router), we no longer rely on the old next/router. Instead, Next.js provides a new set of navigation hooks inside the next/navigation package. These hooks make it easier to handle routing, query parameters, and pathnames in a React-friendly way.

In this blog, we’ll learn how to use the following hooks:

  • useRouter
  • useParams
  • useSearchParams
  • usePathname

πŸ“Œ Importing Navigation Hooks

You start by importing the hooks from next/navigation:

'use client';
import {
  useRouter,
  useParams,
  useSearchParams,
  usePathname,
} from 'next/navigation';

πŸ‘‰ Notice the 'use client' directive β€” these hooks only work in client components, not server components.


πŸ“Œ Example: Using Navigation Hooks in a Component

Here’s a simple component that demonstrates how to use all four hooks:

const PropertyPage = () => {
  const router = useRouter();
  const searchParams = useSearchParams();
  const id = useParams();
  const name = searchParams.get('name');
  const pathname = usePathname();

  return (
    <div>
      <h1>Property Page</h1>
      <p>Property ID: {id?.propertyId}</p>
      <p>Name: {name}</p>
      <p>Current Path: {pathname}</p>
      <button onClick={() => router.push('/')}>Go Home</button>
    </div>
  );
};

πŸ”Ž Hook-by-Hook Explanation

1. useRouter()

  • Works similar to next/router from older versions.
  • Lets you navigate programmatically (push, replace, back).
  • Example: router.push('/dashboard');

2. useParams()

  • Returns dynamic route parameters from the URL.
  • Example: For /property/123, useParams() will return: { propertyId: "123" }

3. useSearchParams()

  • Used to get query parameters (?name=John).
  • Example: const searchParams = useSearchParams(); const name = searchParams.get('name'); // "John"

4. usePathname()

  • Returns the current URL path.
  • Example: For /property/123?name=John, it will return: "/property/123"

βœ… When to Use These Hooks

  • useRouter β†’ when you need navigation actions.
  • useParams β†’ when working with dynamic routes.
  • useSearchParams β†’ when working with query strings.
  • usePathname β†’ when you just need the current path.

🎯 Conclusion

The new Next.js App Router hooks make routing cleaner and easier to manage. Instead of juggling different APIs, you now have a simple, unified way to handle URLs, paths, and query parameters.

πŸ‘‰ Next time you build a page in Next.js 13+, try using these hooks to simplify your navigation logic.

Category: javascript, React, React Interview, web development

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • GitHub Codespaces: A Beginner-Friendly Guide
  • How to Use Tailwind CSS in a Next.js Project (Beginner-Friendly Guide)
  • Using next/navigation Hooks in Next.js 13+: A Beginner-Friendly Guide
  • Agent2Agent (A2A): A New Way for AI Helpers to Work Together
  • πŸ€–What is a Kubernetes Cluster? A Beginner-Friendly Guide for GKE Users

Recent Comments

No comments to show.
© 2025 codeaihub.in | Powered by Minimalist Blog WordPress Theme