Course map
miso // 06

Model, update, view

Read

Actions

An interactive app needs a way for things to happen. In miso, every event is turned into an action: a value of a type you define. The update function receives the action and changes the model.

data Action = Increment | Decrement
  deriving (Show, Eq)

app :: App Int Action
app = component 0 update view

update :: Action -> Effect () () Int Action
update Increment = this += 1
update Decrement = this -= 1

view :: Int -> View () () Int Action
view n =
  H.div_ []
    [ H.button_ [ E.onClick Decrement ] [ "−" ]
    , H.strong_ [] [ text (ms n) ]
    , H.button_ [ E.onClick Increment ] [ "+" ]
    ]

E.onClick Increment is an attribute: when the button is clicked, miso calls update Increment. this is a lens that points at the whole model, and += 1 adds one to it. The view is then re-run with the new model.

The type Effect () () Int Action describes what update may do: change the Int model and, as we will see, schedule actions and IO.

01 / 04