setChildren()
can also become reactive.
When state changes, the entire child tree
can be rebuilt declaratively.
This enables:
dynamic lists,
structural UI updates,
and conditional rendering patterns.
const todo =
document
.createElement("div")
.setAttributes({
class: "card"
})
// ===============================
// LOCAL STATE
// ===============================
.setState({
items: [
"Apple",
"Orange"
]
})
// ===============================
// REACTIVE TREE
// ===============================
.setChildren(({ state }) => {
return [
// ===========================
// HEADER
// ===========================
document
.createElement("h2")
.setText(
`Todo List (${state.items.length})`
),
// ===========================
// ITEMS
// ===========================
...state.items.map((item) =>
document
.createElement("div")
.setAttributes({
class: "item"
})
.setText(item)
),
// ===========================
// BUTTON
// ===========================
document
.createElement("button")
.setText(
"Add Item"
)
.setEvents(() => ({
click() {
todo.setState(
({ state }) => {
state.items.push(
"Item " +
(
state.items.length + 1
)
);
}
);
}
}))
];
});
app
.setChildren(
todo
);