Skip to content

Latest commit

 

History

History
58 lines (51 loc) · 1.34 KB

no-imperative-action.md

File metadata and controls

58 lines (51 loc) · 1.34 KB

Forbid using action creators imperatively

Enforce using action creators declaratively.

Rule Details

Action creators (assign, send, sendParent, respond, raise, forwardTo, choose, pure, log, escalate) produce action objects which are executed later by the XState interpreter. Calling action creators imperatively has no effect.

Examples of incorrect code for this rule:

// ❌ action creators called imperativelly
createMachine({
  entry: [
    () => send('EVENT'),
    () => {
      sendParent('EVENT')
    },
    function () {
      respond('EVENT')
    },
    () => raise('EVENT'),
  ],
  exit: () => assign({ count: 1 }),
  on: {
    TRIGGER: {
      actions: [
        () => forwardTo('someActor'),
        () => choose([]),
        () => pure(() => {}),
        () => log('log me'),
        () => escalate('error'),
      ],
    },
  },
})

Examples of correct code for this rule:

// ✅ all action creators are used declaratively
createMachine({
  entry: [send('EVENT'), sendParent('EVENT'), respond('EVENT'), raise('EVENT')],
  exit: assign({ count: 1 }),
  on: {
    TRIGGER: {
      actions: [
        forwardTo('someActor'),
        choose([]),
        pure(() => {}),
        log('log me'),
        escalate('error'),
      ],
    },
  },
})