I'm using the experimental app
folder in Next.js 13, where they have replaced next/router
with next/navigation
, so I imported the useRouter
hook accordingly. I do not see the property pathname
in the router, which does exist in next/router
's router.
Property 'pathname' does not exist on type 'AppRouterInstance'
My use case is to highlight the link in my navbar that the user is currently on. This is what I tried:
import Link from "next/link";
import { useRouter } from 'next/navigation';
const StyledNavLink: React.FC<NavLinkProps> = ({ to, children }) => {
const router = useRouter();
...
return (
<Link href={to} className={getNavLinkClass({isActive: router.pathname === to})}>
{children}
</Link>
);
};
Is there anything I can do to get the current path name or something else to add my classes to the active link?