#How to reference a provided action in a machine defined action.

1 messages · Page 1 of 1 (latest)

humble ether
#

I'm using xstate with react and have provided toast as an action. Let's say I have several different invoke failures, each with their own error message. I can provide fetch1FailureToast, mutateFailureThingToast, but I was wondering if I could just provide toast and have the machine define actions that call toast itself. Is that possible?

Sorry for all the questions. Think I'm wrapping up soon. (Will probably have several more after the v5 typegen stuff, though 😬 )

#

And without having toast look at the invokeable failure event.

ashen spindle
#

I appreciate the questions! And yes, that is possible, and that's what actions are for

#

E.g. you can have:

actions: [
  { type: 'showToast', params: { message: 'hello' } }
]
humble ether
#

Type '{ type: string; message: string; }' is not assignable to type 'BaseAction<...

#

{ type: 'toastError', params: { message: 'hello' } } seemed to get rid of the error

ashen spindle
#

Ah sorry, yes you need params in v5

humble ether
#

it seems to work with [{ type ... }], but not [({ event }) => ({ type: ...})]. probably something i'm not just getting and should just reference the docs. need the latter to extract the message

#

pure seemed to help

ashen spindle
#

Ah, you're trying to do this?

// Not supported
actions: ({ event }) => ({
  type: 'toastError',
  message: event.message
})