【问题标题】:How to create a stateless and static Halogen component?如何创建无状态的静态卤素组件?
【发布时间】:2017-08-01 07:41:31
【问题描述】:

考虑一下来自 github 的这个 sn-p,https://github.com/slamdata/purescript-halogen/blob/master/examples/basic/src/Button.purs#L42,它尝试使用卤素库呈现一个 html 按钮。

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)

是否有任何可能的方法来获得最“准系统”的 UI 控件,只渲染一个静态 UI 组件,而不涉及状态?

【问题讨论】:

  • 如果你想要一个没有任何状态的静态 UI 组件,也许你根本不需要 Halogen 组件。你不能只在父组件中的位置呈现静态 HTML 吗?

标签: purescript halogen


【解决方案1】:

设置type State = Unit怎么样?然后你的 render 函数看起来像

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

即只需忽略该参数(因为无论如何您都无法从 Unit 值中获取任何信息)。

【讨论】:

    猜你喜欢
    • 2018-04-29
    • 2023-04-06
    • 2020-02-26
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    相关资源
    最近更新 更多