[XState] Track Infinite States with with XState Context

Posted answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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" });

 

以上是关于[XState] Track Infinite States with with XState Context的主要内容,如果未能解决你的问题,请参考以下文章

将事件发送到 XState 中的子服务数组

B. Infinite Prefixes

HUD3689 Infinite monkey theorem

HDU 3689 Infinite monkey theorem [KMP DP]

Codeforces 1295B - Infinite Prefixes

hdu 6223 Infinite Fraction Path