zoukankan      html  css  js  c++  java
  • [XState] Use Internal Transitions in XState to Avoid State Exit and Re-Entry

    Transitions come in two varieties: "external" and "internal". By default, a transition is considered external. This means that a transition will exit the current state node, and enter the next state node (even if that state node is the state the machine is currently in). This exit/enter loop will trigger any actions that are set on the exit and entry properties.

    A transition can be set to internal, either through setting a . (dot) in front of the state node name (as we do in this lesson), or through setting the property internal to true on the transition object.

    When a transition is internal, it will not exit and enter the state node, which means that the actions in the exit/transition/entry loop will not be called.

    const { Machine } = require("xstate");
    
    const idleMachine = Machine(
      {
        id: "idle",
        initial: "idle",
        states: {
          idle: {
            entry: ["logEntry"],
            exit: ["logExit"]
          }
        },
        on: {
          DO_NOTHING: ".idle" // add '.' to point it to itself, without call exit and entry actions
        }
      },
      {
        actions: {
          logEntry: () => {
            console.log("entered");
          },
          logExit: () => {
            console.log("exited");
          }
        }
      }
    );
  • 相关阅读:
    刷题[极客大挑战 2019]HardSQL
    刷题[安洵杯 2019]不是文件上传
    归并排序算法及其JS实现
    快速排序算法原理及其js实现
    圣杯布局
    什么是文档流
    AngularJs四大特性
    call,apply,bind的区别
    计算给定数组 arr 中所有元素的总和的几种方法
    es6之Decorator
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12215442.html
Copyright © 2011-2022 走看看