Pass state between client components with a server component parent
Unanswered
Scale parasitoid posted this in #help-forum
Scale parasitoidOP
Suppose I have a server component
Question:
How can I share state between
If the parent component
- Lift the state up to the parent.
- OR use context.
But since the parent is a server component, it seems like it severs the connection between its children components.
Note: I know adding
ServerComponent that has two client components children like the following ClientComponentA and ClientComponentB. // page.tsx
export default async function ServerComponent(){
// Making heavy computation & database queries
return (
<>
<ClientComponentA />
<ClientComponentB />
</>
);
}Question:
How can I share state between
ClientComponentA and ClientComponentB while keeping the parent component a server component? If the parent component
ServerComponent was a client component, I could either:- Lift the state up to the parent.
- OR use context.
But since the parent is a server component, it seems like it severs the connection between its children components.
Note: I know adding
"use client" will "solve" it. I'm looking for a way to share the state while keeping the parent component a server component (for performance reasons).1 Reply
@Scale parasitoid Suppose I have a server component `ServerComponent` that has two client components children like the following `ClientComponentA` and `ClientComponentB`.
typescript
// page.tsx
export default async function ServerComponent(){
// Making heavy computation & database queries
return (
<>
<ClientComponentA />
<ClientComponentB />
</>
);
}
**Question:**
How can I share state between `ClientComponentA` and `ClientComponentB` while keeping the parent component a server component?
If the parent component `ServerComponent` was a client component, I could either:
- Lift the state up to the parent.
- OR use context.
But since the parent is a server component, it seems like it severs the connection between its children components.
**Note:** I know adding `"use client"` will "solve" it. I'm looking for a way to share the state while keeping the parent component a server component (for performance reasons).
i think context can work. add all your logic to the context provider and import your state inside both client components separately