@xstate/[email protected]
@xstate/[email protected]
xstate
Release Notes
Minor Changes
#5537
28c9961Thanks @davidkpiano! - Effects enqueued viaenq.effect(...)now receive an enqueue object withtrigger,send, andgetSnapshot, so you can dispatch events back into the store after async work and read the latest state — without needing a reference to the store. This is especially useful withcreateStoreLogic(...), where the store is created per-instance and there's no store to close over.const storeLogic = createStoreLogic({ context: () => ({ foo: null, loading: false }), on: { fetchFoo: (context, event, enq) => { enq.effect(({ trigger }) => { myApi.requestFoo().then((response) => trigger.gotFoo({ response })); }); return { ...context, loading: true }; }, gotFoo: (context, event) => ({ ...context, foo: event.response, loading: false }) } });Use
triggerfor fully-typed dispatch;sendis a loosely-typed escape hatch for dynamically-constructed events. After anawait, thecontextargument is stale — usegetSnapshot()to read the current state:enq.effect(async ({ getSnapshot, trigger }) => { await someAsyncWork(); if (getSnapshot().context.loading) { trigger.done(); } });