Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Send Events to the Machine with the XState Send Action Creator

    xstateXState
    >=4.6.7
    javascriptJavaScript

    XState provides the send function to create an action that will send the event passed in to the machine. If we provide the second argument to the send function, the options object, we can send the event to a particular machine. This is useful when you have invoked a machine as a service on a state node, a concept that will be explored in a later lesson.

    Code

    Code

    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: 0:00 Here I have an echo machine with one state, listening, and two events, speak and echo. What I'd like this machine to do is any time I speak, I want the echo event to be triggered. How can I do this? I can use a special action called the send action creator to send events to my machine.

    0:20 I'm going to add the actions property here. Then I'm going to use the send function. The send function receives an event. In this case, it'll be echo. We update our machine. We now see that speak is an available event to send. When we send the speak event, it'll send an echo event on the next tick of the machine.

    0:40 I'm going to open up the console. Reset the machine. We'll call speak. Every time we do, it echoes out. We can also update this from being just a string to an object with a type of event that we want to send. This too will also send echoes.