Pass values when sending events from one machine to another in xState

主宰稳场 提交于 2020-04-17 21:29:15

问题


I have a simple chatMachine that invokes a todoMachine. The todoMachine has an event called 'OPENED_TASK_LIST_CREATOR' which I want to invoke from chatMachine. I've managed to figure this out.

export const chatMachine = Machine({
  id: 'chat',
  initial: 'idle',
  context: { message: '' },
  states: {
    idle: {
      invoke: {
        id: 'todo',
        src: todoMachine
      },
      on: {
        COMMENT_SUBMITTED: {
          actions: 'addComment'
        },
        COMMENT_STARRED: {
          actions: [
            (ctx, e) => console.log('e.payload', e.payload),
            send('OPENED_TASK_LIST_CREATOR', {
              to: 'todo'
            })
          ]
        }
      }
    }
  }
});

The problem I am having is that I want to send a value along with the 'OPENED_TASK_LIST_CREATOR' event. Namely the list id I want opened. I have managed to log it with (ctx, e) => console.log('e.payload', e.payload), just above the send action in COMMENT_STARRED.

Is there a way to passe.payload to send('OPENED_TASK_LIST_CREATOR', { to: 'todo' }) so that I can use the value in todoMachine?


回答1:


COMMENT_STARRED: {
          actions: [
            (ctx, e) => console.log('e.payload', e.payload),
            send(
              (ctx, e) => ({
                type: 'OPENED_TASK_LIST_CREATOR',
                payload: e.payload
              }),
              {
                to: 'todo'
              }
            )
          ]
        }


来源:https://stackoverflow.com/questions/54919831/pass-values-when-sending-events-from-one-machine-to-another-in-xstate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!