1

Suppose I have machine with states A, ..., Z.

I want the machine to go to state Z if the machine receives the event { type: "END_OF_THE_WORLD" }, regardless in which state it is.

Is it possible to define a transition that applies to all states, or do I have to define it in all the states, one by one?

I tried to find a way, but I think it is not possible with the current implementation of Xstate. Just asking, if I did not overlook something.

1 Answers1

4

If I understand the use case correctly, I think you can go with a root event:

const machine = createMachine({
  initial: "A",
  on: { END_OF_THE_WORLD: { target: ".Z" } },
  states: { A: {}, B: {}, Z: {} }
});

This way, regardless of the machine current state, the END_OF_THE_WORLD will always be available.

David Khourshid
  • 4,918
  • 1
  • 11
  • 10
z_lander
  • 104
  • 1
  • 8