DEV Community

Cover image for ⚡️ Check Active Links & Sub-Links in NextJS
Shan Asif
Shan Asif

Posted on

2

⚡️ Check Active Links & Sub-Links in NextJS

⚡️ 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'

Check Active Links & Sub-Links in NextJS

Follow for more NextJS | ReactJS Tips & Simplified Code Snippets

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay