Skip to content

Conditional rendering ​

Add a Details button that hides the doubled result without losing the count.

Add expanded: f.boolean(true) to the model and a toggle action:

ts
const toggle = action(() => {
  self.expanded.value = !self.expanded.value;
});
return { increment, doubled, toggle };

Put a changing structural condition in a function inside view:

Step 1 / 2
view(_props, self) {
const counter = self.model;
function details() {
if (!counter.expanded.value) return null;
return <output>Doubled: {counter.doubled}</output>;
}
return <section>
<h2>{counter.label}</h2>
<button onClick={counter.increment}>Count: {counter.count}</button>
<button onClick={counter.toggle}>Details</button>
{details}
</section>;
}

Pass the details function to JSX. It reads expanded and chooses the output.

Pass {details}, not details(). The renderer calls the function and tracks its read of expanded.value.

Hiding removes only output. The counter's button stays in place. Showing creates a new output with the current result; old and new subtrees are not compared.

Hide the details, increase the count and show them again. The result should include every click.