我有 xstate 反应脚本,用户填写表格,然后按提交。提交时,xstate 收到了一个 send("VALIDATE", {formData}) ,它通过验证表单的服务运行。成功后,脚本将转换到目标:“成功”,我需要最终的“成功”状态来调用实际上保存脚本的外部函数。
我可以将数据获取到验证器函数中,但是在onDone之后,后续的成功状态似乎看不到数据。
如何将数据从验证事件连接到成功事件?
id: 'validator',
initial: 'populating',
context: {},
states: {
populating: {
on: {
VALIDATE: 'validating'
}
},
validating: {
invoke: {
src: (context, data) => doValidate(data),
onDone: {
target: 'success',
actions: assign({ data: "hello world"})
},
onError: 'failure'
}
},
success: {
invoke: {
// I do see the hello world here, but what I want is the 'data' from the doValidate(data)
src: (ctx)=>{console.log("invoked success, what can I see here: ", ctx)}
}
},
我通过以下方式触发验证:send("VALIDATE", formData)