3. Update state

Your development computer

Choose the computer you’ll build on. Your choice applies throughout these six lessons.

Add a counter to the app from lesson 2. Keep the project files and catalog.

1. Add the counter messages

Append to resource/locales/en/app.ftl:

Append to resource/locales/en/app.ftl:

counter_value = Count: { $count }
add_one = Add one

$count defines the argument accepted by the generated counter_value accessor.

2. Store and display the count

Replace src/lib.rs:

Write src/lib.rs:

use day::prelude::*;

day::resources!();
day::day_start!(options: res::window_options(), root);

pub fn root() -> impl Piece {
    let count = Signal::new(0i64);

    column((
        label(res::str::counter_value(count)).id(id!(counter_label)),
        button(res::str::add_one())
            .action(move || count.update(|n| *n = n.saturating_add(1)))
            .id(id!(increment_button)),
    ))
    .spacing(Space::M)
    .padding(Space::L)
}

Signal::new(0i64) creates reactive state in the root’s scope. Pass count to the localized accessor to track changes; count.get() would pass a snapshot.

The move closure copies the signal handle. The label and button share its value. update changes the value and notifies dependents; saturating_add prevents integer overflow. The callback runs on the UI thread. Do not send a Signal to a worker thread.

3. Observe the state

macOS

day launch -t macos-appkit

Windows

day launch -t windows-winui

Linux

day launch -t linux-gtk

macOS

Quit with Command-Q before continuing.

Windows

Close the app window before continuing.

Linux

Close the app window before continuing.

Expect Count: 0 initially and Count: 2 after pressing Add one twice. Relaunching resets the count to zero; state is stored only in memory.

Troubleshooting

If the count does not change, pass count to the label and call count.update(...) in the callback. If the accessor signature is incorrect, check for { $count } in counter_value and rebuild.

Next step

Continue to 4. Arrange the controls.