Skip to content

Instantly share code, notes, and snippets.

@VinayakBagaria
Last active December 19, 2022 07:57
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 VinayakBagaria/17762ee472f7d08f166fe0b7cc144368 to your computer and use it in GitHub Desktop.
Save VinayakBagaria/17762ee472f7d08f166fe0b7cc144368 to your computer and use it in GitHub Desktop.
Transition states
const machine = {
initial: 'cart',
states: {
cart: {
on: {
CHECKOUT: 'shipping',
},
},
shipping: {
on: {
NEXT: 'contact',
},
},
contact: {
on: {},
},
},
};
function transition(state, event) {
return machine.states[state].on?.[event.type] ?? state;
}
console.log(transition('cart', { type: 'CHECKOUT' }));
// shipping
console.log(transition('cart', { type: 'UNKNOWN' }));
// cart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment