One of its parents is marked with "use client"
Unanswered
Britannia Petite posted this in #help-forum
Britannia PetiteOP
using next.js 14
If im getting my token like this I have no errors, but if a deport my getToken inside Navbar
I got error :
You're importing a component that needs next/headers. That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.
Can someone explain me why I got this error ? thanks for the help
// session.ts
import { cookies } from 'next/headers';
export function getToken() {
const session = cookies().get('session')?.value;
if (!session) return null;
return session;
}// layout.tsx
import { getToken } from '@/libs/session';
export default function MainLayout({ children }: { children: ReactNode }) {
const token = getToken();
return (
<div>
<Navbar />
<div>
{children}
</div>
</div>
);
}If im getting my token like this I have no errors, but if a deport my getToken inside Navbar
export const Navbar = () => {
const token = getToken();
console.log(token);
return <></>;
}I got error :
You're importing a component that needs next/headers. That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.
Can someone explain me why I got this error ? thanks for the help
5 Replies
Britannia PetiteOP
after updating next.js to 14.1.0 got the error updated :
You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory
but im using app dir
You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory
but im using app dir
Broad-snouted Caiman
Share the complete code of the Navbar component and its parent component, please.
Britannia PetiteOP
that's it for now, no more
Broad-snouted Caiman
My guess is that a client component is importing Navbar directly, which makes the Navbar a client component as well. In order to main its status of server component, you need to pass it as children (ReactNode).
@Broad-snouted Caiman My guess is that a client component is importing Navbar directly, which makes the Navbar a client component as well. In order to main its status of server component, you need to pass it as children (ReactNode).
Britannia PetiteOP
nope, no client component importing navbar here