Struggling with Active Links in Next.js? This Hook Solves It!
Tired of writing complex logic to highlight active navigation links in Next.js? Say hello to useIsActivePath —a simple yet powerful custom hook that handles all edge cases for you! The Problem with Active Links in Next.js Manually checking pathname for active states gets messy: Exact paths ( /about ) Dynamic routes ( /blog/[slug] ) Relative paths ( about vs /about ) Wildcard matching ( /docs* for nested routes) Most solutions require repetitive code — but not anymore! The Solution: useIsActivePath Hook This tiny hook does all the heavy lifting: import useIsActivePath from "./useIsActivePath" ; const NavItem = ({ path, label }) => { const isActive = useIsActivePath(path); return <Link href={path} className={isActive ? "text-blue-500" : ""}>{label}</Link>; }; Key Features ✔ Exact matches ( /dashboard ) ✔ Relative paths ( dashboard → /dashboard ) ✔ Wildcard support ( /blog*...