Compute a value from state

Store a count in a signal and derive twice that count with a memo:

counter.wisp
<script>let count: Signal<i32> = Signal::new(0);let doubled: Memo<i32> = Memo::new(move || count.get() * 2);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}>Count: {count}</button><p>Double: {doubled}</p>
vapors component source

Replace crates/demos/demos/counter.wisp with this example and run cargo run -p vapors-app --example demo -- counter. Click three times: the button should show “Count: 3” and the paragraph “Double: 6”.

The handler updates only count. The memo reads it and computes the derived value; the template reads both.

A memo tracks its inputs

A memo is evaluated lazily. Its first read computes the value, and later reads reuse it until a dependency changes. Its readers are notified when the derived value changes according to PartialEq.

The explicit Memo<i32> annotation supplies the type for its generated field, just as Signal<i32> does for the count.

Try a change

Change count.get() * 2 to count.get() * 3 and change the paragraph label to “Triple”. Rebuild and click the button. The readout should update automatically, without a second write in the handler.

Should the click handler also update the memo?

No. The memo derives its value from the signal it reads.