Sub topic 8
What can cross the boundary: serializable props
This chapter assumes you know React props and JSX, the difference between Server and Client Components, and what the "use client" directive does. It covers which values a Server Component can pass as props to a Client Component, and what happens when a value cannot be passed.
Why props must be serializable
A Server Component runs on the server. A Client Component runs in the browser. The props passed from one to the other are not handed over in memory. The server writes them into the RSC payload, which is a stream of text, and the browser reads them back. A value can only cross this boundary if it can be written into that stream and rebuilt on the other side. This is called being serializable.
What you can pass
The set of supported values is larger than plain JSON. The exact list depends on your React version, so check the documentation for the version you use. In general these are supported:
- Strings, numbers, booleans,
nullandundefined - Arrays and plain objects whose contents are also serializable
Date,Map,Set,BigIntand typed arrays- JSX elements, which includes Server Components rendered into elements
- Promises, which a Client Component can read with
use - Server Functions, passed by reference (see the chapter on Server Functions)
What you cannot pass
These values have no stable text form, so React rejects them or they break at runtime:
- Ordinary functions, such as event handlers defined in a Server Component
- Class instances, because their methods and prototype are lost
- Symbols that are not registered globally
- Objects with a null prototype or other special internals, such as database connections
// app/page.js (Server Component)
import Button from './Button';
export default function Page() {
// Error: functions cannot be passed to Client Components
return <Button onClick={() => console.log('clicked')} />;
}React reports an error such as "Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with 'use server'". The fix is to define the handler inside the Client Component, or to use a Server Function when the work must run on the server.
// app/Button.js
'use client';
export default function Button() {
return <button onClick={() => console.log('clicked')}>Click</button>;
}Class instances and plain objects
A common mistake is passing a model object from an ORM or a class. The instance may look like a plain object, but its prototype and methods do not survive. Convert it to a plain object first.
// app/page.js (Server Component)
import UserCard from './UserCard';
class User {
constructor(name, email) {
this.name = name;
this.email = email;
}
display() {
return this.name;
}
}
export default function Page() {
const user = new User('Ada', '[email protected]');
const plain = { name: user.name, displayName: user.display() };
return <UserCard user={plain} />;
}Mapping to a plain object has a second benefit. You choose exactly which fields are sent, so you do not leak fields the browser should not see.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| "Functions cannot be passed directly to Client Components" | An event handler or callback was passed as a prop | Define the function in the Client Component, or use a Server Function |
| Methods missing on an object in the browser | A class instance was passed | Pass a plain object with the needed fields |
| Extra private fields visible in the payload | A whole record was passed | Select only the needed fields before passing |
Exercise
A Server Component fetches a user record from a database and passes it to a Client Component as user={user}. The record has a passwordHash field. What are two problems with this, and how do you fix them?
Show answer
First, if user is a class instance, its methods and prototype are lost, so it should be converted to a plain object. Second, every field is serialized into the payload, so passwordHash is exposed to the browser. Fix both by building a plain object with only the needed fields, such as { name: user.name, email: user.email }, and passing that.