Subscriptions
Advanced Pattern: State Change Subscriptions
Section titled “Advanced Pattern: State Change Subscriptions”For observing state changes without affecting them, you can use subscribe
:
// Subscribe to all state changesconst unsubscribe = counter.subscribe((state) => { console.log(`State changed to: ${state.key}`);
if (state.is("Counting")) { console.log(`Current count: ${state.data.count}`); }});
// Later, when no longer needed:unsubscribe();