Performance: Automatic Render Optimization
What you'll learn: How Valtio's snapshot tracking means components only re-render when accessed properties change, not entire state.
The Magic of Granular Subscriptions
One of Valtio's standout features is that you don't need to manually optimize which parts of state trigger re-renders—it happens automatically. When a component uses useSnapshot(), Valtio tracks exactly which properties you read during render. Only changes to those specific properties will cause that component to re-render.
Think of it like a smart security system: instead of triggering the alarm for any movement anywhere in the building, it only alerts you when someone enters the specific rooms you're monitoring.
How It Works in Practice
import { proxy, useSnapshot } from 'valtio';
const state = proxy({
user: { name: 'Alice', email: 'alice@example.com' },
theme: 'dark',
notifications: 5
});
function UserProfile() {
const snap = useSnapshot(state);
// Only reads snap.user.name
return <h1>{snap.user.name}</h1>;
}
function ThemeToggle() {
const snap = useSnapshot(state);
// Only reads snap.theme
return <button>Theme: {snap.theme}</button>;
}
If you update state.notifications = 10, neither UserProfile nor ThemeToggle re-renders—because neither component accessed the notifications property. Only components that read snap.notifications would update.
This is drastically different from Context or Redux, where updating any part of the state object typically triggers all subscribed components (unless you add manual selectors).
No Optimization Work Required
You don't need selectors, memoization, or splitting state into multiple contexts. Valtio's proxy system does the tracking automatically at runtime, making fine-grained reactivity the default behavior, not an optimization you have to add later.
Key Takeaway: Valtio automatically tracks which properties each component reads via useSnapshot(), ensuring components only re-render when their specific accessed data changes—zero manual optimization required.