Next.js Discord

Discord Forum

Update Sidebar state based on URL params

Answered
Asian black bear posted this in #help-forum
Open in Discord
Asian black bearOP
Short Version: Need to update sidebar based on current url parameters so that when I go directly to a page the sidebar displays the correct workspce id and highlights the current project (detailed below).

I have an project management application which has workspaces and each workspace will have projects, you can navigate between workspaces and that updates the sidebar so it shows the correct projects based on the workspace selected, this all works fine.

However, if for example you go directly to a page it will not show the right workspace id (currently I default it to the first workspace in the array) because it isn't aware of the search params.

How could I get acccess to this so I can update my sidebar? I tried useSearchParams but this doesn't work as it doesn't have access to the parameters.

An example is - if I go directly to dashboard/2/3 it will be the page for project 3 in workspace 2, but if I go directly to that URL then the sidebar won't know that it's in project 3 of workspace 2 so I need to find a way to pass that information to my sidebar.
Answered by Mozzy
Can't you parse the path given from usePathname()?
View full answer

3 Replies

Can't you parse the path given from usePathname()?
Answer
Asian black bearOP
That's actually a perfect solution - it works! Thank you 🙂
:ok_check: