首页 > 其他分享 >[XState + React] using @xstate/inspect to display state machine char in webapp

[XState + React] using @xstate/inspect to display state machine char in webapp

时间:2022-11-23 03:33:06浏览次数:44  
标签:count char const inspect machine state using active import

import "./styles.css";
import React from "react";
import ReactDOM from "react-dom";
import { createMachine, assign } from "xstate";
import { useMachine } from "@xstate/react";
import { inspect } from "@xstate/inspect";

inspect();

const toggleMachine = createMachine({
  id: "toggle",
  initial: "inactive",
  context: {
    count: 0
  },
  states: {
    inactive: {
      on: { TOGGLE: "active" }
    },
    active: {
      entry: assign({ count: (ctx) => ctx.count + 1 }),
      on: { TOGGLE: "inactive" }
    }
  }
});

function App() {
  const [state, send] = useMachine(toggleMachine, { devTools: true });
  const active = state.matches("active");
  const { count } = state.context;

  return (
    <div className="App">
      <h1>XState React Viz Template</h1>
      <h2>Fork this template!</h2>
      <button onClick={() => send("TOGGLE")}>
        Click me ({active ? "✅" : "❌"})
      </button>{" "}
      <code>
        Toggled <strong>{count}</strong> times
      </code>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

 

<iframe sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" src="https://codesandbox.io/embed/compassionate-morning-y3dkmf?fontsize=14&hidenavigation=1&theme=dark" style="width: 100%; height: 500px; border: 0; border-radius: 4px; overflow: hidden" title="compassionate-morning-y3dkmf"></iframe>

 

标签:count,char,const,inspect,machine,state,using,active,import
From: https://www.cnblogs.com/Answer1215/p/16917074.html

相关文章