This page covers Elm 0.18

Messages with payload

You can send a payload in your message:

module Main exposing (..)

import Html exposing (Html, button, div, text, program)
import Html.Events exposing (onClick)


-- MODEL


type alias Model =
    Int


init : ( Model, Cmd Msg )
init =
    ( 0, Cmd.none )



-- MESSAGES


type Msg
    = Increment Int



-- VIEW


view : Model -> Html Msg
view model =
    div []
        [ button [ onClick (Increment 2) ] [ text "+" ]
        , text (toString model)
        ]



-- UPDATE


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        Increment howMuch ->
            ( model + howMuch, Cmd.none )



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.none



-- MAIN


main : Program Never Model Msg
main =
    program
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }

Note how the Increment message requires an integer:

type Msg
    = Increment Int

Then in the view we trigger that message with a payload:

onClick (Increment 2)

And finally in update we use pattern matching to extract the payload:

update msg model =
    case msg of
        Increment howMuch ->
            ( model + howMuch, Cmd.none )

results matching ""

    No results matching ""