Pass data from guard to actions it allows to execute / actions in context of guards #1969
Unanswered
serhiipalash
asked this question in
Ideas
Replies: 3 comments 3 replies
-
Using multiple guards will create multiple transitions and it is a proper way. Context is a part of you state and part of your transition, even if you don't move from one distinct state to another. And for your exact case, you can use three transitions, like this: {
on: {
NAME__CHANGE: [
{
cond: "isValueMissing",
actions: ["setValue", "setBirthDateValueMissingError"],
},
{
cond: "isValueInTheFuture",
actions: ["setValue", "setBirthDateValueInTheFutureError"],
},
{
actions: ["setValue", "removeError"],
},
],
},
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
Hey, let's assume the Or how would you get the info which condition failed? |
Beta Was this translation helpful? Give feedback.
1 reply
-
There is no way to define something like onError right?
Mit freundlichen Grüßen
Gerwin Brunner
… On 05.08.2022, at 13:07, Mateusz Burzyński ***@***.***> wrote:
Consider this:
{
on: {
NAME__CHANGE: [
{
// IF
cond: "isValueMissing",
actions: ["setValue", "setBirthDateValueMissingError"],
},
{
// ELSE IF
cond: "isValueInTheFuture",
actions: ["setValue", "setBirthDateValueInTheFutureError"],
},
{
// ELSE
actions: ["setValue", "removeError"],
},
],
},
}
In this situation, the last actions will be executed when no previous transition gets selected. However, you won't be able to tell which specific guard didn't pass - after all, all of them didn't pass if you got to the ELSE "block" here.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi! I develop a lot of forms using XState. Form fields usually have validation. When value is changed, but not valid, I need to set value and show an appropriate error message for user. Today the machine looks something like this
Maybe the better Api is to do like this?
Beta Was this translation helpful? Give feedback.
All reactions