Socket
Socket
Sign inDemoInstall

zustand-middleware-xstate

Package Overview
Dependencies
10
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    zustand-middleware-xstate

Zustand middleware for integrating with XState


Version published
Weekly downloads
769
increased by16.87%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

zustand-middleware-xstate

This middleware allows you to easily put your XState state machines into a global zustand store.

installation

npm install zustand-middleware-xstate zustand xstate

usage

import create from "zustand";
import { createMachine } from "xstate";
import xstate from "zustand-middleware-xstate";

// create your machine
const machine = createMachine({
  id: "machine",
  states: {
    // ...
  },
});

// create a hook using the xstate middleware
const useStore = create(xstate(machine));

// use the store in your components
const App = () => {
  const { state, send, service } = useStore();

  return <div>{state.value}</div>;
};

Or check out the demo for a working example.

selectors

You can also use zustand's selector feature to get slices of the state and avoid unnecessary re-renders. For example:

const context = useStore(s => s.state.context);

This hook will only re-render when the context changes. See the zustand docs for more details.

interpreter options

You can hand over a second argument to xstate function (from this library). This is forwarded to the interpreter of xstate, thus, things like devTools can be enabled.

FAQs

Last updated on 26 Jul 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc