# Active Links (usePathname)

> Reading the current URL path to style navigation.

Subject: Next.js | Lesson 39 of 65 | 3 min read
Interactive lesson: https://codewithanimation.in/nextjs/use-pathname
Previous: [Client Routing (useRouter)](https://codewithanimation.in/nextjs/use-router.md) | Next: [Query Strings (useSearchParams)](https://codewithanimation.in/nextjs/use-search-params.md)

## Reading the Path

`usePathname` is a Client Component hook that lets you read the current URL's pathname.

For example, if the user is at `https://example.com/dashboard/settings`, the hook returns the string `'/dashboard/settings'`.

## The Active State

The most common use case in Next.js for `usePathname` is styling active navigation links.

By comparing the hook's output to a link's `href` attribute, you can easily apply a different CSS class (like a glowing underline or bold text) to highlight the page the user is currently viewing.

## Exact Matching

You import it from `next/navigation`.

Then, inside your `<NavLink>` component, you write: `const isActive = pathname === href;`.

## Common mistake: Query Strings Ignored

A common point of confusion is that `usePathname` completely strips out the domain and the query string.

If the URL is `https://example.com/shop?color=red`, `usePathname` will only return `'/shop'`. If you need to read the `?color=red` part, you must use a different hook (`useSearchParams`).

## Tip: Partial Matching

If you want a parent link like `/blog` to stay active when a user visits a child route like `/blog/post-1`, you can use `pathname.startsWith(href)` instead of an exact `===` check.

## Try it: The Active Link

Click the links in the mock navigation bar below. Watch how the `usePathname` hook dynamically updates, and how that string is compared against each link's `href` to determine the active glow state.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/use-pathname

## Check yourself

### Question 1

If a user visits `https://acme.com/store/shoes?size=10`, what will `usePathname()` return?

- A. `'https://acme.com/store/shoes'`
- B. `'/store/shoes'`
- C. `'/store/shoes?size=10'`
- D. `'?size=10'`

Correct answer: B

`usePathname` only returns the path portion of the URL, completely ignoring the domain and the query string.

- Why not A: It strips the domain.
- Why not C: It strips the query string.
- Why not D: That is what useSearchParams is for.

## Remember this

- `usePathname` is a Client Component hook.
- It is primarily used for styling 'active' states on navigation bars.
- It strips out the domain name.
- It completely ignores query parameters (`?key=value`).
