Next.js · Junior · Question 22 of 75
How do you get the current path in a client component, e.g. to highlight the active link?
- ✓Correct answer:
usePathname() - 2
window.location.pathnameduring render - 3
useRouter().pathname
Explanation
usePathname() returns the path and updates on client-side navigation. Reading window during render breaks SSR and hydration. The pathname property on useRouter existed in the Pages Router; it isn't in next/navigation.