I tried the most minimal example I could think of. A parent machine that sends a message to a child state machine, upon entering its only state. The child state machine simply acknowledges receipt of the message in the log.
But I keep getting the error message in the log: " Unable to send event to actor 'child' from machine 'main'."
What am I missing?
import './App.css';
import { setup, log, sendTo } from 'xstate';
import { useMachine } from '@xstate/react';
const childMachine = setup({
types: {
events: {} as { type: 'foo' },
},
}).createMachine({
id: 'child',
on: {
foo: { actions: log('CHILD GOT FOO') },
},
});
const mainMachine = setup({
actions: {
pingChild: sendTo('child', { type: 'foo' }),
},
actors: {
child: childMachine,
},
}).createMachine({
id: 'main',
initial: 'running',
states: {
running: {
entry: [log('ENTERED PARENT RUNNING'), 'pingChild'],
},
},
});
function App() {
const [_] = useMachine(mainMachine);
return (
<h1>Hello</h1>
);
}
export default App;