Discover how to make your website navigation smart and effortless with active link detection!
Why Active link detection in NextJS? - Purpose & Use Cases
Imagine building a website navigation menu where you want to highlight the current page link so users know where they are.
You try to do this by manually checking the URL and adding styles to the matching link.
Manually checking URLs and updating link styles is slow and error-prone.
Every time the user navigates, you must write extra code to update the active link.
This can lead to bugs where the wrong link is highlighted or no link is highlighted at all.
Active link detection automatically checks the current page and highlights the matching navigation link.
Next.js provides tools to detect the active route and apply styles dynamically, so you don't have to manage this manually.
if (window.location.pathname === '/about') { link.classList.add('active'); } else { link.classList.remove('active'); }
<Link href='/about' className={router.pathname === '/about' ? 'active' : ''}>About</Link>
This makes navigation menus clear and user-friendly by always showing which page is active without extra manual work.
On an e-commerce site, the menu highlights "Home", "Shop", or "Cart" depending on where the user is, helping them understand their location instantly.
Manual active link handling is tedious and error-prone.
Active link detection automates highlighting the current page link.
This improves user experience and reduces code complexity.
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
