Next.js Path Extractor: Custom Hook for URL Paths & Query Parameters

Introducing usePathExtractor – a custom React hook for Next.js that simplifies extracting and managing URL paths, query parameters, and active route states. It helps developers efficiently work with dynamic routing, making navigation handling more intu…


This content originally appeared on DEV Community and was authored by JAKER HOSSAIN

Introducing usePathExtractor – a custom React hook for Next.js that simplifies extracting and managing URL paths, query parameters, and active route states. It helps developers efficiently work with dynamic routing, making navigation handling more intuitive.

"use client";

import { usePathname, useSearchParams } from "next/navigation";
import { useMemo } from "react";

export const usePathExtractor = () => {
  const pathname = usePathname();
  const searchParams = useSearchParams();

  const queryParams = useMemo(() => {
    const params: Record<string, string | string[]> = {};
    searchParams.forEach((value, key) => {
      if (params[key]) {
        params[key] = Array.isArray(params[key])
          ? [...params[key], value]
          : [params[key], value];
      } else {
        params[key] = value;
      }
    });
    return params;
  }, [searchParams]);

  const fullUrl = useMemo(() => {
    if (typeof window !== "undefined") {
      return window.location.href;
    }
    return "";
  }, []);

  const basePath = useMemo(() => {
    return pathname.split("/").slice(0, 2).join("/");
  }, [pathname]);

  const isActiveRoute = (route: string) => {
    return pathname.startsWith(route);
  };

  return {
    pathname,
    fullUrl,
    basePath,
    queryParams,
    isActiveRoute,
  };
};

Benefits:
✅ Effortless Query Parameter Extraction – Converts search params into an easily accessible object.
✅ Retrieve Full URL – Get the complete URL directly from the hook.
✅ Base Path Management – Extract the main route without extra logic.
✅ Active Route Detection – Easily check if a route is currently active.

Perfect for building dynamic dashboards, filtering systems, and improving navigation handling in Next.js projects! 🚀


This content originally appeared on DEV Community and was authored by JAKER HOSSAIN


Print Share Comment Cite Upload Translate Updates
APA

JAKER HOSSAIN | Sciencx (2025-02-05T04:36:08+00:00) Next.js Path Extractor: Custom Hook for URL Paths & Query Parameters. Retrieved from https://www.scien.cx/2025/02/05/next-js-path-extractor-custom-hook-for-url-paths-query-parameters/

MLA
" » Next.js Path Extractor: Custom Hook for URL Paths & Query Parameters." JAKER HOSSAIN | Sciencx - Wednesday February 5, 2025, https://www.scien.cx/2025/02/05/next-js-path-extractor-custom-hook-for-url-paths-query-parameters/
HARVARD
JAKER HOSSAIN | Sciencx Wednesday February 5, 2025 » Next.js Path Extractor: Custom Hook for URL Paths & Query Parameters., viewed ,<https://www.scien.cx/2025/02/05/next-js-path-extractor-custom-hook-for-url-paths-query-parameters/>
VANCOUVER
JAKER HOSSAIN | Sciencx - » Next.js Path Extractor: Custom Hook for URL Paths & Query Parameters. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/05/next-js-path-extractor-custom-hook-for-url-paths-query-parameters/
CHICAGO
" » Next.js Path Extractor: Custom Hook for URL Paths & Query Parameters." JAKER HOSSAIN | Sciencx - Accessed . https://www.scien.cx/2025/02/05/next-js-path-extractor-custom-hook-for-url-paths-query-parameters/
IEEE
" » Next.js Path Extractor: Custom Hook for URL Paths & Query Parameters." JAKER HOSSAIN | Sciencx [Online]. Available: https://www.scien.cx/2025/02/05/next-js-path-extractor-custom-hook-for-url-paths-query-parameters/. [Accessed: ]
rf:citation
» Next.js Path Extractor: Custom Hook for URL Paths & Query Parameters | JAKER HOSSAIN | Sciencx | https://www.scien.cx/2025/02/05/next-js-path-extractor-custom-hook-for-url-paths-query-parameters/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.