Performance: State machines with composables
MEDIUM IMPACT
This affects how efficiently the UI updates in response to state changes and how much unnecessary work the browser does during rendering.
import { createMachine, interpret } from '@xstate/fsm'; const machine = createMachine({ initial: 'idle', states: { idle: { on: { START: 'loading' } }, loading: { on: { FINISH: 'success' } }, success: {} } }); const service = interpret(machine).start(); const state = ref(service.state.value); service.onTransition(s => { state.value = s.value; });
const state = ref('idle'); function start() { state.value = 'loading'; } function finish() { state.value = 'success'; } // UI watches state and updates directly without clear transition rules
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct state changes without machine | High (many updates) | Multiple reflows per state change | High paint cost due to redundant updates | [X] Bad |
| State machines with composables | Minimal (only on valid transitions) | Single reflow per transition | Lower paint cost due to fewer updates | [OK] Good |