
Learn to build state machines with XState and React, covering states, events, transitions. Explore nested and parallel machines, actions, guards, TypeScript, and Code Sandbox with video player and search component.
Explore state machines as a design approach for complex systems, detailing states, events, initial and final states, and transitions to keep web applications transparent and well described.
Understand actions in state machines: functions invoked by events or transitions as side effects, with inline or named definitions and entry or exit actions.
Explore how context stores data in state machines, initialize and update it with the assign function, pass external values, and react to events like increment while referencing a video element.
connect a real video to the player machine by creating a React ref, injecting it into the machine context, and invoking video play and pause from machine actions.
Guards enable conditional transitions and actions in state machines using context and events. A guard function returns true or false to decide whether a transition occurs.
Explains internal, external, eventless, and forbidden transitions in state machines, covering entry and exit actions, nested states, dot-prefix transitions between child states, always handlers, and guards.
Add an optional autoplay flag to the video player state machine and enable guarded, eventless transitions from an unknown state to playing using extra options.
Learn how to invoke services (async functions returning promises) within state machines and execute parallel states, handling outputs as promises resolve or reject, with practical examples of parallel machine behavior.
Discover how to invoke child state machines from a parent in XState and React, treat these machines as promises, and pass final-state data from child to parent.
Explore how delays work in XState using the after property to trigger transitions, such as one second to the dance state, plus self-sent events and dynamic delays via machine options.
This course is an introduction to finite state machines built with Javascript. State machines can describe complex applications straightforwardly, which makes them intuitive to work with and less error-prone. State machines can be easily integrated with React, which allows us to take a whole new approach for state management in React applications.
This course shows some basic concepts about state machines and their usage with XState. Also, it contains multiple coding examples, which allow students to have a better understanding of this topic.
Agenda:
Introduction to state machines
States, events, and transitions
Actions
Context
Guards
More on transition
Invoking services
Invoking machines
Delays
Alongside the presentations, we will also code some real-world examples, like:
Video player with autoplay option
Fetch machine for handling async requests
Search component with debounce