Proxy-Based State: How Valtio Tracks Mutations
What you'll learn: How Valtio achieves automatic reactivity by wrapping state objects in ES6 Proxies that detect mutations without manual update functions.
The Magic Behind Mutable State
Unlike most React state solutions where you call setState or dispatch to signal changes, Valtio lets you mutate state directly and automatically detects those changes. This works through ES6 Proxies—JavaScript objects that intercept operations like property reads and writes.
Think of a Proxy as a security guard standing in front of your state object. Every time you try to read (state.count) or write (state.count = 5), the guard intercepts that action and takes notes. When you write, Valtio's Proxy records which properties changed. When React components read those properties, Valtio remembers which component cares about which piece of state.
How It Works
When you create state with proxy(initialState), Valtio wraps your object in a Proxy. This wrapper:
- Tracks reads during component render—knowing this component accessed
state.user.name - Tracks writes when you mutate—knowing
state.user.namejust changed - Triggers re-renders only in components that read the changed property
This creates a seamless developer experience: you write code that looks synchronous and imperative (state.count++), but Valtio handles the reactive plumbing behind the scenes.
Why This Matters
You avoid the boilerplate of immutable updates, selectors, and action creators. State feels like working with plain JavaScript objects, yet React stays in sync automatically. The Proxy intercepts every mutation, making reactivity "just work."
Key Takeaway: Valtio uses ES6 Proxies to intercept property access and mutations, automatically tracking which components depend on which state slices—enabling direct mutations while maintaining React's reactive model.