useObserver - Memorio
⚛️ React Only: This is a React hook and only works within React components
useObserver is a React hook for observing state changes. It automatically subscribes to state changes and includes powerful auto-discovery features.
Installation
npm install memorio
import 'memorio';
Quick Examples
Example 1: Basic Usage
import { useObserver } from 'memorio';
function Counter() {
useObserver(() => {
console.debug('Counter changed:', state.counter);
}, [state.counter]);
return <div>{state.counter}</div>;
}
Auto-Discovery (Magic Mode)
// Pass your callback WITHOUT dependencies - it auto-discovers!
function MyComponent() {
useObserver(() => {
// This will automatically track ALL state properties used inside
console.debug('Something changed:', state.user.name, state.items.length);
});
return <div>{state.user.name}</div>;
}
Example 2: Intermediate
function UserProfile() {
const [localState, setLocalState] = useState(null);
useObserver(() => {
setLocalState(state.user);
}, [state.user]);
return <div>{localState?.name}</div>;
}
Example 3: Advanced
// Multiple watchers with array
function MultiWatch() {
useObserver(() => {
console.debug('A or B changed:', state.a, state.b);
}, [state.a, state.b]);
return <div>{state.a} - {state.b}</div>;
}
// With string path (for store) function StoreWatcher() { useObserver(() => { console.debug('Store changed'); }, 'store.userPreferences');
return
; }
---
## API Reference
### useObserver(callback, deps)
| Parameter | Type | Description |
| --------- | ---- | ----------- |
| `callback` | `function` | Function to run on change |
| `deps` | `function \| string \| array` | State path(s) to watch |
### Callback Parameters
```javascript
useObserver((newValue, oldValue) => {
console.debug('Changed:', newValue);
}, [state.key]);
Auto-Discovery Mode
When deps is omitted, useObserver automatically discovers all state properties accessed inside the callback:
// No deps needed - magic auto-discovery!
useObserver(() => {
// Automatically tracks state.user, state.items, state.counter
console.log(state.user.name, state.items.length, state.counter);
});
Returns a cleanup function:
useObserver vs observer
| Feature | observer | useObserver |
|---|---|---|
| Framework | Vanilla JS | React |
| Auto-cleanup | Manual | Auto |
| React lifecycle | No | Yes |
Common Patterns
Sync with useState
function MyComponent() {
const [data, setData] = useState(null);
useObserver((newVal) => {
setData(newVal);
}, [state.data]);
return <div>{data}</div>;
}
Multiple Watchers
}, [state.a, state.b]);
Best Practices
- Always use in React components
- Use auto-discovery for simpler code:
useObserver(() => { ... }) - No manual cleanup needed - returns cleanup function automatically
- Use with state for reactive UI
- Check console for auto-discovery logs
Disclaimer
All content on this website, including text and images, has been generated using artificial intelligence technologies. While every effort is made to ensure quality and coherence, I do not assume responsibility for any inaccuracies, errors, or interpretations resulting from the use of this material.
Copyright and Intellectual property
All content on this website is copyrighted. Any copying, reproduction, distribution, or use of materials in any form is strictly forbidden without prior written permission. Violations will be subject to legal action in accordance with copyright law. We appreciate your understanding and compliance.
All content on this website, including text and images, has been generated using artificial intelligence technologies. While every effort is made to ensure quality and coherence, I do not assume responsibility for any inaccuracies, errors, or interpretations resulting from the use of this material.
Copyright and Intellectual property
All content on this website is copyrighted. Any copying, reproduction, distribution, or use of materials in any form is strictly forbidden without prior written permission. Violations will be subject to legal action in accordance with copyright law. We appreciate your understanding and compliance.