01 — Counter
The smallest reactive program. One signal, one event handler, one DOM mutation per click.
tsx
import { use } from 'flexium/core'
export function Counter() {
const [count, setCount] = use(0)
return (
<button onclick={() => setCount(c => c + 1)}>
Count: {count}
</button>
)
}What's happening
use(0)creates a signal with initial value0. Returns[value, setter]—valueis the current value (not a getter call),setteraccepts either a new value or a(prev) => nextupdater.- The
{count}expression in JSX is tracked as a dependency. WhensetCountfires, only this DOM text node is updated — no virtual DOM diff. - Re-rendering a single text node takes microseconds.
Mount it
tsx
import { render } from 'flexium/dom'
import { Counter } from './Counter'
render(<Counter />, document.getElementById('app')!)Functional updater
When the new value depends on the previous one, use the function form:
tsx
setCount(c => c + 1) // ✓ atomic, safe under concurrent updates
setCount(count + 1) // also works since count is the current value at render timeTry it
Edit your component and add a reset button:
tsx
function Counter() {
const [count, setCount] = use(0)
return (
<div>
<button onclick={() => setCount(c => c + 1)}>Count: {count}</button>
<button onclick={() => setCount(0)}>Reset</button>
</div>
)
}API surface used
use(initialValue)— signal hookrender(node, target)— mount
Next
→ Todo list — arrays, keyed reconciliation, batched updates.