xstate-ngx
v1.0.0-alpha.9
Published
This is just a POC while the [PR in the Xstate Monorepository](https://github.com/statelyai/xstate/pull/4816) is being discussed. Eventually this will be moved and deprecated!
Downloads
5
Maintainers
Readme
xstate-ngx
This is just a POC while the PR in the Xstate Monorepository is being discussed. Eventually this will be moved and deprecated!
This package contains utilities for using XState with Angular.
Quick start
- Install
xstate
andxstate-ngx
:
npm i xstate xstate-ngx
- Import the
useMachine
function:
import { useMachine } from 'xstate-ngx';
import { createMachine } from 'xstate';
import {Component, inject} from '@angular/core';
const toggleMachine = createMachine({
id: 'toggle',
initial: 'inactive',
states: {
inactive: {
on: { TOGGLE: 'active' }
},
active: {
on: { TOGGLE: 'inactive' }
}
}
});
const ToggleMachine = useMachine(toggleMachine, {providedIn: 'root'})
@Component({
selector: 'app-toggle',
standalone: true,
imports: [],
template: `<button (click)="toggleMachine.send({type: 'TOGGLE'})">
{{
toggleMachine.snapshot().value === 'inactive'
? 'Click to activate'
: 'Active! Click to deactivate'
}}
</button>
`,
styleUrl: './toggle.component.css'
})
export class ToggleComponent {
public toggleMachine = inject(ToggleMachine);
}