Skip to content

Instantly share code, notes, and snippets.

@mapicard
Created March 5, 2020 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapicard/5fffe20e2772d10289faf5070d65c870 to your computer and use it in GitHub Desktop.
Save mapicard/5fffe20e2772d10289faf5070d65c870 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const increment = context => context.count + 1;
const decrement = context => context.count - 1;
const counterMachine = Machine({
initial: 'active',
context: {
count: 0
},
states: {
active: {
on: {
INC: { actions: assign({ count: increment }) },
DEC: { actions: assign({ count: decrement }) }
}
}
}
});
const counterService = interpret(counterMachine)
.onTransition(state => console.log(state.context.count))
.start();
// => 0
counterService.send('INC');
// => 1
counterService.send('INC');
// => 2
counterService.send('DEC');
// => 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment