Skip to main content

Proxy Pattern: React State with Valtio

Updated 4 min readMarkdown
Proxy Pattern: React State with Valtio

Introduction

Managing state in React often starts with a simple idea: keep data in one place and update it when something changes. But as an application grows, state management can become more complicated than the UI itself.

Traditional approaches often require immutable updates, carefully structured reducers, selectors, and additional boilerplate to keep components synchronized with application state.

These patterns work, but they introduce an extra layer of complexity. Developers spend more time describing how state should change rather than focusing on what the application actually needs.

This is where Valtio offers a different approach.

Instead of relying on manually managed immutable updates, Valtio uses JavaScript Proxies to make state reactive. You work with ordinary JavaScript objects, while Valtio tracks changes behind the scenes

The result is a simpler way to manage shared state without sacrificing performance or control.

The Core Concept: JavaScript Proxies

At the heart of Valtio is the JavaScript Proxy object.

A Proxy allows you to intercept fundamental operations on another object, such as reading, writing, and deleting properties. This gives libraries the ability to observe how an object is being accessed and modified without changing how developers interact with it.

Valtio builds its state management model around this capability.

Instead of creating a reducer or defining update functions for every state transition, you can create a proxy around a regular JavaScript object.

import { proxy } from 'valtio'

const state = proxy({
  count: 0,
  user: { name: 'Alex' }
})

state.count++

The important detail is that the state looks and behaves like a normal JavaScript object.

You can directly modify its properties, update nested objects, and work with familiar JavaScript patterns. Valtio handles the tracking and notification process internally.

This removes much of the ceremony associated with traditional state management.

Fine-Grained Reactivity with useSnapshot

Making state reactive is only part of the solution. In React, the real challenge is knowing when a component should render again.

Valtio solves this through useSnapshot.

ather than giving components direct access to the mutable proxy, useSnapshot provides a snapshot of the current state. It also tracks which properties a component actually reads.

import { useSnapshot } from 'valtio'

function Counter() {
  const snap = useSnapshot(state)

  return <p>{snap.count}</p>
}

Here, the component depends only on count.

If another property changes, such as state.user.name, the Counter component does not need to re-render because of that unrelated update.

This is the key advantage of Valtio's fine-grained reactivity.

With many traditional state management approaches, developers may need to write selectors or carefully organize subscriptions to prevent unnecessary renders.

Valtio tracks property access automatically and uses that information to determine which components need to update.

This makes component subscriptions more precise without requiring additional configuration.

Beyond the Basics

Valtio also provides a useful foundation for patterns that commonly appear in modern frontend applications.

Persistence :

Application state can be synchronized with browser storage, such as localStorage, allowing selected data to survive page refreshes.

This is particularly useful for user preferences, interface settings, and lightweight application data. Persistence is handled separately from the proxy itself, keeping storage concerns independent from state updates.

Derived State :

Valtio supports derived values through JavaScript getters.

Instead of manually updating a calculated value whenever its dependencies change, you can define logic that derives it from existing state.

This keeps related data together while avoiding unnecessary synchronization between multiple state properties.

Both patterns demonstrate an important idea: Valtio does not attempt to replace JavaScript. It builds on the language's existing capabilities.

Valitio in one

Valtio takes a different approach to React state management by using JavaScript Proxies as its foundation.

It removes much of the boilerplate associated with immutable updates while preserving the simplicity of working with regular JavaScript objects.

More importantly, its snapshot-based subscription model provides fine-grained reactivity, allowing components to respond to the exact parts of state they depend on.

The architecture is straightforward:

  • Proxies make ordinary objects observable and mutable.
  • Snapshots provide a consistent view of application state.
  • Fine-grained tracking keeps React components synchronized with the data they actually use.

For modern frontend applications, this combination offers a practical balance between simplicity, performance, and maintainability.

Valtio is a good example of how understanding native JavaScript features can lead to cleaner abstractions and a better developer experience.

Sometimes, the best state management solution is not another layer of complexity, but a smarter way to work with the JavaScript we already know.

About the author

  • Software Engineer blending deep algorithmic problem solving with scalable Frontend Architecture. Obsessed with high-performance web systems, Core Web Vitals, accessibility, and modern UI engineering.

No comments yet. Be the first to comment.