How to make sure an action fires before continuing with a transition? #1630
-
Suppose we have a machine that's started with a So we make an action called actions: {
processStart: (context, event) => {
context.option = event.option
},
}, and we call that when we get a on: {
START: {
actions: 'processStart',
target: 'initializing',
},
}, Then the initializing: {
always: [
{
cond: 'optionIsOne',
target: 'one',
},
{
cond: 'optionIsTwo',
target: 'two',
},
{
// invalid option
target: 'failure',
},
],
} using guards that just check the value of guards: {
optionIsOne: (context) => {
return context.option === 1
},
optionIsTwo: (context) => {
return context.option === 2
},
}, What happens is that the guards are called before
How can I get the information from that initial payload onto context before the guards run? I've made a working example here: https://codesandbox.io/s/xtate-conditions-before-actions-qvebh?file=/src/index.js |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Note: In the example above, I realize that I could just bring the logic from on: {
START: [
{
cond: 'optionIsOne',
actions: 'processStart',
target: 'one',
},
{
cond: 'optionIsTwo',
actions: 'processStart',
target: 'two',
},
{
// invalid option value
target: 'failure',
},
],
}, ... and in the guards, examine My actual situation is a bit more complicated, so FWIW I've created a second CodeSandbox that's more like the real thing: https://codesandbox.io/s/xtate-conditions-before-actions-2-oxdbs?file=/src/index.js In this example, we get two properties with the one: {
always: [
{
cond: 'isAuthenticated',
target: 'authenticated',
},
{
cond: 'isNotAuthenticated',
target: 'notAuthenticated',
},
{
// invalid auth value
target: 'failure',
},
],
},
authenticated: {},
notAuthenticated: {}, But by the time we get to the
|
Beta Was this translation helpful? Give feedback.
-
Note that you should always use That being said - the order in which core and custom actions are called is a known issue right now. We are aware of it and I hope to fix this in the next major version. |
Beta Was this translation helpful? Give feedback.
Note that you should always use
assign
action to manipulate the context value and if you would do this then things would work as expected for you.That being said - the order in which core and custom actions are called is a known issue right now. We are aware of it and I hope to fix this in the next major version.