Showcasing useSharedState - Real-time state sharing without prop drilling!
$99.99
$24.99
$39.99
🛍️ Component A: Product catalog that adds items to shared cart state. Watch Component B update instantly when you add items!
Your cart is empty
Add items from Component A to get started!
🛒 Component B: Shopping cart that shares state with Component A. Add items from the catalog and watch them appear here instantly!
This component demonstrates persistent user settings that survive page refresh and sync across browser tabs. Try opening this page in another tab to see real-time synchronization!
Name not set
Phone not set
Email not set
useSharedState is a revolutionary React hook that enables real-time state sharing between components without prop drilling, context providers, or complex state management libraries. Built on top of SWR's caching mechanism, it provides a simple, elegant solution for cross-component state synchronization. Plus, keys prefixed with @ are automatically persisted to localStorage and synchronized across tabs!
No providers, reducers, or complex setup required.
Changes propagate instantly across all components.
Share only what you need with key-based access.
Full TypeScript support with generic types.
Keys with @ prefix persist to localStorage automatically.
Persistent state syncs across browser tabs in real-time.
key - Unique identifier for the shared state. Keys starting with @ are automatically persisted to localStorage and synced across tabs.initialValue - Optional initial value (used only on first access)[value, setValue] - Current value and setter function (just like useState!)
Uses a Map to store shared state with string keys for efficient access.
Leverages SWR's caching and revalidation system for state synchronization.
When state changes, SWR's mutate() triggers re-renders across all subscribed components.
| Feature | useSharedState | Redux | Context |
|---|---|---|---|
| Setup | ✅ Zero | ❌ Complex | ⚠️ Providers |
| Performance | ✅ Optimized | ✅ Good | ❌ Re-renders |
| Learning Curve | ✅ Minimal | ❌ Steep | ⚠️ Moderate |
@ prefix for data that should persist across page refreshes