Search⌘ K

Configuring XState in the Funbook App

Explore how to configure XState in the Funbook React Native app by adding necessary dependencies and creating a state machine for user login flows. Understand transitions between states and how to integrate XState with React Context using the useInterpret hook. This lesson equips you to implement finite state machines for predictable and maintainable state management in your app.

Let’s see what it takes to use XState in a real app.

Adding dependencies

First things first—we need to add XState to the project. We can do so by running one of the two following commands:

# Install the latest version of XState using npm
npm install xstate@latest --save
# Alternatively, install with Yarn
yarn add xstate@latest --save

XState itself is an unopinionated library, much like MobX. This means it is not ready out-of-the-box to work with React.

Note: The XState documentation has a section called Recipes where we can read more on the implementation with React or other UI libraries, such as Vue or Svelte.

As for us, we need to add the React-related dependency, xstate-react. Let’s do this by running one of the two following commands:

# Install the latest version of XState-React using npm
npm install xstate-react@latest --save
# Alternatively, install with Yarn
yarn add xstate-react@latest --save

Creating state machines

Now that we have the dependencies ready, we can create our very first state machine. We will start with a simple example: user login flow. At a high level, ...