Skip to content

Pathname from layout.ts at new app structure (nextjs 13) #43657

Closed Answered by leerob
ludersGabriel asked this question in Help
Discussion options

You must be logged in to vote

If you need to access to the pathname outside the page, the solution is to usePathname().

'use client'
 
import { usePathname } from 'next/navigation'
 
// This a client component, still prerendered
export function Pathname({ children }) {
  const pathname = usePathname()
  return (
    <div>
      <p>Path: {pathname}</p>
      {children}
    </div>
  )
}

---

// Some nested layout where you don't have `params` and `searchParams` as props
// The child page can be a server component!
export default function DashboardLayout({ children }) {
  return (
    <div>
      <Pathname>
        {children}
      </Pathname>
    </div>
  )
}
  • Yes, this a client component, and yes, that is okay!
  • You ca…

Replies: 13 comments 25 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@thexpand
Comment options

Comment options

You must be logged in to vote
3 replies
@montanaflynn
Comment options

@imprisonedmind
Comment options

@ludersGabriel
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@ivanmeda993
Comment options

@ludersGabriel
Comment options

@leerob
Comment options

@sambowenhughes
Comment options

@guotie
Comment options

Comment options

You must be logged in to vote
8 replies
@Eugene-Mokrushin
Comment options

@abhishekprajapati1
Comment options

@xyba1337
Comment options

@abhishekprajapati1
Comment options

@abhishekprajapati1
Comment options

Comment options

You must be logged in to vote
1 reply
@artemis-prime
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
6 replies
@szymekSKKJ
Comment options

@maskott
Comment options

@szymekSKKJ
Comment options

@williamlmao
Comment options

@xyba1337
Comment options

Answer selected by leerob
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@T04435
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet