Next.js Discord

Discord Forum

How to get pathname in layout.tsx?

Unanswered
New Guinea Freshwater Crocodile posted this in #help-forum
Open in Discord
New Guinea Freshwater CrocodileOP
I need a certain thing to not be rendered in layout.tsx based on the path I'm on. How do I achieve this? On client side I can use usePathname hook but what can I do on the server side in layout.tsx?

29 Replies

New Guinea Freshwater CrocodileOP
sad
so should I make my layout.tsx a client component?
although I can't do that because some children are server components
@New Guinea Freshwater Crocodile although I can't do that because some children are server components
client component layouts do not make the pages under them client components, so nothing wrong with client component layouts
New Guinea Freshwater CrocodileOP
i get this error
though the best course of action would be to make your nav component client component, then import it to the server component layout
New Guinea Freshwater CrocodileOP
but navbar needs to get stuff like user name, proifle picture etc. i can't make it a client component
@New Guinea Freshwater Crocodile i get this error
well you have to reorganise your components and props such that client components do not import next/headers
New Guinea Freshwater CrocodileOP
or at least, i don't want to
@New Guinea Freshwater Crocodile but navbar needs to get stuff like user name, proifle picture etc. i can't make it a client component
you can get them in the server component layout, then pass them to the client component nav
@joulev well you have to reorganise your components and props such that client components do not import next/headers
New Guinea Freshwater CrocodileOP
it only started happening after i made layout.tsx a client component
@New Guinea Freshwater Crocodile it only started happening after i made layout.tsx a client component
yes but as i said above, you cannot determine the pathname in server components. using client components is a must there
@joulev you can get them in the server component layout, then pass them to the client component nav
New Guinea Freshwater CrocodileOP
do you recommend doing that? doesn't layout like, not re-render when u move between tabs
@New Guinea Freshwater Crocodile do you recommend doing that? doesn't layout like, not re-render when u move between tabs
the server component part doesn't render, the client component part does
if your user information changes, revalidateTag/revalidatePath to refresh the server component layout too
New Guinea Freshwater CrocodileOP
is it standard practice now in next14 to make a call to get user data and pass it down to things like navbar?
i'm used to making server calls like this from page.tsxs, but not layout. I generally think of layout as ....something to just hold the layout and nothing more. no business logic/data per se
should I update my priors
New Guinea Freshwater CrocodileOP
well this is still weird because, ideally I don't want the navbar to be rendered at all given a certain path.

so in the client component navbar do i just do

usePathname()

and then

if (path == "/xyz") {
return null
}
?
i would rather do that in the parent but hmm
New Guinea Freshwater CrocodileOP
@joulev thanks, you're a saviour
hope it's not a bad practice to make API calls like this in layout though
well at least i personally do that. if it somehow becomes a bad practice, you have a fellow developer making the same mistake here