Layout Segments
Reading the active route segment below a layout.
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.
The Relative Return
Click the URLs below to see what useSelectedLayoutSegment returns when called from different depths in the file tree.
Browser URL
Hook Execution at Different Depths
// => 'dashboard'
// => 'settings'
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
useSelectedLayoutSegmentreturns the active segment ONE level below where it is called.- It is primarily used for styling tabs or nested navigation inside Layouts.
- It returns
nullif there is no active child segment. - It ignores Route Groups.
Done with this concept?
Mark it complete to track your progress. No login needed.