admin管理员组文章数量:1026989
I am new to xstate, and I'm trying to use it in an application where a user can request different things in an application, based on parent state and/or sub-state. However, there are some requests that the user should be able to make, no matter what state/sub-state the app is in. The response to those events is the same, no matter what the previous state was. How can I configure this event, so that I don't have to repeat define it under all states/sub-states?
I am new to xstate, and I'm trying to use it in an application where a user can request different things in an application, based on parent state and/or sub-state. However, there are some requests that the user should be able to make, no matter what state/sub-state the app is in. The response to those events is the same, no matter what the previous state was. How can I configure this event, so that I don't have to repeat define it under all states/sub-states?
Share Improve this question edited Apr 23, 2020 at 19:33 curiousWebDev asked Apr 23, 2020 at 17:56 curiousWebDevcuriousWebDev 351 silver badge4 bronze badges1 Answer
Reset to default 7Yes - the algorithm for choosing transitions is similar to DOM event propagation, in that it searches from leaf nodes to root node.
You can define transitions on the root node (top-level) which will be handled in any state naturally:
import { createMachine } from 'xstate';
const machine = createMachine({
// ...
// top-level transitions
on: {
ESC: {/* ... */}
},
states: {
// ...
someState: {
on: {
ESC: {/* override top-level transition */}
}
}
}
});
I am new to xstate, and I'm trying to use it in an application where a user can request different things in an application, based on parent state and/or sub-state. However, there are some requests that the user should be able to make, no matter what state/sub-state the app is in. The response to those events is the same, no matter what the previous state was. How can I configure this event, so that I don't have to repeat define it under all states/sub-states?
I am new to xstate, and I'm trying to use it in an application where a user can request different things in an application, based on parent state and/or sub-state. However, there are some requests that the user should be able to make, no matter what state/sub-state the app is in. The response to those events is the same, no matter what the previous state was. How can I configure this event, so that I don't have to repeat define it under all states/sub-states?
Share Improve this question edited Apr 23, 2020 at 19:33 curiousWebDev asked Apr 23, 2020 at 17:56 curiousWebDevcuriousWebDev 351 silver badge4 bronze badges1 Answer
Reset to default 7Yes - the algorithm for choosing transitions is similar to DOM event propagation, in that it searches from leaf nodes to root node.
You can define transitions on the root node (top-level) which will be handled in any state naturally:
import { createMachine } from 'xstate';
const machine = createMachine({
// ...
// top-level transitions
on: {
ESC: {/* ... */}
},
states: {
// ...
someState: {
on: {
ESC: {/* override top-level transition */}
}
}
}
});
本文标签:
版权声明:本文标题:javascript - Using xstate, is it possible to configure an event that is applicable under all states and is handled in the same w 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745670529a2162429.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论