Can you assign context in the "middle" of an action? #4422
Unanswered
anthony-schanen-BAB
asked this question in
Q&A
Replies: 1 comment
-
You can't chain Async behavior in actions like you're trying to achieve here. With XState 4: const countMachine = createMachine({
initial: 'start',
context: { count: 0 }
states: {
start: {
invoke: {
src: () => callApi(),
onDone: {
actions: assign({ count: (ctx, event) => event.data })
}
}
}
}
}); With XState 5: const countMachine = createMachine({
initial: 'start',
context: { count: 0 }
states: {
start: {
invoke: {
src: fromPromise(callApi),
onDone: {
actions: assign({ count: ({ event }) => event.data })
}
}
}
}
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have an action that needs to fetch some data from an API. I want the data from this api stored as context in my machine. I don't see anyway of having an action assign context in the middle of the action implementation. How do I achieve this? Am I approaching the problem incorrectly?
Looking at the docs, I see this example, which only strictly changes the count value.
I am lost as to where to put my API call code if I want a value from elsewhere. is it after
=>
on this line?I am also unsure how assign is going to work when I go to another state. This action also has a state transition depending on the value of
resultFromAPI
Apologies if my question is unclear, but I am not sure if I am approaching the problem correctly / using xState correctly
Beta Was this translation helpful? Give feedback.
All reactions