Reactivity Reference

Marko's reactivity system tracks dependencies between state and DOM output at compile time, updating only the affected nodes when values change. Mutable state starts with <let>.

Reactive Variables

Tag variables, tag parameters, and input are reactive. Writable state comes from <let> and assignable <return> tags; other tag variables (<const>, <id>, <define>, element references) are derived or read-only. $global is not reactive.

Render Expressions

Any expression in a template that reads a reactive variable re-evaluates when that variable changes. Reactive expressions appear in attributes, tag arguments, dynamic text, dynamic tag names, <style> dynamic values, <html-comment>, and <script> bodies.

Note

Module-level statements (import, export, static, server, and client) run once when the template module loads and are outside the reactive graph.

<let/count=0>

<button onClick() { count++ }>Current: ${count}</button>
let/count=0

button onClick() { count++ } -- Current: ${count}

Clicking the button reassigns count, and the button text updates automatically.

Caution

The compiler may group or reorder render expressions into shared update functions, so render expressions must be pure.

Tip

Marko resolves its reactive dependency graph at compile time rather than tracking subscriptions at runtime, generating direct DOM updates for each state variable.

Scheduling Updates

Marko compares updates by reference equality (===) and batches changes into an update queue flushed on the next microtask.

Updates scheduled after the microtask flush but before the browser paints are deferred to the next animation frame. This ensures:

  • Content ready to paint is not blocked
  • Reactive feedback loops are throttled to at most one cycle per frame
  • Updates in background tabs pause until the document is visible

For visual motion, prefer CSS Animations, CSS Transitions, or the Web Animations API.

Stale Derived Values

Assigning to an uncontrolled <let> writes the new value into scope immediately, so subsequent lines in the same handler read the updated value. (A controllable <let> invokes its valueChange handler instead, updating only when the owner updates and flushes.) Derived values such as <const> and rendered DOM nodes recompute when the queue flushes, so they still hold the previous state for the rest of the handler:

<let/quantity=1>
<const/subtotal=quantity * input.unitPrice>

<button onClick() {
  quantity++;

  // ❌ `subtotal` still holds the previous value until the update queue flushes
  console.log(subtotal);

  // ✅ `quantity` holds the new value; compute the total from it directly
  input.onQuantityChange(quantity, quantity * input.unitPrice);
}>
  Add one
</button>

<output>${subtotal}</output>
let/quantity=1
const/subtotal=quantity * input.unitPrice

button onClick() {
  quantity++;

  // ❌ `subtotal` still holds the previous value until the update queue flushes
  console.log(subtotal);

  // ✅ `quantity` holds the new value; compute the total from it directly
  input.onQuantityChange(quantity, quantity * input.unitPrice);
} --
  Add one

output -- ${subtotal}
Warning

Reading subtotal or the DOM inside the handler after quantity++ returns the value from the previous render because the update queue has not flushed yet.


Contributors

Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.