#Emit not working when used with useActorRef?

1 messages Β· Page 1 of 1 (latest)

slate lynx
#

Hello! πŸ‘‹

I'm working with XState as a beginner, and I'm encountering a very strange issue. I created a state machine and I'm trying to use emit via fromCallback after a state transition. Unfortunately, while I verified the callback is being invoked, I'm not getting any notifications from the Actor ref when I call emit. Example code is below.

// StateMachine.ts

const callback = fromCallback(({emit}) => { 
  someCallback.onEvent(data => {
    // This is logged
    console.log(data);
    // This doesn't work πŸ™
    emit({type: 'notify', data}
  });
});

// *snip*

on: {
  triggerEmit: {
    target: '.someState'
  }
},
states: {
  someState: {
    invoke: [
      { src: 'callback' }
    ],
  }
}

// *snip*
// AComponent.tsx

const actor = SomeContext.useActorRef();

useEffect(() => {
  actor.send({type: 'triggerEmit'});

  actor.on('notify', data => {
    // This doesn't work πŸ™
    console.log(data);
  });
}, [])

snow sable
#

I think this isn't working because it's the callback actor that emits the event but you've put the handler on the parent actor

#

I don't think events bubble like that

#

So I would use sendBack to pass to the parent then emit from the parent

slate lynx
#

Thank you!! I got it working by using sendBack and by using emit from within the parent, as you suggested. Thank you for the very helpful tips!