/**
* Memorio useObserver Example
*
* This example demonstrates the useObserver hook for React.
* useObserver automatically tracks state changes and triggers re-renders.
*
* Note: This example requires React to be present in the environment.
* Run in a React environment.
*/
import 'memorio'
// ============================================
// BASIC USEOBSERVER
// ============================================
// Example 1: Watch single value
useObserver(callback, [state.value])
function Counter() {
useObserver(() => {
console.debug('Count changed:', state.count)
}, [state.count])
return
{state.count}
}
// ============================================
// MULTIPLE DEPENDENCIES
// ============================================
// Example 2: Watch multiple values
function UserProfile() {
useObserver(() => {
console.debug('User or theme changed')
}, [state.user, state.theme])
return
{state.user.name}
}
// ============================================
// AUTO-DISCOVERY
// ============================================
// Example 3: No dependencies - auto-discovers all state access
function AutoDiscovery() {
useObserver(() => {
// Automatically tracks state.user, state.items, state.count
console.debug('Changed:', state.user.name, state.items.length, state.count)
})
return
{state.user.name}
}
// ============================================
// SYNC WITH USESTATE
// ============================================
// Example 4: Sync with useState
function SyncedComponent() {
const [localData, setLocalData] = useState(null)
useObserver((newValue) => {
setLocalData(newValue)
}, [state.data])
return