Best Way To Access invoked Actor from Parent using @xstate/react #4955
-
Hi there! First off want to say I love this library so much, thank you guys for all your great work! I'm newer to XState and state machines in general so still continuing to learn more and more everyday 😄 I'm in the process of upgrading/rewriting a couple of machines from V4 -> V5. These machines are used for a multistep wizard, where we have different 'steps', each step having different data. We also have the need to handle the fetching of previous save data for that step, saving the step, errors when saving the step, etc (which is handled in a separate machine called The parent machine (called physician: {
invoke: {
src: "updateCaseMachine",
id: "PhysicianStep",
input: ({ context }) => ({
...context,
step: "physician",
}),
onDone: [
{
target: "member",
actions: [
assign({
physicianStepInfo: ({ event }) =>
event.output.physicianStepInfo,
caseId: ({ event }) => event.output.caseId,
error: false,
errorMsg: "",
}),
],
},
],
onError: [
{
target: "physician",
actions: [
assign({
error: true,
errorMsg: "An error occured while saving physician step",
}),
],
},
],
},
}, I'm now using XState V5 in tandem with const [state, send, actorRef] = useMachine(caseMachine, {
input: {
...someInputStuffHere
},
}); Previously before in V4 and the older const machine = actorRef.children.get("PhysicianStep");
const [current, send] = useActor(machine as Interpreter<any, any, any>); This would give me access to The individual step components don't really care about the parent machine's state but mainly the invoked instance of A little lost and would appreciate any help on this! Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Could you give an example of another state that uses |
Beta Was this translation helpful? Give feedback.
I did figure out a solution but not the way I had originally intended. After writing everything out I did in fact realize that they were two separate machines and did not necessarily need to be 'aware' internally of each others state. So the two machines are completely separate now, and I individually invoke
updateCaseMachine
for each step in that steps component. I then pass down a function via props that will movecaseMachine
to it's next state (or the essentially the next step). To listen for when to correctly do this I have the foll…