⚡️ Check Active Links & Sub-Links in NextJS
- You can use usePathname() to determine if a link is active.
- Check if the current pathname matches the href of the link OR the sub-link
Quick Notes:
usePathname() is a Client Component hook that lets you read the current URL's pathname.
It returns a string of the current URL's pathname
Example:
- /dashboard → '/dashboard'
- /dashboard?v=2 → '/dashboard'
- /blog/hello-world → '/blog/hello-world'
Follow for more NextJS | ReactJS Tips & Simplified Code Snippets