Skip to content

Instantly share code, notes, and snippets.

@jeongsd
Created June 1, 2021 07:53
Embed
What would you like to do?
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