zoukankan      html  css  js  c++  java
  • [XState] Assignement actions

    Let's we want to keep tracking how many times on element was click inside Machine model. We can use 'context' & 'assign' function.

    import { assign, createMachine, interpret } from "xstate";
    
    const elBox = document.querySelector("#box");
    
    const setPoint = (context, event) => {
      // Set the data-point attribute of `elBox`
      // ...
      elBox.dataset.point = `${event.clientX} - ${event.clientY} - ${context.count}`;
    };
    
    const countAssign = assign({
      count: (context, event) => {
        return context.count + 1;
      },
    });
    
    const machine = createMachine(
      {
        initial: "idle",
        context: {
          count: 0,
        },
        states: {
          idle: {
            on: {
              mousedown: {
                // Add your action here
                // ...
                target: "dragging",
                actions: [setPoint, countAssign],
              },
            },
          },
          dragging: {
            on: {
              mouseup: {
                target: "idle",
              },
            },
          },
        },
      }
    );
    
    const service = interpret(machine);
    
    service.onTransition((state) => {
      elBox.dataset.state = state.value;
    });
    
    service.start();
    
    elBox.addEventListener("mousedown", (event) => {
      service.send(event);
    });
    
    elBox.addEventListener("mouseup", (event) => {
      service.send(event);
    });
  • 相关阅读:
    MasterPage中找尋控件
    Win2003服务器发布的网站Session经常丢失
    Toolkits
    aspnet_regiis 命令格式說明
    SQL转换数字中文大写
    ASP.NET2.0实现无刷新客户端回调
    SQL的使用规范
    pku3207 2SAT问题入门
    unity3d打包资源
    Vector3.Lerp 插值
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13348234.html
Copyright © 2011-2022 走看看