How to get query params in getmetadata function ?
Answered
Acacia-ants posted this in #help-forum
Acacia-antsOP
Asking this as I wish to create a dynamic meta description tag where I will fetch the description from the db according to the id provided in the query and put the fetched description as the meta description of the page.
14 Replies
@Acacia-ants Asking this as I wish to create a dynamic meta description tag where I will fetch the description from the db according to the id provided in the query and put the fetched description as the meta description of the page.
do you mean
generateMetadata?type Props = {
params: { id: string }
searchParams: { [key: string]: string | string[] | undefined }
}
export async function generateMetadata(
{ params, searchParams }: Props,
parent: ResolvingMetadata
): Promise<Metadata> {
return {}
}it is in the searchParams props
Acacia-antsOP
when i am doing console.log(JSON.stringify(searchParams)) i am getting undefined
@Acacia-ants when i am doing console.log(JSON.stringify(searchParams)) i am getting undefined
are you using it in
page.tsx?Acacia-antsOP
no layout.tsx
layout doesn't receive the searchParams
Answer
Acacia-antsOP
my page.tsx is a client component I have to crate whole new client component and then get the searchParams in the page.tsx
Acacia-antsOP
thank you will implement and tell if this worked or not.
@Ray yes you need to do that
Acacia-antsOP
It worked. Thanks
@Acacia-ants It worked. Thanks
you are welcome