⚡️ Check Active Links & Sub-Links in NextJS

Shan Asif - Oct 19 '23 - - Dev Community

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .