Created
June 1, 2021 07:53
-
-
Save jeongsd/e5e927e99e0acdf1acc3013931e7ab37 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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