Wojciech Tomanik
Wojciech Tomanik

Reputation: 11

Is it possible to define in XState machine a transition from each state - just once

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.

Upvotes: 1

Views: 823

Answers (1)

z_lander
z_lander

Reputation: 152

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.

Upvotes: 4

Related Questions