Class to Tags API

TL;DR
  • Component boundaries carry no state or lifecycle overhead; state lives locally
  • class, state, out, component, and instance methods have been removed
  • input.renderBody is replaced by input.content
  • Event handlers are standard function attributes rather than string-named methods

Marko 4 and 5 used a Class API where each component file managed its own class, state, and lifecycle methods. Marko 6 replaces this with the Tags API, where state, reactivity, and side effects are declared inline as tags and analyzed at compile time.

Components Melt Away

In the Class API, state and lifecycle attach to component boundaries (state, onInput, onMount, onDestroy). In the Tags API, component boundaries serve code organization only: state lives locally in tag variables anywhere in the template (including inside conditionals and loops), and side effects attach to individual tags.

Instance-lookup patterns like getComponent have been removed in favor of declarative data flow and exposed return values.

Updated APIs

Every removed Class API feature maps to a direct Tags API equivalent:

Event Handling

Attribute arguments such as onClick("handleClick") have been removed. Event handlers are standard function attributes written inline or passed by reference:

<let/color="blue">
<button onClick() { color = "red" }>Red</button>

<const/yellowOrGreen() {
  color = Math.random() > 0.5 ? "yellow" : "green";
}>
<button onClick=yellowOrGreen>Random</button>
let/color="blue"
button onClick() { color = "red" } -- Red

const/yellowOrGreen() {
  color = Math.random() > 0.5 ? "yellow" : "green";
}
button onClick=yellowOrGreen -- Random

Because event handlers are plain functions, they can be spread, wrapped, and called directly, replacing this.emit:

<button ...input/>
<button
  ...input
  onClick(e, el) {
    console.log("clicked the second button");
    input.onClick?.(e, el);
  }
/>
export interface Input extends Marko.HTML.Button {}

<button ...input/>

<button
  ...input
  onClick(e, el) {
    console.log("clicked the second button");
    input.onClick?.(e, el);
  }
/>
button ...input
button
  ,...input
  ,onClick(e, el) {
    console.log("clicked the second button");
    input.onClick?.(e, el);
  }
export interface Input extends Marko.HTML.Button {}

button ...input

button
  ,...input
  ,onClick(e, el) {
    console.log("clicked the second button");
    input.onClick?.(e, el);
  }

Pass values back to a parent through standard function parameters:

<for|i| until=5>
  <button onClick() {
    input.onClick?.(i);
  }>
    ${i}
  </button>
</for>
export interface Input {
  onClick?: (i: number) => void;
}

<for|i| until=5>
  <button onClick() { input.onClick?.(i) }>${i}</button>
</for>
for|i| until=5
  button onClick() {
    input.onClick?.(i);
  } -- ${i}
export interface Input {
  onClick?: (i: number) => void;
}

for|i| until=5
  button onClick() { input.onClick?.(i) } -- ${i}

Element Refs

Instead of getEl, native tags expose a tag variable getter for the DOM element, readable from <script>, <lifecycle>, and event handlers:

<input/$el>

<script>
  $el().focus();
</script>
input/$el

script
  --

    $el().focus();

  --
Note

The leading $ in $el is a naming convention to distinguish element getters from regular state variables.

For multiple elements (replacing getEls), hoisted tag variables are iterable:

<let/focus=0>

<for|i| until=5>
  <input/$el onFocus() { focus = i }>
</for>

<button onClick() {
  const els = [...$el];
  focus = (focus + 1) % els.length;
  els[focus].focus();
}>
  Focus next
</button>
let/focus=0

for|i| until=5
  input/$el onFocus() { focus = i }

button onClick() {
  const els = [...$el];
  focus = (focus + 1) % els.length;
  els[focus].focus();
} --
  Focus next

Component Refs

getComponent has been removed. Communicate between parent and child through function callbacks, the controllable pattern, or values exposed via <return>:

parent.marko
<child/child/>
<child/{ changeColor }/>

<button onClick() {
  child.changeColor("blue");
  changeColor("green");
}>
  Change colors
</button>
child/child
child/{ changeColor }

button onClick() {
  child.changeColor("blue");
  changeColor("green");
} --
  Change colors
tags/child.marko
<let/color="red">
<button style={
  "background-color": color,
}>
  ${color}
</button>
<return={
  changeColor(next) {
    color = next;
  },
}>
<let/color="red">
<button style={ "background-color": color }>${color}</button>

<return={
  changeColor(next: string) {
    color = next;
  },
}>
let/color="red"
button style={
  "background-color": color,
} -- ${color}
return={
  changeColor(next) {
    color = next;
  },
}
let/color="red"
button style={ "background-color": color } -- ${color}

return={
  changeColor(next: string) {
    color = next;
  },
}

Lifecycle Hooks

In the Class API, running mount logic inside a loop required extracting a child component:

index.marko
<for|i| from=0 to=2>
  <log-value value=i/>
</for>
for|i| from=0 to=2
  log-value value=i
components/log-value.marko
class {
  onMount() {
    console.log(this.input.value);
  }
}
class {
  onMount() {
    console.log(this.input.value);
  }
}

In the Tags API, <script> attaches directly inside the loop with no extra component:

<for|i| until=3>
  <script>
    console.log(i);
  </script>
</for>
for|i| until=3
  script
    --

        console.log(i);
     ${" "}
    --

<script>

<script> runs browser side effects after mount and re-runs whenever referenced reactive values change, cleaning up through $signal:

<script>
  window.addEventListener("click", () => {
    console.log("clicked anywhere");
  }, { signal: $signal });
</script>
script
  --

    window.addEventListener("click", () => {
      console.log("clicked anywhere");
    }, { signal: $signal });

  --
Caution

Like React's useEffect, reserve <script> for synchronizing with external side effects; prefer <const> and <let> for derived values and state.

<lifecycle>

<lifecycle> wraps imperative browser APIs with explicit onMount, onUpdate, and onDestroy hooks sharing a stable this:

client import { AudioPlayer } from "audio-player";

<let/volume=0.8>
<div/playerEl/>

<lifecycle
  volume=volume
  onMount() {
    return { player: new AudioPlayer(playerEl(), { volume: this.volume }) };
  }
  onUpdate() {
    this.player.setVolume(this.volume);
  }
  onDestroy() {
    this.player.dispose();
  }
>
client import { AudioPlayer } from "audio-player";

let/volume=0.8
div/playerEl

lifecycle
  ,volume=volume
  ,onMount() {
    return { player: new AudioPlayer(playerEl(), { volume: this.volume }) };
  }
  ,onUpdate() {
    this.player.setVolume(this.volume);
  }
  ,onDestroy() {
    this.player.dispose();
  }

Derived Values

In the Class API, derived values relied on inline scriptlets or mutating input in onInput (an anti-pattern):

$ const num = Number(input.num);
<div>${num}</div>
export interface Input {
  num: number | string;
}
$ const num = Number(input.num);
<div>${num}</div>
$ const num = Number(input.num);
div -- ${num}
export interface Input {
  num: number | string;
}
$ const num = Number(input.num);
div -- ${num}
export interface Input {
  num: number | string;
}
class {
  onInput(input) {
    input.num = Number(input.num);
  }
}
<div>${input.num}</div>
export interface Input {
  num: number | string;
}
class {
  onInput(input) {
    input.num = Number(input.num);
  }
}
div -- ${input.num}

In the Tags API, compute derived values with <const>:

<const/num=Number(input.num)>
<div>${num}</div>
export interface Input {
  num: number | string;
}

<const/num=Number(input.num)>
<div>${num}</div>
const/num=Number(input.num)
div -- ${num}
export interface Input {
  num: number | string;
}

const/num=Number(input.num)
div -- ${num}

Further Reading


Contributors

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