Actions

Action

is function that can be called on an event or transition

Event

State

Action

Actions on events

ready: {
  on: {
    EVENT: {
      actions: () => {
        doSomething()
      }
    }
  }
}

// or

const machine = Machine({
  initial: "ready",
    states: {
      ready: {
        on: {
          EVENT: {
            actions: "doSomething"
          }
        }
      },
    }
  },
  {
    actions: {
      doSomething: () => {}
    }
  }
);

Calling actions on events by:

  • function definition
  • function name with definition below machine

Each state can handle special type of actions

  • entry action
    which is executed upon entering a state
     
  • exit action
    which is executed upon exiting a state
ready: {
  entry: () => {
    doSomething()
  },
  exit: () => {
    doSomethingElse()
  }
}


// or

const machine = Machine({
  initial: "ready",
    states: {
      ready: {
        entry: "doSomething",
        exit: "doSomethingElse"
      },
    }
  },
  {
    actions: {
      doSomething: () => {},
      doSomethingElse: () => {}
    }
  }
);

Same as actions on events

coding time!

3. Actions

By Kuba Skoneczny

3. Actions

  • 192