Passing Server Components to Client Components as Props - Docs not clear #63301
-
According to the documentation you can pass a server component to a client component using the This pattern works when I have a server component that passes a component as a prop to another server component e.g.
And
However, if I mark the receiving (second) component as a client component with Long story short, I'm not clear on how to pass a server component to a client component as a prop outside of using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
In your example you should have had: export default function AnotherComponent({cas}){
const Tag = cas;
return <Tas />;
} Otherwise I'd try to render a DOM element called cas. With that out of the way. I've been corrected on this at least once, but let me see if I can get this right this time. The right way to pass a server component to a client component is using JSX, and not a function rather, that is, pass a React Element to the client component. // This WON'T work
import { ClientComponent } from './client';
import { ServerComponent } from './server';
export default function Home() {
return <ClientComponent slot={ServerComponent} />;
} But this will: import { ClientComponent } from './client';
import { ServerComponent } from './server';
export default function Home() {
return <ClientComponent slot={<ServerComponent />} />;
} Where the ClientComponent is: 'use client';
import type { ReactNode } from 'react';
export const ClientComponent = ({ slot }: { slot: ReactNode }) => {
return <>{slot}</>;
}; And the Server Component just renders a div tag. The explanation to this, AFAIK, is here: https://react.dev/reference/react/use-client#serializable-types The serializable types, lists: Another way to see it, is that this is just plain composition, where the Client Component, is parent, but doesn't own the Server Component, and React just knows how to patch them up in the UI, but if you pass a function, then you are making the client component own the passed in component, because it has to render it, rather than only declaring where it goes. Where I've been corrected is in whether or not you can slot Client into Client, through a server Server Component IIRC.
|
Beta Was this translation helpful? Give feedback.
-
i get that this will work `import { ClientComponent } from './client'; export default function Home() { but what if the needed props? what then? and those props can be passed from within the ClientComponent. such as: `export default function ClientComponent() { return ( // how would this work in a scenario like this? |
Beta Was this translation helpful? Give feedback.
In your example you should have had:
Otherwise I'd try to render a DOM element called cas.
With that out of the way.
I've been corrected on this at least once, but let me see if I can get this right this time.
The right way to pass a server component to a client component is using JSX, and not a function rather, that is, pass a React Element to the client component.
But this will: