# Step 3: Actions and reducers

## What you will build in this step

Two **actions**, "add a duck to the basket" and "remove one", and the **reducers** that turn each of them into the next basket.

## Why

The state can't be changed in place, so something has to produce the next state. In Ducky, nobody sets the basket directly. Code describes what happened, as an action, and the slice decides what that means for its state, in a reducer. That keeps every rule about the basket in one class, and every change goes through one door, which is what makes changes easy to follow, log and test.

## Declare the actions

An **action** is a small record that says what happened, in the past or as a request: `AddToBasket("pirate")`. It carries the data the change needs, and nothing else. Add the two actions to `Shop/Basket.cs`, between the records and the slice:

Source: https://github.com/phmatray/Ducky/blob/v2/docs/tutorial/samples/DuckShop/Shop/Basket.cs#L22-L28

```csharp
// What can happen to the basket. [DuckyAction] asks the generator for dispatch helpers:
// store.AddToBasket("classic") dispatches new AddToBasket("classic").
[DuckyAction]
public sealed record AddToBasket(string DuckId);

[DuckyAction]
public sealed record RemoveFromBasket(string DuckId);
```

`[DuckyAction]` asks the generator for **dispatch helpers**, methods named after the action. To **dispatch** an action is to send it to the store, and with the helpers, `store.AddToBasket("pirate")` does the same as `store.Dispatch(new AddToBasket("pirate"))`.

## Add the reducers

A **reducer** takes the current state and an action, and returns the next state. Add this constructor and helper method to `BasketSlice`, above `Initial`:

Source: https://github.com/phmatray/Ducky/blob/v2/docs/tutorial/samples/DuckShop/Shop/Basket.cs#L34-L56

```csharp
public BasketSlice()
{
    // A reducer takes the current state and the action, and returns the next state.
    On<AddToBasket>((state, action) =>
        WithQuantity(state, action.DuckId, state.QuantityOf(action.DuckId) + 1));
    On<RemoveFromBasket>((state, action) =>
        WithQuantity(state, action.DuckId, state.QuantityOf(action.DuckId) - 1));
}

// A new state with the duck's line set to quantity; zero removes the line. When
// nothing changes it returns the same state, and the store commits nothing.
private static BasketState WithQuantity(BasketState state, string duckId, int quantity)
{
    var line = state.Lines.Find(l => l.DuckId == duckId);
    var lines = (line, quantity) switch
    {
        (null, <= 0) => state.Lines,
        (null, _) => state.Lines.Add(new BasketLine(duckId, quantity)),
        (_, <= 0) => state.Lines.Remove(line),
        _ => state.Lines.Replace(line, line with { Quantity = quantity }),
    };
    return lines == state.Lines ? state : state with { Lines = lines };
}
```

- `On<AddToBasket>(...)` registers the reducer for that action type, in the constructor. A slice has at most one reducer per action type.
- `WithQuantity` builds the new list and returns a new state with `state with { Lines = lines }`: a copy of the record with one property replaced. The old state is untouched.
- When nothing changes, for example removing a duck that isn't in the basket, `WithQuantity` returns the same `state` object. The store sees the same reference, keeps the old state as it is, notifies nobody and re-renders nothing.

A reducer is **pure**: given the same state and action it always returns the same result, and it does nothing else. No server call, no clock, no random number, no change to its inputs. Ducky's analyzers check the lambda you pass to `On<T>` as you type. They don't follow calls into a helper such as `WithQuantity`, so keep helpers pure too.

## Another shape: a union of actions

`On<T>` suits a slice that reacts to a few unrelated actions, as the basket does. When a slice owns a closed set of actions, declare them as a C# 15 `union` and derive from `Slice<TState, TAction>`: one `Reduce` with a `switch` that the compiler checks covers every case.

Source: https://github.com/phmatray/Ducky/blob/v2/test/Ducky.Docs.Tests/Core/UnionSliceGuideTests.cs#L13-L35

```csharp
public sealed record Deposit(int Amount);

public sealed record Withdraw(int Amount);

public sealed record CloseAccount;

public union AccountAction(Deposit, Withdraw, CloseAccount);

public sealed record Account(int Balance, bool Closed);

// partial: the generator adds one On<Case> per case of the union, each calling Reduce.
public sealed partial class AccountSlice : Slice<Account, AccountAction>
{
    protected override Account Initial => new(0, false);

    // One exhaustive switch: a case missing here is a CS8509 warning, an error with warnings as errors.
    protected override Account Reduce(Account state, AccountAction action) => action switch
    {
        Deposit d => state with { Balance = state.Balance + d.Amount },
        Withdraw w => state with { Balance = state.Balance - w.Amount },
        CloseAccount => state with { Closed = true },
    };
}
```

Dispatch a case or the union value; the store delivers the case either way. This tutorial keeps `On<T>`, and [Unions in Ducky](../guides/core/unions.md) says when to pick which.

## One action at a time

The store's **dispatcher** takes actions in the order they are dispatched and reduces them one at a time: each reducer sees the state the previous action left. Two buttons clicked at once, or a click while a server answer arrives, can't interleave halfway through a reducer, so a reducer never needs a lock.

## Run it

Save. The build succeeds without a warning, and the page hasn't changed: the next step adds the components that dispatch these actions. The generator has written `AddToBasket(...)` and `RemoveFromBasket(...)` helpers: type `Store.AddTo` in a `.razor` file in the next step and your editor offers `AddToBasket`. The sample's [BasketTests.cs](https://github.com/phmatray/Ducky/blob/v2/docs/tutorial/samples/DuckShop.Tests/BasketTests.cs) runs these reducers: two `AddToBasket("classic")` give one line of two, and removing a duck that isn't there leaves the very same state.

## Checkpoint

- What is the difference between an action and a reducer? (The action says what happened; the reducer, in the slice, computes the next state from it.)
- Why does `WithQuantity` return the same `state` when nothing changes? (So the store keeps the old state and notifies nobody.)
- Why does a reducer never need a lock? (The dispatcher reduces one action at a time.)

## Common mistake

**Reading the clock, or making an id, inside a reducer.** Say each line should record when it was added, and the reducer reads the clock: `On<AddToBasket>((state, action) => state with { Lines = state.Lines.Add(new BasketLine(action.DuckId, 1) { AddedAt = DateTime.Now }) })`. The build warns, [DUCKY001](../diagnostics/DUCKY001.md) (*non-deterministic call in a reducer*). (Inside `WithQuantity` the same call is not reported: the analyzer reads only the lambda.) Either way the reducer is no longer pure: the same action gives a different state each time, so it can't be replayed or tested with a fixed input. Fix: compute the value where you dispatch, and put it in the action, as in `AddToBasket(DuckId, AddedAt)`.

Next: [Step 4: Show state in components](04-components.md).
