Next.js Discord

Discord Forum

How to access queryParams in a layout?

Answered
Gharial posted this in #help-forum
Open in Discord
GharialOP
I am using a Server Component as a Layout and want to know how to get a query parameter inside the render function?

12 Replies

Tonkinese
Could you show me some code?
@Tonkinese if not, this may help you https://stackoverflow.com/questions/74889841/how-to-get-query-params-using-server-component-next-13
GharialOP
Hello, well the code is not working because I am in a layout and this layout component does not get the parameters passed as props. As soon as I use useSearchParams or useRouter, I get an error saying that those can only be used within client components
GharialOP
Well, sorry the suggested workaround using a Middleware and a custom header is working. This does not look greate to be but I am going with that solution for now. Thank you for helping out
Answer
Barbary Lion
layout is not meant to be subroute dependant
@Barbary Lion layout is not meant to be subroute dependant
GharialOP
Thanks for your reply. Can you explain that please?
Barbary Lion
Layout is not reloaded when client side navigating through the subroutes, this means that it ia supposed to be the same for all subroutes
for example a static header is a good layout
the <h1> title of a page is not a good layout
because depends on the page probably
anything page dependant is not a layout and should be placed in page.tsx