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

Edit: Also see #43704 (comment)

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 componen…

Replies: 16 comments 43 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
9 replies
@williamlmao
Comment options

@xyba1337
Comment options

@abhishekprajapati1
Comment options

@njarraud
Comment options

@leerob
Comment options

Answer selected by leerob
Comment options

You must be logged in to vote
8 replies
@abhishekprajapati1
Comment options

@T04435
Comment options

@abhishekprajapati1
Comment options

@T04435
Comment options

@abhishekprajapati1
Comment options

Comment options

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

@dkildar
Comment options

@ElectricCodeGuy
Comment options

@typeofweb
Comment options

@abhishekprajapati1
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

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

@nagyatka
Comment options

@ElectricCodeGuy
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet