Course contentsShow
← React
Lesson 1595 of 2,39639. Server Components in PracticeFree lesson

Complex Objects: Maps, Sets, and Classes

Maps, Sets, and class instances don't serialize; convert them to plain arrays/objects before passing across boundaries.

Complex Objects: Maps, Sets, and Classes

You'll learn why Maps, Sets, and class instances break when crossing the Server-Client boundary, and how to fix them.

The Serialization Wall

When React sends data from Server Components to Client Components, it converts everything to a format that can travel over the network. This process is called serialization. Plain objects and arrays serialize just fine, but JavaScript's more complex built-in types don't make the trip.

What Breaks

Maps and Sets have no standard JSON representation. They're rich data structures with methods and internal state that can't be represented as simple key-value pairs.

Class instances are even trickier. They contain methods, private fields, and prototype chains. You can't reliably reconstruct a new User() instance on the client from serialized data—React doesn't know which constructor to call or how to restore the methods.

The Fix: Convert Before Crossing

Before passing these complex objects as props, convert them to plain JavaScript:

// ❌ This breaks
<ClientComponent userData={userMap} />

// ✅ Convert to plain object
<ClientComponent userData={Object.fromEntries(userMap)} />

// ❌ This breaks
<ClientComponent tags={tagsSet} />

// ✅ Convert to array
<ClientComponent tags={[...tagsSet]} />

// ❌ This breaks
<ClientComponent user={userInstance} />

// ✅ Spread to plain object
<ClientComponent user={{ id: userInstance.id, name: userInstance.name }} />

Think of it like packing luggage: you can't ship a working microwave, but you can ship its parts and reassemble on the other side.

Key Takeaway: Maps, Sets, and class instances must be converted to plain objects or arrays before passing from Server to Client Components—they don't survive serialization.