zoukankan      html  css  js  c++  java
  • [XState] Send Events to the Machine with the XState Send Action Creator

    XState provides the send function to create an action that will send the event passed in to the machine. If we provide the second argument to the send function, the options object, we can send the event to a particular machine. This is useful when you have invoked a machine as a service on a state node, a concept that will be explored in a later lesson.

    const { Machine, interpret, send } = require("xstate");
    
    const echoMachine = Machine({
      id: "echo",
      initial: "listening",
      states: {
        listening: {
          on: {
            SPEAK: {
              actions: send("ECHO") // trigger echo action
            },
            ECHO: {
              actions: () => {
                console.log("echo is called");
              }
            }
          }
        }
      }
    });
    
    const service = interpret(echoMachine).start();
    service.send("SPEAK"); //echo is called
  • 相关阅读:
    Linux操作系统(二)
    匿名函数和内置函数
    BeautifulSoup
    Robots协议
    列表和生成器表达式
    迭代器
    排序总结
    图论专题笔记
    Trie树的二三事QWQ
    二分答案经典入门题:)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12215467.html
Copyright © 2011-2022 走看看