Skip to content

React hooks for interacting with the State Backed backend as a service

License

Notifications You must be signed in to change notification settings

statebacked/react

Repository files navigation

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
}

About

React hooks for interacting with the State Backed backend as a service

Resources

License

Stars

Watchers

Forks

Packages

No packages published