Full Example: API Fetch Machine
export const userMachine = setup({
actors: {
fetchUser: fromPromise(({ input }) =>
fetch(`https://.../users/${input}`).then(r => r.json())
)
}
}).createMachine({
initial: 'idle',
context: { data: null },
states: {
idle: {
on: { LOAD: { target: 'loading' } }
},
loading: {
invoke: {
src: 'fetchUser',
input: ({ event }) => event.userId,
onDone: {
target: 'success',
actions: ({ context, event }) => context.data = event.output
},
onError: 'failure'
}
},
success: {},
failure: {}
}
});