采用以下有限状态机:
const machine = Machine({
initial: "foo",
states: {
foo: {
on: {
BAZ: "baz",
QUX: "qux",
},
},
bar: {
on: {
BAZ: "baz",
QUX: "qux",
},
},
baz: {
on: {
FOO: "foo",
BAR: "bar",
},
},
qux: {
on: {
FOO: "foo",
BAR: "bar",
},
},
},
});
请注意,有两组重复的状态转换:
on: {
FOO: "foo",
bar: "bar",
}
on: {
BAZ: "baz",
QUX: "qux",
}
除了将状态转换定义为机器定义之外的良好 JavaScript 对象之外,是否有一种特殊的方法来做到这一点?
最佳答案
Other than defining the state transitions as good ol' JavaScript objects outside of the Machine definition
你是在自问自答!您已经知道如何对这些数据进行重复数据删除,因此将它们定义为外部对象并在机器内引用它们。不需要特殊的 API 来执行此操作。
关于javascript - 如何在 xstate 中重用状态转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58917680/