Next.js Discord

Discord Forum

Get current route in App dir

Unanswered
Abyssinian posted this in #help-forum
Open in Discord
Avatar
AbyssinianOP
I am trying to fetch the route so I can pass it as a prop. I can't use Next/Router as I get this
You have a Server Component that imports next/router. Use next/navigation instead.

How can I do it with next/nav?

4 Replies

Avatar
Asiatic Lion
they changed the import from next/router to next/navigation for router related things. https://nextjs.org/docs/app/api-reference/functions/use-router but also you cant use these functions in a server component
so you cant really know the pathname in a server component, the closest you can really get is the referer header but that doesnt work when someone comes to your site from not your website
id say for whatever purpose you need the pathname, make a client component with the new usePathname() function and plug that component in, inside the server component
Avatar
AbyssinianOP
cool okay ty