【问题标题】:Purescript Halogen Component, Input vs StatePurescript 卤素组件,输入与状态
【发布时间】:2018-04-29 01:57:02
【问题描述】:

我想制作一个卤素组件,其中组件的输入与其状态不同。根据卤素指南 (https://github.com/slamdata/purescript-halogen/blob/master/docs/5%20-%20Parent%20and%20child%20components.md#input-values),这应该是可能的。我将指南中的示例更改如下

import Prelude
import Data.Int (decimal, toStringAs)
import Halogen as H
import Halogen.HTML as HH
import Halogen.HTML.Events as HHE

type Input = Int

type State = String

data Query a = HandleInput Input a

component :: forall m. H.Component HH.HTML Query Input Void m
component =
  H.component
    { initialState: id
    , render
    , eval
    , receiver: HHE.input HandleInput
    }
  where

  render :: State -> H.ComponentHTML Query
  render state =
    HH.div_
      [ HH.text "My input value is:"
      , HH.strong_ [ HH.text (show state) ]
      ]

  eval :: Query ~> H.ComponentDSL State Query Void m
  eval = case _ of
    HandleInput n next -> do
      oldN <- H.get
      when (oldN /= (toStringAs decimal n)) $ H.put $ toStringAs decimal n
      pure next

但随后我在, receiver: HHE.input HandleInput 行收到编译错误

Could not match type

  String

with type

  Int

我在这里做错了什么?

【问题讨论】:

    标签: purescript halogen


    【解决方案1】:

    initialState 是使用输入值计算的,在您粘贴的代码中,它被实现为 id,因此它试图强制输入和状态类型匹配。

    【讨论】:

      【解决方案2】:

      { initialState: const initialState 中更改{ initialState: id 行并在where 之后添加以下行

      initialState :: State
      initialState = ""
      

      【讨论】:

        猜你喜欢
        • 2017-05-28
        • 2016-12-02
        • 1970-01-01
        • 2018-09-11
        • 1970-01-01
        • 1970-01-01
        • 2016-04-03
        • 1970-01-01
        • 2016-06-08
        相关资源
        最近更新 更多