Cannot pass children to a client component from server component
Unanswered
American black bear posted this in #help-forum
American black bearOP
I have a layout which renders in server side. I also have a context component which is rendering in client side. I want to pass the children node of layout to the context component. But nextjs 14 throws an error since children (reactnode) is not serializable. Therefore, I cannot pass it to the context componrnt.
What's the solution?
I'm using nextjs 14 with next auth session context.
Error: "Only plain objects ... can be passed to client compo from server compo. Classes or null prototypes are not allowed."
Code:
What I tried:
I tried making layout to be client rendering and retrieve session from server action or api. It threw another error saying that app directory must be binded or something. I assumed that layout should be server rendering (I might be wrong).
Any help would be great. Thanks.
What's the solution?
I'm using nextjs 14 with next auth session context.
Error: "Only plain objects ... can be passed to client compo from server compo. Classes or null prototypes are not allowed."
Code:
export async function Layout({children}){
//...
return <SessionContext session={session}>{children}</SessionContext>
}
What I tried:
I tried making layout to be client rendering and retrieve session from server action or api. It threw another error saying that app directory must be binded or something. I assumed that layout should be server rendering (I might be wrong).
Any help would be great. Thanks.
28 Replies
American black bearOP
It's just a context imported from next-auth but turned it into client rendering like this: (since context isn't available in server side)
"use client"
import {SessionProvider} from "next-auth"
export SessionProvider;@American black bear It's just a context imported from next-auth but turned it into client rendering like this: (since context isn't available in server side)
tsx
"use client"
import {SessionProvider} from "next-auth"
export SessionProvider;
try this
export default function SessionContext({children}) {
const session = ...
return <SessionProvider session={session}>{children}</SessionProvider/>
}indeed the problem is your session not the children object
Nextjs needs to integrate a transformer like superjson
Sessions usually have a Date object or undefined values which are not serialisable
They had the idea of a shared context they called "server context" (name which I used for another purpose too because it's dead ambiguous) understood as a shared context but it has been dropped atm
for now I think you have to come up with smth more manual
I wouldn't be super confident with non serializable values being passed around though it tingles my security breach 6th sense
Dates are serializable just annoying to deserialize
Yeah serialising any value is a very dangerous task
But many serialisable values like Date or undefined, which are very common, are not allowed by nextjs unfortunately
@Ray try this
tsx
export default function SessionContext({children}) {
const session = ...
return <SessionProvider session={session}>{children}</SessionProvider/>
}
American black bearOP
I have tried it too.
@Eric Burel indeed the problem is your session not the children object
American black bearOP
No, I have already confirmed it.(I believe)
By removing the session for experiment.
@American black bear No, I have already confirmed it.(I believe)
ah sorry didn't get the message
however it should be possible to render children this way
this is a totally fine pattern, you can interleave RSCs and client as long as the rendering component is an RSC, you can even pass them via props
ReactNode is serializable
so something else mightr be off here
American black bearOP
I'm not sure what else might be causing it. I removed the children and it didn't throw an error. So I thought children might be causing it.
it's probably the specific page then
that is buggy
not the layout
it's not the fact that you pass children to a client component but the bug is in the underlying page
@Eric Burel it's not the fact that you pass children to a client component but the bug is in the underlying page
American black bearOP
Oh that might be true. Because it didn't appear in one page
I'll check once again how they are different