# Step 2: Views and components

> Most of your UI should be plain view functions. Reach for a Component when a piece needs its own state, subs or lifecycle.

Source: https://haskell-miso.org/docs/thinking/components/

In miso there are two ways to split a UI: **view functions** (any function returning a `View`) and **components** (a `Component` with its own model, update and lifecycle). Reach for the first by default.

## Draw the boxes

```
BookmarksApp  (Component — owns the Model)
├── searchBar      (view: query, onInput)
├── tagSidebar     (view: tag counts, tag)
├── bookmarkTable  (view function)
│   └── bookmarkRow (view, keyed by id)
├── detailPane     (view function)
└── "add" +> addBookmarkForm
      (Component — owns its draft,
       validation and submit state)
```

*Boxes are functions. Only two of them are components.*

## When is something a Component?

A `Component` costs a little ceremony (its own model and action type, a key) and buys isolation. Make one when a piece of UI:

- **owns state nobody else needs** — the add form's draft and validation errors are irrelevant to the table;
- **needs subscriptions or lifecycle hooks** — a clock, a websocket, a third-party widget initialised in `mount`;
- **is reused with different props** — the same `avatar` component mounted for each user;
- **should re-render independently** — its model changes often while the parent's does not.

Otherwise write a function. `bookmarkRow :: Bookmark -> View ctx props Model Action` is simpler than a component, is trivially testable, and re-renders as part of its parent.

```haskell
bookmarkRow
  :: Maybe BookmarkId
  -> Bookmark
  -> View ctx props Model Action
bookmarkRow selected b =
  H.tr_
    [ key_ (bookmarkId b)
      -- stable identity in the list
    , HP.classList_
        [ ("selected", isSelected) ]
    , HE.onClick (Select (bookmarkId b))
    ]
    [ H.td_ [] [ text (title b) ]
    , H.td_ [] [ text (MS.intercalate ", " (tags b)) ]
    ]
  where
    isSelected = selected == Just (bookmarkId b)
```

> **Note:** Give list items a `key_`. It keeps the DOM node (and any CSS transition on it) attached to the *same* bookmark when the list is filtered or reordered — see [Keys](https://haskell-miso.org/docs/keys/).

## Where does each piece of state live?

| State | Lives in | Because |
| --- | --- | --- |
| bookmarks, query, filter, selection | `BookmarksApp` model | several views read it; the app owns it |
| the add form's draft & errors | `addBookmarkForm` model | private; nobody else cares until submit |
| the currently selected tag, as seen by the form | props | the form only *reads* it to pre-fill a tag |
| language, theme, current user | context | global; every component may read it |

The rule of thumb: state lives in the **closest common owner** of everything that reads or writes it. Push it up only as far as it needs to go, and pass it down as props.

```haskell
viewApp
  :: Model
  -> View Ctx () Model Action
viewApp m = vcontext $ \ctx ->
  H.main_ []
    [ searchBar (m ^. query)
    , H.div_ [ HP.class_ "columns" ]
        [ tagSidebar
            (m ^. tagFilter)
            (tagCounts (loadedOr [] (m ^. bookmarks)))
        , bookmarkTable (m ^. selected) (visible m)
        , detailPane (selectedBookmark m)
        ]
    , mountWithProps_ "add-form"
        (FormProps (m ^. tagFilter))
        addBookmarkForm
    ]
```

Next: [name the actions and write update](https://haskell-miso.org/docs/thinking/update/).
