DEV Community

Alex Spinov
Alex Spinov

Posted on

Valtio Has a Free Proxy-Based State API That Simplifies React

Valtio turns plain JavaScript objects into reactive state with zero boilerplate. Just mutate your object — React components re-render automatically.

Basic Usage

npm install valtio
Enter fullscreen mode Exit fullscreen mode
import { proxy, useSnapshot } from 'valtio';

const state = proxy({
  count: 0,
  todos: [],
  filter: 'all'
});

function Counter() {
  const snap = useSnapshot(state);
  return (
    <div>
      <p>Count: {snap.count}</p>
      <button onClick={() => state.count++}>+1</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

That is it. No reducers, no actions, no selectors, no context providers.

Advanced Patterns

Derived State with derive

import { derive } from 'valtio/utils';

const state = proxy({ todos: [], filter: 'all' });

const derived = derive({
  filtered: (get) => {
    const { todos, filter } = get(state);
    if (filter === 'done') return todos.filter(t => t.done);
    if (filter === 'pending') return todos.filter(t => !t.done);
    return todos;
  },
  count: (get) => get(state).todos.length
});
Enter fullscreen mode Exit fullscreen mode

Subscriptions Outside React

import { subscribe } from 'valtio';

const unsub = subscribe(state, () => {
  console.log('State changed:', state.count);
  localStorage.setItem('app-state', JSON.stringify(state));
});
Enter fullscreen mode Exit fullscreen mode

DevTools Integration

import { devtools } from 'valtio/utils';
devtools(state, { name: 'app-state' });
Enter fullscreen mode Exit fullscreen mode

Nested Object Tracking

const state = proxy({
  user: { name: 'Alice', settings: { theme: 'dark' } }
});

// Deep mutations just work
state.user.settings.theme = 'light'; // triggers re-render
Enter fullscreen mode Exit fullscreen mode

Why This Matters

  • Zero boilerplate compared to Redux/Zustand
  • Mutable API that feels natural to JavaScript developers
  • Automatic render optimization — only re-renders what changed
  • Works outside React — subscribe from anywhere
  • Tiny bundle — ~3KB gzipped

Need custom React tooling or frontend architecture consulting? I build developer tools and data pipelines. Check out my web scraping actors on Apify or reach out at spinov001@gmail.com for custom solutions.

Top comments (0)