Skip to content

Instantly share code, notes, and snippets.

@jeongsd
Created June 1, 2021 07:53
Show Gist options
  • Save jeongsd/e5e927e99e0acdf1acc3013931e7ab37 to your computer and use it in GitHub Desktop.
Save jeongsd/e5e927e99e0acdf1acc3013931e7ab37 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const searchValid = (context, event) => {
return context.canSearch && event.query && event.query.length > 0;
}
const searchMachine = Machine(
{
id: 'search',
initial: 'idle',
context: {
canSearch: true
},
states: {
idle: {
on: {
SEARCH: [
{
target: 'searching',
// Only transition to 'searching' if the guard (cond) evaluates to true
cond: searchValid // or { type: 'searchValid' }
},
{ target: '.invalid' }
]
},
initial: 'normal',
states: {
normal: {},
invalid: {}
}
},
searching: {
entry: 'executeSearch'
// ...
},
searchError: {
// ...
}
}
},
{
guards: {
searchValid
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment