简体   繁体   中英

How to create a stateless and static Halogen component?

Consider this snippet from github, https://github.com/slamdata/purescript-halogen/blob/master/examples/basic/src/Button.purs#L42 which tries to render an html button using halogen library.

render :: State -> H.ComponentHTML Query
  render state =
    let
      label = if state then "On" else "Off"
    in
      HH.button
        [ HP.title label
        , HE.onClick (HE.input_ Toggle)
        ]
        [ HH.text label ]

  eval :: Query ~> H.ComponentDSL State Query Message m
  eval = case _ of
    Toggle next -> do
      state <- H.get
      let nextState = not state
      H.put nextState
      H.raise $ Toggled nextState
      pure next
    IsOn reply -> do
      state <- H.get
      pure (reply state)

Is there any possible way to get the most 'barebone' UI control, just to render a static UI component, without states involved?

How about setting type State = Unit ? Then your render function would look like

render :: State -> H.ComponentHTML Query
render _ = [...]

ie just ignore the parameter (since you can't get any information out of a Unit value anyway).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM