Maya Shavin
with
Set local state
Update local state
Render component
Multiple clicks?
Loading state?
Error state?
Disabled?
Basic actions
New action
New action
Bug fixes
Bug fixes
Difficult to maintain
Difficult to test
🐛
Hard to understand
Deterministic Finite Automata
one initial state
finite number of states
finite number of events
transition map triggered by events
Promises
Async/Await
Callbacks
Observables
graphs
State machine/chart
State machine/chart
Machine
transition
Events
State machine/chart
Actions
States
const fetchMachine = Machine({
id: 'fetch',
initial: 'idle',
context: { retries: 0 },
states: {
idle: {
on: {
FETCH: 'loading'
}
},
loading: {
on: {
RESOLVE: 'success',
REJECT: 'failure'
}
},
success: {
type: 'final'
},
failure: {
on: {
RETRY: {
target: 'loading',
actions: assign({
retries: (context, event) => context.retries + 1
})
}
}
}
}
});
inactive
toggle
active
toggle
import { Machine } from 'xstate';
export const toggleMachine = Machine({
id: 'toggle',
context: {
/* some data */
},
initial: 'inactive',
states: {
inactive: {
on: { toggle: 'active' }
},
active: {
on: { toggle: 'inactive' }
}
}
});
Code reusability
Test coverage
Code scalability & maintenance
Better code design & planning
Easy to debug and spot bugs
Visually clear view of the code flow
State machine/chart
Senior Software Engineer
Core maintainer of StorefrontUI
NuxtJS Ambassador
Everything about Frontend/Web
Media Developer Expert (MDE)