Skip to content

Instantly share code, notes, and snippets.

@kpratik2015
Created July 18, 2021 12:47
Show Gist options
  • Save kpratik2015/d81f7d97cef4dd6e388f98fa7b1f0d8a to your computer and use it in GitHub Desktop.
Save kpratik2015/d81f7d97cef4dd6e388f98fa7b1f0d8a 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 fetchMachine = Machine({
id: "stepper",
initial: "add",
context: {
count: 0,
},
states: {
add: {
on: {
click: {
target: "step",
actions: assign({
count: ({ count }) => count + 1,
}),
},
},
},
step: {
on: {
INC: {
target: "step",
actions: assign({
count: ({ count }) => count + 1,
}),
},
DEC: [
{
target: "step",
actions: assign({
count: ({ count }) => count - 1,
}),
cond: "doesCountRemainAboveZero",
},
{
target: "add",
actions: assign({
count: ({ count }) => count - 1,
}),
cond: "doesCountGoesZero"
},
],
},
},
},
},
{
guards: {
doesCountGoesZero: (context) => context.count - 1 === 0,
doesCountRemainAboveZero: (context) => context.count - 1 > 0,
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment