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 application logic, and get a sneak peek of never-before-seen upcoming visual tools that will take state management to the next level.

This talk has been presented at JSNation Live 2021, check out the latest edition of this JavaScript Conference.

FAQ

A visual formalism is a diagrammatic and intuitive approach that is mathematically rigorous and precise, used to describe the logic and behavior of entities in state management. It utilizes special notations like arrows and boxes to represent various states and transitions clearly.

Visual representations like state machines and state charts provide an exact specification of application logic that might be difficult to describe textually. This helps in better understanding and managing the application's state by making it easy to see behaviors and transitions at a glance.

Traditional coding approaches often mix logic and data, which can make the code hard to understand, especially as it evolves. This leads to logic that is scattered and hard to manage, making the system prone to errors and difficult to debug.

Reducers provide a centralized way to manage all logic in an application. They help in maintaining a clear separation of concerns by processing actions or events to update the state, thus enforcing a structured flow of data and state transitions.

State machines separate concerns more cleanly by organizing logic into finite states rather than conditional branches like in reducers. This reduces complexity and prevents impossible states and transitions, making the system's behavior easier to predict and manage.

State charts extend the capabilities of state machines by introducing hierarchy and grouping states to represent common transitions more cleanly. This helps in managing complex state interactions and avoiding a combinatorial explosion of states and transitions.

XState is a library for JavaScript that supports state machines and state charts. It is framework-agnostic and allows developers to code state logic cleanly while supporting automatic visualization, making state management more intuitive and manageable.

Yes, XState can integrate with existing frameworks like Redux. It facilitates the use of state machines and can enhance the management of application states by providing a more organized and visual approach to handling transitions and state logic.

David Khourshid
David Khourshid
32 min
09 Jun, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This talk discusses the visual future of state management and the use of state machines and state charts. XState is introduced as a state machine and state chart library for JavaScript, providing a clean and visualizable way to represent state machines. The talk highlights the features of XState, such as its state-first approach and utilities for interpreting the machine as a service. It also mentions the future goals of XState, including visualization, testing, analytics, and the development of a visual software modeling suite called Stately.

1. Introduction to Visual Future of State Management

Short description:

Hello, my name is David Korshid. I want to talk to you today about the visual future of state management. Visuals serve as an exact specification of something that would be hard to describe with just text. The way we typically code application logic doesn't lend itself well to a visual formalism. Enter the reducer, which provides a way to contain all of this logic in a centralized convenient location. Dispatching events is a really good thing.

Hello, my name is David Korshid. I'm David K. Piano, pretty much everywhere online. And I want to talk to you today about the visual future of state management.

Now, what do we mean by visual? So you probably know what a Venn diagram is. It's a visual in an exact way of representing commonalities between two or more different things. And you might have also heard about a sequence diagram. And so this describes how different parts of a system communicate with each other. Now, diagrams like these are very useful for conveying relationships in a visually unambiguous way, each with their own special notations that mean specific things. And state machines and state charts, which I talk about a lot, those fall under the same visually exact diagram category in that they describe the logic and the behavior of some special entity using some special notation like arrows and boxes and regions.

Now, David Harold, the inventor of state charts, calls this a visual formalism. He describes that visual formalisms are diagrammatic and intuitive, yet mathematically rigorous and precise languages. So thus, despite their clear visual appearance, they come complete with a syntax that allows you to determine what's allowed and what's not allowed. And it also comes with semantics that determine what the allowed things actually mean. So these kinds of visuals serve a higher purpose than to just make your boring documentation look a little bit nicer. They serve as an exact specification of something that would be hard to describe with just text.

So the way that we typically code application logic doesn't really lend itself well 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 directly in event handlers or in promises or in callbacks or things like that. Now while this may be convenience to code, the problem is that this logic is hard to understand, especially as it changes over time due to events or anything else that can happen. And you can't really discern what can happen exactly or how an application can respond to an event at any point in time. That connection logic, it resides mostly in the head of the developer who coded that logic, which isn't really useful. And worse with ad hoc logic, that tends to get repeated throughout the code base. So even if you attempt to drive this up, you have that same ad hoc logic living somewhere else instead of a centralized place where all of your logic lives.

So enter the reducer. Of course the reducer was popularized very early on by blocks and state management libraries like Redux. And reducers are what were used to provide a way to contain all of this logic in a centralized convenient location. So one important and hugely beneficial constraints of reducer is that it enforces you to interact with the logic via sending events or actions as they call it in React and Redux land. By the way, the naming of actions, I don't particularly like, I think it was a mistake. We're gonna be using the term events in place of actions throughout this presentation. But here's why dispatching events is a really good thing.

2. Introduction to State Machines and State Charts

Short description:

It forces you to reify what can happen in your app at any point in time. Reducers typically contain switch statements or if statements to discern what should happen when an event is received. State machines cleanly separate behaviors into finite states, preventing impossible states and transitions. State charts take the visual formalism of state machines further by introducing hierarchy and allowing for a clearer representation of complex logic. XState was created to provide a mathematically rigorous visual formalism for state charts.

It forces you to reify what can happen in your app at any 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 events really simplifies the mental model. 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. So that's distinguishing how the behavior of your app can change becomes a lot more difficult. It's mixed into those switch statements and you have to piece together all of that logic and navigate through a bunch of defensive statements like ternaries and if statements just to make sense of the logic.

So it's as if your reducer is a single statement in a state machine with the single state in a bunch of conditional and sometimes unnecessary transitions. It might work and it might do the job that it's supposed to do, but it's difficult to understand and it's still prone to impossible states and transitions. So state machines, by the way, they're like reducers and I talk about this pretty much everywhere online and state machines can also 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. What is the current states of an actor and how it could respond to events. So it might respond to an events by doing an action or by changing its behavior, which is represented by the transition arrows that you see here going from states to states. Or in events might not be handled. In which case the default behavior is to ignore the events. In reducers, this often requires a lot of defensive code like if statements. In state machines, it's built right into the mathematical model. And more practically, this kind of mechanism prevents impossible states, which guarantees that two behaviors can't occur at the same time. And it also prevents impossible transitions since all transitions between finite states must be explicitly written. And like you could tell over here, it lends itself well to visualization. We can understand how some actor can change its behavior by playing the events on the diagram, following the arrows and seeing what the next finite states should be.

Now, state charts take this same idea of a visual formalism introduced by state machines and it takes it one step further. It introduces hierarchy among many other things. So although state machines provide a way to cleanly organize logic, they suffer from combinatorial explosion of states and transitions, especially when different finite states are actually related. By extending the notion of state machines to be a hierarchical graph or a high graph as David Harrell calls it, we can group states together to represent common transitions cleanly. We can also isolate logic so that we see the bigger picture instead of having to understand all the little implementation details at once in a big flat structure. Like state machines, state charts are also mathematically rigorous visual formalisms. They can express a much higher degree of complexity than state machines and they can represent it in a clear and visual way. So that's why I created XState a few years ago.

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
State management is not limited to complex applications and transitioning to a store offers significant benefits. Pinia is a centralized state management solution compatible with Vue 2 and Vue 3, providing advanced devtools support and extensibility with plugins. The core API of Pinia is similar to Vuex, but with a less verbose version of stores and powerful plugins. Pinia allows for easy state inspection, error handling, and testing. It is recommended to create one file per store for better organization and Pinia offers a more efficient performance compared to V-rex.
Using useEffect Effectively
React Advanced Conference 2022React Advanced Conference 2022
30 min
Using useEffect Effectively
Top Content
Today's Talk explores the use of the useEffect hook in React development, covering topics such as fetching data, handling race conditions and cleanup, and optimizing performance. It also discusses the correct use of useEffect in React 18, the distinction between Activity Effects and Action Effects, and the potential misuse of useEffect. The Talk highlights the benefits of using useQuery or SWR for data fetching, the problems with using useEffect for initializing global singletons, and the use of state machines for handling effects. The speaker also recommends exploring the beta React docs and using tools like the stately.ai editor for visualizing state machines.
Modern Web Debugging
JSNation 2023JSNation 2023
29 min
Modern Web Debugging
Top Content
This Talk discusses modern web debugging and the latest updates in Chrome DevTools. It highlights new features that help pinpoint issues quicker, improved file visibility and source mapping, and ignoring and configuring files. The Breakpoints panel in DevTools has been redesigned for easier access and management. The Talk also covers the challenges of debugging with source maps and the efforts to standardize the source map format. Lastly, it provides tips for improving productivity with DevTools and emphasizes the importance of reporting bugs and using source maps for debugging production code.
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
Global state management and the challenges of placing server state in global state are discussed. React Query is introduced as a solution for handling asynchronous server state. The Talk demonstrates the process of extracting logic into custom hooks and fixing issues with state and fetching logic. Optimistic updates with mutation are showcased, along with the benefits of using React Query for data fetching and mutations. The future of global state management is discussed, along with user feedback on React Query. The Talk concludes with an invitation to explore React Query for server state management.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.
Jotai Atoms Are Just Functions
React Day Berlin 2022React Day Berlin 2022
22 min
Jotai Atoms Are Just Functions
Top Content
State management in React is a highly discussed topic with many libraries and solutions. Jotai is a new library based on atoms, which represent pieces of state. Atoms in Jotai are used to define state without holding values and can be used for global, semi-global, or local states. Jotai atoms are reusable definitions that are independent from React and can be used without React in an experimental library called Jotajsx.

Workshops on related topic

React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Mastering advanced concepts in TypeScript
React Summit US 2023React Summit US 2023
132 min
Mastering advanced concepts in TypeScript
Top Content
Featured WorkshopFree
Jiri Lojda
Jiri Lojda
TypeScript is not just types and interfaces. Join this workshop to master more advanced features of TypeScript that will make your code bullet-proof. We will cover conditional types and infer notation, template strings and how to map over union types and object/array properties. Each topic will be demonstrated on a sample application that was written with basic types or no types at all and we will together improve the code so you get more familiar with each feature and can bring this new knowledge directly into your projects.
You will learn:- - What are conditional types and infer notation- What are template strings- How to map over union types and object/array properties.
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.
Integrating LangChain with JavaScript for Web Developers
React Summit 2024React Summit 2024
92 min
Integrating LangChain with JavaScript for Web Developers
Featured Workshop
Vivek Nayyar
Vivek Nayyar
Dive into the world of AI with our interactive workshop designed specifically for web developers. "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" offers a unique opportunity to bridge the gap between AI and web development. Despite the prominence of Python in AI development, the vast potential of JavaScript remains largely untapped. This workshop aims to change that.Throughout this hands-on session, participants will learn how to leverage LangChain—a tool designed to make large language models more accessible and useful—to build dynamic AI agents directly within JavaScript environments. This approach opens up new possibilities for enhancing web applications with intelligent features, from automated customer support to content generation and beyond.We'll start with the basics of LangChain and AI models, ensuring a solid foundation even for those new to AI. From there, we'll dive into practical exercises that demonstrate how to integrate these technologies into real-world JavaScript projects. Participants will work through examples, facing and overcoming the challenges of making AI work seamlessly on the web.This workshop is more than just a learning experience; it's a chance to be at the forefront of an emerging field. By the end, attendees will not only have gained valuable skills but also created AI-enhanced features they can take back to their projects or workplaces.Whether you're a seasoned web developer curious about AI or looking to expand your skillset into new and exciting areas, "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" is your gateway to the future of web development. Join us to unlock the potential of AI in your web projects, making them smarter, more interactive, and more engaging for users.
From Todo App to B2B SaaS with Next.js and Clerk
React Summit US 2023React Summit US 2023
153 min
From Todo App to B2B SaaS with Next.js and Clerk
WorkshopFree
Dev Agrawal
Dev Agrawal
If you’re like me, you probably have a million side-project ideas, some that could even make you money as a micro SaaS, or could turn out to be the next billion dollar startup. But how do you know which ones? How do you go from an idea into a functioning product that can be put into the hands of paying customers without quitting your job and sinking all of your time and investment into it? How can your solo side-projects compete with applications built by enormous teams and large enterprise companies?
Building rich SaaS products comes with technical challenges like infrastructure, scaling, availability, security, and complicated subsystems like auth and payments. This is why it’s often the already established tech giants who can reasonably build and operate products like that. However, a new generation of devtools are enabling us developers to easily build complete solutions that take advantage of the best cloud infrastructure available, and offer an experience that allows you to rapidly iterate on your ideas for a low cost of $0. They take all the technical challenges of building and operating software products away from you so that you only have to spend your time building the features that your users want, giving you a reasonable chance to compete against the market by staying incredibly agile and responsive to the needs of users.
In this 3 hour workshop you will start with a simple task management application built with React and Next.js and turn it into a scalable and fully functioning SaaS product by integrating a scalable database (PlanetScale), multi-tenant authentication (Clerk), and subscription based payments (Stripe). You will also learn how the principles of agile software development and domain driven design can help you build products quickly and cost-efficiently, and compete with existing solutions.
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.