(Invalid) Can't use useActor to interpret a spawned machine #1488
-
Hi, xstate: 4.13.0 In my main machine I use: addAlarm: {
on: {
SET_ALARM: {
actions: assign({
alarms: (context, event) => [
...context.alarms,
spawn(
alarmMachine.withContext({ timeToRing: event.payload })
),
],
}),
target: "idle",
},
},
}, And after passing the spawned machine to a component I try to interpret the machine with useActor. import React from "react";
import { useService, useActor } from "@xstate/react";
const Alarm = (alarm) => {
const x = useActor(alarm);
return (
<div className="w-full flex justify-between items-center p-8">
<div>
<small>alarm set for:</small>
<h1 className="font-mono text-4xl">21:00</h1>
</div>
<div>
<button className="py-2 px-3 rounded border-2 border-white font-bold text-white hover:bg-white hover:text-gray-800">
Cancel
</button>
</div>
</div>
);
};
export default Alarm; This throws an error: Uncaught TypeError: actor.subscribe is not a function. useService also produces an error: Uncaught TypeError: Cannot read property 'bind' of undefined. What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Can you please make a CodeSandbox reproduction (React template) demonstrating the problem? |
Beta Was this translation helpful? Give feedback.
-
It was a React typo, unrelated to XState: -const Alarm = (alarm) => {
+const Alarm = ({ alarm }) => {
const [state, send] = useActor(alarm);
console.log(state, send);
return <p>I'm an Alarm</p>;
}; You'll see it works with those changes. |
Beta Was this translation helpful? Give feedback.
It was a React typo, unrelated to XState:
You'll see it works with those changes.