@statebacked/react
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

React hooks to interact with StateBacked.dev machine instances

GitHub license npm version CI Docs

StateBacked.dev runs state machines as your secure, scalable, serverless backend.

Check out the full State Backed docs for more detailed information and to launch your own state machine backend in just a few minutes.

You can use these React hooks to easily connect your UI to any of your real-time, persistent machine intances running in the State Backed cloud.

Installation

npm install --save @statebacked/react @statebacked/client

Example

import { StateBackedClient } from "@statebacked/client";
import { useStateBackedMachine } from "@statebacked/react";
import { useActor } from "@xstate/react";

const client = new StateBackedClient({
  anonymous: {
    orgId: "org-YOUR_ORG_ID",
  }
});

function MyReactComponent() {
  const { actor } = useStateBackedMachine(
    client,
    {
      // name of a machine you created via the smply CLI or StateBacked.dev
      machineName: "your-machine-name",
      // name of a machine instance to connect to or create
      // you can create as many instances of each machine as you'd like
      instanceName: "your-instance",
      // function to provide the initial context if we have to create the machine instance
      getInitialContext() {
        return {
          any: "initialContextForYourMachine"
        }
      }
    }
  );

  const [state, send] = useActor(actor);

  // now you can interact with your persistent, real-time, multi-player
  // State Backed machine instance just like it was a local state machine

  // send({ type: "any-event", extra: "data" })

  // render your UI based on the current machine state
}

Package Sidebar

Install

npm i @statebacked/react

Weekly Downloads

1

Version

0.1.3

License

MIT

Unpacked Size

14.9 kB

Total Files

9

Last publish

Collaborators

  • abrgr