# Layout Segments

> Reading the active route segment below a layout.

Subject: Next.js | Lesson 42 of 65 | 3 min read
Interactive lesson: https://codewithanimation.in/nextjs/use-selected-layout-segment
Previous: [Dynamic Segments (useParams)](https://codewithanimation.in/nextjs/use-params.md) | Next: [Breadcrumbs](https://codewithanimation.in/nextjs/use-selected-layout-segments.md)

## Active Child Routes

`useSelectedLayoutSegment` is a Client Component hook that tells you which route segment is currently active directly below the layout where the hook is called.

## Layout Navigation

While `usePathname` is great for full-page navigation, `useSelectedLayoutSegment` is perfect for nested navigation, like tabs inside a specific layout.

By knowing which child segment is active, you can highlight the correct tab without needing to parse the full URL string.

## Relative Depth

The hook is relative to the folder it is called in.

If you call it from `/app/dashboard/layout.tsx`, and the user visits `/dashboard/settings`, the hook returns `'settings'`.

If the user visits `/dashboard` (the root of the layout), the hook returns `null` because there is no active segment *below* the layout.

## Common mistake: The Root Trap

A common mistake is calling this hook in the Root Layout (`/app/layout.tsx`) and expecting it to return the deepest active page.

If a user visits `/store/shoes/nike` and you call this in the Root Layout, it will only return `'store'`, because that is the segment directly below the root.

## Tip: Route Groups

Route groups (folders with parentheses like `(auth)`) are completely ignored by this hook since they do not affect the URL structure.

## Try it: The Relative Return

Click the URLs below to see what `useSelectedLayoutSegment` returns when called from different depths in the file tree.

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

## Check yourself

### Question 1

You call `useSelectedLayoutSegment()` inside `/app/layout.tsx`. The user visits `/blog/my-first-post`. What does the hook return?

- A. `'blog/my-first-post'`
- B. `'my-first-post'`
- C. `'blog'`
- D. `null`

Correct answer: C

Because the hook was called in the root layout, it only looks one level down. The segment directly below the root is `'blog'`.

- Why not A: It only returns one segment, not the full path.
- Why not B: It returns the segment directly below the layout.
- Why not D: There is an active segment below the root.

## Remember this

- `useSelectedLayoutSegment` returns the active segment ONE level below where it is called.
- It is primarily used for styling tabs or nested navigation inside Layouts.
- It returns `null` if there is no active child segment.
- It ignores Route Groups.
