Skip to content
codeaihub.in
Menu
  • Understanding script type=”speculationrules”
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

  • Valentineโ€™s Day 2026: Love, Laughter & Last-Minute Plans!
  • India vs Pakistan T20 Match Preview: Rivalry Renewed Tomorrow
  • Reactive Programming in Angular Explained (With Angular Signals + RxJS)
  • TypeScript Learning Guide: Examples, Tips, and Best Practices
  • AVIF Image Format: The Next-Gen Image Standard for the Web

Recent Comments

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