What is the best way to share data between server components in Next.js 14 ?
Unanswered
Valentinh posted this in #help-forum
Hi everyone,
I use Next.js 14 and I retrieve data from a cookie in
I've created a context to share the data in my application's client components and I'd like to know if there's an equivalent for server-side rendering, as I find it redundant to do this in all my server components.
Thank you
I use Next.js 14 and I retrieve data from a cookie in
layout.tsx :const theme = cookies().get("theme")?.valueI've created a context to share the data in my application's client components and I'd like to know if there's an equivalent for server-side rendering, as I find it redundant to do this in all my server components.
Thank you
2 Replies
@Valentinh Hi everyone,
I use Next.js 14 and I retrieve data from a cookie in `layout.tsx` :
js
const theme = cookies().get("theme")?.value
I've created a context to share the data in my application's client components and I'd like to know if there's an equivalent for server-side rendering, as I find it redundant to do this in all my server components.
Thank you
No there are no equivalences in server side rendering, since pages and layouts are rendered independently you cannot pass info between pages and layouts. You have to redo the query.
Siberian
You can wrap this call in a react cache function so it only runs once per request.