Capítulo 191 de 456
Client Component hook that reads all active route segments below the Layout it's called from, as an array — useful for breadcrumbs and other UI that needs full knowledge of the descendant path.
(group)) aparecem no array; filtre com .filter() se não forem desejados na UI.'use client'
import { useSelectedLayoutSegments } from 'next/navigation'
export default function ExampleClientComponent() {
const segments = useSelectedLayoutSegments()
return (
<ul>
{segments.map((segment, index) => <li key={index}>{segment}</li>)}
</ul>
)
}
| Layout | Visited URL | Returned Segments |
|---|---|---|
app/layout.js | / | [] |
app/layout.js | /dashboard | ['dashboard'] |
app/layout.js | /dashboard/settings | ['dashboard', 'settings'] |
app/dashboard/layout.js | /dashboard/settings | ['settings'] |
app/layout.js (catch-all) | /blog/a/b/c | ['blog', 'a/b/c'] |
app/blog/layout.js | /blog/a/b/c | ['a/b/c'] |
filter(s => !s.startsWith('(')) se eles não devem aparecer na UI.cacheComponents ativo, exige Suspense quando algum param dinâmico abaixo não é coberto por generateStaticParams.