Controllable Components
- Controlled components take their state from parent
inputprops - 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>:
<let/count=0>
<button onClick() { count++ }>Count: ${count}</button>let/count=0
button onClick() { count++ } -- Count: ${count}<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:
Mirroring child state into parent state is an anti-pattern. Prefer the controllable pattern so only one source of truth exists.
<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}<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:
<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}<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 <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:
<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}<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=0When 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.countChangeRefining 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:=numCustom 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.