Check out a free preview of the full State Machines in JavaScript with XState, v2 course

The "History States Q&A" Lesson is part of the full, State Machines in JavaScript with XState, v2 course featured in this preview video. Here's what you'd learn in this lesson:

David answers student's questions regarding if history states are similar to having async work coming back to a machine and if there is a way to set default data in the visualizer to avoid typing in data each time. Questions regarding if history works for parallel states and what makes an infinite or recursive machine are also answered in this segment.

Preview
Close

Transcript from the "History States Q&A" Lesson

[00:00:00]
>> Somebody asks, so this is like when we want async work going on and then coming back to a machine? And yes, this is exactly how you could think about it, and this is what we're doing exactly here in this exercise. We have this async work of loading a song, and then once that's done, and we come back to our ready states, we remember what the previous, or not the previous, but we remember what the child state that that ready state was in.

[00:00:31]
Is there a way to set some default data when we trigger an action by clicking events in the inspector to avoid typing the events data each time? So this refers to the visualizer and in the visualizer, you can send in events with payload over here. So I could send some events and I could add some payload, Hello, and then I could go ahead and send that events.

[00:00:59]
Right now, there is no way to have defaults data, but that is coming as a future feature inside the visualizer. Does history work for parallel states? So each region returns to its previously selected state? And the answer is yes, because each region is most of the time, unless you don't have any child states, every region is a compound state.

[00:01:25]
So you can specify a region that has child states and also has a sibling history state that you could go into. What makes an infinite or a recursive machine? So, we have finite state machines, which the finite means the finite number of states events and transitions. An infinite state machine doesn't really make sense.

[00:01:49]
So you could think of an infinite state machine as a finite state machine that just can have infinite data, and that actually sort of is already like these state machines and state charts that have context. So you might imagine, for example, a machine that has a count value that could count all the way up to infinity.

[00:02:10]
Then if you represent each one of the aggregate states as including the count, then, of course, this state machine has an infinite number of states. However, if you just consider the behaviors, then the behaviors should be finite. And even though you might have an infinite amount of combinations of data, which is definitely the case for most applications, the behaviors themselves are finite.

[00:02:38]
So that's what the finite means in finite state machines.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now