Performance: Active link detection
MEDIUM IMPACT
This affects interaction responsiveness and rendering speed when highlighting navigation links based on the current page.
import Link from 'next/link'; import { usePathname } from 'next/navigation'; export default function Nav() { const pathname = usePathname(); return ( <nav> {['/', '/about', '/contact'].map((path) => { const isActive = pathname === path; return ( <Link key={path} href={path} className={isActive ? 'active-link' : ''} aria-current={isActive ? 'page' : undefined} > {path === '/' ? 'Home' : path.slice(1)} </Link> ); })} </nav> ); } /* CSS */ .active-link { color: red; }
import { useRouter } from 'next/router'; export default function Nav() { const router = useRouter(); return ( <nav> {['/', '/about', '/contact'].map((path) => ( <a key={path} href={path} style={{ color: router.pathname === path ? 'red' : 'black' }} > {path === '/' ? 'Home' : path.slice(1)} </a> ))} </nav> ); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline style comparison on every render | Multiple style attribute updates | Multiple reflows on navigation | High paint cost due to style recalculation | [X] Bad |
| CSS class toggling with usePathname hook | Minimal DOM updates, class attribute only | Single reflow per navigation | Low paint cost, efficient style recalculation | [OK] Good |