0
0
NextJSframework~10 mins

Active link detection in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Active link detection
User clicks a link
Next.js Router updates URL
Component checks current URL
Compare link href with current URL
Add active style
Render updated link style
When a user clicks a link, Next.js updates the URL. The component compares the link's href with the current URL. If they match, it adds an active style to highlight the link.
Execution Sample
NextJS
import Link from 'next/link';
import { usePathname } from 'next/navigation';

export default function NavLink({ href, children }) {
  const pathname = usePathname();
  const isActive = pathname === href;
  return <Link href={href} className={isActive ? 'active' : ''}>{children}</Link>;
}
This code defines a NavLink component that highlights itself when its href matches the current URL path.
Execution Table
StepActionCurrent URL (pathname)Link hrefIs Active?Class Applied
1Initial render/home/hometrue'active'
2User clicks link to /about/about/homefalse'' (no class)
3Render NavLink with href='/about'/about/abouttrue'active'
4User clicks link to /contact/contact/aboutfalse'' (no class)
5Render NavLink with href='/contact'/contact/contacttrue'active'
6User clicks link to /home/home/contactfalse'' (no class)
7Render NavLink with href='/home'/home/hometrue'active'
8ExitUser stops clicking, no URL change---
💡 Execution stops when user stops clicking links and URL remains stable.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 7Final
pathname/home/home/about/contact/home/home
href/home/home/about/contact/home/home
isActivetruefalsetruetruetruetrue
className'active''''active''active''active''active'
Key Moments - 2 Insights
Why does the link get the 'active' class only when pathname equals href?
Because the code compares pathname and href exactly (see execution_table steps 1, 3, 5, 7). Only when they match does isActive become true, adding the 'active' class.
What happens if the pathname is '/about/team' but href is '/about'?
The strict equality check fails, so isActive is false and no 'active' class is added. To handle subpaths, code must check if pathname starts with href.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3. What is the value of isActive?
Afalse
Btrue
Cundefined
Dnull
💡 Hint
Check the 'Is Active?' column at Step 3 in the execution_table.
At which step does the link with href='/contact' get the 'active' class?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for when pathname and href both equal '/contact' in the execution_table.
If pathname was '/about/team' and href was '/about', what would isActive be with the current code?
Afalse
Bnull
Ctrue
Dthrows error
💡 Hint
The code uses strict equality (pathname === href), so different strings mean false.
Concept Snapshot
Active link detection in Next.js:
- Use usePathname() to get current URL path.
- Compare pathname with link href.
- If equal, add 'active' class to highlight link.
- Helps users see which page they are on.
- For subpaths, use startsWith instead of strict equality.
Full Transcript
Active link detection in Next.js works by checking the current URL path against each link's href. When a user clicks a link, Next.js updates the URL. The NavLink component uses the usePathname hook to get the current path. It compares this path with the link's href. If they match exactly, the component adds an 'active' CSS class to highlight the link. This helps users know which page they are on. The execution table shows how the pathname and href change as the user clicks different links, and when the 'active' class is applied. Beginners often wonder why the active class only appears on exact matches and what happens with subpaths. The code uses strict equality, so only exact matches get the active style. To handle subpaths, the code would need to check if the pathname starts with the href. This visual trace helps learners see how state changes and rendering happen step-by-step in active link detection.