# 3. Update state

Source: https://daybrite.dev/docs/tutorials/state

Add a counter to the app from [lesson 2](https://daybrite.dev/docs/tutorials/events). Keep the project files and catalog.

## 1. Add the counter messages

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

**All platforms: Append to `resource/locales/en/app.ftl`**

```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`:

**All platforms: Write `src/lib.rs`**

```rust
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)`](https://docs.rs/day-reactive/latest/day_reactive/struct.Signal.html#method.new)
creates reactive state in the root's scope. Pass `count` to the localized accessor to track changes;
[`count.get()`](https://docs.rs/day-reactive/latest/day_reactive/struct.Signal.html#method.get)
would pass a snapshot.

The `move` closure copies the signal handle. The label and button share its value.
[`update`](https://docs.rs/day-reactive/latest/day_reactive/struct.Signal.html#method.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: Run in `first-app`**

```sh
day launch -t macos-appkit
```

**Windows: Run in `first-app`**

```powershell
day launch -t windows-winui
```

**Linux: Run in `first-app`**

```sh
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(...)`](https://docs.rs/day-reactive/latest/day_reactive/struct.Signal.html#method.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](https://daybrite.dev/docs/tutorials/layout).
