Controllable Components

TL;DR
  • Controlled components take their state from parent input props
  • Uncontrolled components manage internal state directly
  • Controllable components default to uncontrolled and become controlled when given a change handler
  • Native tags and <let> implement this pattern via change handlers, with optional := shorthand

Component authors rarely know in advance whether every caller will want to hoist state or leave it internal. Rather than forcing a permanent choice between controlled and uncontrolled APIs, Marko makes controllability a first-class language pattern.

Uncontrolled Components

An uncontrolled component owns its state internally through <let>:

tags/counter.marko
<let/count=0>

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

button onClick() { count++ } -- Count: ${count}
parent.marko
<counter/>

// 🤔 How can we access `count` out here?
<output>${count}</output>
counter

// 🤔 How can we access `count` out here?
output -- ${count}

Because count lives inside counter.marko, callers use <counter/> with zero boilerplate, but a parent cannot read or reset count externally.

State Synchronization

A common workaround is keeping internal state in <counter> while notifying the parent through an event callback:

Warning

Mirroring child state into parent state is an anti-pattern. Prefer the controllable pattern so only one source of truth exists.

tags/counter.marko
<let/count=0>
<button onClick() {
  input.onChange(++count);
}>
  Count: ${count}
</button>
export interface Input {
  onChange: (count: number) => void;
}

<let/count=0>

<button onClick() { input.onChange(++count) }>Count: ${count}</button>
let/count=0
button onClick() {
  input.onChange(++count);
} -- Count: ${count}
export interface Input {
  onChange: (count: number) => void;
}

let/count=0

button onClick() { input.onChange(++count) } -- Count: ${count}
parent.marko
<let/count=0>

<counter onChange(newCount) { count = newCount }/>
<output>${count}</output>
let/count=0

counter onChange(newCount) { count = newCount }
output -- ${count}

This creates two count variables that must be kept in sync manually and can drift whenever either side changes independently.

Controlled Components

A controlled component delegates state ownership entirely to its parent:

tags/counter.marko
<button onClick() {
  input.countChange(input.count + 1);
}>
  Count: ${input.count}
</button>
export interface Input {
  count: number;
  countChange: (count: number) => void;
}

<button onClick() { input.countChange(input.count + 1) }>
  Count: ${input.count}
</button>
button onClick() {
  input.countChange(input.count + 1);
} -- Count: ${input.count}
export interface Input {
  count: number;
  countChange: (count: number) => void;
}

button onClick() { input.countChange(input.count + 1) } -- Count: ${input.count}
parent.marko
<let/count=0>

<counter count=count countChange(newCount) { count = newCount }/>
let/count=0

counter count=count countChange(newCount) { count = newCount }

The parent has full control, but every caller must now allocate state and pass a countChange callback even when it never reads count.

The Controllable Pattern

A controllable component behaves as uncontrolled by default and switches to controlled whenever the caller supplies a <prop>Change handler.

Controllable Native Tags

Stateful HTML elements follow this exact model in Marko via change handlers. Adding valueChange= hands ownership of an <input> to Marko state, and the binding shorthand := writes both attributes together:

<let/textValue="">

<input value=textValue valueChange(v) { textValue = v }>

// equivalent with binding shorthand
<input value:=textValue>
let/textValue=""

input value=textValue valueChange(v) { textValue = v }

// equivalent with binding shorthand
input value:=textValue

Controllable <let>

<let> is itself controllable. Binding it to input.count with := (shorthand for <let/count=input.count valueChange=input.countChange>) makes <counter> work in both modes:

tags/counter.marko
<let/count:=input.count>
<button onClick() {
  count++;
}>
  Count: ${count}
</button>
export interface Input {
  count: number;
  countChange?: (count: number) => void;
}

<let/count:=input.count>

<button onClick() { count++ }>Count: ${count}</button>
let/count:=input.count
button onClick() {
  count++;
} -- Count: ${count}
export interface Input {
  count: number;
  countChange?: (count: number) => void;
}

let/count:=input.count

button onClick() { count++ } -- Count: ${count}
parent.marko
<let/parentCount=0>

// Controlled: `parentCount` is the source of truth
<counter count:=parentCount/>
<output>${parentCount}</output>

// Uncontrolled: counter manages its own state starting at 0
<counter count=0/>
let/parentCount=0

// Controlled: `parentCount` is the source of truth
counter count:=parentCount
output -- ${parentCount}

// Uncontrolled: counter manages its own state starting at 0
counter count=0

When countChange is a function (passed explicitly or via :=), <let> delegates updates to countChange and reflects input.count. When countChange is undefined, <let> manages local state initialized from input.count.

To make count?: number optional with a default value so callers can write <counter/>, pass value= and valueChange= explicitly since := cannot take a fallback such as ?? 0:

<let/count=(input.count ?? 0) valueChange=input.countChange>
let/count=(input.count ?? 0) valueChange=input.countChange

Refining Functions

Insert a refining function (:fn:=) to transform incoming values before assignment, such as parsing strings from native inputs into numbers or normalizing text:

<let/num=0>
<input type="range" value:parseFloat:=num>
let/num=0
input type="range" value:parseFloat:=num

Custom functions work identically:

<let/text="HELLO">
<input value:uppercase:=text>
static function uppercase(str) {
  return str.toUpperCase();
}
<let/text="HELLO">
<input value:uppercase:=text>

static function uppercase(str: string) {
  return str.toUpperCase();
}
let/text="HELLO"
input value:uppercase:=text
static function uppercase(str) {
  return str.toUpperCase();
}
let/text="HELLO"
input value:uppercase:=text

static function uppercase(str: string) {
  return str.toUpperCase();
}

Custom Control Logic

Because a controlled parent intercepts every update through countChange, it can clamp values, validate input, or confirm changes without modifying <counter>:

<let/count=0>

<counter count=count countChange(next) {
    count = Math.min(5, next);
  }/>
let/count=0

counter count=count countChange(next) {
    count = Math.min(5, next);
  }

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.