-
Greetings, In the below example (taken from the const context = {
data: undefined
};
const fetchMachine = createMachine({
id: 'fetch',
initial: 'idle',
context,
states: {
idle: {
on: { FETCH: 'loading' }
},
loading: {
entry: ['load'],
on: {
RESOLVE: {
target: 'success',
actions: assign({
data: (context, event) => event.data
})
}
}
},
success: {}
}
});
const [state, send] = useMachine(fetchMachine, {
actions: {
load: () => {
onFetch().then(res => {
send({ type: 'RESOLVE', data: res });
});
}
}
}); Compared to: const fetchMachine = createMachine({
...
states: {
...
loading: {
invoke: {
src: 'load',
onDone: 'success'
},
on: {
RESOLVE: {
target: 'success',
actions: assign({
data: (context, event) => event.data
})
}
}
},
...
}
});
const [state, send] = useMachine(fetchMachine, {
services: {
load: () => onFetch()
}
}); Thanks |
Beta Was this translation helpful? Give feedback.
Answered by
Andarist
Jan 21, 2021
Replies: 1 comment 1 reply
-
For async operations its usually better to use services - and it would be better if this example wouldnt use an action for that because this can result in warnings if that promise resolves after the component has been unmounted |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
viglucci
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For async operations its usually better to use services - and it would be better if this example wouldnt use an action for that because this can result in warnings if that promise resolves after the component has been unmounted