Skip to content

Instantly share code, notes, and snippets.

@christiannwamba
Created May 14, 2020 18:22
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 christiannwamba/8a2a28c5113c2ce469c4a4307f421168 to your computer and use it in GitHub Desktop.
Save christiannwamba/8a2a28c5113c2ce469c4a4307f421168 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// DEMO: Null or Transient Transitions with ''
// Cick TRY 3 times and it will get to success but will keep coming back to idle at 1 and 2 time
// You can see the state increment in the STATE tab when you click TRY
const ifAtFirstYouDontSucceed = Machine(
{
id: 'tryTryAgain',
initial: 'idle',
context: {
tries: 0,
},
states: {
idle: {
on: { TRY: 'trying' },
},
trying: {
entry: ['incTries'],
on: {
'': [{ target: 'success', cond: 'triedEnough' }, { target: 'idle' }],
},
},
success: {},
},
},
{
actions: {
incTries: assign({
tries: (ctx) => ctx.tries + 1,
}),
},
guards: {
triedEnough: (ctx) => ctx.tries > 2,
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment