Xstate: Send data to child actor

Created on 15 Oct 2019  路  2Comments  路  Source: davidkpiano/xstate

I think this is probably a question not a feature request or bug.

How do we send data to child actors within a machine?

Looking at the example from the docs, we can spawn an actor and send events to it but I'm not sure how to add data to the event.

entry: assign({
  someRef: () => spawn(someMachine)
}),
on: {
  SOME_EVENT: {
    // I want to send some custom data (from either the SOME_EVENT data or the local context) to the PING event 
    actions: send('PING', {
      to: context => context.someRef,
    })
  }
}

With the sendParent API I can do the following:

actions: sendParent(context => ({
  type: 'UPDATE_FROM_CHILD',
  data: context
}))

Does a similar API exist for parent -> child 馃 communication?

documentation question

Most helpful comment

Yes:

actions: send({
  type: 'SOME_EVENT',
  somePayload: 'whatever you want'
}, { to: context => context.someRef })

All 2 comments

Yes:

actions: send({
  type: 'SOME_EVENT',
  somePayload: 'whatever you want'
}, { to: context => context.someRef })

I'll add this to the docs too.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pke picture pke  路  3Comments

dakom picture dakom  路  3Comments

ifokeev picture ifokeev  路  3Comments

carloslfu picture carloslfu  路  3Comments

suku-h picture suku-h  路  3Comments