Can context input
be used with @xstate/react
's useMachine
and/or useActor
hooks?
#5130
-
I feel like this is a "dumb" question, but in xstate v4 I was making extensive use of Here is a simple reproduction in codesandbox. Simplified, abbreviated code: const localMachine = useMemo(
() =>
createActor(testMachine, {
input: { guid: "46436a82-f9c0-47b1-a434-54663bbb48c1" },
}),
[guid]
);
// Problem 1 - typescript is complaining about localMachine here
const [snapshot, send] = useMachine(localMachine);
// Problem 2 - console.log(snapshot.context.guid) is empty/undefined here Problem 1:In my production code, TS complains about my Argument of type 'Actor<StateMachine<MyMachineContextHere, { type: "CANCEL"; } | { type: "CODE_ERROR"; } | { type: "BACK"; } | { type: "NEXT"; } | { type: "COMPLETE"; } | { type: "GENERIC_ERROR"; } | ... 7 more ... | { ...; }, ... 11 more ..., { ...; }>>' is not assignable to parameter of type 'AnyStateMachine'.
Type 'Actor<StateMachine<MyMachineContextHere, { type: "CANCEL"; } | { type: "CODE_ERROR"; } | { type: "BACK"; } | { type: "NEXT"; } | { type: "COMPLETE"; } | { type: "GENERIC_ERROR"; } | ... 7 more ... | { ...; }, ... 11 more ..., { ...; }>>' is missing the following properties from type 'StateMachine<any, any, any, any, any, any, any, any, any, any, any, any, any, any>': config, schemas, implementations, root, and 12 more.ts(2345) Problem 1.A:I also tried using Argument of type 'Actor<StateMachine<MyMachineContextHere, { type: "CANCEL"; } | { type: "CODE_ERROR"; } | { type: "BACK"; } | { type: "NEXT"; } | { type: "COMPLETE"; } | { type: "GENERIC_ERROR"; } | ... 7 more ... | { ...; }, ... 11 more ..., { ...; }>>' is not assignable to parameter of type 'AnyActorLogic'.
Type 'Actor<StateMachine<MyMachineContextHere, { type: "CANCEL"; } | { type: "CODE_ERROR"; } | { type: "BACK"; } | { type: "NEXT"; } | { type: "COMPLETE"; } | { type: "GENERIC_ERROR"; } | ... 7 more ... | { ...; }, ... 11 more ..., { ...; }>>' is missing the following properties from type 'ActorLogic<any, any, any, any, any>': transition, getInitialSnapshot Problem 2:I'm not seeing the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Your code should look like this: const [snapshot, send] = useMachine(localMachine, {
input: { guid: '464...' }
}); Is there a reason that the above pattern does not work for your use-case? |
Beta Was this translation helpful? Give feedback.
Your code should look like this:
Is there a reason that the above pattern does not work for your use-case?