-
Consider the following machine: import { Machine, interpret } from 'xstate'
const machine = Machine({
id: 'test-machine',
initial: 'stepOne',
context: { error: false },
states: {
stepOne: {
exit: [() => {console.log('stepOne - exit')}]
},
beforeStepTwo: {
entry: [
() => {console.log('beforeStepTwo - entry')},
'validate-step-one'
],
always: [
{
target: 'error',
cond: ({ error }) => {
console.log('beforeStepTwo - always')
return error
}
},
{ target: 'stepTwo' }
]
},
stepTwo: {},
error: {}
},
on: {
CONTINUE_TO_STEP_TWO: 'beforeStepTwo'
}
}, {
actions: {
'validate-step-one': () => {
return {
error: true
}
}
}
})
const interpretedMachine = interpret(machine).start()
interpretedMachine.send('CONTINUE_TO_STEP_TWO') This results in the following being logged:
I find this very strange. Why is Is my only option to add another state? So should I do something like this:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Currently, custom actions (both of your I totally understand why this might be confusing. This issue seems to have popped up several times recently. Your example seems to be a little bit broken though - shouldn't |
Beta Was this translation helpful? Give feedback.
Currently, custom actions (both of your
beforeStepTwo.entry
actions are custom) are executed after the machine settles a particular macro transition. OTOH, guards are always executed within the macrostep as they are needed to determine what transitions should be taken.I totally understand why this might be confusing. This issue seems to have popped up several times recently.
Your example seems to be a little bit broken though - shouldn't
validate-step-one
be anassign
action?