Skip to content

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 changes
const 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();