Sub topic 6
What can cross the network: serialization rules
This chapter assumes you can write Server Functions with "use server" and call them from a Client Component. It covers which values you can pass to a Server Function and which values it can return.
Why serialization matters
A Server Function runs on the server, but the caller runs in the browser. A call is an HTTP request, and a return value travels back in the response. React must convert arguments and results to a wire format and rebuild them on the other side. This conversion is called serialization. A value that React cannot convert cannot cross the network.
Values you can send
These are safe as arguments and return values:
- Primitives: string, number, bigint, boolean,
undefinedandnull. - Symbols registered with
Symbol.for. - Iterables of serializable values: arrays,
Map,Set, typed arrays andArrayBuffer. DateandFormDatainstances.- Plain objects, meaning objects created with
{}whose properties are all serializable. - Server Functions themselves.
- Promises.
The check is recursive. An array is only valid if every item in it is valid.
Values you cannot send
- React elements (JSX).
- Functions that are not Server Functions, including event handlers and component functions.
- Instances of your own classes.
- Objects with a null prototype.
- Symbols that were not registered with
Symbol.for.
Anything that is not a plain object, or a built-in type from the list above, fails.
A working example
This Server Function accepts a Date and a Map, and returns a plain object containing a Set.
// app/actions.js
'use server';
export async function summarize(since, scores) {
const names = new Set();
let total = 0;
for (const [name, score] of scores) {
names.add(name);
total += score;
}
return {
since: since.toISOString(),
names,
total,
};
}The Client Component below calls it.
// app/Summary.js
'use client';
import { useState } from 'react';
import { summarize } from './actions';
export function Summary() {
const [result, setResult] = useState(null);
async function run() {
const scores = new Map([['ada', 3], ['lin', 5]]);
setResult(await summarize(new Date('2026-01-01'), scores));
}
return (
<div>
<button onClick={run}>Summarize</button>
{result && (
<p>{[...result.names].join(', ')}: {result.total}</p>
)}
</div>
);
}The Date and Map arrive on the server as a real Date and a real Map. The Set arrives back in the browser as a real Set. This is different from JSON.stringify, which would turn a Map into {}.
Common errors
The most frequent mistake is passing a class instance. It looks like an object, but React rejects it.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
// Fails: Point is a class instance, not a plain object.
await createShape(new Point(1, 2));
// Works: send plain data and rebuild the class on the server.
await createShape({ x: 1, y: 2 });Two other errors are worth knowing:
- Returning an
Erroror a database row object. Many database libraries return class instances or rows with extra prototypes. Map the result to a plain object before returning it. - Passing an event handler or callback. A callback defined in the browser cannot run on the server. Pass data in, and handle the result in the browser after the call returns.
// app/actions.js
'use server';
import { db } from './db';
export async function getNote(noteId) {
const row = await db.notes.find(noteId);
if (!row) return null;
// Copy only the fields the client needs into a plain object.
return { id: row.id, title: row.title };
}Copying fields also keeps private columns, such as an owner's email, from leaking to the browser.
Exercise
A Server Function returns { user: new User('ada') }, where User is your own class. The call fails. What is the cause, and how do you fix it?
Show answer
A class instance is not a plain object, so React cannot serialize it. Return a plain object instead, for example { user: { name: 'ada' } }, built from only the fields the client needs.
Exercise
Which of these can you pass as an argument to a Server Function: a Date, an onClick handler, a Set of strings, a JSX element?
Show answer
A Date and a Set of strings. The onClick handler is not a Server Function, and a JSX element is not serializable.