# Your first Component

> A counter: model, update, view — and how to run it with startApp or hydrate with miso.

Source: https://haskell-miso.org/docs/your-first-component/

The core type of miso is `Component`. To define one, use the `component` smart constructor. Below is a simple counter.

```haskell
module Main where

import           Miso
import           Miso.Lens
import qualified Miso.Html.Element  as H
import qualified Miso.Html.Event    as HE
import qualified Miso.Html.Property as HP

-- The four Component type parameters:
--   context - the type of the global context
--   props   - the props inherited from the parent
--   Int     - the type of the Component model
--   Action  - the action that updates the model
counter
  :: Component context props Int Action
counter = component m u v
  where
    -- | Initial model value
    m :: Int
    m = 0

    u :: Action
      -> Effect context props Int Action
    u = \case
      Add      -> this += 1
      Subtract -> this -= 1

    v :: Int
      -> View context props Int Action
    v x = vfrag
      [ H.button_
          [ HE.onClick Add, HP.id_ "add" ]
          [ "+" ]
      , text (ms x)
      , H.button_
          [ HE.onClick Subtract, HP.id_ "subtract" ]
          [ "-" ]
      ]

main :: IO ()
main = startApp defaultEvents counter

data Action
  = Add
  | Subtract
  deriving (Eq, Show)
```

**Live example: The counter, running**

```haskell
data CounterAction = Add | Subtract
  deriving (Show, Eq)

counter
  :: Component ctx () Int CounterAction
counter = component m u v
  where
    m = 0

    u = \case
      Add      -> this += 1
      Subtract -> this -= 1

    v n =
      H.div_ [ HP.class_ "row" ]
        [ H.button_ [ HE.onClick Subtract ] [ "−" ]
        , H.strong_ [] [ text (ms n) ]
        , H.button_ [ HE.onClick Add ] [ "+" ]
        ]
```

Four type parameters follow every `Component`: the global `context` (shared by the whole tree), the `props` passed by the parent, the component's own `model` and the `action` type its `update` consumes. A top-level application fixes `context` and `props` to `()`; the `App` synonym spells that out:

```haskell
type App model action
  = Component () () model action

startApp
  :: Eq model
  => Events
  -> App model action
  -> IO ()
```

## Running it

We recommend `startApp` as the starting point — it sets up event listeners, performs the initial draw and assumes `<body>` is empty. The `miso` function (and `prerender`) assume `<body>` has already been populated by the result of `view`: instead of drawing, `miso` hydrates. If the structures do not match it falls back to drawing from scratch.

```haskell
main :: IO ()
main = miso defaultEvents $ \uri -> counter
-- hydrate a prerendered page
```

## Doing something on mount

It is possible to execute an initial action when a `Component` is first mounted with the `mount` hook (and, similarly, `unmount`):

```haskell
data Action = Init | Add | Subtract

main :: IO ()
main = startApp defaultEvents
  counter { mount = Just Init }

update
  :: Action
  -> Effect context props Int Action
update = \case
  Init -> io_ (consoleLog "hello world!")
  ...
```

> **Note:** `startApp` and `miso` always infer `context` as `()`. Use `startAppWithContext` (or `misoWithContext` when hydrating) to seed a non-trivial context — see [Context](https://haskell-miso.org/docs/context/).
