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

    Use XState Null Events and Transient Transitions to Immediately Transition States

    xstateXState
    >=4.6.7
    javascriptJavaScript

    It is often useful to identify conditional branching in your machine as a state itself. A state that is designed to determine the next state does not need a specific event sent to trigger the transition. Instead, we can use the "null event" to trigger an immediate, transient transition.

    The null event is identified with an event name of an empty string '', and is immediately sent to the state upon entry. We can setup multiple targets with conditionals, or fire off actions to set up a future state with this transient transitions.

    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: There's a saying, "If at first you don't succeed, try, try again." I want to represent that as a machine. I have states of idle, trying, and success. Every time we enter the trying state, we're going to increment the number of tries in context.

    I don't ever want to stay in the trying state. I should go back to the idle state automatically. If I do happen to try enough times and succeed, I should also automatically go to success. This seems like the kind of thing that should be chosen without an explicit event.

    In XState, we have something called the null event. On the trying state node, I'm going to add to the on property an empty string. This represents the null event. A null event is immediately taken when we enter a state. That's called a transient transition. Every time I enter trying, I'm going to immediately take the transitions I have here.

    Now, with trying, I want to either transition to success if I've tried enough, or I want to transition back to idle. To do this, we'll set two targets by using an array. The first target will be success with a condition. We'll write our tried enough condition in a moment.

    Our second target will be idle, in case our first condition isn't met. Down here in the second argument to machine the options object, we'll add our guard. We'll update our machine.

    We can see now that we have an event here that doesn't have a name. That's our null event. The null event is fired immediately upon entering the state and we attempt a transition to the next one. I find this very useful for setting up conditional branching in states like this.

    Let's give it a try on our machine. You notice that we never went into the trying state. We are immediately back into the idle state. If we look at the state panel, we'll see that our tries incremented by one.

    Let's try again. We see tries are two. Now that we've succeeded, we've tried enough times, we go immediately to success.