Composition, Children & Serialization Across the Boundary
Server and Client Components are designed to be interleaved, not separated into two isolated applications.
A common production tree looks like:
Server Page
βββ Server ProductSummary
βββ Client Modal
β βββ Server Cart
βββ Client AddToCart
The key is understanding which direction module imports can flow and which values can cross the server/client transport boundary.
Server can render Clientβ
This is the normal pattern:
import AddToCart from './add-to-cart'
export default async function ProductPage() {
const product = await getProduct()
return (
<main>
<h1>{product.name}</h1>
<AddToCart productId={product.id} />
</main>
)
}
ProductPage remains a Server Component.
AddToCart owns the interactive client behavior.
Client should not import server-only implementationβ
Avoid this architecture:
'use client'
import { ServerProductList } from './server-product-list'
The client module graph cannot simply pull server-only component implementation into the browser.
Instead, let a Server Component compose the tree and pass server-rendered UI into the Client Component.
The children slot patternβ
Client shell:
'use client'
import { useState } from 'react'
export default function Modal({ children }: { children: React.ReactNode }) {
const [open, setOpen] = useState(false)
return (
<>
<button onClick={() => setOpen(true)}>Open cart</button>
{open ? <div role="dialog">{children}</div> : null}
</>
)
}
Server content:
import Modal from './modal'
import Cart from './cart'
export default function Page() {
return (
<Modal>
<Cart />
</Modal>
)
}
Cart can remain a Server Component because the parent Server Component creates the element and passes it as content to the Client Component.
The Client Component receives a reference to rendered server content; it does not import the server implementation into the client graph.
Think in ownership, not visual nestingβ
Visual structure:
Modal
βββ Cart
might suggest βCart is a child of a Client Component, therefore Cart is client-side.β
That is wrong.
Module ownership matters more than visual nesting.
Server Page creates:
<Modal>
<Cart />
</Modal>
Modal = client module
Cart = server module
React's server/client transport can compose the rendered result together.
Props crossing server to client must be serializable by Reactβ
Example:
export default async function Page() {
const product = await getProduct()
return (
<ProductPanel
id={product.id}
name={product.name}
price={product.price}
/>
)
}
Simple data values are usually the safest boundary contract.
Avoid thinking βmust be JSON only.β React's server transport supports a defined serializable model that is broader than basic JSON in some cases.
But for application architecture, a reliable rule is:
Cross the boundary with intentional data contracts, not arbitrary application objects.
Functions are not normal serializable propsβ
This will fail conceptually:
<ClientButton onClick={() => console.log('clicked')} />
when the function is being created in a Server Component and passed as a normal callback prop.
The function implementation cannot be serialized like a string or number.
Later, Server Functions introduce a special transportable server reference. That is a distinct feature, not normal function serialization.
Prefer IDs and data over rich server objectsβ
Bad boundary:
<ClientEditor user={databaseUserModelInstance} />
Better:
<ClientEditor
user={{
id: user.id,
name: user.name,
role: user.role,
}}
/>
Why?
- smaller payload
- explicit contract
- less accidental data exposure
- easier testing
- fewer serialization surprises
Serialization is also a security boundaryβ
Do not pass an entire database record just because the client UI uses two fields.
Unsafe pattern:
return <AccountCard account={account} />
where account also contains:
passwordHash
internalNotes
fraudScore
billingProviderCustomerId
adminFlags
Even if the Client Component ignores those fields, values sent across the boundary may become observable in the client transport/runtime.
Create a public view model:
type AccountCardData = {
id: string
displayName: string
avatarUrl: string | null
}
and send only what the browser needs.
Boundary DTO patternβ
Server mapping:
function toPublicProject(project: ProjectRecord) {
return {
id: project.id,
name: project.name,
status: project.status,
}
}
Use it at the boundary:
<ProjectActions project={toPublicProject(project)} />
This makes client exposure auditable.
Passing promises for streaming client consumptionβ
Current React/Next.js patterns can pass a Promise from the server into a Client Component and resolve it with React's use() API.
Server:
import { Suspense } from 'react'
import UserPanel from './user-panel'
export default function Page() {
const userPromise = getUser()
return (
<Suspense fallback={<p>Loading userβ¦</p>}>
<UserPanel userPromise={userPromise} />
</Suspense>
)
}
Client:
'use client'
import { use } from 'react'
export default function UserPanel({
userPromise,
}: {
userPromise: Promise<{ name: string }>
}) {
const user = use(userPromise)
return <p>{user.name}</p>
}
This can let server-side work start early while a Client Component consumes the result.
Use it deliberately; the data-fetching and streaming phases will cover when this pattern is preferable to awaiting on the server.
Server elements as named slotsβ
children is not the only option.
'use client'
export function DashboardFrame({
navigation,
content,
}: {
navigation: React.ReactNode
content: React.ReactNode
}) {
return (
<div className="dashboard">
<aside>{navigation}</aside>
<main>{content}</main>
</div>
)
}
Server composer:
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<DashboardFrame
navigation={<ServerNavigation />}
content={children}
/>
)
}
The Client Component owns client interaction/layout behavior while the server can own the rendered content slots.
Do not turn every slot into a client boundaryβ
If DashboardFrame does not need state, events, browser APIs, or client hooks, it should probably remain a Server Component.
A Client Component wrapper should have a reason.
Ask:
What browser capability does this wrapper own?
If the answer is βnone,β remove the boundary.
Data ownership exampleβ
Suppose a product page has:
product details
reviews
cart button
recommendations
A strong composition:
Server ProductPage
βββ Server ProductDetails
βββ Client AddToCart
βββ Server Reviews
βββ Server Recommendations
The browser receives interactive code only for AddToCart, while the server can directly fetch/render the other data.
Bad pattern: callback plumbing from serverβ
You may be tempted to create:
<ClientTable
rows={rows}
onDelete={(id) => deleteRow(id)}
/>
from a Server Component.
Instead, choose an actual mutation architecture:
- Server Function
- Route Handler/API request
- client-local interaction that does not mutate server state
Do not smuggle arbitrary server callbacks through the boundary.
Bad pattern: duplicating the same data on both sidesβ
Server fetches user
β
Client receives user
β
Client immediately fetches /api/user again
That may be justified for live revalidation, but it should not happen accidentally.
Define ownership:
initial server state
client revalidation policy
mutation invalidation policy
Later phases cover those mechanics in depth.
Hydration mismatch and serializable stateβ
Even serializable props can cause hydration issues if the Client Component renders nondeterministically.
Problem:
'use client'
export function Greeting() {
return <p>{new Date().toLocaleTimeString()}</p>
}
The prerendered server time and browser render time may differ.
Boundary correctness includes both:
serializable transport
+
deterministic initial render
Server-to-client data minimizationβ
For each prop ask:
- Does the client truly need this field?
- Can the server render this instead?
- Is the data sensitive?
- Is the object large?
- Is this value stable enough for initial client state?
- Will the client immediately refetch it anyway?
This improves security and payload efficiency together.
Debugging serialization errorsβ
When a boundary fails:
- Find the Server Component creating the Client Component.
- Inspect every prop crossing that edge.
- Remove functions/class instances/opaque library objects.
- Convert domain records into explicit public data.
- Check whether a Promise/Server Function is being used according to its supported transport contract.
- Run the production build.
Architecture diagramβ
SERVER MODULE GRAPH
page.tsx
βββ getProduct()
βββ ProductDetails
βββ Reviews
βββ creates <InteractivePanel data={publicData}>
β
β serialized RSC boundary
βΌ
CLIENT MODULE GRAPH
interactive-panel.tsx ('use client')
βββ useState
βββ browser events
βββ client-only dependencies
Server-rendered ReactNode slots may be passed into the client shell
without importing their server implementation into the client graph.
Interview questionsβ
Can a Client Component visually contain a Server Component?
Yes, when a Server Component higher in the tree creates the server element and passes it as children or another ReactNode prop into the Client Component.
Can a Client Component directly import arbitrary Server Component implementation?
That breaks the intended module-graph model. Compose server elements from a Server Component and pass them into the client shell.
Are Client Component props limited to JSON?
The formal rule is that they must be serializable by React's server/client transport. In application design, prefer explicit minimal data contracts.
Why is serialization also security-relevant?
Anything sent to the browser should be considered client-visible. Avoid passing sensitive or unnecessary server record fields.
Can a normal server callback be passed to a Client Component?
No. Arbitrary function closures are not normal serializable props. Server Functions are a separate supported mechanism.
Exerciseβ
Build a cart modal where:
Modalis a Client Component with open/close stateCartis a Server ComponentPagecomposes<Modal><Cart /></Modal>- cart line items are rendered on the server
- only minimal product IDs/prices needed for client interaction cross the boundary
- no database model instance or server callback is passed to the client
Then draw the server module graph, client module graph, and the props/ReactNode values crossing between them.