Give the component a value that can change
A signal is a piece of reactive state that tracks its dependants and notifies them when its value changes. This counter declares an integer signal, a closure that increments it and a button that calls the closure.
<script>let count: Signal<i32> = Signal::new(0);let increment = move || count.update(|n| *n += 1);</script><style>:global(body) { display: flex; flex-direction: column; align-items: flex-start; gap: 12px; padding: 16px; }button { padding: 8px 12px; }</style><button onclick={increment}>Clicked {count} times</button> Save this over crates/demos/demos/counter.wisp in the checkout from Getting started, then run:
cargo run -p vapors-app --example demo -- counter Click the button twice. Its text should read “Clicked 2 times”. The hosted counter uses the same state and handler, with additional styling.
Follow one click
onclick registers increment. The closure calls count.update, changing the stored integer. The text expression reads count, so its reactive effect updates the text after the write. The handler does not modify the text node itself.
A signal handle is copyable. A move closure captures a handle to the same state; it does not create a second counter.
Keep the type annotation
Write Signal<i32> explicitly. The compiler turns the declaration into a generated struct field and needs that field’s type.
A bare signal name in a text expression is read for you. Rust expressions in conditions and attributes use explicit .get() calls.
Try a change
Change the handler to add two, rebuild and click again. Each click should now advance the displayed count by two. The CSS makes the button a flex item, which is required by the layout implementation at this revision.
See Signals and derived state for batching and disposal, and Styling for component-scoped CSS.