How pmndrs/valtio works
Main Components Structure
a) Core Proxy System
• Proxy creation flow using proxy() function
• Two types of proxies:
• Mutation tracking proxy (write operations)
• Usage tracking proxy (read operations)
b) React Integration Layer
• Connection between proxy state and React components
• useSnapshot hook as the primary bridge
• Subscription system
c) Utilities Layer
• Utility functions (proxyMap, proxySet, devtools, etc.)
• How they extend the core functionality
Data Flow Representation
State Creation:
Raw Object → proxy() → Proxy State Object
React Integration:
Proxy State → useSnapshot() → Immutable Snapshot → React Component
State Updates:
Mutation → Proxy State → Subscription System → Component Re-render
Component Details
Specific elements:
Core:
• Proxy State Handler
• Snapshot Generator
• Subscription Manager
React Integration:
• useSnapshot Hook
• Component Re-render Optimization
• Suspense Integration
Utils:
• DevTools Connection
• Map/Set Proxies
• Watch/Subscribe Utilities
Important Relationships
• Proxy State ←→ Snapshot (bi-directional relationship)
• Component → Snapshot (read-only access)
• Mutation → Proxy State (write access)
• Subscription System → Components (update notification)
Implementation Details to Highlight
Key features:
• Automatic proxy creation for nested objects
• Subscription system architecture
• Snapshot optimization system
• React integration points