Closed ersinakinci closed 3 months ago
Never mind, I figured out what I was doing wrong. The function needs to be assigned to the params
property rather than being an element of the actions
array. This works:
actions: {
type: "persistDraft",
params: ({ event }) => ({ data: event.output }),
},
Hey @ersinakinci - as you probably gathered, this type of dynamic action is not supported:
actions: ({ event }) => {
console.log(event.output); // for example, prints { name: "Testing" }
// This action object never triggers an execution
return {
type: "persistDraft",
params: { data: event.output },
};
},
The reason is because XState needs to assume that executing this function would be impure. You can use enqueueActions(…)
instead:
actions: enqueueActions(({ event, enqueue }) => {
// ...
enqueue({
type: 'persistDraft',
params: { data: event.output }
})
})
But in your case, the dynamic params solution is better 👍
XState version
XState version 5
Description
Not sure whether this is a bug or a feature request 😄
When I invoke a promise actor, I can use a function to generate an action object to execute an action during
onDone
:The function definitely runs, but the action object that it returned doesn't seem to trigger any execution.
When I use
assign
directly, however, I can trigger an action execution:Expected result
I expect the
persistDraft
action that I've defined in the actions section of mysetup
config to execute when I return an object with the shape{ type: "persistDraft" }
from ainvoke.onDone.actions
function.Actual result
The
onDone
function executes and returns an action object, but it doesn't trigger the associated action.Reproduction
https://stackblitz.com/edit/github-6l6yru?file=src%2FfeedbackMachine.ts
Additional context
No response