Course contentsShow
← React
Lesson 1763 of 2,39641. State Management EcosystemFree lesson

Nested Objects and Arrays in Valtio

How Valtio's proxies automatically wrap nested data, making deep mutations reactive without spreading.

Nested Objects and Arrays in Valtio

What you'll learn: How Valtio automatically makes deeply nested data reactive without manual spreading or cloning.

The Magic of Deep Proxies

One of Valtio's most powerful features is that its proxies don't just wrap the top-level object—they automatically wrap everything inside it too. When you mutate a nested object or push to an array three levels deep, Valtio's proxy system detects that change and triggers re-renders for any component watching that specific path.

This means you can treat nested structures like plain JavaScript objects and arrays, mutating them directly without thinking about immutability chains like { ...state, user: { ...state.user, profile: { ...state.user.profile, name: newName }}}.

How It Works in Practice

When you create Valtio state containing nested data:

const state = proxy({
  users: [
    { id: 1, profile: { name: 'Alice', settings: { theme: 'dark' } } }
  ],
  metadata: { lastUpdated: null }
});

Every nested object and array is automatically wrapped in its own proxy. You can mutate at any depth:

// All of these work reactively:
state.users[0].profile.name = 'Alicia';
state.users[0].profile.settings.theme = 'light';
state.users.push({ id: 2, profile: { name: 'Bob', settings: {} } });
state.metadata.lastUpdated = Date.now();

Components using useSnapshot(state).users[0].profile.name will re-render only when that specific value changes—not when unrelated nested properties update.

The Deep Tracking Advantage

Think of it like a building with smart sensors on every door: you don't need to replace the entire floor when someone enters one room. Valtio tracks changes at every level, so mutations anywhere in the tree "just work" without manual intervention.

Key Takeaway: Valtio's proxies automatically wrap nested objects and arrays recursively, making deep mutations fully reactive without spreading or immutable update patterns—just mutate naturally and let the proxies handle tracking.