Skip to main content

Commands

Commands let us send messages, navigate to another page, run a promise and more.

An example

Let's define a Model that will hold a value and a Msg that will tell us how to change that value:

type Model =
{
Value : int
}

type Msg =
| Increment
| Decrement

Now we define the init function that will produce initial state once the program starts running.

let init () =
{
Value = 0
}
, Command.ofMsg Increment

Notice that we return a tuple. The first field of the tuple tells the page's the initial state. The second field holds the command to issue an Increment message.

The update function will receive the change required by Msg, and the current state. It will produce a new state and potentially new command(s).

let update msg model =
match msg with
| Increment when model.Value < 2 ->
{ model with
Value = model.Value + 1
}
, Command.ofMsg Increment
| Increment ->
{ model with
Value = model.Value + 1
}
, Command.ofMsg Decrement
| Decrement when model.Value > 1 ->
{ model with
Value = model.Value - 1
}
, Command.ofMsg Decrement
| Decrement ->
{ model with
Value = model.Value - 1
}
, Command.ofMsg Increment

Again we return a tuple: new state, command.

If we goto this page, it will keep updating the model from 0 to 3 and back.

info

Does this look familiar?

The Command<'msg, 'sharedMsg> type in Elmish Land is an abstraction built on top of Elmish's standard Cmd<'msg> type. The Command module contians convenience functions for using Elmish Cmd:s and functions to allow communication from pages to the Shared module.

Available commands

The Command module provides several functions for common tasks like Command.none and Command.ofMsg.

For complete API documentation including function signatures, parameters, and detailed examples, see the Command Module API Reference.