Active link detection helps users know which page they are on by highlighting the current link in navigation. It improves website usability and navigation clarity.
Active link detection in NextJS
'use client'; import { usePathname } from 'next/navigation'; import Link from 'next/link'; function NavLink({ href, children }) { const pathname = usePathname(); const isActive = pathname === href; return ( <Link href={href} className={isActive ? 'active' : ''} aria-current={isActive ? 'page' : undefined} > {children} </Link> ); }
Use usePathname() from next/navigation to get the current URL path.
Compare the current path with the link's href to determine if it is active.
const pathname = usePathname(); const isActive = pathname === '/about';
<Link href="/contact" className={isActive ? 'active' : ''}>Contact</Link>
aria-current attribute for accessibility to indicate the active link.aria-current={isActive ? 'page' : undefined}This component shows a simple navigation bar with three links. The link matching the current page path is styled differently with bold blue text and an underline. The aria-current attribute helps screen readers know which page is active.
'use client'; import React from 'react'; import Link from 'next/link'; import { usePathname } from 'next/navigation'; export default function Navbar() { const pathname = usePathname(); function NavLink({ href, children }) { const isActive = pathname === href; return ( <Link href={href} className={isActive ? 'active' : ''} aria-current={isActive ? 'page' : undefined} > {children} </Link> ); } return ( <nav aria-label="Main navigation"> <ul style={{ display: 'flex', gap: '1rem', listStyle: 'none' }}> <li><NavLink href="/">Home</NavLink></li> <li><NavLink href="/about">About</NavLink></li> <li><NavLink href="/contact">Contact</NavLink></li> </ul> <style>{` a { text-decoration: none; color: black; font-weight: normal; } a.active { font-weight: bold; color: blue; border-bottom: 2px solid blue; } `}</style> </nav> ); }
Make sure to use usePathname() inside client components only.
For nested routes, you might want to check if the current path starts with the link href instead of exact match.
Always include aria-current="page" on active links for better accessibility.
Active link detection highlights the current page link in navigation.
Use usePathname() from Next.js to get the current URL path.
Compare the path with link href and apply styles and aria-current accordingly.
Practice
Solution
Step 1: Understand active link detection
Active link detection is used to show which page is currently open by highlighting its link in the navigation.Step 2: Identify the purpose in Next.js navigation
Highlighting the current page link helps users know where they are in the site.Final Answer:
To highlight the link of the current page the user is on -> Option AQuick Check:
Active link detection = Highlight current page link [OK]
- Thinking it disables other links
- Confusing with preloading pages
- Assuming it changes the URL automatically
Solution
Step 1: Recall Next.js hooks for routing
Next.js provides usePathname() to get the current URL path in the app router.Step 2: Differentiate from other hooks
useRouter() is more general, but usePathname() specifically returns the current path string.Final Answer:
usePathname() -> Option CQuick Check:
Current path hook = usePathname() [OK]
- Using useRouter() instead of usePathname()
- Confusing with React Router hooks
- Using non-existent hooks like useHref()
isActive if the current path is /about?
const pathname = usePathname(); const isActive = pathname === '/about';
Solution
Step 1: Understand the usePathname() output
usePathname() returns the current URL path as a string, here '/about'.Step 2: Evaluate the comparison
Comparing pathname === '/about' when pathname is '/about' results in true.Final Answer:
true -> Option DQuick Check:
Pathname '/about' === '/about' is true [OK]
- Assuming usePathname() returns an object
- Using == instead of === causing confusion
- Expecting false when paths match exactly
const pathname = usePathname; const isActive = pathname === '/home';
Solution
Step 1: Check usePathname usage
usePathname is a hook and must be called as a function: usePathname()Step 2: Identify the error in assignment
Assigning usePathname without parentheses assigns the function itself, not the path string.Final Answer:
usePathname is not called as a function -> Option BQuick Check:
Hooks must be called: usePathname() [OK]
- Forgetting parentheses on hooks
- Using wrong comparison operators
- Importing pathname from wrong module
aria-current attribute to a Next.js navigation link when it is active using usePathname()? Choose the best code snippet.Solution
Step 1: Understand aria-current usage
aria-current should be set to 'page' when the link is active, otherwise omitted or undefined.Step 2: Check each option's attribute value
const pathname = usePathname(); return <a href='/about' aria-current={pathname === '/about' ? 'page' : undefined}>About</a>;correctly uses 'page' for active and undefined for inactive, which is the recommended pattern.Final Answer:
Option A code snippet -> Option AQuick Check:
aria-current='page' when active, undefined otherwise [OK]
- Using boolean true/false instead of 'page'
- Using string 'true' or 'false' incorrectly
- Setting aria-current to wrong string values
Ready to prove it?
Pass the test and this pattern moves unknown
