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)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

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

Okay