Check out a free preview of the full State Modeling in React with XState course

The "Invoking Actors Solution" Lesson is part of the full, State Modeling in React with XState course featured in this preview video. Here's what you'd learn in this lesson:

David live codes the solution to the Invoking Actors exercise.


Transcript from the "Invoking Actors Solution" Lesson

>> Alright, so let's talk about invoking actors. In exercise seven, we don't have this timer working or actually it is working, probably because I have the solution up here. Just fix that. Okay. So in exercise seven Obviously our timer doesn't work because we removed that use effect. And instead, we want to use that in the actual machine itself.

So here's how we would go about doing that. Just like you saw in the lesson, we're going to invoke a callback. So it's gonna be a source, and the source of this invoke Is we're going to be creating a callback so that we can take the context and the events that caused the invoke.

But we don't need that in this case. So we're just going to be using the context. And we're going to be returning a function. So there's two ways you could do this. You could either return the function here, which is that Send back and, and the receive which we're not going to be using over here.

Or you can make this a little bit more concise and just have context sent back. And then here is where we create the interval. So we say const interval equals or I'm just going to call this I. Equals setInterval. And on the every interval, we're going to send back a tick event.

And so that tick event is going to happen every, Well second times context, dot interval. Now, important note over here, this context represents the context at the time that this invoked actor was created. So it's not going to be in Updated context, you only get the context once basically.

And if the context changes, then you need to communicate that to the actor within the vents. So don't forget to return a function that cleans up that interval. So we're going to clear interval I But yeah, that should do the trick. So when we click, we see that we have our interval working just the same.

Now the good thing about this is that console dot log cleaning up is that we could. Only have this interval run when it's in this state. This is non state specific thing this in original state charts is you could consider it inactivity. It's basically That happens during the states or throughout the state.

So as long as you're in the running states, this invocation is still going to be running. Once you exit it, this is going to be cleaned up. So, we click here because it paused may be reset. So here we go paused, you can see that the timer is being cleaned up so it's no longer sending ticks.

It see cleaning up right over here. And of course if we reset, it's going to be cleaned up again. So it's creating a new timer every single time we enter the running state. And that's so that we don't get any wasted calls or, you know, wasted code basically running when it's not supposed to be running.

So were there any questions about that exercise. I just like how it gets rid of the use context because like our, our component is getting tinier and tinier. It's like pretty much JSX with like one or two hooks now, so it's, it's kind of elegant.
>> Yeah, yeah, because state machines and state charts can handle so much you end up not needing things such as use effect.

And other things like that. So.

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