zoukankan      html  css  js  c++  java
  • [XState] Track Infinite States with with XState Context

    Consider a text input. It would be impossible for anyone to model every value you could possibly put into it, because the number of possible values is infinite. This is an infinite state.

    Infinite state can be tracked and utilized by XState machines as "extended state". This extended state is called context. Context is passed to every function that is triggered by the machine: actions, activities, guards, and more.

    In this lesson we learn how to set context and update it through assign actions.

    const { Machine, interpret, assign } = require("xstate");
    
    const inputMachine = Machine(
      {
        id: "inputMachine",
        initial: "input",
        context: {
          value: "Please enter a color"
        },
        states: {
          input: {
            on: {
              CHANGE_VALUE: {
                actions: ["changeInput"]
              }
            }
          }
        }
      },
      {
        actions: {
          changeInput: assign((context, event) => {
            return { value: event.color };
          })
        }
      }
    );
    
    const service = interpret(inputMachine)
      .onTransition(state => {
        console.log(state.context); // red
      })
      .start();
    service.send({ type: "CHANGE_VALUE", color: "red" });
  • 相关阅读:
    TCP四种定时器--学习笔记
    Python魔术师--self
    python的socket里 gethostbyname 与 gethostbyname_ex 的区别
    用python查看URL编码的中文
    基于linux 的2048
    用灵活的指针访问类私有变量
    ie8无法拉伸背景图
    图片的onerror 事件解析
    stream.js
    Promise
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12215812.html
Copyright © 2011-2022 走看看