Active link detection means identifying which navigation link matches the current page URL, so you can style it differently to show users where they are.
Click to reveal answer
beginner
Which Next.js hook helps detect the current route for active link detection?
The usePathname hook from next/navigation returns the current URL path, which helps in detecting the active link.
Click to reveal answer
intermediate
How can you conditionally apply a CSS class to an active link in Next.js?
Compare the link's href with the current pathname from <code>usePathname</code>. If they match, add an 'active' CSS class to style the link differently.
Click to reveal answer
intermediate
Why is it better to use Next.js built-in hooks for active link detection instead of window.location?
Next.js hooks like usePathname work properly in client components, handling server-side rendering seamlessly without hydration errors, unlike window.location which only exists in the browser.
Click to reveal answer
beginner
Show a simple example of active link detection using Next.js usePathname.
C. Comparison operator should be != instead of ===
D. isActive should be a state variable
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 B
Quick Check:
Hooks must be called: usePathname() [OK]
Hint: Always call hooks with () to get their value [OK]
Common Mistakes:
Forgetting parentheses on hooks
Using wrong comparison operators
Importing pathname from wrong module
5. How would you correctly add an aria-current attribute to a Next.js navigation link when it is active using usePathname()? Choose the best code snippet.