regarding usePathName from next/navigation
Answered
Philadelphia Vireo posted this in #help-forum
Philadelphia VireoOP
i want to know, in my root layout.tsx file, how can i fetch the usePathName cuz ir requires "use client" directive at top
which i cant use in the layout.tsx file because it is exporting metadata and is disallowed
which i cant use in the layout.tsx file because it is exporting metadata and is disallowed
Answered by American Chinchilla
so the navbar file presumably will look something like this
"use client"
import { usePathname } from 'next/navigation';
export default function NavBar() {
const currPath = usePathname();
//...
}30 Replies
American Chinchilla
@Philadelphia Vireo in what context is it needed? if it's for a navbar i usually just make that a client component, and do the usePathName there
Philadelphia VireoOP
yes i need it for the navbar
but how do u make it a client component?
while exporting metadata
it does not allow layout.tsx to be a client component?!?
while exporting metadata
it does not allow layout.tsx to be a client component?!?
American Chinchilla
make a navbar in a new file with the "use client" directive
import the navbar into layout.tsx
Philadelphia VireoOP
ok
then
then
i am already doing what u just saidðŸ‘
i want to alter my navbar ui based on some specific routes
thats what i specifically want
thats what i specifically want
?!
American Chinchilla
Can I see code?
Philadelphia VireoOP
"use client"
import { Inter } from 'next/font/google'
import './globals.css'
import NavBar from '@/components/Navbar/navbar'
import { cn } from '@/lib/utils'
import { usePathname } from 'next/navigation'
const inter = Inter({ subsets: ['latin'] })
export const metadata = {
title: 'Ecommerce',
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
const pathName=usePathname();
return (
<html lang="en">
<body className={cn(inter.className,"bg-gray-300")}>
<NavBar currentPath={pathname}/>
{children}
</body>
</html>
)
}American Chinchilla
oh okay i see what you're doing
we're not going to usePathname at all in this file
remove these:
"use client"
Philadelphia VireoOP
okk tell
American Chinchilla
import { usePathname } from 'next/navigation'
const pathName=usePathname();
currentPath={pathname}
then in your navbar component add those
Philadelphia VireoOP
ohh okay
so when it gets rendered it will fetch the pathname itself right?
it will not give error cuz its in the components folder and not in my app router directory??
so when it gets rendered it will fetch the pathname itself right?
it will not give error cuz its in the components folder and not in my app router directory??
American Chinchilla
it wont give an error, it's being called from the app router so it has that context if that makes sense
Philadelphia VireoOP
got it got itðŸ‘
lemme try
lemme try
American Chinchilla
so the navbar file presumably will look something like this
"use client"
import { usePathname } from 'next/navigation';
export default function NavBar() {
const currPath = usePathname();
//...
}Answer
Philadelphia VireoOP
greatðŸ‘
lemme try
lemme try
@American Chinchilla Thanks bro🤜🤛
Got it rightðŸ‘
Got it rightðŸ‘
American Chinchilla
yep!
@Philadelphia Vireo <@281970530037268480> Thanks bro🤜🤛
Got it rightðŸ‘
American Chinchilla
could you mark as solved?
Philadelphia VireoOP
sure
how?
how?
American Chinchilla
right click on message with solution > apps > mark solution