Created
May 3, 2020 07:04
-
-
Save haishanh/3153e8697425a0930b3ac36474cdeb92 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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 fetchUser = userId => | |
fetch(`url/to/user/${userId}`).then(response => response.json()); | |
const userMachine = Machine({ | |
id: 'user', | |
initial: 'idle', | |
context: { | |
userId: 42, | |
user: undefined, | |
error: undefined | |
}, | |
states: { | |
idle: { | |
on: { | |
FETCH: 'loading' | |
} | |
}, | |
loading: { | |
invoke: { | |
id: 'getUser', | |
src: (context, event) => fetchUser(context.userId), | |
onDone: { | |
target: 'success', | |
actions: assign({ user: (context, event) => event.data }) | |
}, | |
onError: { | |
target: 'failure', | |
actions: assign({ error: (context, event) => event.data }) | |
} | |
} | |
}, | |
success: {}, | |
failure: { | |
on: { | |
RETRY: 'loading' | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment