Xstate: Be able to call assign actions with ActionObject notation

Created on 11 Oct 2018  路  3Comments  路  Source: davidkpiano/xstate

Bug or feature request?

Feature request

Description:

Be able to call assign actions using the ActionObject notation, it's also needed to implement <param> and <content> which would be placed into the event (per the SCXML spec).

Example:

onEntry: { type: 'myAction', foo: 'bar' }

// ...

actions: {
  myAction: (ctx, e) => {
    e.foo === 'bar'; // true
  }
}

Anything passed into the action object will be merged with the event object.

enhancement

Most helpful comment

Added in the 3rd argument in the actionMeta property as of version 4.4. Docs: https://xstate.js.org/docs/guides/actions.html#declarative-actions

All 3 comments

Clarification with this after reading the spec: the extra fields passed into the action object will be merged with the resolved action object, not with the event:

onEntry: { type: 'myAction', foo: 'bar' }

// ...
actions: {
  myAction: (ctx, e) => {
    e.foo === undefined;
  }
}

But when that action is returned as part of the State, it is returned as:

{
  type: 'myAction',
  foo: 'bar',
  exec: (ctx, e) => { e.foo === undefined; }
}

Which makes me think that we should pass the action as a 3rd argument to the action?

Which makes me think that we should pass the action as a 3rd argument to the action?

Yes, I think it is a good solution.

Added in the 3rd argument in the actionMeta property as of version 4.4. Docs: https://xstate.js.org/docs/guides/actions.html#declarative-actions

Was this page helpful?
0 / 5 - 0 ratings