Next.js Discord

Discord Forum

Get params in a layout outside of the dynamic route?

Unanswered
Exzotic posted this in #help-forum
Open in Discord
I have a sidebar component in the layout.tsx outside of the [id] folder. I need to be able to check if the id exists, then change the sidebars content. Any ideas on how to do this?

6 Replies

Siberian Flycatcher
Hi. Can you move the layout inside [id]? If you need to change it conditionally based on the param, it seems to belong there.
Problem is I also need the sidebar outside of that just with different content inside it
I saw there was a useParams() hook but that is client side only and was hoping to be able to do it in a server component
Siberian Flycatcher
Can you describe the desired UX? What type of content should change in the sidebar? Something like a sub-navigation?
Siberian Flycatcher
When we navigate between routes with a shared layout, like in your case, the layout won't re-render. Therefore, it can not receive new params as props.

I'm asking about the desired UX because if you need to change a significant part of the sidebar or load some data based on ID, it might make sense to have 2 distinct layouts, one for the dashboard in general and one for the dynamic part with IDs. But if you want to make slight changes to the existing layout, like marking the active link, useParams may be sufficient enough.