[XState] raise event: trigger an action immediately when reach target state

import { Machine, actions } from "xstate";
const { raise } = actions;

// Demostrate `raise` action
const stubbornMachine = Machine({
  id: "raisedmo",
  initial: "entry",
  states: {
    entry: {
      on: {
        STEP: {
          target: "middle",
        },
        RAISE: {
          target: "middle",
          // immediately invoke the NEXT event in 'middle'
          actions: [raise("NEXT")],
        },
      },
    },
    middle: {
      on: {
        NEXT: "last",
      },
    },
    last: {
      on: {
        RESET: "entry",
      },
    },
  },
});

The raise() action creator queues an event to the statechart, in the internal event queue. This means the event is sent immediately on the current “step” of the interpreter.

https://stately.ai/viz/cced7743-87b5-41ad-b09a-c9bdf0ace635

 

posted @ 2022-11-26 17:50  Zhentiw  阅读(41)  评论(0)    收藏  举报