/** * Memorio React App Example * * This example shows how to use Memorio state in a React application. * * Run: npx ts-node --esm examples/react-app.tsx * Or copy to your React project */ import React, { useState, useEffect } from 'react' import 'memorio' // ============================================ // 1. SETUP - Import once at app start // ============================================ // In your main App.tsx or index.js: // import 'memorio' // ============================================ // 2. DEFINE YOUR STATE SHAPE // ============================================ interface AppState { user: { name: string email: string avatar?: string } | null theme: 'light' | 'dark' notifications: Notification[] cart: CartItem[] isLoading: boolean } interface Notification { id: string message: string read: boolean } interface CartItem { id: number name: string price: number quantity: number } // ============================================ // 3. USE STATE IN COMPONENTS // ============================================ // ------------------------------ // Header Component // ------------------------------ function Header() { // Using useObserver to react to state changes const [theme, setTheme] = useState(state.theme) useObserver(() => { setTheme(state.theme) }, [state.theme]) return (

My App

) } // ------------------------------ // User Profile Component // ------------------------------ function UserProfile() { const [user, setUser] = useState(state.user) // Auto-discover all state changes useObserver(() => { setUser(state.user) }) if (!user) { return
Please log in
} return (
{user.name}

{user.name}

{user.email}

) } // ------------------------------ // Login Form Component // ------------------------------ function LoginForm() { const [name, setName] = useState('') const [email, setEmail] = useState('') const handleSubmit = (e: React.FormEvent) => { e.preventDefault() // Set user state state.user = { name, email, avatar: `https://api.dicebear.com/7.x/avataaars/svg?seed=${name}` } // Also persist to store store.set('lastUser', name) } return (
setName(e.target.value)} placeholder="Name" /> setEmail(e.target.value)} placeholder="Email" />
) } // ------------------------------ // Shopping Cart Component // ------------------------------ function Cart() { const [items, setItems] = useState([]) useObserver(() => { setItems(state.cart || []) }, [state.cart]) const total = items.reduce((sum, item) => sum + item.price * item.quantity, 0) return (

Cart ({items.length})

{items.map(item => (
{item.name} - ${item.price} x {item.quantity}
))} Total: ${total}
) } // ------------------------------ // Add to Cart Button // ------------------------------ function AddToCartButton({ product }: { product: { id: number, name: string, price: number } }) { return ( ) } // ------------------------------ // Notifications Component // ------------------------------ function Notifications() { const [notifications, setNotifications] = useState([]) useObserver(() => { setNotifications(state.notifications || []) }, [state.notifications]) const unread = notifications.filter(n => !n.read).length return (
🔔 {unread} unread {notifications.map(n => (
{n.message}
))}
) } // ------------------------------ // Settings Component // ------------------------------ function Settings() { const [theme] = useState(state.theme) const [savedSettings, setSavedSettings] = useState(null) // Load saved settings from store useEffect(() => { const settings = store.get('appSettings') if (settings) { setSavedSettings(settings) } }, []) const saveSettings = () => { store.set('appSettings', { theme: state.theme }) alert('Settings saved!') } return (

Settings

{savedSettings && (

Saved: {savedSettings.theme}

)}
) } // ============================================ // 4. MAIN APP COMPONENT // ============================================ function App() { // Initialize default state useEffect(() => { // Load theme from store const savedTheme = store.get('appSettings')?.theme if (savedTheme) { state.theme = savedTheme } else { state.theme = 'light' } // Initialize notifications state.notifications = [ { id: '1', message: 'Welcome to Memorio!', read: false }, { id: '2', message: 'Check out our new features', read: false } ] // Check for returning user const lastUser = store.get('lastUser') if (lastUser) { console.debug('Welcome back,', lastUser) } }, []) return (
{state.user ? ( <> ) : ( )}
) } export default App // ============================================ // 5. USAGE SUMMARY // ============================================ /* MEMORIO REACT USAGE GUIDE: 1. IMPORT ONCE (index.js or App.tsx): import 'memorio' 2. SET STATE: state.user = { name: 'Mario', email: 'mario@example.com' } state.theme = 'dark' state.cart = [] 3. READ STATE: const value = state.user.name const theme = state.theme 4. REACT TO CHANGES: useObserver(() => { console.debug('State changed!') }, [state.user]) 5. AUTO-DISCOVERY (watch all): useObserver(() => { console.debug(state.user, state.theme) }) 6. PERSIST DATA: store.set('settings', { theme: 'dark' }) const settings = store.get('settings') 7. SESSION DATA (cleared on tab close): session.set('token', 'abc123') const token = session.get('token') 8. CLEAR DATA: state.removeAll() // Clear all state store.removeAll() // Clear all persisted session.removeAll() // Clear all session */