Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wobsoriano/pinia-xstate

Put your xstate state machines into a global pinia store.
https://github.com/wobsoriano/pinia-xstate

pinia state-machine vue xstate

Last synced: 3 days ago
JSON representation

Put your xstate state machines into a global pinia store.

Awesome Lists containing this project

README

        

# pinia-xstate

Put your [xstate](https://github.com/statelyai/xstate) state machines into a global [pinia](https://pinia.esm.dev/) store.

## Installation

```bash
npm install xstate pinia-xstate
```

## Usage

```ts
import { defineStore } from 'pinia'
import { xstate } from 'pinia-xstate'
import { createMachine } from 'xstate'

export const toggleMachine = createMachine({
id: 'toggle',
initial: 'inactive',
states: {
inactive: {
on: { TOGGLE: 'active' }
},
active: {
on: { TOGGLE: 'inactive' }
}
}
})

// create a store using the xstate middleware
export const useToggleStore = defineStore(
toggleMachine.id,
xstate(toggleMachine)
)
```

```vue

import { useToggleStore } from './store/toggle'

const store = useToggleStore()


{{ store.state.value === 'inactive'
? 'Click to activate'
: 'Active! Click to deactivate' }}

```

## License

MIT