Capítulo 190 de 456
Client Component hook that reads the active route segment one level below the Layout it's called from. Useful for navigation UI like tabs whose style depends on the active child segment.
null; aceita opcionalmente uma chave de parallel route.'a/b/c'), não um array.'use client'
import Link from 'next/link'
import { useSelectedLayoutSegment } from 'next/navigation'
export default function BlogNavLink({ slug, children }: { slug: string; children: React.ReactNode }) {
const segment = useSelectedLayoutSegment()
const isActive = slug === segment
return (
<Link href={`/blog/${slug}`} style={{ fontWeight: isActive ? 'bold' : 'normal' }}>
{children}
</Link>
)
}
| Layout | Visited URL | Returned Segment |
|---|---|---|
app/layout.js | / | null |
app/layout.js | /dashboard | 'dashboard' |
app/dashboard/layout.js | /dashboard | null |
app/dashboard/layout.js | /dashboard/settings | 'settings' |
app/dashboard/layout.js | /dashboard/analytics/monthly | 'analytics' |
app/blog/layout.js | /blog/a/b/c (catch-all) | 'a/b/c' |
'use client'; deve ser chamado num componente cliente separado importado pelo layout.useSelectedLayoutSegments.cacheComponents ativo, rotas com params dinâmicos não cobertos por generateStaticParams fazem o hook suspender, exigindo Suspense.Suspense.parallelRouteKey é relevante.