XState: the Visual Future of State Management

Rate this content
Bookmark

Learn about state modeling with state machines and statecharts can improve the way you develop your React applications, and get a sneak peek of never-before-seen upcoming visual tools that will take state management to the next level.

David Khourshid
David Khourshid
35 min
14 May, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Talk introduces the visual future of state management and how XState is bringing it to reality. It explains the role of reducers in containing application logic and how state machines and state charts provide a visual and mathematically rigorous way to represent behaviors. XState is easily integrable with React and other frameworks, allowing for the management of local, global, and semi-local state. The Talk also highlights new features in XState, such as the use of TypeScript for stronger typing and the introduction of the useSelector hook for improved performance. The future vision includes the combination of diagrams and code, with tools like Stately.ai, and the importance of explicit modeling of application logic using state machines.

1. Introduction to Visual Future of State Management

Short description:

Hey, everyone. My name is David Courchide. I want to talk to you today about the visual future of state management and how XState is going to bring that future vision to reality. State machines and state charts are visually exact diagrams that convey relationships in a visually unambiguous way. They have their own special notations and are mathematically rigorous languages. I recommend reading David Harrell's paper on visual formalisms for more information.

Hey, everyone. My name is David Courchide. I go by David K. Piano online on Twitter, GitHub, whatever. And I want to talk to you today about the visual future of state management and how XState is going to bring that future vision to reality.

So talking about visuals, you probably know what a Venn diagram is. It's a visual and an exact way of representing commonalities between two or more different things. And you might have also used a sequence diagram before to describe how different parts of a system communicate with each other. So, there's also state machines and state charts, of course, which I've been talking about for a while.

And state machines and state charts fall under this visually exact diagram category. Because diagrams like these are really useful in conveying relationships in a visually unambiguous way, and they each have their own special notations for denoting specific things. And so, with state charts, we have the same type of thing. We have arrows and boxes just describing how states and logic can flow over time. David Harrell, who is the inventor of state charts, calls this a visual formalism. And so, he describes that visual formalisms are diagrammatic and intuitive, yet mathematically rigorous languages. Thus, despite their clear visual appearance, they come complete with a syntax that determines what's allowed and the semantics that determine what the allowed things can be. And so, I recommend reading his paper on visual formalisms for more information on this really, really interesting idea of basically these diagrams that are mathematically rigorous and also executable.

2. Coding Logic and the Role of Reducers

Short description:

The way we typically code application logic doesn't lend itself to a visual formalism. The logic is hard to understand and centralize. Reducers provide a way to contain logic in a centralized location, forcing interaction through events. State machines separate behaviors into finite states, representing current states and responses to events.

So, the way that we typically code application logic, whether it's in React or anything else, doesn't really lend itself to a visual formalism or to anything, really. We tend to co-locate data and logic close to the source where it's used, such as in event handlers or sometimes in custom React hooks if we want a little bit more organization.

So, while this may be convenient to code, the problem is that the logic is hard to understand, especially as it changes over time due to events or anything that can happen within the app. The problem is you can't discern what can happen or how an application can respond to any event or signal at any point in time. And so, that connection logic resides in the head of the developer who added the logic, which isn't really useful, and so you end up with things such as ad hoc logic as well, you know, which obviously should be dried up.

But the problem is that when you add that ad hoc logic, the logic, when you dry it up, you might put it in a function or something, and that function may end up, you know, itself being ad hoc logic, just living somewhere else. So you're still not centralizing everything, which, you know, definitely becomes a bit of a problem.

And so enter the reducer, popularized by Flux and state management libraries like Redux, reducers provide a way to contain this logic in a centralized convenient location. So one hugely important beneficial constraint of reducers is that it forces you to interact with the logic by sending events or actions, as they're called in React and Redux. By the way, the naming of actions was sort of a mistake, at least in my opinion. So we're going to be using the term events in this presentation. But here is why dispatching events is actually a really good thing. It forces you to reify what can happen at your app in any given point in time. So the user may click a button, a fetch may resolve or reject, a timer might go off. All of those are events. And thinking about your app in terms of state and events really simplifies the mental model, at least in my opinion.

However, this isn't easily visualized either. Reducers typically contain switch statements or if statements to discern what should happen when an event is received, thus distinguishing how the behavior of your app can change. And this becomes a lot more difficult. It mixes those switch statements and those if statements. And so you have to piece together the logic by navigating through a bunch of these statements and defensive logic just to discern what the behavior of your app can be at any given point in time. It's all in a single function. And it's hard to pull that apart.

So state machines are like reducers. And they can even be written as reducers. But instead of mixing all the logic together, it cleanly separates behaviors into what are known as finite states. A finite state represents a behavior, which is what the current state is of some actor and how it can respond to events. So it might respond to an event by performing an action or by changing its behavior or anything like that. And that's represented by these transition arrows that go from state to state. Or an event might not be handled, in which case the default behavior is to ignore that event.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Everything Beyond State Management in Stores with Pinia
Vue.js London Live 2021Vue.js London Live 2021
34 min
Everything Beyond State Management in Stores with Pinia
Top Content
When we think about Vuex, Pinia, or stores in general we often think about state management and the Flux patterns but not only do stores not always follow the Flux pattern, there is so much more about stores that make them worth using! Plugins, Devtools, server-side rendering, TypeScript integrations... Let's dive into everything beyond state management with Pinia with practical examples about plugins and Devtools to get the most out of your stores.
React Compiler - Understanding Idiomatic React (React Forget)
React Advanced Conference 2023React Advanced Conference 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
React provides a contract to developers- uphold certain rules, and React can efficiently and correctly update the UI. In this talk we'll explore these rules in depth, understanding the reasoning behind them and how they unlock new directions such as automatic memoization. 
Using useEffect Effectively
React Advanced Conference 2022React Advanced Conference 2022
30 min
Using useEffect Effectively
Top Content
Can useEffect affect your codebase negatively? From fetching data to fighting with imperative APIs, side effects are one of the biggest sources of frustration in web app development. And let’s be honest, putting everything in useEffect hooks doesn’t help much. In this talk, we'll demystify the useEffect hook and get a better understanding of when (and when not) to use it, as well as discover how declarative effects can make effect management more maintainable in even the most complex React apps.
Speeding Up Your React App With Less JavaScript
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Top Content
Too much JavaScript is getting you down? New frameworks promising no JavaScript look interesting, but you have an existing React application to maintain. What if Qwik React is your answer for faster applications startup and better user experience? Qwik React allows you to easily turn your React application into a collection of islands, which can be SSRed and delayed hydrated, and in some instances, hydration skipped altogether. And all of this in an incremental way without a rewrite.
React Query: It’s Time to Break up with your "Global State”!
React Summit Remote Edition 2020React Summit Remote Edition 2020
30 min
React Query: It’s Time to Break up with your "Global State”!
Top Content
An increasing amount of data in our React applications is coming from remote and asynchronous sources and, even worse, continues to masquerade as "global state". In this talk, you'll get the lowdown on why most of your "global state" isn't really state at all and how React Query can help you fetch, cache and manage your asynchronous data with a fraction of the effort and code that you're used to.
SolidJS: Why All the Suspense?
JSNation 2023JSNation 2023
28 min
SolidJS: Why All the Suspense?
Top Content
Solid caught the eye of the frontend community by re-popularizing reactive programming with its compelling use of Signals to render without re-renders. We've seen them adopted in the past year in everything from Preact to Angular. Signals offer a powerful set of primitives that ensure that your UI is in sync with your state independent of components. A universal language for the frontend user interface.
But what about Async? How do we manage to orchestrate data loading and mutation, server rendering, and streaming? Ryan Carniato, creator of SolidJS, takes a look at a different primitive. One that is often misunderstood but is as powerful in its use. Join him as he shows what all the Suspense is about.

Workshops on related topic

Rethinking Server State with React Query
React Summit 2020React Summit 2020
96 min
Rethinking Server State with React Query
Top Content
Featured Workshop
Tanner Linsley
Tanner Linsley
The distinction between server state and client state in our applications might be a new concept for some, but it is very important to understand when delivering a top-notch user experience. Server state comes with unique problems that often sneak into our applications surprise like:
- Sharing Data across apps- Caching & Persistence- Deduping Requests- Background Updates- Managing “Stale” Data- Pagination & Incremental fetching- Memory & Garbage Collection- Optimistic Updates
Traditional “Global State” managers pretend these challenges don’t exist and this ultimately results in developers building their own on-the-fly attempts to mitigate them.
In this workshop, we will build an application that exposes these issues, allows us to understand them better, and finally turn them from challenges into features using a library designed for managing server-state called React Query.
By the end of the workshop, you will have a better understanding of server state, client state, syncing asynchronous data (mouthful, I know), and React Query.
State Management in React with Context and Hooks
React Summit Remote Edition 2021React Summit Remote Edition 2021
71 min
State Management in React with Context and Hooks
WorkshopFree
Roy Derks
Roy Derks
A lot has changed in the world of state management in React the last few years. Where Redux used to be the main library for this, the introduction of the React Context and Hook APIs has shaken things up. No longer do you need external libraries to handle both component and global state in your applications. In this workshop you'll learn the different approaches to state management in the post-Redux era of React, all based on Hooks! And as a bonus, we'll explore two upcoming state management libraries in the React ecosystem.