T
TanStack2y ago
plain-purple

How can i access the cookies when i use Server rendering - NEXTJS 14

I'm trying to pre-fetch some data needed for the layout, I followed the guide on this page: https://tanstack.com/query/latest/docs/framework/react/guides/advanced-ssr, I have a setup just like the page mentioned above using the NextJS app router. however, I need to access the server state to get some data from the cookies to send the request, which I cannot get in a client component. I can get the cookies in the server component, but not downstream in the client components. I've tried many things but still nothing seems to work.
2 Replies
genetic-orange
genetic-orange17mo ago
Is your cookie http only? You can't access from JS side if that's the case
fascinating-indigo
fascinating-indigo17mo ago
i dont wanna be a douche but seems like not a query question im looking into it though ahhh nvm it is lol isnt it possible to pass it down ?
import { cookies } from 'next/headers';

export default function RootLayout({ children }) {
const cookieStore = cookies();
const myCookie = cookieStore.get('myCookieName').value;

return (
<ReactQueryClientProvider myCookie={myCookie}>
{children}
</ReactQueryClientProvider>
);
}
import { cookies } from 'next/headers';

export default function RootLayout({ children }) {
const cookieStore = cookies();
const myCookie = cookieStore.get('myCookieName').value;

return (
<ReactQueryClientProvider myCookie={myCookie}>
{children}
</ReactQueryClientProvider>
);
}
something like this?

Did you find this page helpful?