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 Viz to Visually Develop and Test Your Machine

    Kyle ShevlinKyle Shevlin
    xstateXState
    >=4.6.7
    javascriptJavaScript

    XState Viz is an online tool for visualizing our state machines. It allows you to write any state machine in the code panel on the right, and see it visualized on the left. Not only is the visualization helpful, but it is interactive. We're able to test out our machine manually by clicking the various events on the left. Anything lit blue is available for clicking. As we go further in the course, we'll discover other features of the visualization.

    Code

    Code

    Become a Member to view code

    You must be a 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
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Xstate-viz is an online tool for visualizing our state machines. Here, I have already made the light ball machine. It allows you to write any state machine, the code panel on the right, and see it visualizes on the left.

    Not only is the visualization helpful, but it's interactive. We're able to test out our machine manually by clicking the various events on the left. I can click toggle from the unlit state to go to the lit state, toggle to go back, and break to go to broken.

    I can hit reset at any time to reset the machine. As we go further in the course, we'll discover other features of the visualization as well. There're two other tabs on the right panel that are useful to us.

    The first is the state tab. This tab gives us information regarding the current state of our machine. It provides us an object with a value. If we had context, actions, and more, it would say that information as well.

    The second tab is the event tab. We're able to call events here as well. Anything available to the current state notice made into a button here on the bottom right. Notice that there's break and toggle for the unlit and the visualization and break and toggle down here.

    What is useful about the event panel is that we can pass along extra information on our events, then we can with just a string represent a transition target.

    For example, I might want to send the break event, but I also might want to indicate what location, the light ball was in when it broke. We can send the event by clicking the send button and it'll get added to the history of events called.

    We can actually toggle this open and we can see that we call the type break and we added the information location leaving room. We're able to replay or edit any event that we want and we could see timestamps of when they were called.

    Lastly, if we're logged in like I am, we can save our machine definition as a gist. Notice that the URL has been appended with a gist query prem. The string is the ID of our gist. We could see it by going to gist.githug.com/klyeshevlin in the ID.