Next.js Discord

Discord Forum

Can we pass a prop from a layout to a child page

Answered
Western Meadowlark posted this in #help-forum
Open in Discord
Avatar
Western MeadowlarkOP
I'm using Next 14 with app directory
I'm trying to pass a prop from my layout to its children.

I found this answer on stackoverflow https://stackoverflow.com/questions/74573236/pass-props-to-page-jsx-child-from-root-layout-next-js-13
where the author found a workaround by passing the custom key value in the params object in props.

I also tried the same thing but cant seem to get it working. Was it removed or am I missing something?

created a minimum reproducible example as well
https://github.com/cmgchess/can-we-pass-props-from-layout

Thanks in advance 🙂
Answered by Jesse677
And running headers().get(“referer”)
View full answer

65 Replies

Avatar
Jesse677
I've used this before and it's worked, try making the components accepting the prop.params a client component
If this doesn't work, I would look into a state management solution like Zustand and you can create a store where you can set it in your layout and get it in your page
You can also use IndexDB, but it's a bit random.
Also a cookie would work
Avatar
Romanian Mioritic Shepherd Dog
any solution for this?
unfortunately I can't do this as I am calling generateMetadata in this file. since it needs to be a server component, I can't make it as a client component
Avatar
Oriental chestnut gall wasp
What is the specific use case for this?
Perhaps there's another way to do the same thing
Avatar
<Milind ツ />
Maybe use context?
Avatar
Jesse677
Use headers then
Set a header in the layout.tsx and get it from your page
Avatar
Oriental chestnut gall wasp
Again it depends on what the use case is. There's a handful of different solutions, but without know what OP or Rds are trying to accomplish, it is hard to recommend a specific approach
Avatar
Romanian Mioritic Shepherd Dog
Actually i want to have a url like this,
localhost:3000/[locale]/reviews/[id]
but according to my requirements, basePath should be '/reviews'. So I cannot create a dynamic folder inside my app directory for [locale] as basePath was set and locale should come before the 'reviews' segment.

So what I did was I used rewrites method in next.config.js file and set it. Now the issue is I want to get that [local] from my url in the [id] directory's page.tsx file. I have called generateMetadata in that file to dynamically generate meta data for the pages. So what I did was i used usePathname in layout.tsx file to get the locale and tried to pass that as a prop to page.tsx file as params.prop since it's a server component. But it didn't work
For that I need to make both files as client components right?
Yeah but the issue is at the same time i need to access that locale from the url. so for that I had to make that a client using 'use client'.
I've sent my use case. Appreciate if i can a have any kind of a solution for the issue
Avatar
Oriental chestnut gall wasp
is there a reason you need 2 dynamic routes?
Avatar
Romanian Mioritic Shepherd Dog
yes first one is the locale like en-us, zh, en-GB and the second one you can assume as a product id
Avatar
Oriental chestnut gall wasp
could you not then do /review/[id]/[locale]
Avatar
<Milind ツ />
To get pathname, there's really no way to get path without marking a component as client.

Alternatively, instead of generate dynamic route, u could pass local as url query? And use searchparams on page.tsx to get that value
This won't need any client component
Avatar
Jesse677
So some are server components and some are client is what you’re saying?
Avatar
Romanian Mioritic Shepherd Dog
nope. can't change the requirement😐
I know the best solution is going against the requirement but unfortunately cannot change it
Avatar
<Milind ツ />
In here params.prop fetches the last route value

If last route is /id, it will show id
Avatar
Romanian Mioritic Shepherd Dog
No I had to make that layout file as a client as i used usePathname to get the locale segment from the url. Better if i can access it as a server component
Avatar
Jesse677
You can by using headers
Avatar
Romanian Mioritic Shepherd Dog
Avatar
Jesse677
And running headers().get(“referer”)
Answer
Avatar
Romanian Mioritic Shepherd Dog
How do i get the locale from the url using headers?
But i have to get it from the url. so how can i get it? it's not a predefined one
it can be changed
Avatar
Jesse677
Filter out the url so you’re left with your locale
Avatar
Romanian Mioritic Shepherd Dog
how do i filter out it?
Avatar
Oriental chestnut gall wasp
May I ask what this is for and why you can't change your approach?
Avatar
<Milind ツ />
Probably a client requirement
Avatar
Oriental chestnut gall wasp
wait if the base path has to be reviews, then just do /reviews/[id]/[locale] right?
or /reviews/[...slug]
Avatar
Jesse677
Get it and then just remove the url
Avatar
Romanian Mioritic Shepherd Dog
haha couldn't change🙂 . IK the easiest solution is adding that locale after reviews
Avatar
Oriental chestnut gall wasp
what does your url have to be then?
I'm confused
Avatar
Jesse677
Give me an example of what is returned when you use headers().get(“referer”) and then tell me what bit you want
@Romanian Mioritic Shepherd Dog
Avatar
Romanian Mioritic Shepherd Dog
To get it I have to use usePathname right? that means i have to make it client component and in that case I can't call the headers method as it works in server components, right?
Avatar
Jesse677
No
You don’t have to use pathname
Just get the referer from the headers
Avatar
<Milind ツ />
Headers is server feature
Avatar
Romanian Mioritic Shepherd Dog
localhost:3000/en-us/reviews/nike, like this
Avatar
<Milind ツ />
Don't need to mark as client
Avatar
Jesse677
Since that will give you the full url
And then filter it out as I said
Avatar
Oriental chestnut gall wasp
localhost:3000/[locale]/reviews/[id]
right?
what is wrong with that?
Avatar
Romanian Mioritic Shepherd Dog
Thank you very much. It worked. This made my day🥹
Avatar
Jesse677
Don’t forget to mark as answer
Avatar
Oriental chestnut gall wasp
this isn't his post
Avatar
Jesse677
Oh yeah lol
Avatar
Romanian Mioritic Shepherd Dog
Ah now it was fixed. thanks for your help:firevercel:
Avatar
Jesse677
Forgot
Avatar
Oriental chestnut gall wasp
We still don't know why OP needs to pass props down
Avatar
Jesse677
Yeah lol
Avatar
Western MeadowlarkOP
sorry this was asked on behalf of rds 🙂
ill mark it as answer
Avatar
Oriental chestnut gall wasp
Oh ok