# Finite State Machines for UI vs Signals and Fine-Grained Reactivity (Reactive Graph)

- **Canonical URL:** https://frontend.rocks/vs/finite-state-machines-ui-vs-fine-grained-signals/
- **Relation:** Contrasts With
- **Last Verified:** 2026-11-15
- **Verdict:** Signals propagate arbitrary reactive value changes, whereas finite state machines enforce explicit transition guards.

## Comparison Matrix

| Dimension | Finite State Machines for UI | Signals and Fine-Grained Reactivity (Reactive Graph) |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Experimental |
| **Domain** | State & Architecture | State & Architecture |
| **Mental Model** | Replaces overlapping boolean flags (isLoading, isError, isRetrying) with a discriminated union and explicit (state, event) transition table that makes invalid states unrepresentable. | Reactive model built on a directed acyclic dependency graph (Signal, Computed, Effect) updating only subscribed DOM nodes without virtual tree diffing. |
| **Prerequisites** | None | Event Loop, Macrotasks, and Microtask Queue (Reactive effect batching commonly relies on a microtask flush.) |
| **When Not to Use** | When not to use an FSM: For continuous numeric values (scroll offset, canvas coordinates) or simple independent form fields, plain state or reactive signals are far lighter. | When not to use Signals: For simple forms or mostly static pages, native DOM primitives avoid unnecessary abstraction overhead; for multi-step workflows with illegal state combinations, pair or replace raw signals with Finite State Machines. |

## Finite State Machines for UI — Under the Hood
Boolean flags act like four light switches wired to one bulb: nothing stops code from flipping `isLoading` and `isError` on together. A Finite State Machine works like a rotary dial, only one position is active, blocking [Optimistic UI](/concepts/optimistic-ui-rollback) races and [Stale Closures](/concepts/stale-closures-hooks).

1. **Finite states**: Enumerate every legal mode (`idle | loading | success | error`) as a tagged union.
2. **Events**: Represent triggers as explicit objects (`{ type: 'SUBMIT' }`) rather than ad-hoc setter calls.
3. **Transition function**: A pure `transition(state, event)` checks the current state first, ignoring any event not valid for that state.

```ts
type State = { status: 'idle' } | { status: 'loading' } | { status: 'error'; message: string };
type Event = { type: 'FETCH' } | { type: 'FAIL'; message: string } | { type: 'RESET' };

function transition(state: State, event: Event): State {
  if (state.status === 'idle' && event.type === 'FETCH') return { status: 'loading' };
  if (state.status === 'loading' && event.type === 'FAIL') return { status: 'error', message: event.message };
  if (state.status === 'error' && event.type === 'RESET') return { status: 'idle' };
  return state; // Invalid event for current state is safely ignored
}
```

## Signals and Fine-Grained Reactivity (Reactive Graph) — Under the Hood
1. **Automatic Tracking**: When a `computed` or `effect` runs, it pushes its context onto a global tracking stack; each `signal.get()` adds a `signal -> observer` edge.
2. **Push-Pull Propagation**: Calling `signal.set(v)` marks descendants `Dirty` (Push) without eagerly recomputing lazy derived values, avoiding wasted [V8 Hidden Classes](/concepts/v8-hidden-classes-ic) allocations on unused branches.
3. **Lazy Evaluation (Pull)**: A `computed` only recomputes when read, and short-circuits downstream effects (often flushed on the [Event Loop](/concepts/event-loop-tasks-microtasks) microtask checkpoint) if its new value equals the previous one.

```js
const count = signal(1);
const doubled = computed(() => count.get() * 2);

effect(() => {
  // Re-runs ONLY when `doubled` actually changes
  badgeEl.textContent = String(doubled.get());
});
```

## Interactive Labs & Dissections
- [Finite State Machines for UI Lab](https://frontend.rocks/concepts/finite-state-machines-ui/#lab) ([Markdown](https://frontend.rocks/concepts/finite-state-machines-ui.md))
- [Signals and Fine-Grained Reactivity (Reactive Graph) Lab](https://frontend.rocks/concepts/fine-grained-signals/#lab) ([Markdown](https://frontend.rocks/concepts/fine-grained-signals.md))

## Related Architecture Autopsies
- [Autopsy #01: Anatomy of a Double Payment in Production](https://frontend.rocks/autopsies/double-payment-race-condition/) ([Markdown](https://frontend.rocks/autopsies/double-payment-race-condition.md)): Rapid double-click or 3G network blip charges the user twice despite a disabled={isSubmitting} React button.

## Primary Sources
- https://www.w3.org/TR/scxml/
- https://stately.ai/docs/state-machines-and-statecharts
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
- https://github.com/tc39/proposal-signals
